数据框与列联表& chisq.test()

时间:2016-12-14 14:12:20

标签: r chi-squared

首先,如果这似乎是基本问题,我很抱歉。我用一个简单的chisq.test堆栈,我认为它链接到我的数据类(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

我发现无法将数据帧转换为列联表以使用chisq.test函数。是因为我的班级表吗?还有什么问题,为什么我没有p和X²的价值​​?

非常感谢。

1 个答案:

答案 0 :(得分:1)

问题是,您的数据中包含所有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