Quantcast

How to Fit Inflated Negative Binomial

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How to Fit Inflated Negative Binomial

Lorenzo Isella
Dear All,
I am trying to fit some data both as a negative binomial and a zero
inflated binomial.
For the first case, I have no particular problems, see the small snippet
below

library(MASS) #a basic R library

set.seed(123) #to have reproducible results

x4 <- rnegbin(500, mu = 5, theta = 4)

#Now fit and check that we get the right parameters

fd <- fitdistr(x4, "Negative Binomial")

summary(fd)

#and mu and theta are as expected
#now I add artificially some zeros

x5 <- sample(c(x4,rep(0,100)))

However, when I artificially add some zeros (case of x5) I am unsure
about how to proceed.
I found some material online

http://bit.ly/uPVzOT
http://bit.ly/rXnzKi

but I wonder if they are an overkill of what I am really after (a
fitting a negative binomial with too many zeros).
Any suggestion is really appreciated.
Many thanks

Lorenzo

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to Fit Inflated Negative Binomial

trinker

try: library(pscl)
 
There's a zeroinfl for zero inflated neg. binom.
 
Tyler


 

> Date: Thu, 17 Nov 2011 17:50:46 +0100
> From: [hidden email]
> To: [hidden email]
> Subject: [R] How to Fit Inflated Negative Binomial
>
> Dear All,
> I am trying to fit some data both as a negative binomial and a zero
> inflated binomial.
> For the first case, I have no particular problems, see the small snippet
> below
>
> library(MASS) #a basic R library
>
> set.seed(123) #to have reproducible results
>
> x4 <- rnegbin(500, mu = 5, theta = 4)
>
> #Now fit and check that we get the right parameters
>
> fd <- fitdistr(x4, "Negative Binomial")
>
> summary(fd)
>
> #and mu and theta are as expected
> #now I add artificially some zeros
>
> x5 <- sample(c(x4,rep(0,100)))
>
> However, when I artificially add some zeros (case of x5) I am unsure
> about how to proceed.
> I found some material online
>
> http://bit.ly/uPVzOT
> http://bit.ly/rXnzKi
>
> but I wonder if they are an overkill of what I am really after (a
> fitting a negative binomial with too many zeros).
> Any suggestion is really appreciated.
> Many thanks
>
> Lorenzo
>
> ______________________________________________
> [hidden email] 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.
     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to Fit Inflated Negative Binomial

bbolker
Tyler Rinker <tyler_rinker <at> hotmail.com> writes:

>
>
> try: library(pscl)
>
> There's a zeroinfl for zero inflated neg. binom.
>
> Tyler

> >
> > Dear All,
> > I am trying to fit some data both as a negative binomial and a zero
> > inflated binomial.
> > For the first case, I have no particular problems, see the small snippet
> > below
> >

 
set.seed(123) #to have reproducible results

## You don't actually need MASS::rnegbin, rnbinom in base
##  R works fine (different parameter names)

x6 <- c(rep(0,100),rnbinom(500,mu=5,size=4))

## sample() is irrelevant, it just permutes the results

library(pscl)
zz <- zeroinfl(x6~1|1,dist="negbin")
exp(coef(zz)[1])  ## mu
zz$theta          ## theta
plogis(coef(zz)[2]) ## zprob

Alternatively you can use fitdistr with the dzinbinom()
function from the emdbook package:

library(emdbook)
fitdistr(x6,dzinbinom,start=list(mu=4,size=5,zprob=0.2))

The pscl solution is likely to be much more robust.

______________________________________________
[hidden email] 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.
Loading...