Last Update: February 21, 2022
Multiple linear regression in R can be fitted using
lm function. Main parameters within
lm function are
y ~ x1 + … + xp model description and
data.frame object including model variables. Therefore,
lm(y ~ x1 + x2, data = model.data) code line fits model using variables included within
As example, we can fit multiple linear regression of house price explained by its lot size and number of bedrooms using data included within
HousePrices object .
First, we load package
AER for data .
In : 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
In : data(HousePrices) head(HousePrices[,1:3])
Out : 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 where house price is explained by its lot size and number of bedrooms.
In : mlr <- lm(formula = price ~ lotsize + bedrooms, data = HousePrices) mlr
Out : Call: lm(formula = price ~ lotsize + bedrooms, data = HousePrices) Coefficients: (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.
 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.
 AER R Package. Christian Kleiber and Achim Zeileis. (2008). Applied Econometrics with R. Springer-Verlag, New York.