【问题标题】:Why 2 outputs of chisq.test are different in R为什么 chisq.test 的 2 个输出在 R 中不同
【发布时间】:2014-11-13 13:07:27
【问题描述】:

以下,当数据实际上相同时,为什么 2 chisq.test 的输出不同:

> df1
  count position
1     1       11
2     6       12
3    12       13
4    23       14
5    27       15
> df2
  count position
1     1       11
2     4       12
3     9       13
4    24       14
5    24       15
> mm = merge(df1, df2,  by='position')
> mm
  position count.x count.y
1       11       1       1
2       12       6       4
3       13      12       9
4       14      23      24
5       15      27      24

第一种方法:

> chisq.test(mm[2:3])

        Pearson's Chi-squared test

data:  mm[2:3]
X-squared = 0.6541, df = 4, p-value = 0.9569

Warning message:
In chisq.test(mm[2:3]) : Chi-squared approximation may be incorrect

第二种方法:

> chisq.test(df1$count, df2$count)

        Pearson's Chi-squared test

data:  df1$count and df2$count
X-squared = 15, df = 12, p-value = 0.2414

Warning message:
In chisq.test(df1$count, df2$count) :
  Chi-squared approximation may be incorrect
> 

编辑:回复评论:以下看起来相同:

> mm[2:3]
  count.x count.y
1       1       1
2       6       4
3      12       9
4      23      24
5      27      24
> 

> mm[,2:3]
  count.x count.y
1       1       1
2       6       4
3      12       9
4      23      24
5      27      24

数据:

> dput(df1)
structure(list(count = c(1L, 6L, 12L, 23L, 27L), position = 11:15), .Names = c("count", 
"position"), class = "data.frame", row.names = c(NA, -5L))
> dput(df2)
structure(list(count = c(1L, 4L, 9L, 24L, 24L), position = 11:15), .Names = c("count", 
"position"), class = "data.frame", row.names = c(NA, -5L))

【问题讨论】:

  • 你能打印mm[2:3]吗?我认为你应该改写mm[,2:3]
  • 它们看起来一模一样。请参阅我上面的编辑。 data.frames 默认将这些作为列,而不是行。
  • 请让您的示例易于重现。
  • 上面添加的数据的输入。

标签: r chi-squared


【解决方案1】:

参见 ?chisq :在第一种情况下,将 mm[2:3] 作为列联表,在第二种情况下,计算列联表。

chisq.test(table(df1$count, df2$count))

        Pearson's Chi-squared test

data:  table(df1$count, df2$count)
X-squared = 15, df = 12, p-value = 0.2414

Warning message:
In chisq.test(table(df1$count, df2$count)) :
  Chi-squared approximation may be incorrect

所以,真的,你是这张桌子的计算 chisq :

     1 4 9 24
  1  1 0 0  0
  6  0 1 0  0
  12 0 0 1  0
  23 0 0 0  1
  27 0 0 0  1

【讨论】:

    【解决方案2】:

    chisq.test 的 R 文档中有说明

    如果x是至少两行两列的矩阵,则将其视为二维列联表

    因此,当您键入chisq.test(mm[2:3]) 时,您的矩阵就是列联表。

    在第二种情况下,当您键入chisq.test(df1$count, df2$count) 时,从向量df1$countdf2$count 计算列联表(使用函数table

    【讨论】:

    • 我想说... ;-)
    • 除了我之外,似乎每个人都知道!
    • 我刚刚通过阅读文档发现了它;)
    • chisq.test(cbind(df1$count, df2$count)) 给出与 chisq.test(mm[2:3]) 相同的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2018-08-18
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多