我试图估计有限的tweedie(或复合Poisson-gamma)分布混合物。我已经搜索了我能想到的任何资源,却没有找到任何有关如何做到这一点的资源。
我目前正在尝试使用R中的 flexmix 包编写不同的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")
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 或其他方式指出我正确的方向来实现这一目标?
答案 0 :(得分: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())