Evaluate a model using test/train split
Now you will test the model mpg_model on the test data, mpg_test. 
Functions rmse() and r_squared()  to calculate RMSE and R-squared have been provided for convenience:
rmse(predcol, ycol)
r_squared(predcol, ycol)
where:
- predcol: The predicted values
- ycol: The actual outcome
You will also plot the predictions vs. the outcome.
Generally, model performance is better on the training data than the test data (though sometimes the test set "gets lucky"). A slight difference in performance is okay; if the performance on training is significantly better, there is a problem.
The mpg_train and mpg_test data frames, and the mpg_model model have been pre-loaded, along with the functions rmse() and r_squared().
Este exercício faz parte do curso
Supervised Learning in R: Regression
Instruções do exercício
- Predict city fuel efficiency from hwyon thempg_traindata. Assign the predictions to the columnpred.
- Predict city fuel efficiency from hwyon thempg_testdata. Assign the predictions to the columnpred.
- Use rmse()to evaluate RMSE for both the test and training sets. Compare. Are the performances similar?
- Do the same with r_squared(). Are the performances similar?
- Use ggplot2to plot the predictions againstctyon thetestdata.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Examine the objects that have been loaded
ls.str()
# predict cty from hwy for the training set
mpg_train$pred <- ___
# predict cty from hwy for the test set
mpg_test$pred <- ___
# Evaluate the rmse on both training and test data and print them
(rmse_train <- ___)
(rmse_test <- ___)
# Evaluate the r-squared on both training and test data.and print them
(rsq_train <- ___)
(rsq_test <- ___)
# Plot the predictions (on the x-axis) against the outcome (cty) on the test data
ggplot(___, aes(x = ___, y = ___)) + 
  geom_point() + 
  geom_abline()