首先,要进行拟合优度测试,需要观察到的频率和bin 概率。
observed = c(3, 4, 9, 7, 9, 8, 2, 3, 7, 2, 1, 0, 1, 1, 0) # keep counts
概率是正确的:
mn = 4.578947
prob = c()
for (i in cases) (prob <- c(prob, dpois(i, lambda = mn)))
prob <- c(prob, (1-ppois(13, lambda=mn))) # prob for 13 and plus category
最重要的是, bin/category 中的预期频率至少应为 5。Chisq-test对小样本无效。这就是您收到警告 的原因(请参阅类别 1,2 和 8-15的预期频率) :
poisson_df <- data.frame(observed, prob)
poisson_df$expected = sum(poisson_df$observed)*poisson_df$prob
poisson_df
# observed prob expected
#1 3 0.0102657004 0.58514492
#2 4 0.0470060980 2.67934759
#3 9 0.1076192157 6.13429530
#4 7 0.1642608950 9.36287101
#5 9 0.1880354831 10.71802253
#6 8 0.1722009022 9.81545143
#7 2 0.1314164674 7.49073864
#8 3 0.0859641485 4.89995646
#9 7 0.0492031600 2.80458012
#10 2 0.0250331846 1.42689152
#11 1 0.0114625626 0.65336607
#12 0 0.0047714970 0.27197533
#13 1 0.0018207026 0.10378005
#14 1 0.0006413001 0.03655410
#15 0 0.0002986829 0.01702492
chisq.test(x = poisson_df$observed, p= poisson_df$prob)
# Chi-squared test for given probabilities
# data: observed
# X-squared = 58.036, df = 14, p-value = 2.585e-07
Warning message:
In chisq.test(x = poisson_df$observed, p= poisson_df$prob) :
Chi-squared approximation may be incorrect
因此,您需要适当地创建垃圾箱。需要注意的是, Chisq-test对binning很敏感,一种 binning 的方式如下:
cat_eq_3_less <- apply(poisson_df[1:3,], 2 , sum) # sum of 1 to 3 categories
cat_eq_8_plus <- apply(poisson_df[8:15,], 2 , sum) # sum 8 to 15 categories
corrected_df <- rbind(cat_eq_3_less, poisson_df[4:7,], cat_eq_8_plus)
corrected_df
# observed prob expected
# 16 0.1648910 9.398788
# 7 0.1642609 9.362871
# 9 0.1880355 10.718023
# 8 0.1722009 9.815451
# 2 0.1314165 7.490739
# 15 0.1791952 10.214129
chisq.test(x = corrected_df$observed, p = corrected_df$prob)
Chi-squared test for given probabilities
data: corrected_df$observed
X-squared = 12.111, df = 5, p-value = 0.0333