我试图估计一个有限混合的tweedie (或复合Poisson-γ)分布。我已经搜索了我能想到的任何资源,却没有找到任何关于如何做到这一点的资源。
我目前正在尝试使用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")
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分布?你能指出正确的方向来完成这一点,使用柔性混合或其他吗?
发布于 2015-10-12 07:50:55
问题就在重量部分的某个地方,如果你去掉它,它就会起作用:
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())https://stackoverflow.com/questions/29378515
复制相似问题