4

I am trying to export a regression table using stargazer. The regression output comes from glm and looks like:

Call:
glm(formula = formula, family = binomial(logit), data = data)

Deviance Residuals: 
Min       1Q   Median       3Q      Max  
-1.2913  -0.11888  -0.3239  -0.3216   2.6627  

Coefficients:
                        Estimate Std. Error z value Pr(>|z|)    
(Intercept)               -3.4839244  0.2439274 -14.283  < 2e-16 ***
data$var              0.00144  0.003666   0.021  0.2724    

unfortunately I have no control over the variable names of that regression. When I try to run stargazer to export the table in tex I get the error

$ operator is invalid for atomic vectors

What should I do? I tried to change the labels of the variables with stargazer but this does not work.

stargazer(glm_output,
      title            = "results",
      covariate.labels = c("newname"),
      dep.var.caption  = "caption",
      dep.var.labels   = "dep",
      rownames = FALSE)

Many thanks!!!

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 1
    If it does not need to be stargazer, you can try one of the following packages: sjPlot, texreg or extracting the information first with broom and then format them with pixiedust. – Helix123 Aug 06 '15 at 13:55
  • 2
    You can use `pixiedust` if you can live with markdown or HTML output, but `pixiedust` doesn't yet support LaTeX. If you need the LaTeX format, you could extract the information with `broom` and print the `broom` output to a table with either `stargazer` or `xtable` – Benjamin Aug 06 '15 at 14:17
  • 2
    this could go for a reproducible example. i suspect there's an issue with inputs... – MichaelChirico Aug 06 '15 at 14:20
  • 1
    If you used the data argument to glm() and just gave a formula `depvar~var` you wouldn't be seeing this issue. – IRTFM Aug 06 '15 at 14:37

3 Answers3

6

Is your object glm_output the result of summary(glm(...)) or glm(...)? stargazer() should be called on the glm object itself, not its summary.

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
jackbauer
  • 155
  • 2
  • 14
1

I find gtsummary package (by Daniel D. Sjoberg et al) very helpful for exporting regression and summary statistics tables. In the case above the tbl_regression function from the package can be used to export the glm result. Sample code for doing this below:

library(tidyverse)
library(gtsummary)


height <- runif(100, min=140, max=200)
weight <- runif(100, min=40, max=110)
status <- rbinom(n=100, size=1, prob=0.25)

df <- data.frame(height=height,
                 weight=weight,
                 status=status)
   
   
mod <- glm(status ~ weight + height, df, family = binomial)

Table1 <- tbl_regression(mod, exponentiate = TRUE)

tmp <- "~path/name.docx"  ## specifying the directory path and name of the word document

Table1 %>% 
  as_flex_table() %>% 
  flextable::save_as_docx(path=tmp) ##export result as word doc

The result of the glm as shown in the output below is then saved in the same format as a word document. You can also modify the variable labels, include more information in the output table, etc. See the package website for detailed examples.

enter image description here

https://cran.r-project.org/web/packages/gtsummary/index.html https://www.danieldsjoberg.com/gtsummary/articles/tbl_regression.html

Ekow_ababio
  • 163
  • 9
-4

best solution was

  • tidy up the data with broom
  • use stargazer on the cleaned dataframe

thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235