简体   繁体   English

R:使用texreg从zelig tobit模型创建tex输出

[英]R: creating tex output from zelig tobit model with texreg

What I am trying to do should be fairly easy: I estimate a tobit model using the R package Zelig . 我想做的事情应该很容易:我使用R包Zelig估算了一个轨道模型。 From this I want to create a tex output using texreg . 由此,我想使用texreg创建一个tex输出。 But what I get back is the error message: 但是我得到的是错误消息:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'extract' for signature '"Zelig-tobit"' 错误(函数(类,fdef,mtable):无法找到签名“ Zelig-tobit”的函数“ extract”的继承方法

This is rather strange as texreg does have an extraction method for tobit models. 这是相当奇怪的,因为texreg 确实有针对位模型的提取方法。 I also tried specifying a function myself but could not get it to work. 我也尝试自己指定一个函数,但无法使其正常工作。 Here is an example code: 这是示例代码:

library(Zelig)
library(texreg)

a <- c(2, 2, 2, 4, 3, 5, 9, 9, 9)
b <- c(18, 20, 19, 17, 22, 48, 12, 22, 37)
c <- c(0.1, 0.02, 0.5, 1.2, 0.9, 0.1, 1.1, 0.7, 0.6)

dat <- data.frame(a, b, c)

model <- zelig(a ~ b + c, below = 2, above = 9, model = "tobit", data = dat)

texreg(model)

I am using R Studio on a Windows computer, the texreg version is 1.36.4 and the Zelig version 5.0-11. 我在Windows计算机上使用R Studiotexreg版本为1.36.4,而Zelig版本为5.0-11。

This question seemed closely related to my problem: texreg-ing Tobit output from zelig package (R) 这个问题似乎与我的问题密切相关: zelig软件包(R)中的Tobit输出的texreging

However, according to this it should have been fixed a few versions ago, which is not the case for me. 但是,据此,它应该在几个版本之前已修复,对我而言不是这种情况。

Thanks in advance for your help! 在此先感谢您的帮助!

(Btw, I also tried using stargazer instead of texreg which yielded me just another error message.) (顺便说一句,我还尝试使用stargazer而不是texreg ,这给了我另一个错误消息。)


I tried to write my own extract function but due to my amateurishness in function writing can't get it to work. 我试图编写自己的提取函数,但由于我对函数编写的业余爱好,无法使其正常工作。 Here is what I did: 这是我所做的:

extract.tob <- function(model, include.iterations = TRUE, include.loglik = TRUE,
                        include.wald = TRUE, ...) {

  s <- model
  names <- rownames(s$coef)
  co <- s$coef[, 1]
  se <- s$coef[, 2]
  pval <- s$coef[, 4]

  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()

  if (include.iterations == TRUE) {
    it <- s$iterations
    gof <- c(gof, it)
    gof.names <- c(gof.names, "Number of\\iterations")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.loglik == TRUE) {
    ll <- s$logLik
    gof <- c(gof, ll)
    gof.names <- c(gof.names, "Log-\\likelihood")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.wald == TRUE) {
    wd <- s$wald
    gof <- c(gof, wd)
    gof.names <- c(gof.names, "Wald-\\statistic")
    gof.decimal <- c(gof.decimal, TRUE)
  }

  tr <- createTexreg(
  coef.names = names,
  coef = co,
  se = se,
  pvalues = pval,
  gof.names = gof.names,
  gof = gof,
  gof.decimal = gof.decimal
  )
  return(tr) 

}  

setMethod("extract", signature = className("Zelig-tobit", "Zelig"),
          definition = extract.tob)

As I see it, the zelig model is already "summarized", that is why I set s <- model instead of summary(model) as in the example. 正如我所看到的,zelig模型已经“汇总”了,这就是为什么我设置s <-模型而不是示例中的summary(model)的原因。 My main problem seems to be that I can't get the needed statistics (log-likelihood, wald ...) out of the model as I do not know how to adress them. 我的主要问题似乎是我无法从模型中获得所需的统计信息(对数似然,瓦尔德...),因为我不知道如何处理它们。 The output of str() and so on does not help me with this. str()等的输出对我没有帮助。 Apart from simply not knowing the "names" of the statistics, there also seems to be a problem with how to adress them. 除了不知道统计信息的“名称”外, 如何处理它们似乎还存在问题。

When I try something like "model$coef" I get: 当我尝试类似“ model $ coef”的东西时,我得到:

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : envRefInferField(x,what,getClass(class(x)),selfEnv)中的错误:
'coef' is not a valid field or method name for reference class “Zelig-tobit” “ coef”不是参考类“ Zelig-tobit”的有效字段或方法名称

With "model@coef" I get: 通过“ model @ coef”,我得到:

Error: no slot of name "coef" for this object of class "Zelig-tobit" 错误:该类“ Zelig-tobit”的对象没有名称为“ coef”的插槽

And model[,1] yields me: 和model [,1]产生我:

Error in modelt6[, 1] : object of type 'S4' is not subsettable modelt6 [,1]中的错误:'S4'类型的对象不可子集化

Does anyone have any idea how to make the extract function work? 有谁知道如何使提取功能起作用? Or another, easier way to get the model output into Latex? 还是将模型输出输入到Latex的另一种更简单的方法?

It looks like Zelig-tobit objects as defined in the Zelig package are merely containers that include tobit objects as defined in the AER package, among other things. 看起来Zelig软件包中定义的Zelig-tobit对象仅仅是容器,其中包括AER软件包中定义的tobit对象。 Therefore you should be able to run texreg on the tobit object that is contained in model : 因此,您应该能够在model包含的tobit对象上运行texreg

screenreg(model$zelig.out$z.out[[1]])

yields: 产量:

==========================
                Model 1   
--------------------------
(Intercept)     -18.42    
                (16.34)   
b                 0.49    
                 (0.36)   
c                17.51    
                (11.49)   
Log(scale)        1.76 ***
                 (0.49)   
--------------------------
AIC              33.55    
BIC              34.34    
Log Likelihood  -12.78    
Deviance          9.46    
Total             9       
Left-censored     3       
Uncensored        3       
Right-censored    3       
Wald Test         2.35    
==========================
*** p < 0.001, ** p < 0.01, * p < 0.05

It is possible to write an extract method to do that automatically. 可以编写一个extract方法来自动执行此操作。 Here is an example: 这是一个例子:

# extension for Zelig-tobit objects (Zelig package)
extract.Zeligtobit <- function(model, include.aic = TRUE, include.bic = TRUE, 
    include.loglik = TRUE, include.deviance = TRUE, include.nobs = FALSE, 
    include.censnobs = TRUE, include.wald = TRUE, ...) {
  e <- extract(model$zelig.out$z.out[[1]], include.aic = include.aic, 
      include.bic = include.bic, include.loglik = include.loglik, 
      include.deviance = include.deviance, include.nobs = include.nobs, 
      include.censnobs = include.censnobs, include.wald = include.wald, ...)
  return(e)
}  

setMethod("extract", signature = className("Zelig-tobit", "Zelig"), 
    definition = extract.Zeligtobit)

Now you can just write: 现在您可以编写:

screenreg(model)

which yields the same output as above. 产生与上述相同的输出。

I never quite understood why people use Zelig instead of the original packages like AER . 我从未完全理解为什么人们使用Zelig而不是AER之类的原始软件包。 Zelig merely provides wrappers for other existing estimation functions and thereby complicates the data structures in an unnecessary way. Zelig仅提供其他现有估计功能的包装器,从而以不必要的方式使数据结构复杂化。 Eg, why do you not just use the AER package? 例如,为什么不只使用AER软件包?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM