问题描述
我想知道是否有人提出了一种解决方案,可以使用 stargazer
显示分类变量的参考类别?
I was wondering if someone came with a solution to show up the reference categories of categorical variables using stargazer
?
library(stargazer)
让我们假设 gear
和 carb
是分类变量
Let us imagine that gear
and carb
are categorical variables
mtcars$gear = factor(mtcars$gear)
mtcars$carb = factor(mtcars$carb)
我用
lm1 = lm(disp ~ gear + carb, mtcars)
和 stargaze
结果.
stargazer(lm1, single.row = TRUE, omit.table.layout = "sn")
我明白了
但是,我发现自己总是回到 tex
文件来自定义参考类别来获取
However, I find myself always going back to the tex
file to custom the reference categories to get
基本上,我所做的是在变量之间添加 latex
:
Basically, what I do is to add to the latex
in between variables :
gear (ref = 3) & \
-hspace{0.3cm} gear4 & $-$202.921$^{***}$ (22.477) \
等等.
有人知道我是否可以在 stargazer
函数中添加这类 lines
吗?
Anyone had any idea if I can add these kind of lines
in the stargazer
function ?
推荐答案
你可以通过向stargazer
提供covariate.labels
来实现你想要的输出:
You can achieve the output you want by providing covariate.labels
to stargazer
:
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
egin{table}[!htbp] centering
caption{}
label{}
egin{tabular}{@{extracolsep{5pt}}lc}
\[-1.8ex]hline
hline \[-1.8ex]
& multicolumn{1}{c}{ extit{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 添加参考类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!