首页 > 解决方案 > 如何使用ggplot2随着时间的推移绘制p值?

问题描述

所以我有一些看起来像这样的数据:

DATE        GROUP      Value    Visitors
2021-01-01  Treatment  12       40
2021-01-01  Control    4        43
2021-01-02  Treatment  7        34
2021-01-02  Control    2        39
2021-01-03  Treatment  10       23
2021-01-03  Control    10       29
2021-01-04  Treatment  19       30
2021-01-04  Control    7        23

如果你总结所有这些数据,实验结束时的最终结果是

Group       Value Visitors Conversion (Value/Size)
Control     23    134      .172
Treatment   48    127      .378

所以我需要计算这个数据的 p 值和置信区间(即转换),不仅在最后,而且在实验过程中,使用 t 检验。

我在这里寻找的是一个折线图,它绘制了 p 值如何随时间累积变化。我真的想不出任何方法来绘制随时间变化的置信区间,所以一张每日置信区间增长表就足够了

标签: rtidyverseconfidence-intervalp-valuet-test

解决方案


那是你要找的吗?

df <- read.table(textConnection('DATE        GROUP      Value    Visitors
2021-01-01  Treatment  12       40
2021-01-01  Control    4        43
2021-01-02  Treatment  7        34
2021-01-02  Control    2        39
2021-01-03  Treatment  10       23
2021-01-03  Control    10       29
2021-01-04  Treatment  19       30
2021-01-04  Control    7        23'),header=T)

library(tidyverse)
library(gridExtra)

new_df <- df %>%
mutate(Conversion=Value/Visitors) %>%
group_by(DATE,GROUP) %>%
summarise(Cumulative_Conversion=cumsum(Conversion),.groups='drop') %>%
group_by(DATE) %>%
summarise(P.Value=t.test(Cumulative_Conversion)$p.value,
          Conf.Int=t.test(Cumulative_Conversion)$conf.int,
          Mean=mean(Cumulative_Conversion),.groups='drop') 

new_df %>%
ggplot(aes(x=DATE,y=P.Value,fill=P.Value))+
geom_col() -> plot1 

new_df %>%
ggplot(aes(x=DATE,y=Mean,fill=Mean))+
geom_col()+
geom_point(aes(x=DATE,y=Conf.Int))+
geom_line(aes(x=DATE,y=Conf.Int)) -> plot2


final_plot <-grid.arrange(plot1,plot2)

final_plot

在此处输入图像描述


推荐阅读