[R] Glm and user defined variance functions

Ben Bolker bbolker at gmail.com
Wed Mar 14 21:45:59 CET 2012


Craig Lyon <craig.lyon <at> rogers.com> writes:

> 
> Hi,
> 
> I am trying to run a generalized linear regression using a negative binomial
> error distribution. However, I want to use an overdispersion parameter that
> varies (dependent on the length of a stretch of road) so glm.nb will not do.
> 
> >From what I've read I should be able to do this using GLM by specifying my
> own quasi family and describing the variance function using varfun, validmu,
> dev.resids and initiate. However I cannot find any detailed discussion of
> this method. Any help would be appreciated.
> 
> ps I've done this in SAS so I know it should be possible!
> 

  I think you'd be better off with the bbmle package, e.g.
something like

  library(bbmle)
  mle2(response~dnbinom(mu=exp(logmu),size=exp(logk)),data=mydata,
    parameters=list(logmu~fac1+fac2,logk=roadlength),
    start=list(logmu=1,logk=0))

Slightly less efficient than setting up your own quasi family
but probably much faster in terms of analyst time ...



More information about the R-help mailing list