Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish (sweave) regression formulas?

How can I publish a regression formula nicely?

fit1<-dynlm(dep~indep1+indep2+indep3)
s1<-summary(fit1)
s1$call

How can I Sweave s1$call ? I mean I do not want to have somethin like `dynlm(formula=dep~indep1+indep2+indep3)´ in my pdf document. I´d prefer to have a more textbook style over this function call style. Plus I´d like to (manually?) add intercept and errorterm to the model (because it's actually in there).

Note that I found outreg on google (which seems a little bit too heavyweighted right now) and not exactly fitting my needs at first sight.

EDIT: Trying to post sample output, actually I´d love to, but I don't know how to do it better with the SO editor:

 dep = alpha + beta_1*indep1 + beta_2*indep2 + beta_3*indep3 + epsilon

Some matrix notation would also be fine, but printing the model definition would be nice no matter how. Of course adding it manually is also possible, but when you are in a robustness check phase the model variables might change often and the documentation has got to be up to date.

(Using http://texify.com :)

img]http://www.texify.com/img/%5CLARGE%5C%21%5Cmbox%7Bdep%7D%20%3D%20%5Calpha%20%2B%20%5Cbeta_1%20%5Ccdot%20%5Cmbox%7Bindep1%7D%20%2B%20%5Cbeta_2%20%5Ccdot%20%5Cmbox%7Bindep2%7D%20%2B%20%5Cepsilon.gif[/img

like image 751
Matt Bannert Avatar asked Jun 16 '11 21:06

Matt Bannert


2 Answers

This Rnw file:

\documentclass{article}
\begin{document}
<<>>=
data("USDistLag", package = "lmtest")
library(dynlm)
dfm1 <- dynlm(consumption ~ gnp + L(consumption), data = USDistLag)
@ 

<<echo=FALSE>>=
cc <-dfm1$call
f <- cc$formula
LHS <- as.character(f)[2]
RHS <- as.character(f)[3]
coefs <- gsub(" +","",strsplit(RHS,"\\+")[[1]])
mbox <- function(x) { paste("\\\\mbox{",x,"}",sep="") }
pars <- paste("\\\\beta_",0:(length(coefs)-1),sep="")
p <- paste(mbox(LHS),"=",paste(pars,mbox(coefs),sep=" \\\\cdot ",collapse="+"),
           "+ \\\\epsilon")
@ 

$$
\Sexpr{p}
$$
\end{document}

leads to this TeX fragment:

\documentclass{article}
\begin{document}
\begin{Schunk}
\begin{Sinput}
> data("USDistLag", package = "lmtest")
> library(dynlm)
> dfm1 <- dynlm(consumption ~ gnp + L(consumption), data = USDistLag)
\end{Sinput}
\end{Schunk}


$$
\mbox{consumption} = \beta_0 \cdot \mbox{gnp}+\beta_1 \cdot \mbox{L(consumption)} + \epsilon
$$
\end{document}
like image 173
Ben Bolker Avatar answered Oct 20 '22 20:10

Ben Bolker


I found this function I wrote when I was taking an intro class to regression modeling. I'm sure it's less than optimal, but it worked well enough for that course. If nothing else, maybe this will get you pointed in the right direction to take it further.

writeCoef <- function(x) {
  require(plyr)

    coefnames <- as.data.frame(coef(x))
    coefnames$betas <- row.names(coefnames) 
    coefnames <- adply(coefnames, 1, function(x) paste(round(x[1],3), x[2] , sep = " * "))

    dependent <- paste(as.character(x$call$formula)[2], " = ", sep = "")

    ret <- paste(dependent, paste(coefnames[,3], sep = "", collapse = " + "))
    ret <- gsub("\\*\\s\\(Intercept\\)", "", ret)
  return(ret)
    }

And in action:

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)

> writeCoef(lm.D9)
[1] "weight =  5.032 + -0.371 * groupTrt"
like image 41
Chase Avatar answered Oct 20 '22 19:10

Chase