[R] Fitting a Tweedie distribution

Ben Bolker bbolker at gmail.com
Fri Jan 2 21:04:59 CET 2015


Ben Bolker <bbolker <at> gmail.com> writes:

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

[snip]
 
> 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.

  PS in hindsight, you're better off with the built-in tweedie.power()
recommended by another poster.  Estimating the power parameter for 
Tweedie distributions is known to be difficult, and the naive approach I show
above may only work in best-case scenarios.



More information about the R-help mailing list