首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么chisq.test的两个输出在R上不同

为什么chisq.test的两个输出在R上不同
EN

Stack Overflow用户
提问于 2014-11-13 13:07:27
回答 2查看 654关注 0票数 3

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

代码语言:javascript
复制
> 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

第一种方法:

代码语言:javascript
复制
> 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

第二种方法:

代码语言:javascript
复制
> 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
> 

编辑:回复评论:下面看起来是相同的:

代码语言:javascript
复制
> 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

数据:

代码语言:javascript
复制
> 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))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-13 13:21:15

见?chisq :在第一种情况下,mm2:3作为一个偶合表,在第二种情况下,计算出列联表。

代码语言:javascript
复制
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的:

代码语言:javascript
复制
     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
票数 5
EN

Stack Overflow用户

发布于 2014-11-13 13:23:05

chisq.test的R文档中指出

如果x是至少有两行和两列的矩阵,则它被视为二维列。

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

在第二种情况下,当yout类型为chisq.test(df1$count, df2$count)时,从向量df1$countdf2$count计算内容表(使用函数table)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26909628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档