線性迴歸
對 1975 年與 2012 年的資料各自執行一次線性迴歸。接著,對迴歸參數進行配對自助(pairs bootstrap)估計。回報迴歸線斜率與截距的 95% 信賴區間。
你會使用你在【第 2 章】撰寫的 draw_bs_pairs_linreg() 函式。
提醒:它的呼叫介面為 draw_bs_pairs_linreg(x, y, size=1),並會回傳 bs_slope_reps 與 bs_intercept_reps。喙長資料分別儲存在 bl_1975 與 bl_2012,而喙深資料則儲存在 bd_1975 與 bd_2012。
本練習屬於課程
Statistical Thinking in Python(第 2 部分)
練習說明
- 計算 1975 年與 2012 年兩個資料集的斜率與截距。
- 使用你的
draw_bs_pairs_linreg()函式,為線性迴歸取得 1000 組配對自助樣本。 - 計算斜率與截距的 95% 信賴區間。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compute the linear regressions
slope_1975, intercept_1975 = ____
slope_2012, intercept_2012 = ____
# Perform pairs bootstrap for the linear regressions
bs_slope_reps_1975, bs_intercept_reps_1975 = \
____
bs_slope_reps_2012, bs_intercept_reps_2012 = \
____
# Compute confidence intervals of slopes
slope_conf_int_1975 = ____
slope_conf_int_2012 = ____
intercept_conf_int_1975 = ____
intercept_conf_int_2012 = ____
# Print the results
print('1975: slope =', slope_1975,
'conf int =', slope_conf_int_1975)
print('1975: intercept =', intercept_1975,
'conf int =', intercept_conf_int_1975)
print('2012: slope =', slope_2012,
'conf int =', slope_conf_int_2012)
print('2012: intercept =', intercept_2012,
'conf int =', intercept_conf_int_2012)