Comparing regression models with R - r

Is there a tool available in R to produce publication ready regression tables? I am working on a course paper in which I need to compare several regression models and I would be very glad if I could make them nest within a single table like this one, from the estout Stata package.
I have checked xtable, but could not reach the same results. Any tips would be appreciated.
Here's what I have in mind:

You probably want the mtable function in 'memisc' package. It has associated LaTeX output arguments:
==========================================================================
Model 1 Model 2 Model 3
--------------------------------------------------------------------------
Constant 30.628*** 6.360*** 28.566***
(7.409) (1.252) (7.355)
Percentage of population under 15 -0.471** -0.461**
(0.147) (0.145)
Percentage of population over 75 -1.934 -1.691
(1.041) (1.084)
Real per-capita disposable income 0.001 -0.000
(0.001) (0.001)
Growth rate of real per-capita disp. income 0.529* 0.410*
(0.210) (0.196)
--------------------------------------------------------------------------
sigma 3.931 4.189 3.803
R-squared 0.262 0.162 0.338
F 8.332 4.528 5.756
p 0.001 0.016 0.001
N 50 50 50
==========================================================================
This is the LaTeX code you get:
texfile123 <- "mtable123.tex"
write.mtable(mtable123,forLaTeX=TRUE,file=texfile123)
file.show(texfile123)
#------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Calls:
% Model 1: lm(formula = sr ~ pop15 + pop75, data = LifeCycleSavings)
% Model 2: lm(formula = sr ~ dpi + ddpi, data = LifeCycleSavings)
% Model 3: lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tabular}{lcD{.}{.}{7}cD{.}{.}{7}cD{.}{.}{7}}
\toprule
&&\multicolumn{1}{c}{Model 1} && \multicolumn{1}{c}{Model 2} && \multicolumn{1}{c}{Model 3}\\
\midrule
Constant & & 30.628^{***} && 6.360^{***} && 28.566^{***}\\
& & (7.409) && (1.252) && (7.355) \\
Percentage of population under 15 & & -0.471^{**} && && -0.461^{**} \\
& & (0.147) && && (0.145) \\
Percentage of population over 75 & & -1.934 && && -1.691 \\
& & (1.041) && && (1.084) \\
Real per-capita disposable income & & && 0.001 && -0.000 \\
& & && (0.001) && (0.001) \\
Growth rate of real per-capita disp. income & & && 0.529^{*} && 0.410^{*} \\
& & && (0.210) && (0.196) \\
\midrule
sigma & & 3.931 && 4.189 && 3.803 \\
R-squared & & 0.262 && 0.162 && 0.338 \\
F & & 8.332 && 4.528 && 5.756 \\
p & & 0.001 && 0.016 && 0.001 \\
N & & 50 && 50 && 50 \\
\bottomrule
\end{tabular}

The R wikibook has some good sources on production quality output in R.
I think this function from Paul Johnson that is listed in the wikibook is exactly what you're looking for:
http://pj.freefaculty.org/R/WorkingExamples/outreg-worked.R
I edited the function for my own use to work with the booktabs format and allow for models that have extra attributes:
http://chandlerlutz.com/R/outregBkTabs.r

xtable can do this, but its somewhat of a hack.
Take two linear models, named lm.x and lm.y.
If you use the following code:
myregtables <- rbind(xtable(summary(lm.x)), xtable(summary(lm.y)))
xtable will then produce a table with both regression models. If you add a \hline (or perhaps two) in LaTeX then it should look OK. You'll still only have one label and caption for the two models. As I said, its somewhat of a hacky solution.

Related

Plotting coefficients in r

I have this LaTex table formatted from an output of OxMetrics:
\begin{document}
\begin{tabularx}{\textwidth}{#{} L*{7}{c} #{}}
\toprule
& \multicolumn{6}{c}{Dependent variables} \\
\cmidrule(l){2-7}
& {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\
\midrule
Disabilities &-8.016{$^{***}$} &9.221{$^{***}$} &5.625{$^{**}$} &-2.589 &0.739 &4.281\\
&(2.121) & (3.090) & (2.588) &(3.105) & (3.917) & (2.730)\\
Disabled x Unemployed
&5.068\\
&(3.224)\\
\midrule
Visual
&-7.146{$^{**}$}&-2.684&10.615{$^{**}$}&4.429&13.730{$^{*}$}&0.270\\
&(3.316)&(5.870)&(4.689)&(5.907)&(7.514)&(5.250)\\
Hearing
&-6.515&8.604&12.039{$^{**}$}&7.222&7.964&1.007\\
&(3.735)&(6.628)&(5.877)&(6.878)&(8.704)&(5.969)\\
Motor
&-11.283{$^{***}$}&7.511{$^{*}$}&7.856&2.546&5.034&-2.770\\
&(2.544)&(5.168)&(4.455)&(5.443)&(6.434)&(4.499)\\
Episodic
&-0.183&5.794&1.384&2.780&7.353&2.667\\
&(2.391)&(4.516)&(3.751)&(4.578)&(5.718)&(3.981)\\
\midrule
L
&-4868& -2251& -1954& -2227& -2411& -2361\\
N
&1159& 640& 624& 628& 645& 639\\
\bottomrule
\end{tabularx}
\caption*{\tiny{{***}$p<0.01$.
{**}$p<0.05$.
{*}$p<0.1$. Standard errors in parentheses -- Intercept and other socio-demographic variables (age, sex, marital status, etc.) included but not reported}}
\end{document}
Which gives:
I want to plot these coefficients but I have no clue how to proceed. If the model was estimated with R, it'd be easier. But it's done with another software (OxMetrics). Any suggestions? Thanks.

texreg - create empty column

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)).

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

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)
)

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.

Remove additional footnote from R's xtable() table output in LaTeX

I am using the following R code to call xtable and generate a LaTeX table for a Sweave document.
ifelse(LaTeX==1, print(xtable(rule1.results.noTC, caption="Rule 1 Results 0 Transaction Costs",
digits=c(1,2,4,4,4), display=c("d","d","f","f","f"))),
print(rule1.results))
This produces the following LaTeX
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Sun Jul 28 16:54:42 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrrr}
\hline
& L & profits & annCumulExReturn & sharpe \\
\hline
1 & 5 & -888.8215 & -0.1501 & -4.3939 \\
2 & 10 & -909.8941 & -0.1533 & -6.8882 \\
3 & 20 & -893.6245 & -0.1509 & -6.9081 \\
4 & 40 & -865.6764 & -0.1466 & -9.8462 \\
5 & 80 & -832.4700 & -0.1417 & -11.7260 \\
6 & 160 & -757.0690 & -0.1305 & -16.3088 \\
7 & 320 & -626.9162 & -0.1118 & -31.6134 \\
8 & 640 & -340.8740 & -0.0730 & -44.2321 \\
\hline
\end{tabular}
\caption{Rule 1 Results with Transaction Costs}
\end{table}
When I convert this to pdf, I get a nice table. However, it is followed by a weird note:
[1] "
And I get several of these if I plot multiple tables in a row. How can I eliminate this either via R's xtable or by editing the LaTeX code.
Thanks
I can't say for sure without your data and an example of the .tex file, but I'm pretty confident this is due to your use of ifelse. I imagine the following will not give you the weird print out:
if(LaTeX==1) {
print(xtable(rule1.results.noTC,caption="Rule 1 Results 0 Transaction Costs",
digits=c(1,2,4,4,4), display=c("d","d","f","f","f")))
} else {
print(rule1.results))
}
This is because ifelse returns its result, which you're also printing. See, for example:
> ifelse(TRUE,print("true"),print("false"))
[1] "true"
[1] "true"

Resources