# [R] Goodness of fit to Poisson / NegBinomial

Yudi Pawitan yudi at stat.ucc.ie
Wed Feb 7 16:14:06 CET 2001

```I had a very similar example before. This is for Mark's data:
(the last frequency is for k>= 5). (Mark should be happy with
the neg-binomial fit.)

-YP-

x_ c(70, 38, 17, 10,  9, 6)
kk_ 0:5
n_ sum(x)

fn_ function(p){
r_ p[1]
th_ p[2]
ll _ -sum(x[1:max(kk)]*log(dnbinom(kk[-length(kk)],r,th))) -
x[length(kk)]*log(1-pnbinom((max(kk)-1),r,th))
return(ll)
}

a_ nlm(fn,p=c(.9,.5),hes=T)
est_ a\$est

phat_ c(dnbinom(kk[-length(kk)],est[1],est[2]),
1-pnbinom((max(kk)-1),est[1],est[2]))
E_ n*phat
chi2_ sum((x-E)^2/E)
cat('Chi2 goodness-of-fit = ',chi2,'\n')
cat('P-value=',1-pchisq(chi2,df=length(kk)-1-2),'\n')

print(cbind(O=x,E=round(E,1),err=round((x-E)^2/E,1)))

==========================

At 09:14 AM 2/7/01 +0000, Mark Myatt <mark at myatt.demon.co.uk> wrote:
>All,
>
>I have some data on parasites on apple leaves and want to do a
>goodness of fit test to a Poisson distribution. This seems to
>do it:
>
>  mites <- c(rep(0,70),
>             rep(1,38),
>             rep(2,17),
>             rep(3,10),
>             rep(4,9),
>             rep(5,3),
>             rep(6,2),
>             rep(7,1))
>
>  tab <- table(mites)
>  NSU <- length(mites)
>  N <- sum(mites)
>  NSU.Mean <- N / NSU
>  exp <- dpois(as.numeric(dimnames(tab)[[1]]), NSU.Mean) *  NSU
>  chi2 <- sum(((as.vector(tab) - exp)^2) / exp)
>
>  tab
>  exp
>  chi2
>
>I have some small expected values and so need to collapse some
>categories. If anyone has code to do that already ... but my
>question is how would I do the same for a negative binomial
>distribution?
>
>Mark
>
>--
>Mark Myatt
>
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
._._
>
Yudi Pawitan     yudi at stat.ucc.ie
Department of Statistics UCC
Cork, Ireland
Ph   353-21-490 2906
Fax 353-21-427 1040
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```