R 如何更改卡方检验中的自由度

R 如何更改卡方检验中的自由度,r,statistics,p-value,R,Statistics,P Value,我试图用泊松分布的拟合优度检验来计算p值 观测数据点为:118 64 18,预期数据点为:120 61.25 18.8 我用泊松分布计算了概率,因此df值为3-1-1=1 我从R得到df=4 这是我在R中输入的内容: Chi.Observed <- c(118,64,18) Chi.Expected <- c(120,61.2,18.8) chisq.test(Chi.Observed, Chi.Expected) 我将在一分钟内演示如何更改测试,但这里有一些问题。(除了测向调整外

我试图用泊松分布的拟合优度检验来计算p值

观测数据点为:118 64 18,预期数据点为:120 61.25 18.8

我用泊松分布计算了概率,因此df值为3-1-1=1

我从R得到df=4

这是我在R中输入的内容:

Chi.Observed <- c(118,64,18)
Chi.Expected <- c(120,61.2,18.8)
chisq.test(Chi.Observed, Chi.Expected)

我将在一分钟内演示如何更改测试,但这里有一些问题。(除了测向调整外,覆盖的范围与此答案完全相同,还有更多…)

  • 这将有助于了解更多关于如何得出预期计数的信息。重建:

    • dpois(0:1,lambda=0.51)*200
      给出
      (120.09912,61.25055)
      ppois(1,lambda=0.51,lower.tail=FALSE)
      给出18.6,所以我假设这里的概率是200个计数中的0、1和>=2个计数
    • sum(Chi.Observed)
      是200,
      sum((0:2)*Chi.Observed/sum(Chi.Observed))
      是0.5,所以这非常符合
    因此,您已经从3个数值中导出了2条信息来生成预期值,并且您的df应该为1似乎是合理的

  • 指定
    x
    y
    并不能达到您认为(或我认为)的效果:正如@Dave2e所指出的,您真正想要的是指定
    p

    如果x是向量 如果没有给出“y”,则进行拟合优度测试。。。这个 检验的假设是总体概率是否相等 “p”中的值,如果没有给出“p”,则所有值都相等

    以下是如何破解您的测试:


然后对其进行列联表分析(即检验行/列独立性的零假设)!这是我很久以来见过的最好的R陷阱之一…

在思考了这个问题并阅读了Ben上面的答案后,我相信我有了一个解释和/或答案。这个问题是双重的,使用正确的Chisq测试形式和获得正确的自由度

使用正确形式的chisq.测试中的第一个问题。如果使用以下形式:
chisq.test(x,y)
这将导致创建一个3x3列联表,并导致p值过低。
请参见下面的测试1
test1$observed
test1$expected
未返回正确的输入

正确的格式是chisq.test(x,p)#其中p是x的预期概率。
这如下面的test2所示。现在p值从19%变为90%。(这是我的答案,但我会听从更好的统计学家。)

要将自由度调整为1,请参见Ben Bolker的答案。现在结果显示为test3,p值为66%

希望这能提供一个可接受的解释

Chi.Observed <- c(118,64,18)
Chi.Expected <- c(120,61.2,18.8)

test1<-chisq.test(Chi.Observed, Chi.Expected) # this is 3x3 contgency table.
test1
# Pearson's Chi-squared test
# 
# data:  Chi.Observed and Chi.Expected
# X-squared = 6, df = 4, p-value = 0.1991
# 
#This result is incorrect as it...
# forms a 3x3 contingency table as shown by: 
test1$observed   # observed counts 
test1$expected   # expected counts under the null


#chisq using the expected probabilities:
test2<-chisq.test(Chi.Observed, p= Chi.Expected/sum(Chi.Expected))
test2
# Chi-squared test for given probabilities
# 
# data:  Chi.Observed
# X-squared = 0.19548, df = 2, p-value = 0.9069


#adjust degrees of freedon as per Ben's answer
test3 <- chisq.test(Chi.Observed,  p = Chi.Expected/sum(Chi.Expected))
test3$parameter <- c(df=1)
test3$p.value <- pchisq(test3$statistic, df=test3$parameter, lower.tail=FALSE)
test3 
# Chi-squared test for given probabilities
# 
# data:  Chi.Observed
# X-squared = 0.19548, df = 1, p-value = 0.6584

Chi.Observed你确定它不是df=6-2或4吗?是的,因为有三个类别,我已经从数据中估计了平均值,因此使df为1,答案是a p=0.66,我没有看到只有两个类别。我仍然不知道他们是怎么做到的。你说的“泊松”是指“截断的泊松”,还是这是“泊松”的拼写错误?泊松的拼写错误问题是:数字是独立生成的吗。我从一个表中得出它们,其中0的频率为118;1为64,大于1为18。然后利用泊松公式计算独立性。H0表示个体具有泊松分布,Ha表示个体没有泊松分布。所以我需要用X^2的拟合优度来计算它。@BenBolker,你能再详细说明一下你的解吗?1.4%的p值似乎太低,因为第一个料仓预计120个料仓中有118个料仓。直觉表明H0是可行的。如果您使用此
c更仔细地阅读文档,我认为您是对的:指定
x
y
并没有达到我预期的效果。
Chi.Observed <- c(118,64,18)
Chi.Expected <- c(120,61.2,18.8)
cc <- chisq.test(Chi.Observed, 
         p = Chi.Expected/sum(Chi.Expected))
cc$parameter <- c(df=1)
cc$p.value <- pchisq(cc$statistic,df=cc$parameter,
      lower.tail=FALSE)
cc 
## Pearson's Chi-squared test    
## data:  Chi.Observed and Chi.Expected
## X-squared = 0.19548, df = 1, p-value = 0.6584
table(factor(Chi.Expected), factor(Chi.Observed))

       18 64 118
  18.8  1  0   0
  61.2  0  1   0
  120   0  0   1
Chi.Observed <- c(118,64,18)
Chi.Expected <- c(120,61.2,18.8)

test1<-chisq.test(Chi.Observed, Chi.Expected) # this is 3x3 contgency table.
test1
# Pearson's Chi-squared test
# 
# data:  Chi.Observed and Chi.Expected
# X-squared = 6, df = 4, p-value = 0.1991
# 
#This result is incorrect as it...
# forms a 3x3 contingency table as shown by: 
test1$observed   # observed counts 
test1$expected   # expected counts under the null


#chisq using the expected probabilities:
test2<-chisq.test(Chi.Observed, p= Chi.Expected/sum(Chi.Expected))
test2
# Chi-squared test for given probabilities
# 
# data:  Chi.Observed
# X-squared = 0.19548, df = 2, p-value = 0.9069


#adjust degrees of freedon as per Ben's answer
test3 <- chisq.test(Chi.Observed,  p = Chi.Expected/sum(Chi.Expected))
test3$parameter <- c(df=1)
test3$p.value <- pchisq(test3$statistic, df=test3$parameter, lower.tail=FALSE)
test3 
# Chi-squared test for given probabilities
# 
# data:  Chi.Observed
# X-squared = 0.19548, df = 1, p-value = 0.6584