I can't get the partykit package's mob function to do univariate MLE fit.
# 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)
Error in UseMethod("estfun") : no applicable method for 'estfun' applied to an object of class "fitdistr" The above error message does not appear when glm is used instead of fitdistr
# estfun runs OK outside of call to mob!
estfun(logit(PimaIndiansDiabetes$diabetes,PimaIndiansDiabetes$glucose))