相關與 R-squared
兩個變數 \(x\) 與 \(y\) 的線性相關,衡量它們之間線性關係的強度。當 \(x\) 與 \(y\) 分別是:
- 以最小平方誤差為目標(如線性迴歸)的迴歸模型之預測結果,及
- 訓練資料的真實結果,
則其相關係數的平方會等於 $R^2$。你將在本練習中驗證這件事。
unemployment 與 unemployment_model 已可供你使用。
本練習屬於課程
R 中的監督式學習:回歸
練習說明
- 使用
cor()(說明文件)計算模型預測與女性失業率之間的相關係數。將結果指定給變數rho並列印出來。請確保使用 Pearson 相關(預設值)。 - 將
rho平方並指定給rho2,再列印出來。 - 將
rho2與模型的 $R^2$(使用glance()取得)相比較。是否相同?
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# unemployment is available
summary(unemployment)
# unemployment_model is available
summary(unemployment_model)
# Get the correlation between the prediction and true outcome: rho and print it
(rho <- ___)
# Square rho: rho2 and print it
(rho2 <- ___)
# Get R-squared from glance and print it
(rsq_glance <- ___(___)$___)