我有这个data.frame:
df <- data.frame(xy = c("x", "y"), V1 = c(3, 0), V2 = c(0, 0), V3 = c(5, 0), V4 = c(5, 2))
df
xy V1 V2 V3 V4
1 x 3 0 5 5
2 y 0 0 0 2
我想知道x
或y
是否与V1
,V2
,V3
或V4
中的任何一项更相关联。为了测试这个,我可以使用卡方。
这是我尝试过的,但都不起作用:
chisq.test(df)
chisq.test(as.matrix(df))
chisq.test(as.table(df))
如何在df
上进行卡方检验?
答案 0 :(得分:0)
使用它:
df <- as.table(rbind(c(3,0,5,5),c(0,0,0,2)))
> df
A B C D
A 3 0 5 5
B 0 0 0 2
> chisq.test(df)
Pearson's Chi-squared test
data: df
X-squared = NaN, df = 3, p-value = NA
Warning message:
In chisq.test(df) : Chi-squared approximation may be incorrect
结果得到警告可能是因为您的数据包含零。
答案 1 :(得分:0)
以下两项工作(您需要删除第一列):
chisq.test(df[,-1])
chisq.test(as.matrix(df[,-1]))
> chisq.test(df[,-1])
Pearson's Chi-squared test
data: df[, -1]
X-squared = NaN, df = 3, p-value = NA
Warning message:
In chisq.test(df[, -1]) : Chi-squared approximation may be incorrect
>
>
>
>
>
> chisq.test(as.matrix(df[,-1]))
Pearson's Chi-squared test
data: as.matrix(df[, -1])
X-squared = NaN, df = 3, p-value = NA
Warning message:
In chisq.test(as.matrix(df[, -1])) :
Chi-squared approximation may be incorrect
>