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? [[alternative HTML version deleted]]
The tweedle package[1] claims to have "functions for computing and fitting the Tweedie family of distributions". Hope that helped. -- H 1. http://cran.r-project.org/web/packages/tweedie On 2 January 2015 at 10:33, Paul Hudson <paulhudson028 at gmail.com> wrote:> 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? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- OpenPGP: https://hasan.d8u.us/gpg.key Sent from my mobile device Envoy? de mon portable [[alternative HTML version deleted]]
On Jan 2, 2015, at 10:33 AM, Paul Hudson wrote:> 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?install.packages("sos") findFn("tweedie") #Scrolling down the resulting page, I do see a 'tweedie' package. require(tweedie) # has the rtweedie function y <- rtweedie(100, xi=1.5, mu=20, phi=3) help(pack=tweedie) # Bottom of page says tweedie.profile will estimate power res <- tweedie.profile(y~1) #1.2 1.3 1.4 1.5 1.6 1.7 1.8 #.......Done. str(res) #------------- List of 12 $ y : num [1:50] -410 -408 -406 -405 -403 ... $ x : num [1:50] 1.2 1.21 1.22 1.24 1.25 ... $ ht : num -396 $ L : num [1:7(1d)] -410 -399 -395 -394 -395 ... $ xi : num [1:7] 1.2 1.3 1.4 1.5 1.6 1.7 1.8 $ xi.max : num 1.49 $ L.max : num -394 $ phi : num [1:7(1d)] 4.85 4.16 3.43 2.8 2.3 ... $ phi.max : num 2.83 $ ci : num [1:2(1d)] 1.37 1.62 $ method : chr "inversion" $ phi.method: chr "mle" Seems to estimate both the power and dispersion parameters reasonably well.> > [[alternative HTML version deleted]]This is a plain-text mailing list. -- David Winsemius Alameda, CA, USA
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.
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.