始める無料で始める

P値を可視化する

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

最初の準備として、group_duration_shortgroup_duration_long、および compute_test_statistic()shuffle_and_split()plot_test_statistic_effect() をあらかじめ読み込んであります。

この演習はコースの一部です

Pythonで学ぶ線形モデリング入門

コースを見る

演習の手順

  • compute_test_statistic() を使って group_duration_shortgroup_duration_long から test_statistic_unshuffled を計算し、np.mean() で効果量を求めます。
  • shuffle_and_split() を使って shuffle_half1shuffle_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)
コードを編集して実行