r - Finite mixture of tweedie -


i'm trying estimate finite mixture of tweedie (or compound poisson-gamma) distributions. have scoured resources can think of, without finding resources on how this.

i trying use flexmix package in r writing different m-step driver, outlined in flexmix vignette on pages 12-14. here code, relies on cplm package:

tweedieclust <- function(formula = .~.,offset = null){ require(tweedie) require(cplm) require(plyr) require(dplyr) retval <- new("flxmc", weighted = true, formula = formula, dist = "tweedie",               name = "compound poisson clustering")  retval@definecomponent <- expression ({     predict <- function(x, ...) {         pr <- mu     }     loglik <- function(x, y, ...){         dtweedie(y, xi = p, mu = mu, phi = phi) %>%              log     }     new("flxcomponent",         parameters=list(coef=coef),         loglik=loglik, predict=predict,         df=df) }) retval@fit <- function (x, y, w, component) {     fit <- cpglm(formula = y ~ x, link = "log", weights=w, offset=offset)     with(list(coef = coef(fit), df = ncol(x),mu = fit$fitted.values,               p = fit$p, phi = fit$phi),          eval(retval@definecomponent)) } retval } 

however, results in following error:

error in dtweedie(y, xi = p, mu = mu, phi = phi) : binary operation on non-conformable arrays

has done or seen finite mixture of tweedie distributions? can point me in right direction accomplish this, using flexmix or otherwise?

the problem somewhere in weights part, if remove it, works:

tweedieclust <- function(formula = .~.,offset = null){   require(tweedie)   require(statmod)   require(cplm)   require(plyr)   require(dplyr)   retval <- new("flxmc", weighted = f, formula = formula, dist = "tweedie",             name = "compound poisson clustering")    retval@definecomponent <- expression ({     predict <- function(x, ...) {       pr <- mu     }     loglik <- function(x, y, ...){       dtweedie(y, xi = p, mu = mu, phi = phi) %>%         log     }     new("flxcomponent",         parameters=list(mu=mu,xi=p,phi=phi),         loglik=loglik, predict=predict,         df=df)   })   retval@fit <- function (x, y, w, component) {     fit <- cpglm(formula = end~.,data=dmft, link = "log")     with(list(df = ncol(x), mu = fit$fitted.values,               p = fit$p, phi = fit$phi),          eval(retval@definecomponent))   }   retval } 

example:

library(flexmix) data("dmft", package = "flexmix") m1 <- flexmix(end ~ .,data=dmft, k = 4, model = tweedieclust()) 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -