1
votes

Stargazer appears to take most Zelig model objects except logistic regression:

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

I receive the following warning from the above code:

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

Anyone have any alternatives for presenting such a model in a regression output table using Knitr to produce a .tex/.Rnw file?

3
Can you provide a reproducible example of this?CL.
The title of this question doesn't match the actual question. There is nothing about stargazer in the example...Ista

3 Answers

3
votes

Building on Constantin's helpful answer, I created a simple function unzelig to take a glm zelig object and return a conventional glm object:

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

Also, there's an in-development function to convert some non-Zelig objects to Zelig-friendly objects with a command to_zelig. This would allow estimating a model with standard R functions like lm or glm, using stargazer or another package to generate a table and then converting the object to use Zelig functions like setx and sim. See more, here: http://docs.zeligproject.org/reference/to_zelig.html

2
votes

This might be coming too late, but a solution is to re-estimate the model with glm() from base R. The following code worked for me, on a logistic regression estimated with the new reference classes syntax for 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")))

The same strategy would work when estimating a linear regression with Zelig.

1
votes

The Zelig package now includes a convenience function that makes it easy to extract fitted model objects:

stargazer(from_zelig_model(m1))

should produce the desired result.