從失業率模型進行預測
在這個練習中,你會用你的失業率模型 unemployment_model,對 unemployment 資料做預測,並把預測的女性失業率與訓練資料 unemployment 中實際觀察到的女性失業率做比較。你也會用模型對 newrates 中的新資料進行預測;這份資料只有 1 筆觀測,男性失業率為 5%。
lm 模型的 predict()(docs)介面如下:
predict(model, newdata)
你會使用 ggplot2 套件來繪圖,所以請先把預測結果新增為 unemployment 資料框中的一個欄位。你將繪製實際值對上預測值,並與代表「完美預測」(也就是實際值等於預測值)的那條線做比較。
繪製 dframe$outcome 對 dframe$pred 的散佈圖(pred 在 x 軸、outcome 在 y 軸),並加上一條 outcome == pred 的藍色直線的 ggplot2 指令如下:
ggplot(dframe, aes(x = pred, y = outcome)) +
geom_point() +
geom_abline(color = "blue")
unemployment、unemployment_model 和 newrates 已為你預先載入。
本練習屬於課程
R 中的監督式學習:回歸
練習說明
- 使用
predict()以unemployment資料預測女性失業率。將結果指定到新欄位prediction。 - 使用
library()載入ggplot2套件。 - 使用
ggplot()比較預測值與實際失業率。把預測值放在 x 軸。結果與完美預測線有多接近? - 使用資料框
newrates,在男性失業率為 5% 時預測女性失業率。將結果指定給變數pred,並印出它。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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