From 36e9a1b0b02dbd19ee7af4b3219b9e9fbfbb3e2a Mon Sep 17 00:00:00 2001 From: Randy Nguyen Date: Fri, 7 May 2021 17:22:25 -0400 Subject: [PATCH] Add fallback if chisq throws a warning in example --- vignettes/table1-examples.Rmd | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vignettes/table1-examples.Rmd b/vignettes/table1-examples.Rmd index 485603f..96c501a 100644 --- a/vignettes/table1-examples.Rmd +++ b/vignettes/table1-examples.Rmd @@ -454,8 +454,13 @@ pvalue <- function(x, ...) { # For numeric variables, perform a standard 2-sample t-test p <- t.test(y ~ g)$p.value } else { - # For categorical variables, perform a chi-squared test of independence - p <- chisq.test(table(y, g))$p.value + p <- tryCatch({ + # For categorical variables, perform a chi-squared test of independence + chisq.test(table(y, g))$p.value + }, warning = function(w) { + # If a warning appears (due to expected cell counts <5 or too few observations), use Fisher's Exact test + return(fisher.test(table(y, g))$p.value) + }) } # Format the p-value, using an HTML entity for the less-than sign. # The initial empty string places the output on the line below the variable label.