How to publish (sweave) regression formulas?

How can I post a regression formula?

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 something like `dynlm (formula = dep ~ indep1 + indep2 + indep3) 'in my pdf document. I would rather have more tutorial style over this function call style. Plus, I would like (manually?) To add intercept and errorterm to the model (because it is actually there).

Notice that I found outreg on google (which now seems a little too heavy) and does not quite meet my needs at first sight.

EDIT: Trying to publish a sample output, actually I would like to, but I don't know how to do it better using the SO editor:

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

Some matrix notation will also be fine, but printing the definition of a model will be nice no matter how. Manual addition is also possible, of course, but when you are at the reliability check stage, model variables can change frequently and documentation should be updated.

(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

+7
source share
3 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} 
+5
source

I found this function that I wrote when I introduced the intro class to model regression. I am sure this is less than optimal, but it worked well enough for this course. If nothing else, perhaps this will force you to point 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" 
+3
source

This short Rnw file shows a printout of a long regression formula wrapping several lines:

 \documentclass{article} \usepackage{breqn} \begin{document} \SweaveOpts{concordance=TRUE} <<Printmodel,echo=FALSE,results=tex>>= #Specify any formula model1 <- formula("outcome ~ (variable1 +variable2 + variable3 + variable4 + variable5 + variable6 )^2 + variable7 + variable8 + variable7 * variable8") #Converts formula to LaTeX cat(paste("\\begin{dmath*}\n",gsub("~","\\\\sim",deparse(model1,width.cutoff = 500L)),"\n\\end{dmath*}",sep="")) @ \end{document} 

The resulting LaTeX code:

 \begin{dmath*} outcome \sim (variable1 + variable2 + variable3 + variable4 + variable5 + variable6)^2 + variable7 + variable8 + variable7 * variable8 \end{dmath*} 

which wraps well over multiple lines using the breqn package and the dmath framework.

0
source

All Articles