Stargazer() does not fit the page - r

Good afternoon, I was trying to find the answer to such question, although there is no information. The point is that I want to put the output of my models (3 models) in the latex file. Although, when I do such thing using stargazer() it leads to 2 problems - first, when I want to show 3 models or more the resulting table does not fit on the page, in particular - going to the right so far, second, when I have many variables, it does not fit the page as well so many variables are not shown. How to deal with it?
\usepackage{dcolumn}
\begin{table}[!htbp] \centering
\caption{Results}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} D{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{2}{c}{\textit{Dependent variable:}} \\
\cline{2-3}
\\[-1.8ex] & \multicolumn{2}{c}{log(Price)} \\
\\[-1.8ex] & \multicolumn{1}{c}{\textit{OLS}} & \multicolumn{1}{c}
{\textit{panel}} \\
& \multicolumn{1}{c}{\textit{}} & \multicolumn{1}{c}{\textit{linear}}
\\
\\[-1.8ex] & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)}\\
\hline \\[-1.8ex]
Coll & 0.513^{***} & 0.019 \\
& (0.028) & (0.039) \\
& & \\
Constant & 0.110^{***} & \\
& (0.038) & \\
& & \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{14,727} & \multicolumn{1}{c}{14,727} \\
R$^{2}$ & \multicolumn{1}{c}{0.256} & \multicolumn{1}{c}{0.011} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.255} & \multicolumn{1}{c}{-0.341} \\
Residual Std. Error & \multicolumn{1}{c}{0.297 (df = 14699)} & \\
F Statistic & \multicolumn{1}{c}{187.710$^{***}$ (df = 27; 14699)} &
\multicolumn{1}{c}{14.477$^{***}$ (df = 8; 10868)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05;
$^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
Here I basically provide a sample with only 2 models with less variables (just for convenience), however, when I use all of them it does not fit the page.

Try wrapping the tabular part inside scalebox. So it would be something like:
\begin{table}[!htbp] \centering
\caption{Results}
\label{}
\scalebox{0.85}{
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} D{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{2}{c}{\textit{Dependent variable:}} \\
\cline{2-3}
\\[-1.8ex] & \multicolumn{2}{c}{log(Price)} \\
\\[-1.8ex] & \multicolumn{1}{c}{\textit{OLS}} & \multicolumn{1}{c}
{\textit{panel}} \\
& \multicolumn{1}{c}{\textit{}} & \multicolumn{1}{c}{\textit{linear}}
\\
\\[-1.8ex] & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)}\\
\hline \\[-1.8ex]
Coll & 0.513^{***} & 0.019 \\
& (0.028) & (0.039) \\
& & \\
Constant & 0.110^{***} & \\
& (0.038) & \\
& & \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{14,727} & \multicolumn{1}{c}{14,727} \\
R$^{2}$ & \multicolumn{1}{c}{0.256} & \multicolumn{1}{c}{0.011} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.255} & \multicolumn{1}{c}{-0.341} \\
Residual Std. Error & \multicolumn{1}{c}{0.297 (df = 14699)} & \\
F Statistic & \multicolumn{1}{c}{187.710$^{***}$ (df = 27; 14699)} &
\multicolumn{1}{c}{14.477$^{***}$ (df = 8; 10868)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05;
$^{***}$p$<$0.01} \\
\end{tabular}
}
\end{table}
I set scale to 0.85, but you can play around with it. If the print gets too small and the table still doesn't fit, try using the longtable package so you can spread the table over multiple pages.

Related

Using texreg to print mean of dependent variable instead of R squared in R

I have some regression models that I want to export to LaTeX. I am using the package "texreg". The final output is:
\begin{table}
\begin{center}
\begin{tabular}{l c c}
\hline
& Model 1 & Model 2 \\
\hline
as.factor(treat)1 & $145227140331.44$ & \\
& $(2648642987059.38)$ & \\
as.factor(msj)1 & & $-647801609586.01$ \\
& & $(3435830264390.74)$ \\
as.factor(msj)2 & & $433277589458.84$ \\
& & $(3288915232361.39)$ \\
as.factor(msj)3 & & $-1691975577225.66$ \\
& & $(3293603457594.94)$ \\
as.factor(msj)4 & & $-843647941334.40$ \\
& & $(3359975197112.69)$ \\
as.factor(msj)5 & & $576301517588.01$ \\
& & $(3349253720672.64)$ \\
as.factor(msj)6 & & $3301581741709.62$ \\
& & $(3403672471154.21)$ \\
as.factor(msj)7 & & $17431285033.90$ \\
& & $(3695545300670.01)$ \\
\hline
Num. obs. & $1000$ & $1000$ \\
R$^2$ (full model) & $0.02$ & $0.03$ \\
R$^2$ (proj model) & $0.00$ & $0.00$ \\
Adj. R$^2$ (full model) & $0.01$ & $0.01$ \\
Adj. R$^2$ (proj model) & $-0.01$ & $-0.01$ \\
Num. groups: as.factor(strata) & $10$ & $10$ \\
\hline
\multicolumn{3}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
Instead of the latex output displaying the R-squared and Adjusted R-squared, can I get it to show the mean of the dependent variable?

Latex - table not showing

I have seen several discussions on this board treating the topic but none of the solutions seem to work. I use R stargazer and have downloaded a table under Latex format, but when I copy paste the TeX code in my Latexfile, it does not show up in the PDF. I have also tried to use \input{myfile} with the table but the results is the same. Please find below a shorter version of the table. Does anybody have a solution ?
\documentclass[12pt]{article}
\begin{document}
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lcccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Pctl(25)} & \multicolumn{1}{c}{Median} & \multicolumn{1}{c}{Pctl(75)} & \multicolumn{1}{c}{Max} \\
\hline \\[-1.8ex]
total\_asset & 10 & 30 & 40 & 50 & 60 & 50.000 & 5,556.000 \\
EBITDA\_margin & 6 & $-$5 & 12 & $-$1,250.000 & $-$0.085 & 0.1 & 0.033 & 42,500.000 \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
\end{document}
Kind regards,

Stargazer not publishing regression table

The packages I currently have downloaded are: tinytex, stargazer, and rmarkdown.
I have used this function to create a publishable table:
stargazer(EB734_May_2010_model_u)
But got this instead:
% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Sep 02, 2020 - 09:30:46
\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] & trust \\
\hline \\[-1.8ex]
D\_economy2 & $-$0.220$^{*}$ \\
& (0.119) \\
& \\
D\_economy0 & 0.243$^{**}$ \\
& (0.118) \\
& \\
E\_economy2 & $-$0.166 \\
& (0.112) \\
& \\
E\_economy0 & $-$0.168 \\
& (0.112) \\
& \\
Support\_EMU0 & 0.655$^{***}$ \\
& (0.027) \\
& \\
Constant & $-$2.038$^{***}$ \\
& (0.148) \\
& \\
\hline \\[-1.8ex]
Observations & 18,282 \\
Log Likelihood & $-$22,543.580 \\
Akaike Inf. Crit. & 45,141.170 \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
I need to publish the summary for my logistic regression model into word from R.
Any idea on how to fix this? Thanks. :)
The easiest way would be:
stargazer(EB734_May_2010_model_u, type = "text")
which will give you output in text form that you can use immediately.
Alternatively, if you want a good-looking HTML table that you can then paste into Word:
stargazer(EB734_May_2010_model_u, out = "output.html")
Don't worry if your Rmd shows a lot of code below as output, just go into your working directory after you run it, and look for a file called "output.html". You should be able to open that with Word, and then copy and paste into your document.

R markdown won't compile a piece of LaTeX code

I have the following piece of code, which I would like to run I R markdown. I have tried to run it in a LaTeX compiler, which works absolutely fine. But when I add it into R markdown it won't compile the piece of code.
---
title: "Titre"
date: Fecha
output:
pdf_document:
keep_tex: true
includes:
in_header: mystyle.sty
---
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & \multicolumn{1}{c}{mpg} \\
\hline \\[-1.8ex]
cyl & -2.876^{***} \\
& (0.322) \\
& \\
Constant & 37.885^{***} \\
& (2.074) \\
& \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{32} \\
R$^{2}$ & \multicolumn{1}{c}{0.726} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.717} \\
Residual Std. Error & \multicolumn{1}{c}{3.206 (df = 30)} \\
F Statistic & \multicolumn{1}{c}{79.561$^{***}$ (df = 1; 30)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
The file mystyle.sty contains the following
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{booktabs}
\newcommand{\mc}[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\usepackage{verbatim}
Are there someone who can explain why R markdown can't compile what's above?
By adding the table code line by line and recompiling each time, I isolated the problem to the lines with carets, e.g. the line that reads cyl & -2.876^{***} \\.
I was able to compile the document by enclosing the superscripted part in two $ signs, i.e.
cyl & -2.876$$^{***}$$ \\
(I don't know why you need two, rather than just one, $.)
This compiles just fine:
---
title: "Titre"
date: Fecha
output:
pdf_document:
keep_tex: true
includes:
in_header: mystyle.sty
---
\begin{table}[!htbp] \centering
\begin{tabular}{#{\extracolsep{5pt}}lD{.}{.}{-3} }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{1}{c}{\textit{Dependent variable:}} \\
\cline{2-2}
\\[-1.8ex] & \multicolumn{1}{c}{mpg} \\
\hline \\[-1.8ex]
cyl & -2.876$$^{***}$$ \\
& (0.322) \\
& \\
Constant & 37.885$$^{***}$$ \\
& (2.074) \\
& \\
\hline \\[-1.8ex]
Observations & \multicolumn{1}{c}{32} \\
R$^{2}$ & \multicolumn{1}{c}{0.726} \\
Adjusted R$^{2}$ & \multicolumn{1}{c}{0.717} \\
Residual Std. Error & \multicolumn{1}{c}{3.206 (df = 30)} \\
F Statistic & \multicolumn{1}{c}{79.561$^{***}$ (df = 1; 30)} \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{1}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

Using stargazer for systemfit objects

I wonder how to use stargazer for systemfit objects. My working example is below which gives two different tables rather than one.
library("systemfit")
data("Kmenta")
eqDemand <- consump ~ price + income
eqSupply <- consump ~ price + farmPrice + trend
eqSystem <- list(demand = eqDemand, supply = eqSupply)
fitols <- systemfit(eqSystem, data=Kmenta)
fitsur <- systemfit(eqSystem, method = "SUR", data=Kmenta)
library(stargazer)
stargazer(
coef(fitols)
, coef(fitsur)
, title="Regression Results"
, align=TRUE
)
It can be done using texreg function from texreg package:
texreg(list(fitols, fitsur))
\begin{table}
\begin{center}
\begin{tabular}{l c c c c }
\hline
& Model 1 & Model 2 & NA & NA \\
\hline
(Intercept) & $99.90^{***}$ & $58.28^{***}$ & $99.33^{***}$ & $61.97^{***}$ \\
& $(7.52)$ & $(11.46)$ & $(7.51)$ & $(11.08)$ \\
price & $-0.32^{**}$ & $0.16$ & $-0.28^{**}$ & $0.15$ \\
& $(0.09)$ & $(0.09)$ & $(0.09)$ & $(0.09)$ \\
income & $0.33^{***}$ & & $0.30^{***}$ & \\
& $(0.05)$ & & $(0.04)$ & \\
farmPrice & & $0.25^{***}$ & & $0.21^{***}$ \\
& & $(0.05)$ & & $(0.04)$ \\
trend & & $0.25^{*}$ & & $0.34^{***}$ \\
& & $(0.10)$ & & $(0.07)$ \\
\hline
R$^2$ & 0.76 & 0.65 & 0.76 & 0.61 \\
Adj. R$^2$ & 0.74 & 0.59 & 0.73 & 0.54 \\
Num. obs. & 40 & 40 & 40 & 40 \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.001$, $^{**}p<0.01$, $^*p<0.05$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
Not supported yet, but planned for a future release. If you have any suggestion regarding what the final table should look like (and/or what is important in systemfit regression table output), please e-mail stargazer's author.

Resources