4

我试图估计特威迪(或复合泊松伽马)分布的有限混合。我搜索了我能想到的任何资源,但没有找到任何关于如何做到这一点的资源。

我目前正在尝试使用 R 中的flexmix包编写不同的 M-step 驱动程序,如第 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")

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
}

但是,这会导致以下错误:

dtweedie(y, xi = p, mu = mu, phi = phi) 中的错误:对不一致数组的二元运算

有没有人做过或见过 tweedie 分布的有限混合?您能指出我使用flexmix还是其他方式来完成此任务的正确方向吗?

4

1 回答 1

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

  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
}

例子:

library(flexmix)
data("dmft", package = "flexmix")
m1 <- flexmix(End ~ .,data=dmft, k = 4, model = tweedieClust())
于 2015-10-12T07:50:55.170 回答