You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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-010.0490.2070.00020.414
2021-01-020.0180.2020.0360.367
2021-01-030.2470.072-0.0520.197
2021-01-040.00030.2060.0960.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 10:57:03