2
$\begingroup$

I am trying to calculate the odds ratio using the epitools package. The counts in 2 cells of my 2x2 table are $< 10$ so I keep receiving the following error "Error in chisq.test(xx, correct = correction) : (converted from warning) Chi-squared approximation may be incorrect". I tried defining the method as "fisher" and "small" but I keep getting the same error. How can I get this test to run?

install.packages('epitools')
library(epitools)
mat1 <- c(15, 49)
mat2 <- c(2, 7)
mat <- rbind(mat1,mat2)
mat
oddsratio(mat)
oddsratio.fisher(mat)
oddsratio.small(mat)
$\endgroup$
4
  • $\begingroup$ Welcome to CV, Katie! $\endgroup$
    – whuber
    Commented Mar 7, 2023 at 14:03
  • 1
    $\begingroup$ To the close-voters, this seems on-topic to me! $\endgroup$ Commented Mar 9, 2023 at 15:33
  • 1
    $\begingroup$ When I run your code, I do not get an error, I get a warning, and the output spits out just fine as shown in the answer from James. How does an "error convert from a warning" in R? $\endgroup$
    – AdamO
    Commented Mar 9, 2023 at 16:15
  • $\begingroup$ @AdamO, the best lead I saw was here: stackoverflow.com/a/72754921/1834244 which suggests there might have been a change to the value of warn within options() $\endgroup$ Commented Mar 15, 2023 at 7:41

2 Answers 2

1
$\begingroup$

You are not actually getting an error, you are getting a warning. At least when I run your code,

From a programmatic perspective, the warning comes from the subroutine chisq.test. Each of the methods (Fisher and small) use separate methods to calculate the standard errors for the OR, but it's still the same uncorrected OR as the point estimate - thus the same warning.

"Correction" refers to using an Agresti-Couli type correction which adds 1 (or 0.5) to either the diagonal or all table cell entries. This replaces the empirical odds ratio with a biased but much less variable alternative that behaves better in small samples.

$\endgroup$
0
$\begingroup$

The issue is that oddsratio, oddsratio.fisher and oddsratio.small all calculate a "regular" chi-squared statistic for reporting each time: you can see this in the example output for oddsratio.fisher(mat) below (the $p.value table, chi.square column).

So each of these variants of oddsratio is triggering the approximation warning on each run from the call to chisq.test().

The test is still running (for me at least): if you try chisq.test(mat) which uses the base stats package function, you will see that this message is printed as a Warning, rather than an Error.

oddsratio.fisher(mat)
$data
  col1 col2 Total
mat1    15   49    64
mat2     2    7     9
Total   17   56    73

$measure                        
                         NA
odds ratio with 95% C.I. estimate     lower    upper
                    mat1  1.00000        NA       NA
                    mat2  1.07043 0.1766049 11.63845

$p.value
                         NA
two-sided midp.exact fisher.exact chi.square
     mat1         NA           NA         NA
     mat2  0.9784459            1  0.9356279

$correction
[1] FALSE

attr(,"method")
[1] "Conditional MLE & exact CI from 'fisher.test'"
Warning message:
In chisq.test(xx, correct = correction) :
Chi-squared approximation may be incorrect
$\endgroup$
3
  • $\begingroup$ Thanks for your response. Mine is still printing as an error instead of a warning. Maybe it is a versional difference, but I am using the latest update. $\endgroup$
    – Katie
    Commented Mar 7, 2023 at 2:23
  • $\begingroup$ OK (I'm on R 4.2.2, and epitools 0.5-10.1). If you still get the output at all, then you can treat this message as a warning about the appropriateness of the chi-squared test for your data; if it's an error that prevents the output from printing then that's more complex! $\endgroup$ Commented Mar 7, 2023 at 2:44
  • $\begingroup$ This should be a Warning. Your initial question notes this is an "Error...converted from warning", which may relate to your settings. See e.g. stackoverflow.com/questions/72754423/… $\endgroup$ Commented Mar 7, 2023 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.