R 包 stargazer 产生两个 table 输出而不是一个

R package stargazer produces two table outputs instead of one

我目前正在为 r markdown、knitr 和 stargazer 软件包苦苦挣扎。更具体地说,我想生成逻辑回归的输出:

{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)

但是,该包一直生成两个表而不是一个 (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) & ($-[=12=].104, 1.666) \
## Private sector & 0.077 & $-[=12=].283 \
## & ($-[=12=].695, 0.850) & ($-.213, 0.648) \
## MNE & 0.467 & 0.260 \
## & ($-[=12=].237, 1.172) & ($-[=12=].516, 1.036) \
## Org. age & $-[=12=].003 & $-[=12=].004 \
## & ($-[=12=].009, 0.002) & ($-[=12=].010, 0.002) \
## Strategic integration & & 0.176 \
## & & ($-[=12=].313, 0.665) \
## Devolvement of HRM & & $-[=12=].174 \
## & & ($-[=12=].725, 0.376) \
## Trade unions’ influence & & $-[=12=].138 \
## & & ($-[=12=].444, 0.168) \
## Intercept & $-.129$^{*}$ & $-.039 \
## & ($-.456, 0.197) & ($-.247, 2.168) \
## \textit{N} & 178 & 145 \
## Log Likelihood & $-9.004 & $-.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}

有没有人遇到过类似的问题and/or知道如何解决这些问题?非常感谢!

编辑:这里我使用 iris 数据集重现了错误

---
title: "Reproduction for Whosebug 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)

```

您可以使用名为 gtsummary 的包进行回归 tables。

用这个替换你的 stargazer 行:

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")

tbl_regression() 创建一个基本的 table 并且在管道之后调用的其他函数进一步自定义 table。

详情请看这里:https://www.danieldsjoberg.com/gtsummary/