2

I'm currently struggling with r markdown, knitr, and, the stargazer package. More specifically, I want to produce the output of a logistic regression:

{r table1, include = TRUE, echo = FALSE, results='asis', message = FALSE}

testmodel <- glm(hra_target_bin ~ size_log + as.factor(private) + as.factor(MNE) + org_age, data = cdclean, family = binomial)
realmodel <- glm(hra_target_bin ~ size_log + as.factor(private) + as.factor(MNE) + org_age + strat_int + devolvement + s5v2, data = cdclean, family = binomial)
        
stargazer(testmodel, realmodel, type = "latex",  title = "Logistic regression of
organizational characteristics on the adoption of HR analytics", no.space = TRUE,
dep.var.labels="Adoption of HR analytics", covariate.labels = c("Size (log)","Private 
sector", "MNE", "Org. age", "Strategic integration", "Devolvement of HRM", "Trade unions' influence",
"Intercept"), ci = TRUE, odd.ratio = TRUE, header = FALSE)

However, the package keeps producing two tables instead of one (LaTex):

##
## \begin{table}[!htbp] \centering
## \caption{Logistic regression of organizational characteristics on the adoption of HR analytics}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}}lcc}
## \\[-1.8ex]\hline
## \hline \\[-1.8ex]
## \\[-1.8ex] & \multicolumn{2}{c}{Adoption of HR analytics} \\
## \\[-1.8ex] & (1) & (2)\\
## \hline \\[-1.8ex]
## Size (log) & 0.820$^{**}$ & 0.781$^{*}$ \\
## & (0.026, 1.614) & ($-$0.104, 1.666) \\
## Private sector & 0.077 & $-$0.283 \\
## & ($-$0.695, 0.850) & ($-$1.213, 0.648) \\
## MNE & 0.467 & 0.260 \\
## & ($-$0.237, 1.172) & ($-$0.516, 1.036) \\
## Org. age & $-$0.003 & $-$0.004 \\
## & ($-$0.009, 0.002) & ($-$0.010, 0.002) \\
## Strategic integration & & 0.176 \\
## & & ($-$0.313, 0.665) \\
## Devolvement of HRM & & $-$0.174 \\
## & & ($-$0.725, 0.376) \\
## Trade unions’ influence & & $-$0.138 \\
## & & ($-$0.444, 0.168) \\
## Intercept & $-$2.129$^{*}$ & $-$1.039 \\
## & ($-$4.456, 0.197) & ($-$4.247, 2.168) \\
## \textit{N} & 178 & 145 \\
## Log Likelihood & $-$119.004 & $-$95.994 \\
## Akaike Inf. Crit. & 248.008 & 207.989 \\
## \hline
## \hline \\[-1.8ex]
## \textit{Notes:} & \multicolumn{2}{l}{\parbox[t]{\textwidth}{Logistic regression. Dependent variable: ## \end{tabular}
## \end{table}
##
## \begin{table}[!htbp] \centering
## \caption{Logistic regression of organizational characteristics on the adoption of HR analytics}
## \label{}
## \begin{tabular}{@{\extracolsep{5pt}} c}
## \\[-1.8ex]\hline \\[-1.8ex]
## TRUE \\
## \hline
## \hline \\[-1.8ex]
## \multicolumn{1}{l}{\parbox[t]{\textwidth}{Logistic regression. Dependent variable: an indicator varible ## \end{tabular}
## \end{table}

Has anyone experienced similar issues and/or has an idea how to fix them? Thank you very much!

EDIT: Here I reproduced the error using the iris dataset

---
title: "Reproduction for StackOverflow using iris"
author: "M.Rapp"
date: "4 5 2022"
output: pdf_document
---    

```{r loading, include = FALSE, message=FALSE, warning=FALSE, error=FALSE}

library(foreign)
library(dplyr)
library(haven)
library(tidyverse)
library(tidymodels)
library(lme4)
library(ggplot2)
library(stargazer)
library(knitr)
library(foreign)
library(rmarkdown)
library(tinytex)


options(scipen = 999) # avoid scientific notation
options(max.print = 2000) # print up to 2000 lines


```

```{r tabelle, include = TRUE, echo = FALSE, results='asis', message = FALSE}

ir_data<- iris

set.seed(100)
samp<-sample(1:100,80)
ir_test<-ir_data[samp,]
ir_ctrl<-ir_data[-samp,]

y <- ir_test$Species
x <- ir_test$Sepal.Length
glfit<-glm(y~x, family = 'binomial')



stargazer(glfit, type = "latex",  title = "Logistic regression on species", no.space = TRUE, dep.var.labels="Species", covariate.labels = c("Sepal length", "Intercept"), ci = TRUE, odd.ratio = TRUE, header = FALSE)

```
Marco Rapp
  • 23
  • 5
  • can you share your data and model statements – Mike May 04 '22 at 15:31
  • Hey Mike, thanks for asking! I added the model statements but I can't share the data. If needed I may reproduce it using the iris dataset – Marco Rapp May 04 '22 at 15:36
  • if you can reproduce it with iris that would be great. – Mike May 04 '22 at 15:37
  • are you open to using other packages or do you need it to be stargazer – Mike May 05 '22 at 13:16
  • when you specify the `odd.ratio = TRUE` that is when the second table appears, I don't know why though – Mike May 05 '22 at 13:24
  • 1
    Hey Mike, thanks for the quick answer! I tried all the options but failed to imagine that deleting them might fix it. I'll contact the developers, thank you for pointing that one out. No I'm not tied to stargazer, but preferred it out of convenience. If you've another suggestions please let me know. Thanks again and best wishes from Vienna, – Marco Rapp May 05 '22 at 13:39

2 Answers2

2

You can use the package called gtsummary to make regression tables.

Replace your stargazer line with this:

library(gtsummary)
tbl_regression(glfit, exponentiate = TRUE,
               intercept = TRUE) %>% 
    add_significance_stars() %>% 
    add_glance_source_note(include = c(AIC, BIC, df.residual,nobs)) %>% 
    modify_caption("Logistic regression on species")

The tbl_regression() creates a basic table and the other functions called after pipes further customize the table.

see here for more details: https://www.danieldsjoberg.com/gtsummary/

Mike
  • 3,797
  • 1
  • 11
  • 30
0

I had the same issue, and it was resolved once I added "include=FALSE" to the chunk where the stargazer function is called.