開始使用免費開始

檢定統計量與效果量

如何用 bootstrap 重採樣來探索線性關係?回到健行的例子吧!每一條路線在圖上是一個點,我們可以看到總行走距離與經過時間之間呈現線性關係。若把行走距離視為經過時間的「效果」,就能進一步探索線性迴歸與統計推論之間的連結。

在這個練習中,你會把資料分成兩個母群(或「類別」):較早的時間與較晚的時間。接著,你會比較這兩個母群中總行走距離的「差異」。這個「差異」會作為「檢定統計量」,而它的分佈將用來檢驗以時間分組對距離的影響。

ch04_ex11_fig03.png

本練習屬於課程

Python 線性建模入門

檢視課程

練習說明

  • 使用 numpy 的「邏輯索引」,例如 sample_distances[sample_times < 5],將範例 distances 分成較早與較晚兩個時間族群。
  • 使用 replacement=Truenp.random.choice(),為兩個時間分箱各建立一個 resample
  • test_statistic 陣列計算為 resample_long - resample_short,並用 np.mean()np.std() 求出並印出效果量與不確定度。
  • 使用預先定義的 fig = plot_test_statistic() 繪製 test_statistic 的分佈。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create two poulations, sample_distances for early and late sample_times.
# Then resample with replacement, taking 500 random draws from each population.
group_duration_short = sample_distances[____ < 5]
group_duration_long = sample_distances[____ > 5]
resample_short = np.random.choice(____, size=500, replace=____)
resample_long = np.random.choice(____, size=500, replace=____)

# Difference the resamples to compute a test statistic distribution, then compute its mean and stdev
test_statistic = resample_long - resample_short
effect_size = np.mean(____)
standard_error = np.std(____)

# Print and plot the results
print('Test Statistic: mean={:0.2f}, stdev={:0.2f}'.format(____, ____))
fig = plot_test_statistic(____)
編輯並執行程式碼