[code]library(MASS) x=c(rep(0,8096), rep(1,1629), rep(2,233), rep(3,38), rep(4,4) ) x.bar=round(mean(x),4) x.var=round(var(x),4) p.hat=round(x.bar/x.var,4) alpha.hat=round(x.bar*p.hat/(1-p.hat),4) fitdistr(x, "Negative Binomial") fitdistr(x, "Poisson")[/code] 1- fitdistr(x, "Negative Binomial") the parameters got here, is it for negative binomial type 2? how can i ask it to use the methods of moments to calculat the parameters? (p.hat and alpha.hat which i derived from methods of moments seem to give a different value) 2-how can i fit it and than test the goodness of it? 3-then compare with the Poisson model? Thanks ============================================== by negative binomial type two i mean -- View this message in context: http://n4.nabble.com/test-the-goodness-of-it-for-negative-binomial-type-2-tp1575892p1575892.html Sent from the R help mailing list archive at Nabble.com.
Achim Zeileis
2010-Mar-03 00:12 UTC
[R] test the goodness of it for negative binomial type 2
On Tue, 2 Mar 2010, casperyc wrote:> > [code]library(MASS) > x=c(rep(0,8096), > rep(1,1629), > rep(2,233), > rep(3,38), > rep(4,4) > ) > > x.bar=round(mean(x),4) > x.var=round(var(x),4) > > p.hat=round(x.bar/x.var,4) > alpha.hat=round(x.bar*p.hat/(1-p.hat),4) > > fitdistr(x, "Negative Binomial") > fitdistr(x, "Poisson")[/code] > > > 1- fitdistr(x, "Negative Binomial") > the parameters got here, is it for negative binomial type 2?Yes.> how can i ask it to use the methods of moments to calculat the > parameters?You can't with fitdist()> (p.hat and alpha.hat which i derived from methods of moments seem to give > a different value)It is a different parametrization of NB2 that you are using. Your alpha.hat corresponds to "size" and p.hat corresponds to "prob" in ?dnbinom. However, fitdistr() uses the "mu" parametrization, i.e. R> alpha.hat * (1 - p.hat) / p.hat [1] 0.222497 which roughly matches the ML estimate for mu from fitdistr().> 2-how can i fit it and than test the goodness of it?You could use goodfit() from "vcd": gf_pois <- goodfit(x, type = "poisson") plot(gf_pois) summary(gf_pois) gf_nbin <- goodfit(x, type = "nbinom") plot(gf_nbin) summary(gf_nbin) which shows that the fit from the NB is satisfactory while the fit from the Poisson is not. hth, Z> 3-then compare with the Poisson model? > > Thanks > > ==============================================> > by negative binomial type two i mean > > > > > -- > View this message in context: http://n4.nabble.com/test-the-goodness-of-it-for-negative-binomial-type-2-tp1575892p1575892.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >