Path: blob/master/2019-spring/materials/worksheet_09/worksheet_09.ipynb
2715 views
Worksheet 9 - Regression cont'd
Lecture and Tutorial Learning Goals:
By the end of the week, students will be able to:
In the context of k-nn regression, compare and contrast goodness of fit and prediction properties (namely RMSE vs RMSPE).
In a dataset with 2 variables, perform simple ordinary least squares regression in R using
caret'strainwithmethod = "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.
Understanding and
Question 1.0
What does stand for?
A. root mean squared prediction error
B. root mean squared percentage error
C. root mean squared performance error
D. root mean squared preference error
Save the letter of the answer you think is correct to a variable named answer1.0. Make sure you put quotations around the letter and pay attention to case.
Question 1.1
Of those shown below, which is the correct formula for ?
A.
B.
C.
D.
Save the letter of the answer you think is correct to a variable named answer1.1. Make sure you put quotations around the letter and pay attention to case.
Question 1.2
Which statement(s) below is/are incorrect?
A. is a measure of model goodness of fit
B. is a measure of how well the model predicts on the training data
C. is a measure of how well the model predicts on the testing data
D. is a measure of how well the model predicts on the training data
E. is a measure of how well the model predicts on the testing data
Save the letter of the answer(s) you think are incorrect to a variable named answer1.2. Save these as a character vector (scaffolding and example given below). Make sure you put quotations around the letter and pay attention to case.
Understanding Linear Regression
Consider this small and simple data set:
Now consider these three potential lines of best fit for the same data set:
Question 2.0
Use the graph titled "Line A" to roughly calculate the average squared vertical distance between the points and the blue line. Save your answer to a variable named answer2.0.
We re-reprint the plot for you in a larger size to make it easier to estimate the locations on the graph.
Question 2.1
Use the graph titled "Line B" to roughly calculate the average squared vertical distance between the points and the purple line. Save your answer to a variable named answer2.1.
We re-reprint the plot for you in a larger size to make it easier to estimate the locations on the graph.
Question 2.2
Use the graph titled "Line C" to roughly calculate the average squared vertical distance between the points and the green line. Save your answer to a variable named answer2.2.
We re-reprint the plot for you in a larger size to make it easier to estimate the locations on the graph.
Question 2.3
Based on your calculations above, which line would linear regression by ordinary least squares choose given our small and simple data set? Line A, B or C? Assign the letter that corresponds the line to a variable named answer2.3. Make sure you put quotations around the letter and pay attention to case.
Marathon training revisited with linear regression!

Source: https://media.giphy.com/media/BDagLpxFIm3SM/giphy.gif
Remeber our question from last week: what predicts which athletes will perform better than others? Specifically, we are interested in marathon runners, and looking at how the maximum distance ran per week during training predicts the time it takes a runner to end the race?
This time around however we will analyze the data using linear regression. And then in the end we will compare our results to what we found last week with k-nn regression.
Question 3.0
Load the data and assign it to an object called marathon.
Question 3.1
Using all the observations in the data set, create a scatterplot of to assess the relationship between race time (time_hrs) given a particular value of maximum distance ran per week during training (max). Put time_hrs on the y-axis and max on the x-axis. Assign this plot to an object called marathon_eda. Remember to do all the things to make this an effective visualization.
Question 3.2
Create a training and testing dataset using 75% of the data as training data. Use set.seed(2000) and the max column as the input to createDataPartition (as we did in the last worksheet) so that we end up with the same training data set for linear regression that we had for k-nn regression (so we can compare our results between these two weeks).
At the end of thiw question you should have 4 objects named X_train, Y_train, X_test and Y_test.
Question 3.3
Now use caret's train function with method = "lm" to fit you linear regression model. Name your linear regression model object lm_model.
Question 3.4
Now, let's visualize the model predictions as a straight line overlaid on the training data. Use geom_smooth with method = "lm" and se = FALSE to visualize the predictions as a straight line. Name your plot lm_predictions.
Question 3.5
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 3.6
Calculate using the test data. Return a single numerical value named lm_rmspe.
Question 3.7
Compare RMSPE between k-nn and linear regression, which is greater?
A. Linear regression has a greater RMSPE
B. k-nn regression has a greater RMSPE
Save the letter of the answer you think is correct to a variable named answer3.7. Make sure you put quotations around the letter and pay attention to case.
Question 3.8
Which model does a better job of predicting on the test data set?
A. Linear regression
B. k-nn regression
C. Linear regression, but only very slightly
D. k-nn regression, but only very slightly
Save the letter of the answer you think is correct to a variable named answer3.8. Make sure you put quotations around the letter and pay attention to case.
Question 3.9
(optional - not graded)
Given that the linear regression model is a straight line, we can write our model as a mathematical equation. We can get the two numbers we need for this (y-intercept and slope) from the finalModel attribute from our model object as shown below:
Use the numbers output in the cell above to write the model as a mathematical equation.
YOUR ANSWER HERE