【问题标题】:Run chi-squared test on a data.frame在 data.frame 上运行卡方检验
【发布时间】:2014-08-17 15:24:58
【问题描述】:

我有这个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 上运行卡方检验?

【问题讨论】:

  • 试试chisq.test(as.matrix(df[,-1]))
  • 对不起,我不明白你的数据结构。 xy 的观测值有多少? x 是 1、5、13 还是 >13?对于y,是 1、2 还是 >2?如果 >,您/我们不需要知道有多少?
  • 对 x 的 13 次观测和 y 的 2 次观测
  • 查看我对类似问题的回答here

标签: r matrix dataframe chi-squared


【解决方案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
> 

【讨论】:

    【解决方案2】:

    使用这个:

    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
    

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

    【讨论】:

    • 请评论后回答更多。因此利用海报代码和 Clarice 游览 remraketten Aboutaleb “警告”
    猜你喜欢
    • 2021-12-12
    • 2017-12-02
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多