无法获得partykit包的mob函数来进行单变量MLE拟合 [英] Can't get partykit package's mob function to do univariate MLE fit

查看:258
本文介绍了无法获得partykit包的mob函数来进行单变量MLE拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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) 

UseMethod("estfun")中的错误:没有适用于'estfun'的方法 应用于类"fitdistr"的对象上面的错误信息 使用glm代替fitdistr时不会出现

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)) 

推荐答案

原则上,将mob()用于您想做的事是可行的,但是对estfun()方法应该做的事有误解以及它的调用方式.

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()需要来自模型对象的以下信息来执行树的构建:

mob() needs the following pieces of information from a model object to carry out the construction of the tree:

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

对于类"fitdistr"的对象,前两个可用,而后者不可用:

For objects of class "fitdistr" the former two are available but the latter is not:

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

因此:

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"

您定义的estfun()函数由于以下两个原因而无法工作:(1)不是方法estfun.fitdistr()可以由在包的sandwich::estfun()调用>. (2)它没有计算正确的数量:这是对数似然,但我们需要关于两个参数的对数密度的导数,并在每次观察时进行评估.后者将是一个n x 2矩阵.

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. (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. The latter would be an n x 2 matrix.

我认为用手计算伽玛分布的得分函数应该不难.但这也应该已经在某些R软件包中提供了,可能是gamlss.dist或其他软件包.

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.

这篇关于无法获得partykit包的mob函数来进行单变量MLE拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆