在data.frame上运行卡方检验

问题描述 投票:0回答: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

我想知道xy是否与V1V2V3V4中的任何一个更相关。为了测试这个,我可以使用卡方。

这是我尝试过的,没有一个工作:

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

我如何在df上进行卡方检验?

r matrix dataframe chi-squared
2个回答
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

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


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
> 
© www.soinside.com 2019 - 2024. All rights reserved.