library(ggplot2)
library(dplyr)
set.seed(2018-03-23)
# Generate exmaple data
data <- data_frame(Condition = c(rep("Control", 50), rep("Treatment", 75)),
Status = c(rbinom(50, 1, .5), rbinom(75, 1, .35)))
data %>%
slice(c(1:5 , 51:55))
## # A tibble: 10 x 2
## Condition Status
## <chr> <int>
## 1 Control 1
## 2 Control 1
## 3 Control 1
## 4 Control 1
## 5 Control 0
## 6 Treatment 1
## 7 Treatment 1
## 8 Treatment 0
## 9 Treatment 1
## 10 Treatment 0
# Visualize binary proportions in each group
ggplot(data, aes(Status)) +
geom_bar(aes(fill = Condition))
data_avgs <- data %>%
group_by(Condition) %>%
summarize(Avg = mean(Status),
SD = sd(Status),
N = n()) %>%
ungroup() %>%
mutate(L_CI = Avg - 1.96 * sqrt(Avg * (1 - Avg) / N)) %>%
mutate(U_CI = Avg + 1.96 * sqrt(Avg * (1 - Avg) / N))
data_avgs
## # A tibble: 2 x 6
## Condition Avg SD N L_CI U_CI
## <chr> <dbl> <dbl> <int> <dbl> <dbl>
## 1 Control 0.560 0.501 50 0.422 0.698
## 2 Treatment 0.320 0.470 75 0.214 0.426
# Visualize average proportion
ggplot(data_avgs, aes(Condition, Avg)) +
geom_col(fill = "royalblue1") +
geom_errorbar(aes(ymin = L_CI, ymax = U_CI), width = .25)