2016-11-18 47 views
2

我想知道是否有人用解決方案顯示使用stargazer的分類變量的參考類別?R - stargazer添加參考類別

library(stargazer) 

讓我們想象一下gearcarb是分類變量

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

我運行一個ols

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

stargaze結果。

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函數中添加這些lines

+0

我認爲這將是等效的,或許更容易地捆綁在的名稱所有的參考值截距。 –

+0

你能告訴我如何? – giacomo

回答

1

如果您願意接受我的修訂策略,然後提取lm1對象中xlevels -lidt-item的名稱及其關聯的第一級別,並用粘貼的字符值替換「(截距)值:

baselines = sapply(lm1$xlevels, "[[", 1) 
names(lm1$coefficients)[1] = paste0(names(baselines), " = ", baselines, 
            collapse="; ") 

我現在得到:

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

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu 
% Date and time: Sat, Nov 19, 2016 - 07:49:18 
\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 = 3; carb = 1 & 250.226$^{***}$ (24.363) \\ 
    gear4 & $-$202.921$^{***}$ (22.477) \\ 
    gear5 & $-$160.898$^{***}$ (36.282) \\ 
    carb2 & 71.282$^{**}$ (27.919) \\ 
    carb3 & 25.574 (39.919) \\ 
    carb4 & 155.852$^{***}$ (27.355) \\ 
    carb6 & 55.672 (68.065) \\ 
    carb8 & 211.672$^{***}$ (68.065) \\ 
\hline \\[-1.8ex] 
\hline 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

我不似乎有一個正確配置的乳膠工具鏈了,可能是由於「增強的安全功能,」蘋果在過去的OSX「升級」出臺。

+0

這是一個有趣的解決方案謝謝 – giacomo

2

可以通過提供covariate.labelsstargazer達到你想要的輸出:

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}