数据框与应急 table & chisq.test()

dataframe vs contingency table & chisq.test()

首先,如果这看起来是个基本问题,我很抱歉。我正在堆叠一个简单的 chisq.test,我认为它链接到我的数据 class (data.frame),即使我认为它的形状没问题:

cat3=structure(list(`1` = c(1, 3, 0, 0, 0, 2, 0, 3, 0, 0, 1.5, 4, 
0, 0, 0, 0, 0, 0, 9, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 
5, 0, 0, 0, 0, 0, 0.5), `2` = c(1, 0, 0, 0, 0, 2, 0, 2, 0.5, 
0, 2.5, 6, 0, 0, 0, 0, 0, 0, 11.5, 1, 2, 1.5, 0, 0, 1, 0, 0, 
0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 1.5), `3` = c(0, 0, 0, 0, 
2.5, 2, 0, 0, 0, 0, 0, 5.5, 2, 0, 1, 0, 0, 0, 3, 1, 0, 1, 0, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 0, 0, 0, 0, 0, 3.5), `4` = c(0, 
1, 0.5, 0, 0, 1, 0, 1, 0, 0, 0, 3, 1, 0, 3, 0, 0, 0, 1, 7, 0, 
2, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0.5, 0, 0, 3.5, 4), 
    `5` = c(0, 2, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1.5, 0, 2, 0, 
    0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 2.5, 4), `6` = c(0, 0, 0, 0, 0, 2.5, 0, 1, 0, 1, 
    0, 1.5, 0, 0, 3.5, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 
    1, 0, 0, 0, 0.5, 0, 2.33333333333333, 0, 0, 0, 2, 1), `7` = c(1, 
    1, 1, 0, 0, 3.5, 0, 0, 0, 0, 0, 1.5, 0, 0, 1, 0, 0, 0, 0, 
    4, 0, 0, 0, 0, 0, 1, 0.5, 0, 0.5, 0, 1, 0, 0, 0, 3, 1, 2, 
    0, 1.5, 0), `8` = c(1, 0, 0, 0, 0, 1.5, 0, 0, 0, 2, 0, 1, 
    0, 0, 0.5, 2, 1, 0, 0, 0.5, 0, 0, 0, 0.333333333333333, 0.5, 
    0, 0, 0, 1.5, 0, 0, 0, 0, 0, 1.5, 1.5, 0, 0, 1, 0), `9` = c(2, 
    0, 0, 0, 0.5, 0.5, 0, 3.5, 0, 0, 0, 0.5, 0, 0, 0, 0, 1, 0, 
    0, 3, 0, 0, 0, 0, 0, 0.5, 1, 1, 2, 0, 0, 0, 0, 3, 3, 3, 0, 
    0, 0, 0.5), `10` = c(2, 0, 0, 0, 0, 0, 0, 2.5, 0, 0, 0, 0, 
    0, 0, 0, 1.5, 1.5, 1, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0.333333333333333, 
    7, 0, 0, 0, 0, 0, 2, 3.5, 0, 0, 0, 0), `11` = c(1, 0, 0, 
    0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0.333333333333333, 
    4.33333333333333, 0, 0, 0, 0), `12` = c(1, 0, 0, 0.5, 0, 
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
    0, 7, 0, 0, 1, 0, 0, 0, 0, 0, 0, 4.5, 0, 0, 0, 0)), .Names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"), class = "data.frame", row.names = c(NA, 
-40L))

我正在尝试 运行 chisq.test(cat3) 函数,但我没有 p 值也没有 X²。

        Pearson's Chi-squared test

data:  cat3
X-squared = NaN, df = 429, p-value = NA

我发现无法将数据帧转换为应急事件 table 以使用 chisq.test 函数。是因为我的 class table 吗?否则有什么问题,为什么我对 p 和 X² 没有价值?

非常感谢。

问题是,您的数据中全是 0(空)行:

> any(rowSums(cat3) == 0)
[1] TRUE

您可以删除空行:

> cat3_sub <- cat3[rowSums(cat3) != 0, ]
> chisq.test(cat3_sub)

Pearson's Chi-squared test

data:  cat3_sub
X-squared = 731.21, df = 407, p-value < 2.2e-16

Warning message:
In chisq.test(cat3_sub) : Chi-squared approximation may be incorrect