Multiple Linear Regression using R to predict housing prices
By: Abdul Qureshi
Tags: Data Analysis, Predictive Analytics, Data Science, Linear Regression, Machine Learning

The goal of this story is that we will show how we will predict the housing prices based on various independent variables. This will be a simple multiple linear regression analysis as we will use a limited set of variables from the data columns.

We will obtain the data from the Kaggle web site for the ames housing prices data as shown on the bottom in the references section. Below we load the data set in a dataframe.

ames <- read.csv(“ames.csv”, header = TRUE)

Next we will explore the data and then clean if needed and also pre-process the data. We will see the dimensions and the various summary statistics and details using the str() and summary commands.

Using the str() function gives us these results, which shows the various data items and the type and some of the values of the same.

Using the summary() gives us these results which shows the min, medium, mean, max etc

There are lots of variables here, and for this study perspective we will use some of the significant numeric variables, that we will see that have effect on the sale price.

The machine learning task that we will be performing will be supervised machine learning , where we have lots of features and based on that we will be predicting the housing prices.

We will divide the data set into two parts training and validation set, we will also specify the percentages of the data in these two divisions.

# we set the seed to (1) m so that we get the same partitions when re-running #the code

set.seed(1)

# we will partition into 70% training and 30% validation

trainrows <- sample(rownames(ames), dim(ames)[1]*0.7)

traindata <- ames[trainrows, ]

# we will set the difference of the training into the validation set i.e. 30%

validrows <- setdiff(rownames(ames), trainrows)

validdata <- ames[validrows, ]

We will be using the machine learning technique to predict the value of the house prices. We will use the training data to run this model, and then will use the validation data to validate the model and see how accurate this is.

We will use the model to predict the task . We will use lm() function for multiple regression.

The interpretation of the multiple linear regression results shows that all the variables that we selected are significant except the value of Open.Porch.SF. We can improve our model summary statistics by removing this as a feature and re-running the model, or we can run Lasso regression or ridge regression which automatically handle such features which have less significance as well in future studies. The square value is quite high 0.7816 which shows that model shows the variation that our model explains.

We can now use the predict features to see how does the model works on the Validation data. We will test first few rows of the header and see the prediction results. We will use these commands in R to predict using validation data.

We can see that even though the model had high R square on the training data the model does not fit quite well on the validation data, as many of the initial values for the Sale price prediction vs actual have quite difference.

We can make this model further rigorous by using more advanced models, plus we have to check for multi-colinearity and can use feature engineering as well to reduce the number of numeric variables, as it is quite possible that some of the variables are co-related. More advanced models will also utilize many dummy variables as there are many categorical features that could be useful in the model as well.

References: