We will start by using the lm() function to fit a simple linear regression model, with medv as the response and lstat as the predictor. The basic syntax is lm(y ∼ x,data), where y is the response, x is the predictor, and data is the data set in which these two variables are kept.

> lm.fit <- lm(medv ~ lstat)
Error in eval(expr , envir , enclos) : Object "medv" not found

The command causes an error because R does not know where to find the variables medv and lstat. The next line uses the data option in the lm() function to tell R that the variables are in Boston.

lm.fit <- lm(medv ~ lstat, data = Boston)

Another option is to ‘attach’ the dataset. If we attach Boston, the first line works fine because R now recognizes the variables.

attach(Boston)
lm.fit <- lm(medv ~ lstat)

Attaching a dataset is not limited to the lm() function, a dataset is attached in the environment. It enables us to use medv directly instead of typing Boston$medv.

Try creating a regression model with y (response variable) being medv and x (predictor) being rm (average number of rooms) instead of lstat.


Assume that: