Reputation: 13
I'm trying to do a goodness-to-fit test against a Poisson on a series of observations using R. I'm counting how many people did a certain thing per minute, over 57 minutes. I never got any observations greater than 13, and i got the following data: (for the cases 0 to 13+ people):
observed = c(3/57, 4/57, 9/57, 7/57, 9/57, 8/57, 2/57, 3/57, 7/57, 2/57, 1/57, 0, 1/57, 1/57, 0)
meaning that 3 times i observed 0 people, 4 times 1 people, 9 times 2 people and so on (the last 0 means i never saw 14 or more people).
mn = 4.578947
cases = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
estimated = c()
for (i in cases)(estimated <- c(estimated, dpois(i, lambda = mn)))
estimated <- c(estimated, (1-ppois(13, lambda=mn)))
where mn
is the mean obtained from the data.
Finally, i run
chisq.test(observed, p=estimated)
and i get:
Chi-squared test for given probabilities
data: observed
X-squared = 1.0182, df = 14, p-value = 1
Warning message:
In chisq.test(observed, p = estimated) :
Chi-squared approximation may be incorrect
I'm not well-versed in this area (neither statistics nor programming on R), but i have the idea that i'm not supposed to get a p-value of exactly 1.0. What am i doing wrong? (By the way: My code is most likely not optimal for what i'm trying to do, but i barely use R and it's not the focus of my work right now.)
Upvotes: 1
Views: 1446
Reputation: 2208
Firstly, to conduct a goodness of fit test, observed frequencies and bin probabilities are required.
observed = c(3, 4, 9, 7, 9, 8, 2, 3, 7, 2, 1, 0, 1, 1, 0) # keep counts
Probabilities are correct:
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
Most importantly, Expected frequencies in a bin/ category should be at least 5. Chisq-test is not valid for small samples. This is why you get a warning (see expected frequencies for category 1,2 and 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
Therefore, you need to create bins appropriately. It should be noted that Chisq-test is sensitive to binning, one way to bin is as below:
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
Upvotes: 0
Reputation: 3335
Your observed values should be counts, not proportions:
> chisq.test(observed*57, p=estimated)
Chi-squared test for given probabilities
data: observed * 57
X-squared = 58.036, df = 14, p-value = 2.585e-07
Per the R help file for chisq.test
:
If x is a matrix with one row or column, or if x is a vector and y is not given, then a goodness-of-fit test is performed (x is treated as a one-dimensional contingency table). The entries of x must be non-negative integers.
(Emphasis mine)
You can test this with some of the example code in the manual
How it should be done:
> x <- c(89,37,30,28,2)
> p <- c(0.40,0.20,0.20,0.19,0.01)
> chisq.test(x, p = p)
Chi-squared test for given probabilities
data: x
X-squared = 5.7947, df = 4, p-value = 0.215
Warning message:
In chisq.test(x, p = p) : Chi-squared approximation may be incorrect
And making the same mistake as you have:
> chisq.test(x/sum(x), p = p)
Chi-squared test for given probabilities
data: x/186
X-squared = 0.031154, df = 4, p-value = 0.9999
Warning message:
In chisq.test(x/186, p = p) : Chi-squared approximation may be incorrect
Upvotes: 2