計算 RMSE
在這個練習中,你要計算你的失業率模型的 RMSE。前一個程式練習中,你在 unemployment 資料集中新增了兩個欄位:
- 模型的預測值(
predictions欄) - 預測與實際結果之間的殘差(
residuals欄)
你可以從殘差向量 \(res\) 計算 RMSE:
$$ RMSE = \sqrt{\operatorname{mean}(res^2)} $$
RMSE 越小越好。那「小」到什麼程度才算小呢?一個經驗法則是把 RMSE 和目標變數的標準差做比較。好的模型其 RMSE 應該更小。
unemployment 資料框已為你載入。
本練習屬於課程
R 中的監督式學習:回歸
練習說明
- 檢視上一個練習的
unemployment資料。 - 為了方便使用,將
unemployment中的residuals欄指定給變數res。 - 計算 RMSE:先把
res平方,取其平均,再開根號。將結果指定給變數rmse並印出。- 小技巧:你可以用一行完成,把指定動作包在括號中:
(rmse <- ___)
- 小技巧:你可以用一行完成,把指定動作包在括號中:
- 計算
female_unemployment的標準差並指定給變數sd_unemployment。將其印出。 「模型的 rmse 與資料的標準差相比如何?」
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Print a summary of unemployment
summary(unemployment)
# For convenience put the residuals in the variable res
res <- ___
# Calculate RMSE, assign it to the variable rmse and print it
(rmse <- ___)
# Calculate the standard deviation of female_unemployment and print it
(sd_unemployment <- ___)