gpt4 book ai didi

R - stargazer 添加引用类别

转载 作者:行者123 更新时间:2023-12-02 01:17:08 30 4
gpt4 key购买 nike

我想知道是否有人提出了使用stargazer显示cal分类变量的引用类别的解决方案?

library(stargazer) 

让我们假设 gearcarb 是分类变量

mtcars$gear = factor(mtcars$gear)
mtcars$carb = factor(mtcars$carb)

我使用

运行 ols
lm1 = lm(disp ~ gear + carb, mtcars)

观星结果。

stargazer(lm1, single.row = TRUE,  omit.table.layout = "sn")

我明白

enter image description here

但是,我发现自己总是返回到 tex 文件来自定义引用类别以获取

enter image description here

基本上,我所做的就是添加到变量之间的latex:

gear (ref = 3) &  \\ 
\-\hspace{0.3cm} gear4 & $-$202.921$^{***}$ (22.477) \\

等等。

有人知道我是否可以在 stargazer 函数中添加这些类型的吗?

最佳答案

您可以通过向 stargazer 提供 covariate.labels 来实现您想要的输出:

library(magrittr)
library(stringr)
library(stargazer)

covlabels <-
names(lm1$coefficients)[-1] %>%
if_else(str_sub(., 1, 4) == "gear" | str_sub(., 1, 4) == "carb", paste("\\-\\hspace{0.3cm}", .), .) %>%
if_else(str_sub(., 18, 24) == "gear4", paste("gear (ref=3) \\\\", .), .) %>%
if_else(str_sub(., 18, 24) == "carb2", paste("carb (ref=1) \\\\", .), .)

stargazer(lm1, single.row = TRUE, omit.table.layout = "sn", covariate.labels=covlabels)

产量

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Mon, Jan 08, 2018 - 3:18:09 AM
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}}lc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & disp \\
\hline \\[-1.8ex]
gear (ref=3) \\ \-\hspace{0.3cm} gear4 & $-$202.921$^{***}$ (22.477) \\
\-\hspace{0.3cm} gear5 & $-$160.898$^{***}$ (36.282) \\
carb (ref=1) \\ \-\hspace{0.3cm} carb2 & 71.282$^{**}$ (27.919) \\
\-\hspace{0.3cm} carb3 & 25.574 (39.919) \\
\-\hspace{0.3cm} carb4 & 155.852$^{***}$ (27.355) \\
\-\hspace{0.3cm} carb6 & 55.672 (68.065) \\
\-\hspace{0.3cm} carb8 & 211.672$^{***}$ (68.065) \\
Constant & 250.226$^{***}$ (24.363) \\
\hline \\[-1.8ex]
\hline
\hline \\[-1.8ex]
\end{tabular}
\end{table}

关于R - stargazer 添加引用类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40683646/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com