Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

This seemed to me to be basic, but I can't seem to find a solution online, so I wondered what I might be missing.

I wish to include the output of an lm summary object inside an Sweave (.Rnw) document. I can either output the summary.lm as is, or use the xtable/Hmisc packages (through xtable or latex commands). Is there something like xtable that also gives the summary information which available from outside the table? ($R^2$, F statistics etc...?)

Thanks.

share|improve this question
Why should this be closed? Should it go on SO instead?! – Tal Galili Dec 4 '11 at 15:24
3  
I think it can stay here, the question is more relevant for statisticians, than for programmers. – mpiktas Dec 4 '11 at 16:16

4 Answers

Look at the package apsrtable. You can tweak then the output the way you want, and summarise several models instead of one.

share|improve this answer
Hi mpiktas. Thank you for the response. I am less interested in showing several tables - and wish to simply have a summary of one model, but that will look "nice". I can open the code and do it, but I am surprised that it wasn't done before... – Tal Galili Dec 4 '11 at 16:42
1  
@Tal I've +1 this response because I'm pretty sure there's a way to achieve what you want although I didn't take time to investigate package options in depth (I used it once to display several models as you said). – chl Dec 4 '11 at 19:34

One possible solution is swst: Print statistical results in Sweave package by Sacha Epskamp.

Examples

library(swst)
x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1)
y <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8)
corTest <- cor.test(x, y, method = "kendall", alternative = "greater")
swst(corTest)

($T=26$, $p=0.06$)

# Chi-square test:
M <- as.table(rbind(c(762, 327, 468), c(484,239,477)))
dimnames(M) <- list(gender=c("M","F"),
party=c("Democrat","Independent", "Republican"))
chisqTest <- chisq.test(M)
swst(chisqTest)

($\\chi^2(2)=30.07$, $p<0.001$)

# Linear model:
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
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)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept
swst(lm.D9)

($F( 1,18)=1.419$, $p=0.249$)

swst(lm.D90)

($F( 2,18)=485.051$, $p<0.001$)

share|improve this answer
up vote 0 down vote accepted

I gave up and played with the code to produce something similar. Not the prettiest thing though. If any one feels like improving it - I'd be happy to use your code.

print.summary.lm.xtable <- function (x, digits = max(3, getOption("digits") - 3), symbolic.cor = x$symbolic.cor, 
    signif.stars = getOption("show.signif.stars"), ...) 
{

if(!require(xtable)) stop("This function requires the package 'xtable' - please make sure you get it")


cat("\\begin{verbatim}")

    cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"), 
        "\n\n", sep = "")
    resid <- x$residuals
    df <- x$df
    rdf <- df[2L]
    cat(if (!is.null(x$w) && diff(range(x$w))) 
        "Weighted ", "Residuals:\n", sep = "")
    if (rdf > 5L) {
        nam <- c("Min", "1Q", "Median", "3Q", "Max")
        rq <- if (length(dim(resid)) == 2L) 
            structure(apply(t(resid), 1L, quantile), dimnames = list(nam, 
                dimnames(resid)[[2L]]))
        else {
            zz <- zapsmall(quantile(resid), digits + 1)
            structure(zz, names = nam)
        }
        print(rq, digits = digits, ...)
    }
    else if (rdf > 0L) {
        print(resid, digits = digits, ...)
    }
    else {
        cat("ALL", df[1L], "residuals are 0: no residual degrees of freedom!\n")
    }
#     if (length(x$aliased) == 0L) {
#         cat("\nNo Coefficients\n")
#     }
#     else {
#         if (nsingular <- df[3L] - df[1L]) 
#             cat("\nCoefficients: (", nsingular, " not defined because of singularities)\n", 
#                 sep = "")
#         else cat("\nCoefficients:\n")
#         coefs <- x$coefficients
#         if (!is.null(aliased <- x$aliased) && any(aliased)) {
#             cn <- names(aliased)
#             coefs <- matrix(NA, length(aliased), 4, dimnames = list(cn, 
#                 colnames(coefs)))
#             coefs[!aliased, ] <- x$coefficients
#         }
#         printCoefmat(coefs, digits = digits, signif.stars = signif.stars, 
#             na.print = "NA", ...)
#     }


cat("\\end{verbatim}")

print(xtable(x),   latex.environments = "left") # x is a summary of some lm object

cat("\\begin{verbatim}")
    cat("Residual standard error:", format(signif(x$sigma, 
        digits)), "on", rdf, "degrees of freedom\n")
    if (nzchar(mess <- naprint(x$na.action))) 
        cat("  (", mess, ")\n", sep = "")
    if (!is.null(x$fstatistic)) {
        cat("Multiple R-squared:", formatC(x$r.squared, digits = digits))
        cat(",\tAdjusted R-squared:", formatC(x$adj.r.squared, 
            digits = digits), "\nF-statistic:", formatC(x$fstatistic[1L], 
            digits = digits), "on", x$fstatistic[2L], "and", 
            x$fstatistic[3L], "DF,  p-value:", format.pval(pf(x$fstatistic[1L], 
                x$fstatistic[2L], x$fstatistic[3L], lower.tail = FALSE), 
                digits = digits), "\n")
    }
    correl <- x$correlation
    if (!is.null(correl)) {
        p <- NCOL(correl)
        if (p > 1L) {
            cat("\nCorrelation of Coefficients:\n")
            if (is.logical(symbolic.cor) && symbolic.cor) {
                print(symnum(correl, abbr.colnames = NULL))
            }
            else {
                correl <- format(round(correl, 2), nsmall = 2, 
                  digits = digits)
                correl[!lower.tri(correl)] <- ""
                print(correl[-1, -p, drop = FALSE], quote = FALSE)
            }
        }
    }
    cat("\n")
cat("\\end{verbatim}")
    invisible(x)
}
share|improve this answer

Personally I enjoy texreg, which plays nice with booktabs and is also highly customizable.

Not exactly what you're looking for, but I think this is also good reading for this sort of work.

*Note, I am no relation to the Philip who wrote that package. Lol.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.