Run chi-squared test on a data.frame

匆匆过客 提交于 2019-12-11 10:03:57

问题


I have this 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

I want to know if x or y is more associated with any of V1, V2, V3 or V4. To test this, I can use a chi-squared.

This is what I've tried, none of which work:

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

How can I run a chi-squared test on df?


回答1:


use this:

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

the result got warning maybe because of your data containing zero.




回答2:


Both of following work (you need to remove first column):

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
> 


来源:https://stackoverflow.com/questions/25350618/run-chi-squared-test-on-a-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!