How to Use lm() Function in R to Fit Linear Models?
Last Updated :
19 Dec, 2021
In this article, we will learn how to use the lm() function to fit linear models in the R Programming Language.
A linear model is used to predict the value of an unknown variable based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. The lm() function is used to fit linear models to data frames in the R Language. It can be used to carry out regression, single stratum analysis of variance, and analysis of covariance to predict the value corresponding to data that is not in the data frame. These are very helpful in predicting the price of real estate, weather forecasting, etc.
To fit a linear model in the R Language by using the lm() function, We first use data.frame() function to create a sample data frame that contains values that have to be fitted on a linear model using regression function. Then we use the lm() function to fit a certain function to a given data frame.
Syntax:
lm( fitting_formula, dataframe )
Parameter:
- fitting_formula: determines the formula for the linear model.
- dataframe: determines the name of the data frame that contains the data.
Then, we can use the summary() function to view the summary of the linear model. The summary() function interprets the most important statistical values for the analysis of the linear model.
Syntax:
summary( linear_model )
The summary contains the following key information:
- Residual Standard Error: determines the standard deviation of the error where the square root of variance subtracts n minus 1 + # of variables involved instead of dividing by n-1.
- Multiple R-Squared: determines how well your model fits the data.
- Adjusted R-Squared: normalizes Multiple R-Squared by taking into account how many samples you have and how many variables you’re using.
- F-Statistic: is a “global” test that checks if at least one of your coefficients is non-zero.
Example: Example to show usage of lm() function.
R
# sample data frame
df <- data.frame( x= c(1,2,3,4,5),
y= c(1,5,8,15,26))
# fit linear model
linear_model <- lm(y ~ x^2, data=df)
# view summary of linear model
summary(linear_model)
Output:
Call:
lm(formula = y ~ x^2, data = df)
Residuals:
1 2 3 4 5
2.000e+00 5.329e-15 -3.000e+00 -2.000e+00 3.000e+00
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -7.0000 3.0876 -2.267 0.10821
x 6.0000 0.9309 6.445 0.00757 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.944 on 3 degrees of freedom
Multiple R-squared: 0.9326, Adjusted R-squared: 0.9102
F-statistic: 41.54 on 1 and 3 DF, p-value: 0.007575
Diagnostic Plots
The diagnostic plots help us to view the relationship between different statistical values of the model. It helps us in analyzing the extent of outliers and the efficiency of the fitted model. To view diagnostic plots of a linear model, we use the plot() function in the R Language.
Syntax:
plot( linear_model )
Example: Diagnostic plots for the above fitted linear model.
R
# sample data frame
df <- data.frame( x= c(1,2,3,4,5),
y= c(1,5,8,15,26))
# fit linear model
linear_model <- lm(y ~ x^2, data=df)
# view diagnostic plot
plot(linear_model)
Output:

Plotting Linear model
We can plot the above fitted linear model to visualize it well by using the abline() method. We first plot a scatter plot of data points and then overlay it with an abline plot of the linear model by using the abline() function.
Syntax:
plot( df$x, df$y)
abline( Linear_model )
Example: Plotting linear model
R
# sample data frame
df <- data.frame( x= c(1,2,3,4,5),
y= c(1,5,8,15,26))
# fit linear model
linear_model <- lm(y ~ x^2, data=df)
# Plot abline plot
plot( df$x, df$y )
abline( linear_model)
Output:

Predict values for unknown data points using the fitted model
To predict values for novel inputs using the above fitted linear model, we use predict() function. The predict() function takes the model and data frame with unknown data points and predicts the value for each data point according to the fitted model.
Syntax:
predict( model, data )
Parameter:
- model: determines the linear model.
- data: determines the data frame with unknown data points.
Example: Predicting novel inputs
R
# sample data frame
df <- data.frame( x= c(1,2,3,4,5),
y= c(1,5,8,15,26))
# fit linear model
linear_model <- lm(y ~ x^2, data=df)
# Predict values
predict( linear_model, newdata = data.frame(x=c(15,16,17)) )
Output:
1 2 3
83 89 95
Similar Reads
How to Use the linearHypothesis() Function in R
In statistics, understanding how variables relate to each other is crucial. This helps in making smart decisions. When we build regression models, we need to check if certain combinations of variables are statistically significant. In R Programming Language a tool called linear hypothesis () in the
4 min read
How to Use file.path() Function in R
R programming language is becoming popular among developers, analysts, and mainly for data scientists. Students are eagerly learning R with Python language to use their analytical skills at their best. While learning any language, one is faced with many difficulties, and the individual learning R Pr
3 min read
How to use Summary Function in R?
The summary() function provides a quick statistical overview of a given dataset or vector. When applied to numeric data, it returns the following key summary statistics:Min: The minimum value in the data1st Qu: The first quartile (25th percentile)Median: The middle value (50th percentile)3rd Qu: The
2 min read
How to plot user-defined functions in R?
Plotting user-defined functions in R is a common task for visualizing mathematical functions, statistical models, or custom data transformations. This article provides a comprehensive guide on how to plot user-defined functions in R, including creating simple plots, enhancing them with additional fe
3 min read
How to Use the coeftest() Function in R
In R Programming language, we use coeftest() function to perform hypothesis tests and construct confidence intervals for regression coefficients. It is used after fitting regression models using functions like lm() (for linear regression), glm() (for generalized linear models), or any other function
5 min read
How to Use sum Function in R?
In this article, we will discuss how to use the sum() function in the R Programming Language. sum() function: This is used to return the total/sum of the given data Syntax: sum(data) Arguments: data can be a vector or a dataframeExample 1: Using sum() function to calculate the sum of vector elements
5 min read
How to Find Good Start Values for nls Function in R
The nls (nonlinear least squares) function in R is used for fitting nonlinear models to data. Unlike linear models, nonlinear models can be more challenging to fit because they often require careful selection of starting values for the parameters. Poor starting values can lead to convergence issues
5 min read
How Linear Mixed Model Works in R
Linear mixed models (LMMs) are statistical models that are used to analyze data with both fixed and random effects. They are particularly useful when analyzing data with hierarchical or nested structures, such as longitudinal or clustered data. In R Programming Language, the lme4 package provides a
4 min read
Fitting Linear Models to the Data Set in R Programming - glm() Function
glm() function in R Language is used to fit linear models to the dataset. Here, glm stands for a generalized linear model. Syntax: glm(formula)Parameters: formula: specified formula  Example 1:  Python3 # R program to illustrate # glm function # R growth of orange trees dataset Orange # Putting a
2 min read
Comparing Two Linear Models with anova() in R
Comparing two linear models is a fundamental task in statistical analysis, especially when determining if a more complex model provides a significantly better fit to the data than a simpler one. In R, the anova() the function allows you to perform an Analysis of Variance (ANOVA) to compare nested mo
4 min read