P値を可視化する
この演習では、推定した効果(「speed」)が、サンプル内の偶然のばらつきによって生じた可能性、つまり p 値を可視化します。具体的には、シャッフルして得た検定統計量の分布において、非シャッフルのサンプルから計算した検定統計量(「効果量」)の平均より右側に位置する点の割合として表現します。
最初の準備として、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()で効果量を求めます。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)