視覺化 P 值
在此練習中,你要視覺化 p 值,也就是我們所估計的效果(或「速度」)來自樣本隨機變動的機率。你的目標是,將其視覺化為:在經過洗牌的檢定統計量分佈中,落在未洗牌樣本所計算之檢定統計量平均值(「effect size」)右側的點所佔比例。
為了幫你開始,我們已預先載入 group_duration_short、group_duration_long,以及函式 compute_test_statistic()、shuffle_and_split() 和 plot_test_statistic_effect()。
本練習屬於課程
Python 線性建模入門
練習說明
- 使用
compute_test_statistic()從group_duration_short和group_duration_long取得test_statistic_unshuffled;接著用np.mean()計算 effect size。 - 使用
shuffle_and_split()建立shuffle_half1與shuffle_half2,並用compute_test_statistic()計算test_statistic_shuffled。 - 建立布林遮罩
condition,使test_statistic_shuffled的值大於或等於effect_size,然後使用此遮罩計算p_value。 - 列印
p_value,並使用plot_test_statistic_effect()繪製兩個檢定統計量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compute the test stat distribution and effect size for two population groups
test_statistic_unshuffled = compute_test_statistic(____, ____)
effect_size = np.mean(____)
# Randomize the two populations, and recompute the test stat distribution
shuffled_half1, ____ = shuffle_and_split(group_duration_short, ____)
test_statistic_shuffled = compute_test_statistic(shuffled_half1, ____)
# Compute the p-value as the proportion of shuffled test stat values >= the effect size
condition = ____ >= ____
p_value = len(test_statistic_shuffled[____]) / len(test_statistic_shuffled)
# Print p-value and overplot the shuffled and unshuffled test statistic distributions
print("The p-value is = {}".format(____))
fig = plot_test_stats_and_pvalue(test_statistic_unshuffled, test_statistic_shuffled)