開始使用免費開始

相關與 R-squared

兩個變數 \(x\) 與 \(y\) 的線性相關,衡量它們之間線性關係的強度。當 \(x\) 與 \(y\) 分別是:

  • 以最小平方誤差為目標(如線性迴歸)的迴歸模型之預測結果,及
  • 訓練資料的真實結果,

則其相關係數的平方會等於 $R^2$。你將在本練習中驗證這件事。

unemploymentunemployment_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 <- ___(___)$___)
編輯並執行程式碼