this is my first question on stackoverflow, so hopefully I will give you all the necessary information.
I'm trying to conduct a chi-squared test based on an exact distribution, using the chisq_test
function in the coin
package. I'd like to compare two groups concerning one variable but I'm running in a warning message. Here's a minimum reproducible example (at least I hope it is one):
library(coin)
dt <- as.data.frame(alzheimer)
xtabs(~ disease + gender, data = dt)
chisq_test(disease ~ gender, data = dt, distribution = "exact")
I'm checking the contingency table to make sure, there are enough cases per cell. When I'm conducting the test, I get this output:
> chisq_test(disease ~ gender, data = dt, distribution = "exact")
Exact Pearson Chi-Squared Test
data: disease by gender (Female, Male)
chi-squared = 9.7121, p-value = 0.3895
Warning messages:
1: In T - expectation(object) :
longer object length is not a multiple of shorter object length
2: In (T - expectation(object))^2/variance(object) :
longer object length is not a multiple of shorter object length
I calculated the p-value using an approximate distribution, to make sure, the warning really is a problem:
> chisq_test(disease ~ gender, data = dt, distribution = approximate(nresample = 1e+05))
Approximative Pearson Chi-Squared Test
data: disease by gender (Female, Male)
chi-squared = 9.7121, p-value = 0.00756
Since both p-values differ a lot, I guess there is something going wrong using the exact distribution, but I just can't figure out the problem.
Maybe someone can help me solve the problem or understand the reason for the warning.
I wrote to Torsten Hothorn, one of the authors of the package. He told me that the exact distribution for the chi-squared test only works in the case of 2x2 problems. So the warning or the fact that you don't get an error message is a bug in the current version 1.3-1 of the coin
package. In the R-forge version of the package this bug is already fixed.