texreg - create empty column - r

I use texreg to create a LaTeX table from R. I would like to insert an empty column into the output (for stylistic reasons). I can of course do this manually, but it would great to be able to create this automatically. Here is an example:
library(texreg)
x1 <- rnorm(n=100,m=1,s=1)
x2 <- rnorm(n=100,m=1,s=1)
e <- rnorm(n=100,m=0,s=1)
y <- 0.1 + 0.3 * x1 + 0.4 * x2 + e
reg1 <- lm(y~x1)
reg2 <- lm(y~x2)
reg3 <- lm(y~x1+x2)
texreg(list(reg1,reg2,reg3)
,stars=c(0.01,0.05,0.1)
,digits=2
,dcolumn=T
,use.packages=F
,file="tabfile.tex")
This gives me the output:
\begin{table}
\begin{center}
\begin{tabular}{l D{.}{.}{3.5} D{.}{.}{3.5} D{.}{.}{3.5} }
\hline
& \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} & \multicolumn{1}{c}{Model 3} \\
\hline
(Intercept) & 0.59^{***} & 0.53^{***} & 0.05 \\
& (0.16) & (0.16) & (0.19) \\
x1 & 0.36^{***} & & 0.40^{***} \\
& (0.11) & & (0.10) \\
x2 & & 0.43^{***} & 0.48^{***} \\
& & (0.11) & (0.11) \\
\hline
R$^2$ & 0.10 & 0.13 & 0.25 \\
Adj. R$^2$ & 0.09 & 0.12 & 0.23 \\
Num. obs. & 100 & 100 & 100 \\
RMSE & 1.05 & 1.03 & 0.96 \\
\hline
\multicolumn{4}{l}{\scriptsize{$^{***}p<0.01$, $^{**}p<0.05$, $^*p<0.1$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
What I would like to have instead is:
\begin{table}
\begin{center}
\begin{tabular}{lc D{.}{.}{3.5} D{.}{.}{3.5} D{.}{.}{3.5} }
\hline
&& \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} & \multicolumn{1}{c}{Model 3} \\
\hline
(Intercept) && 0.59^{***} & 0.53^{***} & 0.05 \\
&& (0.16) & (0.16) & (0.19) \\
x1 && 0.36^{***} & & 0.40^{***} \\
&& (0.11) & & (0.10) \\
x2 && & 0.43^{***} & 0.48^{***} \\
&& & (0.11) & (0.11) \\
\hline
R$^2$ && 0.10 & 0.13 & 0.25 \\
Adj. R$^2$ && 0.09 & 0.12 & 0.23 \\
Num. obs. && 100 & 100 & 100 \\
RMSE && 1.05 & 1.03 & 0.96 \\
\hline
\multicolumn{5}{l}{\scriptsize{$^{***}p<0.01$, $^{**}p<0.05$, $^*p<0.1$}}
\end{tabular}
\caption{Statistical models}
\label{table:coefficients}
\end{center}
\end{table}
It would of course be even better, if there was a way to insert arbitrary empty columns.

You can insert new columns using the custom.columns argument of texreg. The new columns should be handed over as a named list. The custom.col.pos argument can be used to indicate where the columns should be located. For example, you could insert a new, empty column by adding the argument custom.columns = list("new column" = rep("", 3)).

Related

Equation in a table in R

I want a table like this and make it as a kable so I can make footnote:
but I didn't manage so I made a table latex like this :
$$
\begin{array}{lccccc}
\hline
{Copula} & {Distribution} & {Parameter range} & {Complete dependence} & {Independence} \\
\hline
{Normal} & {C_{Normal} (u_1,u_2,\rho)=\Phi_{\rho}(\Phi^{-1}(u_1),\Phi^{-1}(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Student-t} & {C_t (u_1,u_2;\rho,d)=t_{\rho,d}(t^{-1}_d(u_1),t^{-1}_d(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Gumbel} & {C_{Gumbel} (u_1,u_2,\beta)=exp\{-[(-ln(u_1))^{\frac{1}{\beta}}+(-ln(u_2))^{\frac{1}{\beta}}]^{\beta}\}} & {\beta\in(0,1)} & {\beta=0} & {\beta=1} \\
\hline
{RG} & {C_{RG} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Gumbel} (1-u_1,1-u_2,\alpha)} & {\alpha\in[1,\infty)} & {\alpha\rightarrow\infty} & {\alpha=1} \\
\hline
{Clayton} & {C_{Clayton} (u_1,u_2,\theta)=max((u_1^{-\theta}+u_2^{-\theta}-1)^{-\frac{1}{\theta}},0)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
{RC} & {C_{RC} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Clayton} (1-u_1,1-u_2,\theta)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
\end{array}
$$
and it looks like this:
It's not bad until I found it's not easy to add footnotes since I used to make kable.
Is there any way I can add a footnote for it? or how can I convert it into a table so I can use kable to display it?

How to avoid changes in the row names when I use xtable package?

Let's say that I have the following information.
My_inf<-structure(c(0.00376, 0.00332, 0.00256, 0.00371, 0.00179, 0.00817,
0.00817, 0.00746, 0.00959, 0.00533, 3.44713, 3.44713, 3.44713,
3.44713, 3.44713, -3.44713, -3.44713, -3.44713, -3.44713, -3.44713,
1.0005, 0.99994, 1.00172, 1.00108, 1.00147, -0.00941, -0.00833,
-0.00816, -0.00956, -0.00468, 2.95108, 2.95429, 2.94188, 2.94577,
2.94165), .Dim = c(5L, 7L), .Dimnames = list(c("NSW$^{+}$", "QLD$^{+}$",
"SA$^{+}$", "TAS$^{+}$", "VIC$^{+}$"), c("Mean", "Median", "Max",
"Min", "Std. Dev.", "Skewness", "Kurtosis")))
I want to use the xtable command for transform this data frame into a latex table. Using xtable(My_inf) but i get the following output.
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrr}
\hline
& Mean & Median & Max & Min & Std. Dev. & Skewness & Kurtosis \\
\hline
NSW\$\verb|^|\{+\} \$& 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\
QLD\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\
SA\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.94 \\
TAS\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\
VIC\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.00 & 2.94 \\
\hline
\end{tabular}
\end{table}
As you can see the row names have changed according to xtable command. My desired output would be, for example:
NSW$^{+}$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\
How can I get my desired output? I should change the row names in the original data frame or maybe use another package for latex tables.
This is interesting, but every R package library generates a little another code.
Stargazer
library(stargazer)
stargazer(My_inf, type = 'latex')
% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Ср, ноя 03, 2021 - 20:42:52
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{#{\extracolsep{5pt}} cccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& Mean & Median & Max & Min & Std. Dev. & Skewness & Kurtosis \\
\hline \\[-1.8ex]
NSW\$$\hat{\mkern6mu}$\{+\}\$ & $0.004$ & $0.008$ & $3.447$ & $$-$3.447$ & $1.000$ & $$-$0.009$ & $2.951$ \\
QLD\$$\hat{\mkern6mu}$\{+\}\$ & $0.003$ & $0.008$ & $3.447$ & $$-$3.447$ & $1.000$ & $$-$0.008$ & $2.954$ \\
SA\$$\hat{\mkern6mu}$\{+\}\$ & $0.003$ & $0.007$ & $3.447$ & $$-$3.447$ & $1.002$ & $$-$0.008$ & $2.942$ \\
TAS\$$\hat{\mkern6mu}$\{+\}\$ & $0.004$ & $0.010$ & $3.447$ & $$-$3.447$ & $1.001$ & $$-$0.010$ & $2.946$ \\
VIC\$$\hat{\mkern6mu}$\{+\}\$ & $0.002$ & $0.005$ & $3.447$ & $$-$3.447$ & $1.001$ & $$-$0.005$ & $2.942$ \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
kableExtra
library(kableExtra)
kable(My_inf, "latex")
\begin{tabular}{l|r|r|r|r|r|r|r}
\hline
& Mean & Median & Max & Min & Std. Dev. & Skewness & Kurtosis\\
\hline
NSW\$\textasciicircum{}\{+\}\$ & 0.00376 & 0.00817 & 3.44713 & -3.44713 & 1.00050 & -0.00941 & 2.95108\\
\hline
QLD\$\textasciicircum{}\{+\}\$ & 0.00332 & 0.00817 & 3.44713 & -3.44713 & 0.99994 & -0.00833 & 2.95429\\
\hline
SA\$\textasciicircum{}\{+\}\$ & 0.00256 & 0.00746 & 3.44713 & -3.44713 & 1.00172 & -0.00816 & 2.94188\\
\hline
TAS\$\textasciicircum{}\{+\}\$ & 0.00371 & 0.00959 & 3.44713 & -3.44713 & 1.00108 & -0.00956 & 2.94577\\
\hline
VIC\$\textasciicircum{}\{+\}\$ & 0.00179 & 0.00533 & 3.44713 & -3.44713 & 1.00147 & -0.00468 & 2.94165\\
\hline
\end{tabular}
But if you are discontented - you can parse an output LaTeX code and remove redundant symbols.

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?

Stargazer() does not fit the page

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.

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