我无法让 partykit 包的 mob 函数进行单变量 MLE 拟合。
# Trying to convert vignette example here https://cran.r-project.org/web/packages/partykit/vignettes/mob.pdf on page 7 to do univariate MLE gamma fits.
data("PimaIndiansDiabetes", package = "mlbench")
library("partykit")
library("fitdistrplus")
# Generating some fake data to replace the example data.
op <- options(digits = 3)
set.seed(123)
x <- rgamma(nrow(PimaIndiansDiabetes), shape = 5, rate = 0.1)
PimaIndiansDiabetes$diabetes<-x
PimaIndiansDiabetes$glucose<-x
#Hopefully this change to the formula means fit a gamma to just the diabetes vector of values!
pid_formula <- diabetes ~ 1 | pregnant + pressure + triceps + insulin + mass + pedigree + age
#Defining my own, negative of log likelihood since mob will minimize it.
estfun<-function(z) {-logLik(z)}
#replacing the call to glm that is successful in the vignette example.
class(fitdistr) <- append(class(fitdistr),estfun)
logit <- function(y, x, start = NULL, weights = NULL, offset = NULL, ...) {
fitdistr(y, "gamma")
}
#fail! The mob() function still does not see my artificially created estfun().
pid_tree <- mob(pid_formula, data = PimaIndiansDiabetes, fit = logit)
UseMethod("estfun") 中的错误:没有适用于 'estfun' 的方法应用于类“fitdistr”的对象 当使用 glm 而不是 fitdistr 时,不会出现上述错误消息
# estfun runs OK outside of call to mob!
estfun(logit(PimaIndiansDiabetes$diabetes,PimaIndiansDiabetes$glucose))