Get startedGet started for free

Predicting from the unemployment model

In this exercise, you will use your unemployment model unemployment_model to make predictions from the unemployment data, and compare predicted female unemployment rates to the actual observed female unemployment rates on the training data, unemployment. You will also use your model to predict on the new data in newrates, which consists of only one observation, where male unemployment is 5%.

The predict() (docs) interface for lm models takes the form

predict(model, newdata)

You will use the ggplot2 package to make the plots, so you will add the prediction column to the unemployment data frame. You will plot outcome versus prediction, and compare them to the line that represents perfect predictions (that is when the outcome is equal to the predicted value).

The ggplot2 command to plot a scatterplot of dframe$outcome versus dframe$pred (pred on the x axis, outcome on the y axis), along with a blue line where outcome == pred is as follows:

ggplot(dframe, aes(x = pred, y = outcome)) + 
       geom_point() +  
       geom_abline(color = "blue")

unemployment, unemployment_model, and newrates have been pre-loaded for you.

This exercise is part of the course

Supervised Learning in R: Regression

View Course

Exercise instructions

  • Use predict() to predict female unemployment rates from the unemployment data. Assign it to a new column: prediction.
  • Use the library() command to load the ggplot2 package.
  • Use ggplot() to compare the predictions to actual unemployment rates. Put the predictions on the x axis. How close are the results to the line of perfect prediction?
  • Use the data frame newrates to predict expected female unemployment rate when male unemployment is 5%. Assign the answer to the variable pred and print it.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# unemployment is available
summary(unemployment)

# newrates is available
newrates

# Predict female unemployment in the unemployment dataset
unemployment$prediction <-  ___

# Load the ggplot2 package
___

# Make a plot to compare predictions to actual (prediction on x axis). 
ggplot(___, aes(x = ___, y = ___)) + 
  ___ +
  geom_abline(color = "blue")

# Predict female unemployment rate when male unemployment is 5%
pred <- ___
pred
Edit and Run Code