How to add a horizontal line in tables::tabular() output? - r

I would like to add a horizontal line (using Hline()) in a moderately complex tables::tabular() output, when there are two factors on one side of the table.
Consider this:
require(tables)
require(car)
latex(
tabular( (Factor(partner.status, "Status") + 1)
* (Factor(fcategory, "Authoritarianism") + 1)
* ((Pct=Percent()))*Format(digits=1)
~ 1, data=Moore)
)
Which will yield:
\begin{tabular}{lllc}
\hline
Status & Authoritarianism & & \multicolumn{1}{c}{All} \\
\hline
high & high & Pct & $\phantom{0}16$ \\
& low & Pct & $\phantom{0}11$ \\
& medium & Pct & $\phantom{0}24$ \\
& All & Pct & $\phantom{0}51$ \\
low & high & Pct & $\phantom{0}18$ \\
& low & Pct & $\phantom{0}22$ \\
& medium & Pct & $\phantom{00}9$ \\
& All & Pct & $\phantom{0}49$ \\
All & high & Pct & $\phantom{0}33$ \\
& low & Pct & $\phantom{0}33$ \\
& medium & Pct & $\phantom{0}33$ \\
& All & Pct & $100$ \\
\hline
\end{tabular}
Which compiles (with the help of \usepackage{booktabs}) to:
In the output above, I would like to place a horizontal line after the 1st and the 2nd All fields from the Authoritarianism column (that is, a line before the low row and another line before the All row). Ho can I do that?
I tried to use Hline() as follows, but the resulting code isn't compilable by LaTeX:
latex(
tabular( (Factor(partner.status, "Status") + 1)
* (Factor(fcategory, "Authoritarianism") + 1 + Hline())
* ((Pct=Percent()))*Format(digits=1)
~ 1, data=Moore)
)

Related

R markdown running tex file error regarding toprule

I have the following chunk of codes in markdown file
---
title: "Ag Productivity"
author: "Abdullah Mamun"
date: '2022-06-23'
output:
pdf_document:
includes:
in_header: "table1.tex"
---
and in .tex file I have this
\begin{document}
\begin{table}
\centering
\begin{tabular}[t]{lccc}
\toprule
& Model 1 & Model 2 & Model 3\\
\midrule
lag(YGrowth, 1) & \num{1.000}(\num{0.000})*** & \num{-0.347}(\num{0.030})*** & \num{-0.436}(\num{0.068})***\\
lag(Inputs, 1) & \num{0.000}(\num{0.000}) & \num{-0.100}(\num{0.127}) & \num{2.321}(\num{1.078})*\\
lag(GDPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.270}(\num{0.069})*** & \num{0.341}(\num{0.531})\\
lag(lnPDENS, 1) & \num{0.000}(\num{0.000}) & \num{-0.124}(\num{0.031})*** & \num{0.158}(\num{0.076})*\\
lag(GRRAT, 1) & \num{0.000}(\num{0.000}) & \num{-0.280}(\num{0.095})** & \num{0.858}(\num{0.378})*\\
lag(EMPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.317}(\num{0.138})* & \num{0.708}(\num{1.571})\\
\midrule
Num.Obs. & \num{1070} & \num{1025} & \\
R2 & \num{1.000} & \num{0.141} & \\
R2 Adj. & \num{1.000} & \num{0.076} & \\
AIC & \num{-79950.8} & \num{-2904.1} & \\
BIC & \num{-79781.6} & \num{-2911.2} & \\
RMSE & \num{0.00} & \num{0.06} & \\
\bottomrule
\end{tabular}
\end{table}
I get this error message:
! Undefined control sequence. l.106 \toprule
Appreciate your help.
the table does not belong in the header. You should move it to the body of the document. Even though you try to force the body to start early with \begin{document}, this won't work because rmarkdown will include preamble-only stuff after this
if you want to use special macros like \toprule or \num you must load the package which provide them
your table is missing a floating specifier that tells latex where the table can be placed, I suggest [htbp
rmarkdown file:
---
title: "Ag Productivity"
author: "Abdullah Mamun"
date: '2022-06-23'
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{booktabs}
- \usepackage{siunitx}
---
\include{table1}
table1.tex:
\begin{table}[htbp]
\centering
\begin{tabular}[t]{lccc}
\toprule
& Model 1 & Model 2 & Model 3\\
\midrule
lag(YGrowth, 1) & \num{1.000}(\num{0.000})*** & \num{-0.347}(\num{0.030})*** & \num{-0.436}(\num{0.068})***\\
lag(Inputs, 1) & \num{0.000}(\num{0.000}) & \num{-0.100}(\num{0.127}) & \num{2.321}(\num{1.078})*\\
lag(GDPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.270}(\num{0.069})*** & \num{0.341}(\num{0.531})\\
lag(lnPDENS, 1) & \num{0.000}(\num{0.000}) & \num{-0.124}(\num{0.031})*** & \num{0.158}(\num{0.076})*\\
lag(GRRAT, 1) & \num{0.000}(\num{0.000}) & \num{-0.280}(\num{0.095})** & \num{0.858}(\num{0.378})*\\
lag(EMPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.317}(\num{0.138})* & \num{0.708}(\num{1.571})\\
\midrule
Num.Obs. & \num{1070} & \num{1025} & \\
R2 & \num{1.000} & \num{0.141} & \\
R2 Adj. & \num{1.000} & \num{0.076} & \\
AIC & \num{-79950.8} & \num{-2904.1} & \\
BIC & \num{-79781.6} & \num{-2911.2} & \\
RMSE & \num{0.00} & \num{0.06} & \\
\bottomrule
\end{tabular}
\end{table}

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?

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