I'm using R to do a chi-squared test on two sets of data to check if they follow a distribution. In the first case the chi-squared statistic given by chisq.test matches the one calculated by hand, but in the other case it does not.
First case
> e1 = c(18, 38, 78, 107, 107, 72, 35, 15)
> o1 = c(10 ,30 ,80 ,180 ,60 ,40 ,40 ,30)
> sum((o1-e1)**2/e1)
[1] 105.6762
> chisq.test(o1,p=e1,rescale.p=T)$statistic
X-squared
105.6762
Both methods give 105.67 as the statistic. But in the other case:
> o2=c(401, 1235, 2989, 5682, 8489, 9966, 9196, 6668, 3800, 1701, 599, 165)
> e2=c(385, 1204, 2945, 5643, 8472, 9966, 9186, 6635, 3754, 1665, 578, 157)
> sum((o2-e2)**2/e2)
[1] 5.111822
> chisq.test(o2,p=e2,rescale.p=T)$statistic
X-squared
3.301292
Now the statistics are different and I don't understand why. Am I doing something wrong or missing something?