In this article, we will learn about the lm() function in R, what it is and how to use it for linear regression analysis. Let’s go into detail now.
What is the lm() function in R?
The lm() function in the R language is a linear model function used for fit linear models and linear regression analysis.
Syntax:
lm(formula, data)
Parameters:
- formula: a symbolic description of the model.
- data: an optional data frame or environment containing the variables in the model.
How to use the lm() function in R?
We will use specific examples to help you learn how to use the lm() function in R.
Fitting a linear model
The linear model of the form: y = a + b*x + errors
The lm() function takes as its first argument a formula, which is a symbolic description of the model. For example, y ~ x is a formula stating that y depends on x. The second argument of the lm() function is data, which defines the data frame where x and y are found. The lm() function returns an object which contains information about the fitted model.
Example:
# Sample data frame dataSample <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6)) # Fit a linear model rel <- lm(y ~ x, dataSample) print(rel)
Output:
Call:
lm(formula = y ~ x, data = dataSample)
Coefficients:
(Intercept) x
3 1
Linear regression analysis
A simple example of linear regression analysis is predicting height when knowing a person’s weight.
To predict, we need to have a relationship between a person’s weight and height. The steps are:
- Measure the height and weight value of many people.
- Use the lm() function in R to fit linear models.
- Use the summary() function to see detailed information on the model’s performance and coefficients.
- Use predict() function in R to predict a person’s height.
Example:
Here is an example of measured height and weight data.
Weight:
48, 56, 52, 42, 65, 61, 70, 58, 60, 57
Height:
159, 172, 160, 171, 166, 174, 178, 164, 169, 170
Code:
# Height height <- c(159, 172, 160, 171, 166, 174, 178, 164, 169, 170) # Weight weight <- c(48, 56, 52, 42, 65, 61, 70, 58, 60, 57) # Fit linear models fitModel <- lm(height ~ weight) # Detailed information on the model's performance and coefficients summaryModel <- summary(fitModel) print(summaryModel) # Predict a person's height df <- data.frame(weight <- c(50, 60)) result <- predict(fitModel, df) print(result)
Output:
Call:
lm(formula = height ~ weight)
Residuals:
Min 1Q Median 3Q Max
-6.5644 -5.0492 0.6333 4.1905 7.9777
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 148.1457 13.3607 11.088 3.91e-06 ***
weight 0.3542 0.2327 1.522 0.166
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.656 on 8 degrees of freedom
Multiple R-squared: 0.2246, Adjusted R-squared: 0.1277
F-statistic: 2.317 on 1 and 8 DF, p-value: 0.1665
1 2
165.856 169.398
Based on the available data, we predict the height of two people weighing 50 kg and 60 kg.
The predict() function in R is used to predict values based on input data.
Syntax:
prediction (object, new data)
Parameter:
- object: The class inherits from the linear model.
- new data: Enter data to predict values.
Summary
We shared the lm() function in R and how to use it for linear model tuning and linear regression analysis. We hope the information in this article has been helpful to you. Thank you for reading.
Maybe you are interested:
- What Is The gather() Function In R And How To Use It
- The arrange() Function In R
- What Is The summary() Function In R

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP