This function is superseded. Please use either srv_prop_test_ref or srv_prop_test_within.
Usage
srv_add_prop_test(
data,
x,
y,
wt = NULL,
overall = TRUE,
overall_name = "Overall",
p_adjust = "fdr"
)Arguments
- data
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).
- x
Unquoted first variable to group by. If
overall = Fshould be maximum of 2 categories.- y
Unquoted second variable to group by.
- wt
Unquoted weight variable
- overall
Logical. Should the proportions be tested against the overall proportion of the group?
- overall_name
Character. Name of the overall proportion to append to your data (e.g. "Full Sample").
- p_adjust
Character. Should the p.value be adjusted? Use methods provided by
p.adjust
Value
An object of the same type as .data. Will have the count n added,
total count in group sum_n and the test statistics prop_pval, std_error
and ci.
Examples
library(dplyr)
library(titanic)
df <- titanic::titanic_test
df %>%
srv_add_prop_test(x = Sex, y = Embarked, overall = TRUE, overall_name = "overall")
#> # A tibble: 8 × 9
#> Embarked sum_n Sex n percentage prop_pval prop_pval_adj std_error
#> <chr> <int> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 C 102 female 40 0.392 0.674 0.899 0.0483
#> 2 C 102 male 62 0.608 0.674 0.899 0.0483
#> 3 Q 46 female 24 0.522 0.0527 0.211 0.0737
#> 4 Q 46 male 22 0.478 0.0527 0.211 0.0737
#> 5 S 270 female 88 0.326 0.352 0.703 0.0285
#> 6 S 270 male 182 0.674 0.352 0.703 0.0285
#> 7 overall 418 female 152 0.364 1 1 0.0235
#> 8 overall 418 male 266 0.636 1 1 0.0235
#> # ℹ 1 more variable: conf_int <dbl>
df %>%
srv_add_prop_test(x = Sex, y = Embarked, overall = FALSE)
#> # A tibble: 6 × 9
#> Embarked Sex n sum_n percentage prop_pval prop_pval_adj std_error
#> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 C female 40 102 0.392 0.0294 0.0441 0.0483
#> 2 C male 62 102 0.608 0.0294 0.0441 0.0483
#> 3 Q female 24 46 0.522 0.768 0.768 0.0737
#> 4 Q male 22 46 0.478 0.768 0.768 0.0737
#> 5 S female 88 270 0.326 0.0000000106 0.0000000318 0.0285
#> 6 S male 182 270 0.674 0.0000000106 0.0000000318 0.0285
#> # ℹ 1 more variable: conf_int <dbl>
# Easy to use in a chart to plot significant differences
# with confidence intervals:
library(ggplot2)
df %>%
srv_add_prop_test(x = Sex, y = Embarked, overall = FALSE) %>%
ggplot(aes(x = Embarked, y = percentage, fill = Sex)) +
geom_col(position = position_dodge(0.5), width = 0.5) +
geom_errorbar(aes(ymin = percentage - std_error, ymax = percentage + std_error),
width = 0.3,
position = position_dodge(0.5),
color = "darkgray"
) +
geom_text(aes(label = ifelse(prop_pval < 0.05,
paste0(round(percentage * 100, 1), "%"),
paste0(round(percentage * 100, 1), "%")
)),
position = position_dodge(0.5)
) +
theme_minimal()