ocbe-uio / contingencytables

Statistical Analysis of Contingency Tables
https://ocbe-uio.github.io/contingencytables/
GNU General Public License v3.0
3 stars 2 forks source link

Chi-squared test with continuity correction doesn’t always return same result as chisq.test() #34

Closed huftis closed 2 years ago

huftis commented 2 years ago

The Pearson_chi_squared_test_CC_2x2() function should return the same results as chisq.test(..., correct = TRUE). It does so for the contingency tables in the help file, but not for all contingency tables. Example:

library(contingencytables)
#> Welcome to contingencytables 1.0.1.
#> Please run ?chapX or list_functions(x) to see the functions related to chapter X.
x = matrix(c(1, 2, 19, 33), ncol = 2)
Pearson_chi_squared_test_CC_2x2(x)
#> [1] The Pearson chi-squared test: P = 0.61359, T = 0.255 (df = 1)
chisq.test(x, correct = TRUE)
#> Warning in chisq.test(x, correct = TRUE): Chi-squared approximation may be
#> incorrect
#> 
#>  Pearson's Chi-squared test with Yates' continuity correction
#> 
#> data:  x
#> X-squared = 1.5178e-30, df = 1, p-value = 1

In fact, for this table, the functions returns a lower P-value (0.61) than the corresponding function(s) that don’t use continuity correction (0.91):

Pearson_chi_squared_test_2x2(x)
#> [1] The Pearson chi-squared test: P = 0.91066, T = 0.013 (df = 1)
chisq.test(x, correct = FALSE)
#> Warning in chisq.test(x, correct = FALSE): Chi-squared approximation may be
#> incorrect
#> 
#>  Pearson's Chi-squared test
#> 
#> data:  x
#> X-squared = 0.012592, df = 1, p-value = 0.9107
wleoncio commented 2 years ago

Thank you for your submission, I will investigate and report back ASAP.

wleoncio commented 2 years ago

Thanks for reporting! The bug seems to happen whenever abs(n[1, 1] * n[2, 2] - n[1, 2] * n[2, 1]) - (N / 2)) < 0, so that expression has been wrapped around max(0, ...) (see theoretical reference here). The fix is already on our develop branch (contingencytables 1.0.1.9000) and should be available within a few days on CRAN (contingencytables 1.0.2).

wleoncio commented 2 years ago

For the record: contingencytables 1.0.2 is on CRAN with the fix. 😄