The twoxtwo package provides a collection of functions
to display, summarize, and analyze data in two-by-two contingency
tables. Statistical analysis functions are oriented towards
epidemiological investigation of exposure/outcome relationships.
## install.packages("devtools")
devtools::install_github("vpnagraj/twoxtwo", build_vignettes = TRUE)twoxtwo(): Construct twoxtwo objectodds_ratio(): Estimate odds ratio and confidence
intervalrisk_ratio(): Estimate risk ratio and confidence
intervalrisk_diff(): Estimate risk difference and confidence
intervalfisher(): Perform Fisher’s exact testchisq(): Perform Pearson’s chi-squared testarp(): Estimate attributable risk proportion (ARP) and
confidence intervalparp(): Estimate population attributable risk
proportion (PARP) and confidence intervalein(): Estimate exposure impact number (EIN) and
confidence intervalcin(): Estimate case impact number (CIN) and confidence
intervalecin(): Estimate exposed cases impact number (ECIN) and
confidence intervalsummary.twoxtwo(): Summarize twoxtwo
objectprint.twoxtwo(): Print twoxtwo objectdisplay(): Render twoxtwo table contents
as a knitr::kableFirst load twoxtwo and dplyr to help prep
data:
library(twoxtwo)
library(dplyr)Next create a object with S3 class twoxtwo. For this
example, use the twoxtwo::titanic dataset. Note that
“exposure” and “outcome” variables must each be binary variables:
crew_2x2 <-
  titanic %>%
  twoxtwo(.data = ., exposure = Crew, outcome = Survived)
crew_2x2
# |         |           |OUTCOME      |OUTCOME     |
# |:--------|:----------|:------------|:-----------|
# |         |           |Survived=Yes |Survived=No |
# |EXPOSURE |Crew=TRUE  |212          |673         |
# |EXPOSURE |Crew=FALSE |499          |817         |The twoxtwo class has its own
summary.twoxtwo() method that computes effect measures
(odds ratio, risk ratio, and risk difference):
summary(crew_2x2)
# 
# |         |           |OUTCOME      |OUTCOME     |
# |:--------|:----------|:------------|:-----------|
# |         |           |Survived=Yes |Survived=No |
# |EXPOSURE |Crew=TRUE  |212          |673         |
# |EXPOSURE |Crew=FALSE |499          |817         |
# 
# 
# Outcome: Survived
# Outcome + : Yes
# Outcome - : No
# 
# Exposure: Crew
# Exposure + : TRUE
# Exposure - : FALSE
# 
# Number of missing observations: 0
# 
# Odds Ratio: 0.516 (0.426,0.624)
# Risk Ratio: 0.632 (0.551,0.724)
# Risk Difference: -0.14 (-0.178,-0.101)Individual measures of effect, hypothesis tests, and impact numbers
can be calculated using the twoxtwo object. For
example:
crew_2x2 %>%
  odds_ratio()
# # A tibble: 1 x 6
#   measure    estimate ci_lower ci_upper exposure         outcome         
#   <chr>         <dbl>    <dbl>    <dbl> <chr>            <chr>           
# 1 Odds Ratio    0.516    0.426    0.624 Crew::TRUE/FALSE Survived::Yes/Nocrew_2x2 %>%
  chisq()
# # A tibble: 1 x 9
#   test      estimate ci_lower ci_upper statistic    df   pvalue exposure outcome
#   <chr>     <lgl>    <lgl>    <lgl>        <dbl> <int>    <dbl> <chr>    <chr>  
# 1 Pearson'… NA       NA       NA            46.5     1 8.97e-12 Crew::T… Surviv…Note that data analysis can also be performed without first creating
the twoxtwo object:
titanic %>%
  odds_ratio(.data = ., exposure = Crew, outcome = Survived)
# # A tibble: 1 x 6
#   measure    estimate ci_lower ci_upper exposure         outcome         
#   <chr>         <dbl>    <dbl>    <dbl> <chr>            <chr>           
# 1 Odds Ratio    0.516    0.426    0.624 Crew::TRUE/FALSE Survived::Yes/Notitanic %>%
  chisq(.data = ., exposure = Crew, outcome = Survived)
# # A tibble: 1 x 9
#   test      estimate ci_lower ci_upper statistic    df   pvalue exposure outcome
#   <chr>     <lgl>    <lgl>    <lgl>        <dbl> <int>    <dbl> <chr>    <chr>  
# 1 Pearson'… NA       NA       NA            46.5     1 8.97e-12 Crew::T… Surviv…The package includes vignettes to describe usage in more detail.
For details on the twoxtwo data structure and
demonstration of basic usage:
vignette("basic-usage", package = "twoxtwo")For formulas and examples of how to calculate measures of effect:
vignette("measures-of-effect", package = "twoxtwo")For information on hypothesis testing functionality in the package:
vignette("hypothesis-testing", package = "twoxtwo")For formulas and demonstration of attributable fraction and impact number calculations:
vignette("af-impact", package = "twoxtwo")Please use GitHub issues to report bugs or request features. Contributions will be reviewed via pull requests.