[R] function to convert lm model to LaTeX equation

Frank E Harrell Jr f.harrell at vanderbilt.edu
Sun Oct 18 15:09:24 CEST 2009


Ista Zahn wrote:
> Dear list,
> I've tried several times to wrap my head around the Design library,
> without much success. It does some really nice things, but I'm often
> uncomfortable because I don't understand exactly what it's doing.
> Anyway, one thing I really like is the latex.ols() function, which
> converts an R linear model formula to a LaTeX equation.
> 
> So, I started writing a latex.lm() function (not actually using
> classes at this point, I just named it that for consistency). This
> turned out to be easy enough for simple cases (see code below), but
> now I'm wondering a) if anyone knows of existing functions that do
> this (again, for lm() models, I know I'm reinventing the wheel in as
> far as the Design library goes), or if not, b) if anyone has
> suggestions for improving the function below.
> 
> Thanks,
> Ista
> 
> ### Function to create LaTeX formula from lm() model. Needs amsmath
> package in LaTeX. ###
> 
> latex.lm <- function(object, file="", math.env=c("$","$"),
> estimates="none", abbreviate = TRUE, abbrev.length=8, digits=3) {
> # Get and format IV names
>   co <- c("Int", names(object$coefficients)[-1])
>   co.n <-  gsub("p.*)", "", co)
>     if(abbreviate == TRUE) {
>       co.n <- abbreviate(gsub("p.*)", "", co), minlength=abbrev.length)
>     }
> # Get and format DV
>   m.y <- strsplit((as.character(object$call[2])), " ~ ")[[1]][1]
> # Write coefficent labels
>   b.x <- paste("\\beta_{", co.n ,"}", sep="")
> # Write error term
>   e <- "\\epsilon_i"
> # Format coefficint x variable terms
>   m.x <- sub("}Int","}", paste(b.x, co.n, " + ", sep="", collapse=""))
> # If inline estimates convert coefficient labels to values
>     if(estimates == "inline") {
>     m.x <- sub("Int", "",
> paste(round(object$coefficients,digits=digits), co.n, " + ", sep="",
> collapse=""))
>     m.x <- gsub("\\+ \\-", "-", m.x)
>   }
> # Format regression equation
>   eqn <- gsub(":", " \\\\\\times ", paste(math.env[1], m.y, " = ",
> m.x, e, sep=""))
> # Write the opening math mode tag and the model
>   cat(eqn, file=file)
> # If separae estimates format estimates and write them below the model
>   if(estimates == "separate") {
>     est <- gsub(":", " \\\\\\times ", paste(b.x, " = ",
> round(object$coefficients, digits=digits), ", ", sep="", collapse=""))
>     cat(", \\\\ \n \\text{where }", substr(est, 1, (nchar(est)-2)), file=file)
>   }
> # Write the closing math mode tag
>   cat(math.env[2], "\n", file=file)
> }
> 
> # END latex.lm
> 
> Xvar1 <- rnorm(20)
> Xvar2 <- rnorm(20)
> Xvar3 <- factor(rep(c("A","B"),10))
> Y.var <- rnorm(20)
> D <- data.frame(Xvar1, Xvar2, Xvar3, Y.var)
> 
> x1 <- lm(Y.var ~ pol(Xvar1, 3) + Xvar2*Xvar3, data=D)
> latex.lm(x1)
> 

It's not reinventing the wheel, in the sense that you are not attempting 
to handle the most needed features (simplifying regression splines and 
factoring out interaction terms with brackets).  I don't think you 
followed the posting guide though.  You didn't state your exact problem 
with Design and you didn't include any code.  Also note that the Design 
package is replaced with the rms package although latex features have 
not changed.

-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list