简体   繁体   English

无法获得partykit包的mob函数来进行单变量MLE拟合

[英]Can't get partykit package's mob function to do univariate MLE fit

I can't get the partykit package's mob function to do univariate MLE fit. 我无法获得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) 

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 UseMethod(“ estfun”)中的错误:没有适用于“ estfun”的适用方法应用于“ fitdistr”类的对象当使用glm而不是fitdistr时,不会出现上述错误消息

# estfun runs OK outside of call to mob! 
estfun(logit(PimaIndiansDiabetes$diabetes,PimaIndiansDiabetes$glucose)) 

In principle, it is feasible to use mob() for what you want to do but there is a misunderstanding of what the estfun() method is supposed to do and how it is being called. 原则上,将mob()用于您想做的事情是可行的,但是对estfun()方法应该做的事情以及如何调用它存在误解。

mob() needs the following pieces of information from a model object to carry out the construction of the tree: mob()需要来自模型对象的以下信息来进行树的构造:

  • The estimated parameters, typically extracted by coef(object) . 估计的参数,通常由coef(object)提取。
  • The optimized objective function, typically extracted by logLik(object) . 优化的目标函数,通常由logLik(object)提取。
  • The estimating functions aka scores aka gradient contributions, typically extracted by estfun(object) . 估计函数又称为梯度贡献,通常由estfun(object)提取。 See vignette("sandwich-OOP", package = "sandwich") for an introduction. 有关简介,请参见vignette("sandwich-OOP", package = "sandwich")

For objects of class "fitdistr" the former two are available but the latter is not: 对于"fitdistr"类的对象,前两个可用,但后者不可用:

methods(class = "fitdistr")
## [1] coef   logLik print  vcov  
## see '?methods' for accessing help and source code

Hence: 因此:

f <- fitdistr(x, "gamma")
coef(f)
## shape  rate 
## 5.022 0.103 
logLik(f)
## 'log Lik.' -3404 (df=2)
sandwich::estfun(f)
## Error in UseMethod("estfun") : 
##   no applicable method for 'estfun' applied to an object of class "fitdistr"

The estfun() function you have defined does not work for the following two reasons: (1) It is not a method estfun.fitdistr() that could be called by the generic function sandwich::estfun() that is used through the package's NAMESPACE . 您定义的estfun()函数由于以下两个原因而无法正常工作:(1)并非该estfun.fitdistr()方法可以通过包的通用函数sandwich::estfun()调用NAMESPACE (2) It does not compute the right quantity: it's the log-likelihood but we need the derivative of the log-density with respect to both parameters and evaluated at each observation. (2)它没有计算正确的数量:这是对数似然,但我们需要关于两个参数的对数密度的导数,并在每次观察时进行评估。 The latter would be an nx 2 matrix. 后者将是nx 2矩阵。

I think it shouldn't be too hard to compute the score function of the gamma distribution by hand. 我认为手动计算伽玛分布的得分函数应该不难。 But this should also be available in some R package already, possibly gamlss.dist or also other packages. 但这也应该已经在某些R包中可用,可能是gamlss.dist或其他包中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 mob() 树(partykit 包)与 nls() 模型一起使用 - Using mob() trees (partykit package) with nls() model 使用 mob() 树(partykit 包)和logistic() model - Using mob() trees (partykit package) with logistic() model 如何在MOB树的每个节点中找到观测值? (partykit软件包) - How to find the observation in each node of the MOB tree? (partykit package) partykit::mob() + mlogit: `错误没有指定合适的 function 配件` - partykit::mob() + mlogit: `Error no suitable fitting function specified` R中的ar()函数错误-仅针对单变量序列实现MLE - Error with ar() function in R - MLE only implemented for univariate series 缺少参数“适合”:方包中的R mob() - argument “fit” missing : R mob() in party package Montecarlo Simulation 使用 mob() 算法(partykit 包)来恢复正确识别模型的数量 - Montecarlo Simulation using mob() algorithm (partykit package) to recover the count of the correctly identified models 是否可以使用基于模型的树(即 partykit 包中的“mob()”)构建随机森林 - Is it possible to build a random forest with model based trees i.e., `mob()` in partykit package 如何在R中的partykit / glmertree包的绘图功能中缩小内部节点的表示形式? - How can I make the internal node representation smaller in the plotting function of the partykit/glmertree package in R? 单变量拟合时在mclust-package中出错 - getting error in mclust-package while working on univariate fit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM