Reputation: 585
I am combining regression tables in knitr
. I tried to using texreg
, but it didn't work. What else can I do? Thanks. Example:
\documentclass{article}
\begin{document}
<<result='asis'>>=
library(texreg)
reg1 <- lm(Petal.Length ~ Petal.Width, data=iris)
reg2 <- lm(Sepal.Length ~ Sepal.Width, data=iris)
texreg(list(reg1,reg2))
@
\end{document}
Upvotes: 0
Views: 911
Reputation: 30104
I'm not sure if this is a bug or feature of the texreg
package; you should probably talk to the author of texreg
:
> library(texreg)
Version: 1.15
Date: 2012-11-13
> reg1 <- lm(Petal.Length ~ Petal.Width, data=iris)
> reg2 <- lm(Sepal.Length ~ Sepal.Width, data=iris)
> texreg(list(reg1,reg2))
\usepackage{booktabs}
\usepackage{dcolumn}
\begin{table}
\begin{center}
\begin{tabular}{l D{.}{.}{3.5} @{}D{.}{.}{3.5} @{}}
\toprule
& \multicolumn{1}{c}{Model 1} & \multicolumn{1}{c}{Model 2} \\
\midrule
(Intercept) & 1.08^{***} & 6.53^{***} \\
& (0.07) & (0.48) \\
Petal.Width & 2.23^{***} & \\
& (0.05) & \\
Sepal.Width & & -0.22 \\
& & (0.16) \\
\midrule
R$^2$ & 0.93 & 0.01 \\
Adj. R$^2$ & 0.93 & 0.01 \\
Num. obs. & 150 & 150 \\
\bottomrule
\vspace{-2mm}\\
\multicolumn{3}{l}{\textsuperscript{***}$p<0.01$,
\textsuperscript{**}$p<0.05$,
\textsuperscript{*}$p<0.1$}
\end{tabular}
\end{center}
\caption{Statistical models}
\label{table:coefficients}
\end{table}
[1] "\n\\usepackage{booktabs}\n\\usepackage{dcolumn}\n\n\\begin{table}\n\\begin{center}\n\\begin{tabular}{l D{.}{.}{3.5} @{}D{.}{.}{3.5} @{}}\n\\toprule\n & \\multicolumn{1}{c}{Model 1} & \\multicolumn{1}{c}{Model 2} \\\\\n\\midrule\n(Intercept) & 1.08^{***} & 6.53^{***} \\\\\n & (0.07) & (0.48) \\\\\nPetal.Width & 2.23^{***} & \\\\\n & (0.05) & \\\\\nSepal.Width & & -0.22 \\\\\n & & (0.16) \\\\\n\\midrule\nR$^2$ & 0.93 & 0.01 \\\\\nAdj. R$^2$ & 0.93 & 0.01 \\\\\nNum. obs. & 150 & 150 \\\\\n\\bottomrule\n\\vspace{-2mm}\\\\\n\\multicolumn{3}{l}{\\textsuperscript{***}$p<0.01$, \n \\textsuperscript{**}$p<0.05$, \n \\textsuperscript{*}$p<0.1$}\n\\end{tabular}\n\\end{center}\n\\caption{Statistical models}\n\\label{table:coefficients}\n\\end{table}\n"
The problem is that texreg()
prints the TeX table in the console and returns a visible character vector at the same time (see the last line above), while I guess the latter is unnecessary, although you can fix it by putting invisible()
around texreg()
or assign it to a variable to avoid printing. Here is a full example:
\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\begin{document}
<<results='asis'>>=
library(texreg)
reg1 <- lm(Petal.Length ~ Petal.Width, data=iris)
reg2 <- lm(Sepal.Length ~ Sepal.Width, data=iris)
invisible(texreg(list(reg1,reg2), use.packages=FALSE))
@
\end{document}
Also note you need use.packages=FALSE
to avoid \usepackage{dcolumn}
and \usepackage{booktabs}
in the middle of the LaTeX document, which does not make sense (they should be in the preamble instead).
Upvotes: 2
Reputation: 49810
Your second call to lm
overwrites the previous value of reg
?texreg
says that the first argument can be "a list
of statistical models", so try making a list.
texreg(list(lm(Petal.Length ~ Petal.Width, data=iris),
lm(Sepal.Length ~ Sepal.Width, data=iris)))
Upvotes: 4