对数据框执行卡方检验 [英] Run chi-squared test on a data.frame

查看:239
本文介绍了对数据框执行卡方检验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个data.frame:

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

我想知道,如果 x y 更多地与 V1 V2 V3 V4 。为了测试这个,我可以使用卡方。

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))

如何在 df

推荐答案

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

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
> 

这篇关于对数据框执行卡方检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆