我正在尝试估计花呢(或复合泊松 Gamma )分布的有限混合。我已经搜索了我能想到的任何资源,但没有找到有关如何执行此操作的任何资源。
我目前正在尝试使用 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
}
但是,这会导致以下错误:
有没有人做过或看到过花呢分布的有限混合?你能指出我正确的方向来完成这个,使用 flexmix 或其他方式吗?
最佳答案
问题出在权重部分的某个地方,如果您将其删除,它会起作用:
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())
关于r - 花呢的有限混合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29378515/