绘制子组比例之间的差异

时间:2018-04-05 13:28:53

标签: r ggplot2

我有一个数据框(调查数据)随着时间的推移,感兴趣的结果(1T)适用于两组(T用于控制,set.seed(3546) Data <- data.frame( date = sample((as.Date(as.Date("2011-12-30"):as.Date("2012-01-04"), origin="1970-01-01")), 1000, replace = TRUE), treatment_group = sample(c("C", "T"), 1000, replace = TRUE), outcome = sample(c("1", "0"), 1000, replace = TRUE) ) 用于治疗),如下所示:

1

为此,我分别绘制了两组显示结果Data %>% mutate(treatment_group = factor(treatment_group, levels = c("T", "C")), date = as.POSIXct(date)) %>% group_by(treatment_group, date) %>% summarise(prop = sum(outcome=="1")/n()) %>% #calculate proportion ggplot() + theme_classic() + xlab("Date") + ylab('Proportion outcome mentioned')+ scale_color_manual(values = c('C' = 'black', 'T' = 'darkgrey'), labels = c('C' = 'Remaining sample', 'T' = 'Treated Group'), name = "Legend") + geom_smooth(aes(x = date, y = prop, color = treatment_group), se = F, method = 'loess') + geom_point(aes(x = date, y = prop, color = treatment_group)) 的比例,我用以下代码进行了分析:

1

我得到以下情节: Proportion of the outcome "1" by group

我想要的 - 但无法弄清楚如何 - 是一行显示每个时间点的值与相应置信度之间的 差异 间隔(对于比例差异的点估计),大致相同(显然风格将保持不变 - 只是为了给你一个想法) desired sample plot

该行应指明该特定日期结果<?php $query = "INSERT INTO details (username, password, email) VALUES('$username', '$pass', '$email')"; mysqli_query($database, $query); echo "<script>window.open(''index.php','_self')</script>"; $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; ?> 的比例之间的差异。非常感谢您的帮助。 :)

1 个答案:

答案 0 :(得分:1)

如果您对prop中的不确定性没有任何衡量标准,您希望如何计算CI?

除此之外,您可以通过以下方式重新设定日期以绘制比例差异:

Data %>%
    mutate(
        treatment_group = factor(treatment_group, levels = c("T", "C")),
        date = as.POSIXct(date)) %>% #convert date to date
    group_by(treatment_group, date) %>% #group
    summarise(
        prop = sum(outcome == "1") / n()) %>% #calculate proportion
    spread(treatment_group, prop) %>%
    mutate(propdiff = T - C) %>%
    ggplot(aes(date, propdiff)) +
    geom_line() + 
    geom_point()

enter image description here

说明:关注summarise后,我们会将数据从long转换为wide,并将propdiff计算为prop(T) - prop(C)