R: Function to create tables in LaTex or Lyx to display regression model results

19Jun09

Most people using LaTex feel that creating tables is no fun. Some days ago I stumbled across a neat function written by Paul Johnson that produces LaTex code as well as LaTex code that can be used within Lyx. The output can be used for regression models and looks like output from the Stata outreg command. His R function that produces the LaTex code has the same name:  outreg(). The outreg code can be found on his website or in the PDF copy of the code from his website.

I took the code, put it into a .rnw file and sweaved it. It worked like a charm and produced beautiful results (see the picture on the left and the PDF). Below you can find the code for the noweb file (.rnw). Latex code is colored grey, R-code is colored blue. Just have a look at all the results as a PDF file. Besides, Paul Johnson has also created a nice list of R-Tips that can be found on his website as well.

###############################################################

\documentclass[a4paper,10pt]{article}

\title{Regression Tables for LaTex}
\author{Mark Heckmann. Code by Paul Johnson}
\date{\today}

\begin{document}
\maketitle

<<echo=FALSE>>=
x1 <- rnorm(100)
x2 <- rnorm(100)
y1 <- 5*rnorm(100)+3*x1 + 4*x2
y2 <- rnorm(100)+5*x2
m1 <- lm (y1~x1)
m2 <- lm (y1~x2)
m3 <- lm (y1 ~ x1 + x2)
gm1 <- glm(y1~x1)
@

<<echo=FALSE, results=tex>>=
outreg(m1,title="My One Tightly Printed Regression", lyx=F )
outreg(m1,tight=F,modelLabels=c("Fingers"),
title="My Only Spread Out Regressions" ,lyx=F)
outreg(list(m1,m2),modelLabels=c("Mine","Yours"),
varLabels=list(x1="Billie"),
title="My Two Linear Regressions Tightly Printed" ,lyx=F)
outreg(list(m1,m2),modelLabels=c("Whatever","Whichever"),
title="My Two Linear Regressions Not Tightly  Printed",
showAIC=F, lyx=F)
outreg(list(m1,m2,m3),title="My Three Linear Regressions", lyx=F)
outreg(list(m1,m2,m3),tight=F,
modelLabels=c("I Love love love really long titles",
"Hate Long","Medium"), lyx=F)
outreg(list(gm1),modelLabels=c("GLM"), lyx=F)
outreg(list(m1,gm1),modelLabels=c("OLS","GLM"), lyx=F)
@

\end{document}

###############################################################


15 Responses to “R: Function to create tables in LaTex or Lyx to display regression model results”

1. 1 Charles

It does not worked in LaTex ;(

2. 2 markheckmann

Hi Charles,

when I tried the script it worked perfectly. So just send me yours and I will have a look at it, as I cannot tell what’s wrong from your description.

Mark

3. 3 naresh

Looks nice, but I was wondering if there is any way of producing similar table in .rtf format like the one created by esttab in stata. Thanks.

4. 4 markheckmann

Hi naresh,

well indeed, this would be nice! But one question: how do you produce the rft output? I did not know R could do that before R2wd.
There is a new package on CRAN written by Christian Ritter which is called R2wd that can produce a .doc document as output. It also has some table features, which might suit your needs. Christian told me that .rtf output would be possible with some slight modifications. I will test the package in the next weeks and post about it.

Mark

5. 5 Stephen

I tried it in LaTex and couldn’t get it to work either. Although I am new to both R and LaTex so that didn’t help. I am assisting in some research, and if I could get this to work, it would really come in handy. I think where I am going wrong is with the line breaks in the code. If anyone can offer any help, or even better the files that they used, I’d be truly thankful.

– Stephen

6. 6 markheckmann

Hi,
it is impossible to tell where your try went wrong without your code. The code above works fine! Maybe try a more stripped down example for the start. The following code contains a simple example (.rnw) code. Make sure to take care of the following things:

1) set the working dir properly (change it in the code below)
2) Make sure outreg_code.r which contains the code for outreg exists and is in the same directory.
3) Sweave the document
4) make sure sweave.sty is in your working dir or in the latex search path when running latex

\documentclass[a4paper,10pt]{article}

\begin{document}

<>=
source(“outreg_code.r”)
x1 <- rnorm(100)
y1 <- 5*rnorm(100)+3*x1
m1 <- lm (y1~x1)
@

<>=
outreg(m1,title=”My One Tightly Printed Regression”, lyx=F )
outreg(m1,tight=F,modelLabels=c(“Fingers”),
title=”My Only Spread Out Regressions” ,lyx=F)
@

\end{document}

7. 7 Stephen

I didn’t realize that the outreg function needed its own file, I had it in the Doc. I will try the suggestions and hopefully get it right. Thanks

8. 8 Stephen

Still no luck, all it keeps putting out is a .tex file that says:
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\usepackage{noweb}
\makeatother
\usepackage{babel}
\usepackage{Sweave}
\begin{document}
\textbackslash{}documentclass{[}a4paper,10pt{]}\{article\}

\textbackslash{}begin\{document\}

=
source(\textquotedblleft{}outreg\_code.R\textquotedblright{})

x1 <- rnorm(100)
y1 <- 5{*}rnorm(100)+3{*}x1
m1 <- lm (y1\textasciitilde{}x1)

=
outreg(m1,title=\textquotedblright{}My One Tightly Printed Regression\textquotedblright{},
lyx=F )

outreg(m1,tight=F,modelLabels=c(\textquotedblleft{}Fingers\textquotedblright{}),

,lyx=F)
\textbackslash{}end\{document\}
\end{document}

The PDF simply shows the code that I put in.
i ran kpsewhich Sweave.sty and it was able to find it. I will double check the outreg file, it is simply the code Paul Johnson gives right? I basically copied and pasted it(I did stop before the variables and examples at the bottom), and just removed some of his explanations in it. Thanks again.

9. 9 Stephen

The “” are in the code before the =, the forum took them out

10. 10 mat

WHaou!
That’s helping a lot.
Thanks Mark (you could almost be in my thesis aknowledgements)

11. 11 Jon

Do you need to use Sweave to use outreg? And why does R keep telling me that there is no such thing as “outreg” when I try to give it an outreg command? (I’m new to R, so feel free to beat me up like a newb in the commenta section. As long as you provide help as well. Thanks.)

12. 12 markheckmann

Hi Jon,
you’re welcome to ask, the R community generally seems to be very helpful.
R needs to know the outreg function. So you need to include the outreg code if you want to use the function when running Sweave.
So the answer is no. outreg is a simple R function. Sweave on the other hand is used for processing .Rnw files, i.e. it parses and executes the R code chunks in the file.
Mark

13. 13 Ajmery Jaman

Great !!!!!
It is really useful.

14. 14 MrX

Just use knitr

15. 15 markheckmann

Yes! Since knitr things have become much easier.