2015-03-31 44 views
3

我試圖估計Tweedie(或複合Poisson-gamma)分佈的有限混合。我已經搜尋了我能想到的任何資源,但沒有找到任何資源來說明如何做到這一點。Tweedie的有限混合

我目前正在使用flexmix包R寫入不同的M-步進驅動器,如在12-14頁的flexmix小插曲概述。這裏是我的代碼,它依賴於CPLM包:

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") 

[email protected] <- 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) 
}) 
[email protected] <- 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([email protected])) 
} 
retval 
} 

然而,這會導致以下錯誤:

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

有沒有人做過或者看到特威迪分佈的有限混合?你能指出我在正確的方向來完成這個,使用flexmix或其他?

+0

你找到了一個解決方案? – spore234 2015-10-09 08:57:49

回答

2

的問題是某處的重量部分,如果你刪除它,它的工作原理:

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") 

    [email protected] <- 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) 
    }) 
    [email protected] <- 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([email protected])) 
    } 
    retval 
} 

例如:

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