2

我有这个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的任何一个更相关。为了测试这一点,我可以使用卡方。V1V2V3V4

这是我尝试过的,但都不起作用:

chisq.test(df)
chisq.test(as.matrix(df))
chisq.test(as.table(df))

如何运行卡方检验df

4

2 回答 2

1

以下两项工作(您需要删除第一列):

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
> 
于 2014-08-17T16:47:36.233 回答
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

结果得到警告可能是因为您的数据包含零。

于 2014-08-17T15:49:54.763 回答