# 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 = model.data) code line fits model $\hat{y}_{i}&space;=&space;\hat{\beta}_{0}&space;+&space;\hat{\beta}_{1}&space;x_{1i}&space;+&space;\hat{\beta}_{2}&space;x_{2i}$ using variables included within model.data 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]:
library(AER)

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]:
data(HousePrices)
head(HousePrices[,1:3])
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}&space;=&space;\hat{\beta}_{0}&space;+&space;\hat{\beta}_{1}&space;lotsize&space;+&space;\hat{\beta}_{2}&space;bedrooms$ where house price is explained by its lot size and number of bedrooms.

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

Coefficients:
(Intercept)      lotsize     bedrooms
5612.600        6.053    10567.352

Courses

My online courses are hosted at Teachable website.

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

References

[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.

+