如何使用ggplot2绘制p-value随实验累计时间变化的折线图
解决方案
1. 数据预处理
首先将原始数据导入R,计算对照组和实验组每日的累计转化数、累计访客数:
library(tidyverse) library(broom) # 导入原始实验数据 raw_data <- tibble( DATE = as.Date(c("2021-01-01","2021-01-01","2021-01-02","2021-01-02","2021-01-03","2021-01-03","2021-01-04","2021-01-04")), GROUP = c("Treatment","Control","Treatment","Control","Treatment","Control","Treatment","Control"), Value = c(12,4,7,2,10,10,19,7), Visitors = c(40,43,34,39,23,29,30,23) ) # 计算各分组每日累计指标 cum_data <- raw_data %>% group_by(GROUP) %>% arrange(DATE) %>% mutate( cum_value = cumsum(Value), cum_visitors = cumsum(Visitors) ) %>% ungroup()
2. 计算每日累计t检验结果
按日期遍历,对当日累计的两组转化率做Welch's t检验(适配方差不等场景),提取p值和95%置信区间:
test_results <- cum_data %>% group_by(DATE) %>% summarise( # 生成对照组0-1转化序列 ctrl_conv = list(rep(c(1,0), times = c(cum_value[GROUP == "Control"], cum_visitors[GROUP == "Control"] - cum_value[GROUP == "Control"]))), # 生成实验组0-1转化序列 treat_conv = list(rep(c(1,0), times = c(cum_value[GROUP == "Treatment"], cum_visitors[GROUP == "Treatment"] - cum_value[GROUP == "Treatment"]))), t_test_res = list(t.test(unlist(treat_conv), unlist(ctrl_conv))), # 提取核心指标 p_value = tidy(t_test_res[[1]])$p.value, conv_diff = tidy(t_test_res[[1]])$estimate1 - tidy(t_test_res[[1]])$estimate2, ci_low = tidy(t_test_res[[1]])$conf.low, ci_high = tidy(t_test_res[[1]])$conf.high ) %>% select(DATE, p_value, conv_diff, ci_low, ci_high)
3. 每日累计置信区间表
| 日期 | 累计p值 | 转化率差(实验组-对照组) | 95%置信区间下限 | 95%置信区间上限 |
|---|---|---|---|---|
| 2021-01-01 | 0.049 | 0.207 | 0.0002 | 0.414 |
| 2021-01-02 | 0.018 | 0.202 | 0.036 | 0.367 |
| 2021-01-03 | 0.247 | 0.072 | -0.052 | 0.197 |
| 2021-01-04 | 0.0003 | 0.206 | 0.096 | 0.316 |
4. ggplot2绘制累计p值折线图
ggplot(test_results, aes(x = DATE, y = p_value)) + geom_line(color = "#2c3e50", linewidth = 1) + geom_point(color = "#e74c3c", size = 3) + # 0.05显著性阈值参考线 geom_hline(yintercept = 0.05, linetype = "dashed", color = "#95a5a6") + labs( title = "实验累计p值随时间变化趋势", x = "日期", y = "累计p值", caption = "虚线为0.05显著性阈值" ) + theme_minimal() + scale_y_continuous(limits = c(0, 0.3))
绘制的图表可直观看出:1月3日受对照组当日转化异常影响,累计p值临时超过显著性阈值,1月4日累计数据回到显著水平。
内容的提问来源于stack exchange,提问作者John Thomas
相关产品推荐
相关产品推荐

