[R] Fitting a Tweedie distribution

Ben Bolker bbolker at gmail.com
Fri Jan 2 21:01:13 CET 2015


Paul Hudson <paulhudson028 <at> gmail.com> writes:

> 
> Hello all,
> 
> I want to fit a tweedie distribution to the data I have.
> 
> The R packages I have been able to find assume that I want to use it as
> part as of a generalized linear model.
> 
> This is not the case, I want to directly fit the distribution to the data.
> 
> Is there a package that allows this?

This took a little bit of fussing but it seems OK:

library("tweedie")
set.seed(1001)
r <- rtweedie(1000,1.5,mu=2,phi=2)
library("bbmle")
dtweedie2 <- function(x,power,mu,phi,log=FALSE,debug=FALSE) {
    if (debug) cat(power,mu,phi,"\n")
    res <- dtweedie(y=x,xi=power,mu=mu,phi=phi)
    if (log) log(res) else res
}
m <- mle2(r~dtweedie2(power=exp(logpower),
                     mu=exp(logmu),
                     phi=exp(logphi)),
          ## don't start with logpower=0 (power=1)
          start=list(logpower=0.1,logmu=0,logphi=0),
          data=data.frame(r),
          method="Nelder-Mead")

dtweedie2(r,xi=exp(0.1),mu=1,phi=1)

In principle MASS::fitdistr could be made to work too.



More information about the R-help mailing list