开始使用免费开始使用

可视化 P 值

在本练习中,您将可视化 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)
编辑并运行代码