The tidyenrich R package computes enrichments from the results of dplyr::count, supporting grouping variables
You can install the development version of tidyenrich like so:
if (!require("remotes"))
install.packages("remotes")
remotes::install_github("selkamand/tidyenrich")
library(tidyenrich)
# Start with a tidy dataset
titanic <- load_titanic_data()
# Run Fisher Test
enrichments <- tidyenrich(data = titanic, col_group = "Survived", col_features = c("Age", "Sex"))
enrichments
#> element p.value odds_ratio conf_level conf.int.lower conf.int.upper
#> 1 Age 1.233820e-05 2.40914890 0.95 1.60553639 3.6219766
#> 2 Sex 2.690694e-96 0.09869816 0.95 0.07734453 0.1253659
#> null.value alternative odds_ratio_explained
#> 1 1 two.sided OR: The increase in odds of [Died] given [Adult]
#> 2 1 two.sided OR: The increase in odds of [Died] given [Female]
#> fdr group group_numerator group_denominator feature_numerator
#> 1 1.233820e-05 Survived Died Survived Adult
#> 2 5.381387e-96 Survived Died Survived Female
#> feature_denominator feature tally_a tally_b tally_c tally_d a
#> 1 Child Age 1438 52 654 57 Died & Adult
#> 2 Male Sex 126 1364 344 367 Died & Female
#> b c d
#> 1 Died & Child Survived & Adult Survived & Child
#> 2 Died & Male Survived & Female Survived & Male
# Visualise as forest plot
plot_forest(enrichments)