量化模型擬合

使用 Python 中的 statsmodels 進行回歸入門

Maarten Van den Broeck

Content Developer at DataCamp

Bream 與 Perch 模型

鯉科歐鯿(Bream) 先前出現過的散佈圖:bream 體重對長度,含趨勢線。

鱸魚(Perch) 先前出現過的散佈圖:perch 體重對長度,含趨勢線。

使用 Python 中的 statsmodels 進行回歸入門

判定係數

也稱為「r-squared」或「R-squared」。

響應變數變異中,由解釋變數可預測的比例

  • 1 代表完全擬合
  • 0 代表最差的擬合
使用 Python 中的 statsmodels 進行回歸入門

.summary()

查看標為「R-Squared」的數值。

mdl_bream = ols("mass_g ~ length_cm", data=bream).fit()
print(mdl_bream.summary())
# 部分輸出省略                          

                            OLS Regression Results                         
Dep. Variable:                 mass_g   R-squared:                       0.878
Model:                            OLS   Adj. R-squared:                  0.874
Method:                 Least Squares   F-statistic:                     237.6
使用 Python 中的 statsmodels 進行回歸入門

.rsquared 屬性

print(mdl_bream.rsquared)
0.8780627095147174
使用 Python 中的 statsmodels 進行回歸入門

其實就是相關係數平方

coeff_determination = bream["length_cm"].corr(bream["mass_g"]) ** 2
print(coeff_determination)
0.8780627095147173
使用 Python 中的 statsmodels 進行回歸入門

殘差標準誤(RSE)

先前看到的:bream 體重對長度的殘差圖

  • 預測值與觀測值的「典型」差距
  • 單位與響應變數相同。
  • MSE = RSE²
使用 Python 中的 statsmodels 進行回歸入門

.mse_resid 屬性

mse = mdl_bream.mse_resid
print('mse: ', mse)
mse:  5498.555084973521
rse = np.sqrt(mse)
print("rse: ", rse)
rse:  74.15224261594197
使用 Python 中的 statsmodels 進行回歸入門

計算 RSE:殘差平方

residuals_sq = mdl_bream.resid ** 2

print("residuals sq: \n", residuals_sq)
residuals sq: 
0      138.957118
1      260.758635
2     5126.992578
3     1318.919660
4      390.974309
    ...
30    2125.047026
31    6576.923291
32     206.259713
33     889.335096
34    7665.302003
Length: 35, dtype: float64
使用 Python 中的 statsmodels 進行回歸入門

計算 RSE:殘差平方和

residuals_sq = mdl_bream.resid ** 2

resid_sum_of_sq = sum(residuals_sq)

print("resid sum of sq :",
      resid_sum_of_sq)
resid sum of sq : 181452.31780412616
使用 Python 中的 statsmodels 進行回歸入門

計算 RSE:自由度

residuals_sq = mdl_bream.resid ** 2

resid_sum_of_sq = sum(residuals_sq)

deg_freedom = len(bream.index) - 2

print("deg freedom: ", deg_freedom)

自由度 等於觀測數減去模型係數數量。

deg freedom:  33
使用 Python 中的 statsmodels 進行回歸入門

計算 RSE:比值開根號

residuals_sq = mdl_bream.resid ** 2

resid_sum_of_sq = sum(residuals_sq)

deg_freedom = len(bream.index) - 2

rse = np.sqrt(resid_sum_of_sq/deg_freedom)

print("rse :", rse)
rse : 74.15224261594197
使用 Python 中的 statsmodels 進行回歸入門

解讀 RSE

mdl_bream 的 RSE 為 74

對 bream 的預測體重與實際體重,典型差距約 74g。

使用 Python 中的 statsmodels 進行回歸入門

均方根誤差(RMSE)

residuals_sq = mdl_bream.resid ** 2

resid_sum_of_sq = sum(residuals_sq)

deg_freedom = len(bream.index) - 2

rse = np.sqrt(resid_sum_of_sq/deg_freedom)

print("rse :", rse)
rse : 74.15224261594197
residuals_sq = mdl_bream.resid ** 2

resid_sum_of_sq = sum(residuals_sq)

n_obs = len(bream.index)

rmse = np.sqrt(resid_sum_of_sq/n_obs)

print("rmse :", rmse)
rmse : 72.00244396727619
使用 Python 中的 statsmodels 進行回歸入門

一起來練習吧!

使用 Python 中的 statsmodels 進行回歸入門

Preparing Video For Download...