如何在 R 中对指数分布使用卡方检验
How to use Chi-square test for exponential distribution in R
在我的数据集中,我有 15 个观察值,我想测试这个分布是否可以用 rate=0.54 的指数分布来表示。
变量x如下:
table(x)
x
0 1 2 4 5 7 8 10
2 1 4 2 2 2 1 1
知道如何在 R 中实现这个吗?
我们可以试试
set.seed(1)
observed <- c(2, 1, 4, 2, 2, 2, 1, 1)
prob.exp <- dexp(c(0, 1, 2, 4, 5, 7, 8, 10), rate=0.54) # prob for the exp dist. variable for the values
chisq.test(observed, p=prob.exp, rescale.p = TRUE)
#X-squared = 73.523, df = 7, p-value = 2.86e-13
我们也可以试试这个(有理论定义):
set.seed(1)
observed <- c(2, 1, 4, 2, 2, 2, 1, 1)
prob.exp <- dexp(c(0, 1, 2, 4, 5, 7, 8, 10), rate=0.54)
prob.exp <- prob.exp / sum(prob.exp) # normalize
expected <- sum(observed)*prob.exp
# expected frequency of the values
chisq.stat <- sum((observed-expected)^2/expected)
# [1] 73.52297
1-pchisq(sum(chisq.stat),df=8-1)
# [1] 2.859935e-13
他们给出的结果与预期完全相同(拟合优度检验的原假设被拒绝,因此数据不是来自分布)
您可以测试 link 数值 "names" 和具有偏移量的 table 观察值之间的对数 link(即测量水平的指数分布)日志(率)。如果添加 log(rate) 偏移量的截距与 0 显着不同,则拒绝特定假设(并且它是......不是):
summary( glm( vals ~ nm+offset(rep(0.54, 8)) ,family=poisson))
Call:
glm(formula = vals ~ nm + offset(rep(0.54, 8)), family = poisson)
Deviance Residuals:
Min 1Q Median 3Q Max
-0.9762 -0.3363 -0.1026 0.1976 1.1088
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.36468 0.40787 0.894 0.371
nm -0.06457 0.08027 -0.804 0.421
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 3.3224 on 7 degrees of freedom
Residual deviance: 2.6593 on 6 degrees of freedom
AIC: 26.38
Number of Fisher Scoring iterations: 4
在我的数据集中,我有 15 个观察值,我想测试这个分布是否可以用 rate=0.54 的指数分布来表示。 变量x如下:
table(x)
x
0 1 2 4 5 7 8 10
2 1 4 2 2 2 1 1
知道如何在 R 中实现这个吗?
我们可以试试
set.seed(1)
observed <- c(2, 1, 4, 2, 2, 2, 1, 1)
prob.exp <- dexp(c(0, 1, 2, 4, 5, 7, 8, 10), rate=0.54) # prob for the exp dist. variable for the values
chisq.test(observed, p=prob.exp, rescale.p = TRUE)
#X-squared = 73.523, df = 7, p-value = 2.86e-13
我们也可以试试这个(有理论定义):
set.seed(1)
observed <- c(2, 1, 4, 2, 2, 2, 1, 1)
prob.exp <- dexp(c(0, 1, 2, 4, 5, 7, 8, 10), rate=0.54)
prob.exp <- prob.exp / sum(prob.exp) # normalize
expected <- sum(observed)*prob.exp
# expected frequency of the values
chisq.stat <- sum((observed-expected)^2/expected)
# [1] 73.52297
1-pchisq(sum(chisq.stat),df=8-1)
# [1] 2.859935e-13
他们给出的结果与预期完全相同(拟合优度检验的原假设被拒绝,因此数据不是来自分布)
您可以测试 link 数值 "names" 和具有偏移量的 table 观察值之间的对数 link(即测量水平的指数分布)日志(率)。如果添加 log(rate) 偏移量的截距与 0 显着不同,则拒绝特定假设(并且它是......不是):
summary( glm( vals ~ nm+offset(rep(0.54, 8)) ,family=poisson))
Call:
glm(formula = vals ~ nm + offset(rep(0.54, 8)), family = poisson)
Deviance Residuals:
Min 1Q Median 3Q Max
-0.9762 -0.3363 -0.1026 0.1976 1.1088
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.36468 0.40787 0.894 0.371
nm -0.06457 0.08027 -0.804 0.421
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 3.3224 on 7 degrees of freedom
Residual deviance: 2.6593 on 6 degrees of freedom
AIC: 26.38
Number of Fisher Scoring iterations: 4