用Stargazer或其他包装在Knitr中呈现Zelig Logistic回归

时间:2016-02-09 22:13:33

标签: r latex knitr

除了逻辑回归之外,Stargazer似乎占据了大多数Zelig模型对象:

m1 <- zelig(voted ~ . - weight_full - by,
        weights = mydata$weight_full,
        data=mydata,
        model="logit",
        cite = FALSE)

我从以上代码收到以下警告:

# Error in envRefInferField(x, what, getClass(class(x)), selfEnv)  
# ‘result’ is not a valid field or method name for reference class “Zelig-logit”

任何人都可以使用Knitr在回归输出表中显示这样的模型来生成.tex / .Rnw文件吗?

3 个答案:

答案 0 :(得分:1)

这可能为时已晚,但解决方案是使用基础R中的glm()重新估计模型。以下代码对我有用,使用Zelig的新引用类语法估计的逻辑回归

mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
model1 <- zlogit$new()
model1$zelig(admit ~ gpa,
             data=mydata)
library(stargazer)
stargazer(glm(model1$zelig.out$z.out[[1]], family=binomial(link = "logit")))

使用Zelig估算线性回归时,同样的策略也可行。

答案 1 :(得分:1)

基于康斯坦丁的有用答案,我创建了一个简单的函数unzelig来获取glm zelig对象并返回常规的glm对象:

# extract data + model + family zelig object, return glm object
unzelig <- function(zelig_model) {
  z_out <- zelig_model$zelig.out$z.out[[1]]
  z_family <- z_out[["family"]][["family"]]

  # when zelig(model = 'ls') assign family <- "gaussian"
  if(is.null(z_family)) {z_family <- "gaussian"}

  glm(z_out, 
      family = z_family) 
  }

# simple example
z1 <- zelig(mpg ~ cyl, model = 'ls', data = mtcars, cite = FALSE)
z2 <- zelig(I(mpg > 15) ~ cyl, model = 'logit', data = mtcars, cite = FALSE)
z3 <- zelig(as.integer(mpg) ~ cyl, model = 'poisson', data = mtcars, cite = FALSE)

g1 <- unzelig(z1)
g2 <- unzelig(z2)
g3 <- unzelig(z3)

stargazer(g1, g2, g3, type = 'text')  

# error check 
g1 <- glm(mpg ~ cyl, data = mtcars)
g2 <- glm(I(mpg > 15) ~ cyl, family = binomial, data = mtcars)
g3 <- glm(as.integer(mpg) ~ cyl, family = poisson, data = mtcars)

stargazer(g1, g2, g3, type = 'text')  

此外,还有一个正在开发的功能,可使用命令to_zelig将一些非Zelig对象转换为Zelig友好对象。这将允许使用lm或其他包来估计具有标准R函数(如glmstargazer的模型,使用Zelig或其他包来生成表,然后将对象转换为使用setx函数sim <?= GridView::widget([ 'dataProvider' => $dataProvider, 'rowOptions' => function ($model) { //add your condition here if ($model->id == 1 || $model->id == 2 || $model->id == 3 ) { return ['style' => ' position:fixed;']; } }, 'filterModel' => $searchModel, 'headerRowOptions' => ['class' => 'kartik-sheet-style'], 'filterRowOptions' => ['class' => 'kartik-sheet-style'], 'columns' => [ 'id', 'employee_id', 'fullname', 'gender', 'birth', 'birthplace', 'hometown', 'address', 'addressnow', 'phone', 'mailcom', 'mail', 'skype', 'cmnd', 'datecmnd', 'placecmnd', 'race', 'religion', 'skill', 'datecom', 'team', 'bhxh', 'datebhxh', 'placebhxh', 'emergency', 'emergencynum', 'groupmail', ['class' => 'yii\grid\ActionColumn'], ], ]); ?> 。在此处查看更多信息: http://docs.zeligproject.org/reference/to_zelig.html

答案 2 :(得分:1)

Zegig软件包现在包括一个便捷功能,可以轻松提取拟合的模型对象:

def new_week(self, detentions, day, motivation)

应该产生预期的结果。