Path: blob/master/2020-spring/materials/tutorial_09/tutorial_09.ipynb
2051 views
Tutorial 9: Regression Continued
Regression learning objectives:
Recognize situations where a simple regression analysis would be appropriate for making predictions.
Explain the k-nearest neighbour (k-nn) regression algorithm and describe how it differs from k-nn classification.
Interpret the output of a k-nn regression.
In a dataset with two variables, perform k-nearest neighbour regression in R using
caret::train()
to predict the values for a test dataset.Using R, execute cross-validation in R to choose the number of neighbours.
Using R, evaluate k-nn regression prediction accuracy using a test data set and an appropriate metric (e.g., root means square prediction error).
In a dataset with > 2 variables, perform k-nn regression in R using
caret
'strain
withmethod = "knn"
to predict the values for a test dataset.In the context of k-nn regression, compare and contrast goodness of fit and prediction properties (namely RMSE vs RMSPE).
Describe advantages and disadvantages of the k-nearest neighbour regression approach.
Perform ordinary least squares regression in R using
caret
'strain
withmethod = "lm"
to predict the values for a test dataset.Compare and contrast predictions obtained from k-nearest neighbour regression to those obtained using simple ordinary least squares regression from the same dataset.
In R, overlay the ordinary least squares regression lines from
geom_smooth
on a single plot.
Predicting credit card balance
Source: https://media.giphy.com/media/LCdPNT81vlv3y/giphy-downsized-large.gif
Here in this worksheet we will work with a simulated data set that contains information that we can use to create a model to predict customer credit card balance. A bank might use such information to predict which customers might be the most profitable to lend to (customers who carry a balance, but do not default, for example).
Specifically, we wish to build a model to predict credit card balance (Balance
column) based on income (Income
column) and credit rating (Rating
column).
Question 1.0
{points: 1}
Load the data located at this URL and assign it to an object called credit
.
Question 1.1
{points: 1}
Select only the columns of data we are interested in using for our prediction (both the predictors and the response variable). Name the modified data frame credit
.
Question 1.2
{points: 1}
Before we perform exploratory data analysis, we should create our training and testing data sets. We will use 60% of the data as training data. Use set.seed(2000)
and use the Balance
column as the input to createDataPartition()
.
At the end of this question you should have 4 objects named X_train
, Y_train
, X_test
and Y_test
.
Question 1.3
{points: 1}
Using only the observations in the data set, create a ggpairs
scatterplot of all the columns we are interested in including in our model. Name the plot object credit_eda
.
Question 1.4
{points: 3}
Discuss the relationship you observe in the scatter plots above between the response variable and each predictor.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 1.5
{points: 1}
Now use caret
's train
function with method = "lm"
to fit your linear regression model. Name your linear regression model object lm_model
.
Question 1.6
{points: 1}
Let's print out a table of the regression slopes/coefficients. To do this, we want to access some attributes of the model object. We provide scaffolding below. To get the slopes/coefficients as a nice data frame, we then use the t()
(transpose) function to pivot the data, and then data.frame()
to convert it to a data frame. Name the resultant data frame lm_coeffs
.
Question 1.7
{points: 3}
Looking at the slopes/coefficients above from each of the predictors, write a mathematical equation for your prediction model.
A couple hints:
surrounding your equation with
$
signs in a markdown cell, makes it a LaTeX equationto add white space in a LaTeX equation, use
\:
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 1.8
{points: 1}
Calculate the to assess goodness of fit on your lm_model
(remember this is how well it predicts on the training data used to fit the model). Return a single numerical value named lm_rmse
.
Question 1.9
{points: 1}
Calculate using the test data. Return a single numerical value named lm_rmspe
.
Question 1.9.1
{points: 3}
Redo this analysis using k-nn regression instead of linear regression. Use set.seed(2000)
at the beginning of this code cell to make it reproducible. Assign a single numeric value for for your k-nn model as your answer, and name it knn_rmspe
. Use the same predictors and train - test data split as you used for linear regression, and use 5-fold cross validation to choose from the range 1-20. Remember to scale and shift your variables using preProcess
on your training data, and to apply that same standardization to your test data!
Question 1.9.2
{points: 3}
Discuss which model gives better predictions and why you think that might be happening.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
2. Ames Housing Prices
Source: https://media.giphy.com/media/xUPGGuzpmG3jfeYWIg/giphy.gif
If we take a look at the Business Insider report What do millenials want in a home?, we can see that millenials like newer houses that have their own defined spaces. Today we are going to be looking at housing data to understand how the sale price of a house is determined. Finding highly detailed housing data with the final sale prices is very hard, however researchers from Truman State Univeristy have studied and made available a dataset containing multiple variables for the city of Ames, Iowa. The data set describes the sale of individual residential property in Ames, Iowa from 2006 to 2010. You can read more about the data set here. Today we will be looking at 5 different variables to predict the sale price of a house. These variables are:
Lot Area:
lot_area
Year Built:
year_built
Basement Square Footage:
bsmt_sf
First Floor Square Footage:
first_sf
Second Floor Square Footage:
second_sf
We are going to be looking at two approaches, linear regression and KNN regression. First, load the data with the script given below.
Question 2.1
{points: 3}
Split the data into a train dataset and a test dataset, based on a 70%-30% train-test split. Use set.seed(2019)
. Remember that we want to predict the sale_price
based on all of the other variables.
Assign the objects to X_train
, Y_train
, X_test
, and Y_test
respectively.
Use 2019 as your seed for the split.
Question 2.2
{points: 3}
Let's start by exploring the training data. Use the ggpairs()
function from the GGally package to explore the relationships between the different variables.
Assign your plot object to a variable named answer2.2
.
Question 2.3
{points: 3}
Now that we have seen the multiple relationships between the variables, which one(s) do you think will be strong predictors for sale_price
? On what do you base this? If you think none would be good predictors, explain why.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 2.4 - Linear Regression
{points: 3}
Fit a linear regression model with X_train
and y_train
using all the variables in the data set and save it to an object called lm_reg
. Extract the coefficients of the model and assign them to an object named lm_coefs.
Question 2.5
{points: 3}
Provide a brief interpretation of the coefficients above and discuss the relationship between sale_price
and each variable (i.e., does the slope indicate a positive or negative relationship between house sale price and each of the predictors).
You don't have to interpret the intercept.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 2.6
{points: 3}
Use the coefficients above to write a mathematical equation for your prediction model. You can round the coefficients to two decimal places for this.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 2.7
{points: 3}
Could we easily visualize the predictions of the model above as a line or a plane in a single plot? If so, explain how. If not, explain why not.
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 2.8
{points: 3}
We need to evaluate how well our model is doing. For this calculate the RMSPE of the linear regression model and assign it to an object named lm_rmspe
.
Question 2.9
{points: 3}
Above we calculated to assess how well our model predicts on new data. Why did we not calculate to answer that question? (hint - think of the definitions of and )
DOUBLE CLICK TO EDIT THIS CELL AND REPLACE THIS TEXT WITH YOUR ANSWER.
Question 2.10
(optional)
"Logarithmically transforming variables in a regression model is a very common way to handle situations where a non-linear relationship exists between the independent and dependent variables. Using the logarithm of one or more variables instead of the un-logged form makes the effective relationship non-linear, while still preserving the linear model." - Linear Regression Models with Logarithmic Transformations
Take the logarithm of the sale_price
variable and fit your linear model again. Do you have a lower RMSPE? Do you have the same model as in question 1.4?