绘制亚组比例差异图:基于时间序列分组调查数据
Hey there! Let's walk through how to visualize the difference in outcome proportions between your treatment (T) and control (C) groups across each date subgroup. We'll use the tidyverse ecosystem for data wrangling and ggplot2 for plotting—super straightforward and flexible for this task.
Step 1: Load Required Packages
First, make sure you have the tidyverse installed (if not, run install.packages("tidyverse")), then load it:
library(tidyverse)
Step 2: Prepare Your Data
We'll start with your existing data, then calculate the proportion of outcome=1 for each group on each date, followed by the difference (T proportion minus C proportion) for each date.
Here's the full code including your data generation, plus the wrangling step:
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) ) # Convert outcome to numeric first for easier proportion calculation Data <- Data %>% mutate(outcome = as.numeric(outcome)) # Calculate proportion of outcome=1 per group and date, then compute the difference prop_diff_data <- Data %>% group_by(date, treatment_group) %>% summarize( prop_outcome1 = mean(outcome), n = n(), # Optional: track sample size per subgroup .groups = "drop" ) %>% pivot_wider( names_from = treatment_group, values_from = prop_outcome1 ) %>% mutate(prop_diff = T - C) # Treatment proportion minus control proportion
Step 3: Visualize the Proportion Differences
Now we can plot the difference over time. A line plot with points works great here—we can also add a horizontal line at 0 to easily see when treatment has a higher/lower proportion than control:
ggplot(prop_diff_data, aes(x = date, y = prop_diff)) + geom_hline(yintercept = 0, color = "gray50", linetype = "dashed") + # Reference line for no difference geom_line(color = "#2c3e50", linewidth = 1) + geom_point(size = 3, color = "#e74c3c") + labs( title = "Difference in Outcome=1 Proportions (Treatment vs Control)", x = "Date", y = "Proportion Difference (T - C)" ) + theme_minimal() + theme( plot.title = element_text(hjust = 0.5, face = "bold"), axis.title = element_text(face = "bold") )
Bonus: Add Confidence Intervals (Optional)
If you want to include 95% confidence intervals for the proportion differences (to show uncertainty), you can modify the data wrangling step to calculate them using the binomial proportion formula:
prop_diff_data_with_ci <- Data %>% group_by(date, treatment_group) %>% summarize( prop_outcome1 = mean(outcome), n = n(), se = sqrt(prop_outcome1 * (1 - prop_outcome1) / n), # Standard error ci_low = prop_outcome1 - 1.96 * se, ci_high = prop_outcome1 + 1.96 * se, .groups = "drop" ) %>% pivot_wider( names_from = treatment_group, values_from = c(prop_outcome1, se, ci_low, ci_high) ) %>% mutate( prop_diff = prop_outcome1_T - prop_outcome1_C, diff_ci_low = ci_low_T - ci_high_C, # Approximate CI for difference diff_ci_high = ci_high_T - ci_low_C ) # Plot with confidence intervals ggplot(prop_diff_data_with_ci, aes(x = date, y = prop_diff)) + geom_hline(yintercept = 0, color = "gray50", linetype = "dashed") + geom_errorbar(aes(ymin = diff_ci_low, ymax = diff_ci_high), width = 0.2, color = "#2c3e50") + geom_line(color = "#2c3e50", linewidth = 1) + geom_point(size = 3, color = "#e74c3c") + labs( title = "Difference in Outcome=1 Proportions (T vs C) with 95% CIs", x = "Date", y = "Proportion Difference (T - C)" ) + theme_minimal() + theme( plot.title = element_text(hjust = 0.5, face = "bold"), axis.title = element_text(face = "bold") )
Key Notes:
- We convert
outcometo numeric somean()gives us the proportion of 1s directly (since mean of 0s and 1s is the proportion of 1s). - The
pivot_widerstep lets us easily compute the difference between T and C proportions for each date. - The confidence interval calculation here is a simplified approximation—for more precise CIs for proportion differences, you could use the
prop.test()function in a nested group, but this works well for visualization purposes.
内容的提问来源于stack exchange,提问作者Ivo

