I am creating a regression table using the stargazer
package in R. However, I am struggling with the alignment of different aspects: (i) how can I horizontally center the model numbers and statistics in the respective columns and (ii) how can I vertically align the model numbers and statistics in the respective rows?
In particular, for the top values there is way too much space above the values, for the bottom values the issue is less severe but still clearly visible. In addition, I want the values to be centered in the respective columns. Is there a way to achieve this directly in R?
Corresponding R code:
library(stargazer)
library(dplyr)
df <- cars
mod1 <- lm(dist ~ speed, data = df)
mod2 <- lm(dist ~ dplyr::lag(speed, 1), data = df)
stargazer(
mod1, mod2,
omit = c("Constant"),
omit.stat = c("f", "ser", "rsq"),
dep.var.caption = "",
dep.var.labels.include = FALSE,
covariate.labels = c("a", "b"),
no.space=TRUE,
align = TRUE,
type = "latex")
Corresponding Latex input:
documentclass[letterpaper]{article}
usepackage{dcolumn}
begin{document}
begin{table}[!htbp] centering
caption{}
label{}
begin{tabular}{@{extracolsep{5pt}}lD{.}{.}{-3} D{.}{.}{-3} }
\[-1.8ex]hline
hline \[-1.8ex]
\[-1.8ex] & multicolumn{1}{c}{(1)} & multicolumn{1}{c}{(2)}\
hline \[-1.8ex]
a & 3.932^{***} & \
& (0.416) & \
b & & 4.042^{***} \
& & (0.408) \
hline \[-1.8ex]
Observations & multicolumn{1}{c}{50} & multicolumn{1}{c}{49} \
Adjusted R$^{2}$ & multicolumn{1}{c}{0.644} & multicolumn{1}{c}{0.669} \
hline
hline \[-1.8ex]
textit{Note:} & multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \
end{tabular}
end{table}
end{document}