ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

export regression table with stargazer: $ operator is invalid for atomic vectors

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

Upvotes: 4

Views: 4855

Answers (3)

Ekow_ababio
Ekow_ababio

Reputation: 163

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

Upvotes: 1

jackbauer
jackbauer

Reputation: 155

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

Upvotes: 6

ℕʘʘḆḽḘ
ℕʘʘḆḽḘ

Reputation: 19375

best solution was

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

thanks!

Upvotes: -4

Related Questions