\documentclass[a4paper]{article} \usepackage{booktabs,dcolumn} \begin{document} We fit a linear regression for the economic journals demand model. <>= data("Journals", package = "AER") journals_lm <- lm(log(subs) ~ log(price/citations), data = Journals) journals_lm @ A scatter plot with the fitted regression line is shown in Figure~\ref{fig:fig1} below. \begin{figure}[h] \begin{center} <>= plot(log(subs) ~ log(price/citations), data = Journals) abline(journals_lm) @ \caption{\label{fig:fig1} Scatter plot with fitted regression line for the \texttt{Journals} data.} \end{center} \end{figure} The fitted regression line is \[ \log(\mbox{subscriptions}) \quad = \quad \Sexpr{round(coef(journals_lm)[1], digits = 2)} \Sexpr{if(coef(journals_lm)[2] < 0) "-" else "+"} \Sexpr{abs(round(coef(journals_lm)[2], digits = 2))} \cdot \log(\mbox{price per citation}) \] Table~\ref{tab:journals1} summarizes the results of the model along with a slightly extended model in type of table commonly used in economics and social science publications. This can be easily produced with the \emph{memisc} package. Alternative packages for producing such tables are \emph{texreg} or \emph{stargazer}. \begin{table}[h] \begin{center} \caption{\label{tab:journals1} Automatic regression summaries for \texttt{Journals} data (via \emph{memisc}).} <>= journals_lm2 <- lm(log(subs) ~ log(price/citations) + foundingyear, data = Journals) library("memisc") toLatex(mtable("Model 1" = journals_lm, "Model 2" = journals_lm2, summary.stats = c("N", "R-squared", "BIC"))) @ \end{center} \end{table} Furthermore, it may also be necessary (and not too hard) to generate the table ``by hand'' using text manipulation in R. See Table~\ref{tab:journals2}. \begin{table}[h] \begin{center} \caption{\label{tab:journals2} Hand-crafted regression summary for \texttt{Journals} data.} \begin{tabular}{rrrrr} \hline & Estimate & Std.~error & $t$ statistic & $p$ value \\ \hline <>= x <- summary(journals_lm)$coefficients x[] <- as.character(round(x, digits = 3)) x[,4] <- ifelse(as.numeric(x[,4]) < 0.001, "< 0.001", x[,4]) cat(paste(rownames(x), "& $", apply(x, 1, paste, collapse = "$ & $"), "$\\\\ \n")) @ \hline \end{tabular} \end{center} \end{table} Finally, summaries of a single model can also be automatically generated by the \emph{xtable} package, see Table~\ref{tab:journals3}. <>= library("xtable") print( xtable(journals_lm, caption = "Automatic regression summary for \\texttt{Journals} data (via \\emph{xtable}).", label = "tab:journals3"), caption.placement = "top") @ \end{document}