EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial lm Function in R
Secondary Sidebar
R programming Tutorial
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

lm Function in R

By Priya PedamkarPriya Pedamkar

im function in r

What is lm Function?

In this article, we will discuss on lm Function in R. lm function helps us to predict data. Let’s consider a situation wherein there is a manufacturing plant of soda bottles and the researcher wants to predict the demand of the soda bottles for the next 5 years. With the help of lm function, we can solve this problem. There is some information the researcher has to supply to this function to predict the output. In this problem, the researcher has to supply information about the historical demand for soda bottles basically past data.

The function will work on this past data/historical data and predict the values of the soda bottles. The number of bottles that the model has predicted, the manufacturing plant must have to make that number of bottles.

Let’s take another example of a retail store. Here the problem statement is that a store wants to estimate the demand for rice. Basically, the store wants to see how many packets they should stock in order to meet the demand. In this problem, the researcher first collects past data and then fits that data into the lm function. Lm function provides us the predicted figures. With the help of this predicted dataset, the researcher can take an effective call that how many rice packets they must stock in order to fulfill the demand.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

lm(formula, data, subset, weights, na.action,
method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE,
singular.ok = TRUE, contrasts = NULL)

Where,

The formula is a set of variables among which lm function needs to define. There is one dependent variable and can be multiple independent variables in this function. For example, variables can be distance and speed or Property rate, location, size of the property and income of the person.

  • Data: It is a set data frame basically the environment from where the data is picked for the function.
  • Subset: It is helpful when we have to pick specific observation from the data which we have to use in our lm function.
  • Weights: It is an optional vector, it is used in the fitting process. Weights should be a numeric vector or NULL.
  • Na.function: Na.functionis used when the researcher has to pass instructions like what to do when data has NA values in it.
  • Method: It is used for the fitting process, the predefined method in syntax is “qr”.
  • Model: It is a logical function, If it is TRUE then the corresponding components of the fit are returned.
  • X: It is a logical function, If it is TRUE then the corresponding components of the fit are returned.
  • Y: It is a logical function, If it is TRUE then the corresponding components of the fit are returned.
  • qr: It is a logical function, If it is TRUE then the corresponding components of the fit are returned.
  • singular.ok: It is also a logical function, it is false then the singular fit is an error.

Examples of lm Function in R

Let’s put some numbers in our above example.

Example #1

Problem Statement: There is a manufacturing plant of soda bottles and the researcher wants to predict the demand for soda bottles for the next 5 years.

Historical data of the last 20 years are mentioned below:

Year

Sales(Number of bottles)
2000

3310935

2001

5709184
2002

5274510

2003

5346064
2004

5866117

2005

3410297

2006

6280975

2007

3938247

2008

3393123
2009

3071252

2010

5176335
2011

7931652

2012

4793641
2013

4732211

2014

4349286
2015

6772890

2016

6966808
2017

3640308

2018

5502686
2019

6187610

Solution: Here we will make an lm function while using this historical data. Lm function provides us the regression equation, with the help of which we can predict the data.

Regression equation:

Y = β1 + β2X + ϵ
Where β1 is the intercept of the regression equation and β2 is the slope of the regression equation. β1 & β2 are also known as regression coefficients. ϵ is the error term.
  • β1: Intercept of The Regression Equation
  • β2: Slope of The Regression Equation
  • Y: Dependent Variable
  • X: Independent Variable

For the convenience and making steps easy, we put the above data in the CSV file.

Syntax for the problem looks like:

soda_dataset = read.csv("lm function in R.csv", header = TRUE)>
lm_soda_dataset = lm(Sales~Year, data = soda_dataset)>
lm_soda_dataset

lm Function in R-1.1

When we fit this input in the regression equation:

Sales = – 108665112 + 56605*Year

When we supply more data to this information we will get the predicted value out of it.

Example #2

Problem Statement: A retail store wants to estimate the demand for rice. Basically, the store wants to see how many packets they should stock in order to meet the demand. But before this, they will like to conduct some studies around the price of rice and demand for it. They have the last 10 years of data for both the price of rice and the demand of rice.

Historical Data:

Year Price Demand
2010 34 3459288
2011 36 5378760
2012 32 7996213
2013 45 5612215
2014 38 5379417
2015 46 5634967
2016 34 7650572
2017 42 4875672
2018 44 3788315
2019 49 6840814

Syntax:

rice_dataset = read.csv("lm function in R.csv", header = TRUE)>
lm_rice_dataset = lm(Demand~Price, data = rice_dataset)>
lm_rice_dataset

lm Function in R-1.2

When we fit this input in the regression equation:

Demand= 7493061 – 45786*Price

When we supply more data to this information we will get the predicted value out of it.

Advantage of lm Function

Advantage of lm function is given below:

  • It is a simple and powerful statistic function.
  • One of the functions which helps the researcher/academicians/statistician to predict data.
  • Helps us to take better business decision.
  • It helps us to analyze the data.
  • Helps us to rectify error if any.

Conclusion

lm function in R provides us the linear regression equation which helps us to predict the data. It is one of the most important functions which is widely used in statistics and mathematics. The only limitation with the lm function is that we require historical data set to predict the value in this function. But we can’t treat this as any limitation because historical data is a must if we have to predict anything. Historical data shows us the trend and with the help of a trend, we can predict the data.

Recommended Articles

This is a guide to the lm Function in R. Here we discuss the introduction and examples of lm function in R along with advantage. You may also have a look at the following articles to learn more –

  1. Confidence interval of Predict Function in R
  2. Examples of Matlab Inverse Function
  3. Inbuilt Functions in R
  4. Basic AutoCAD Function Keys
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more