Skip to content

Multiple Linear Regression in R

Last Update: February 21, 2022

Multiple linear regression in R can be fitted using stats package lm function. Main parameters within lm function are formula with y ~ x1 + … + xp model description and data with data.frame object including model variables. Therefore, lm(y ~ x1 + x2, data = code line fits model \hat{y}_{i} = \hat{\beta}_{0} + \hat{\beta}_{1} x_{1i} + \hat{\beta}_{2} x_{2i} using variables included within object.

As example, we can fit multiple linear regression of house price explained by its lot size and number of bedrooms using data included within AER package HousePrices object [1].

First, we load package AER for data [2].

In [1]:

Second, we create HousePrices data object from AER package using data function and print first six rows and three columns of data using head function to view data.frame structure.

In [2]:
Out [2]:
  price lotsize bedrooms
1 42000    5850        3
2 38500    4000        2
3 49500    3060        3
4 60500    6650        3
5 61000    6360        2
6 66000    4160        3

Third, we fit model with lm function using variables within HousePrices data object, store outcome within mlr object and print its coefficients estimates. Within lm function, parameter formula = price ~ lotsize + bedrooms fits model \hat{price} = \hat{\beta}_{0} + \hat{\beta}_{1} lotsize + \hat{\beta}_{2} bedrooms where house price is explained by its lot size and number of bedrooms.

In [3]:
mlr <- lm(formula = price ~ lotsize + bedrooms, data = HousePrices)
Out [3]:
lm(formula = price ~ lotsize + bedrooms, data = HousePrices)

(Intercept)      lotsize     bedrooms  
   5612.600        6.053    10567.352


My online courses are hosted at Teachable website.

For more details on this concept, you can view my Linear Regression in R Course.


[1] Data Description: Sales prices of houses sold in the city of Windsor, Canada, during July, August and September, 1987.

Original Source: Anglin, P., and Gencay, R. (1996). Semiparametric Estimation of a Hedonic Price Function. Journal of Applied Econometrics, 11, 633–648.

[2] AER R Package. Christian Kleiber and Achim Zeileis. (2008). Applied Econometrics with R. Springer-Verlag, New York.

My online courses are closed for enrollment.