Making predictions
Okey, so let's assume that we have a linear model which seems to fit our standards. What can we do with it?
The model quantifies the relationship between the explanatory variable(s) and the dependent variable. The model can also be used for predicting the dependent variable based on new observations of the explanatory variable(s).
In R, predicting can be done using the predict()
function. (see ?predict
). The first argument of predict is a model object and the argument newdata
(a data.frame) can be used to make predictions based on new observations. One or more columns of newdata
should have the same name as the explanatory variables in the model object.
This exercise is part of the course
Helsinki Open Data Science
Exercise instructions
- Create object
m
and print out a summary of the model - Create object
new_attitudes
- Adjust the code: Create a new data frame with a column named 'attitude' holding the new attitudes defined in
new_attitudes
- Print out the new data frame
predict()
the new student's exam points based on their attitudes, using thenewdata
argument
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# learning2014 is available
# Create model object m
m <- lm(points ~ attitude, data = learning2014)
# print out a summary of the model
# New observations
new_attitudes <- c("Mia" = 3.8, "Mike"= 4.4, "Riikka" = 2.2, "Pekka" = 2.9)
new_data <- data.frame(attitude = "change me!")
# Print out the new data
# Predict the new students exam points based on attitude
predict(m, newdata = "change me!")