开发者

Combining several regression tables into one for use in xtable with Sweave in R

xtable in Sweave works awesome, but does one table per regression. You can feed it a data frame, too, so I have been manually rbinding and pasteing results into data frames, but that doesn't seem very scalable.

Is there a more automated/robust solution that works like xtable, but on multiple lm objects? Are all of the tables that I see in papers/books generated manually? Is there a better solution to my janky code that generates a data frame to feed to xtable?

    library(reshape2)

    data <- data.frame(matrix(rnorm(50), 10, 5))
    names(data) <- letters[1:5]
    l.raw <- list()
    l.raw[["a"]] <- lm(a ~ d + e, data=data)
    l.raw[["b"]] <- lm(b ~ d + e, data=data)
    l.raw[["c"]] <- lm(c ~ d + e, data=data)

    form.table.from.lm <- function(l.raw) {
    summ <- list()

    for (i in names(l.raw)) {
        temp <- coef(summary(l.raw[[i]]))
        summ[[i]] <- data.frame(param=rownames(temp), test=i, temp)
    }

    df.res <- do.call("rbind", summ)
    df.res <- transform(df.res, t.value = paste("(", signif(t.value), ")", sep=""), Estimate = signif(Estimate))
    df.res.long <- melt(df.res, id.vars=c("test", "param"))
    df.res.wide <- dcast(df.res.long, test + variable ~ param)

    temp <- subset(df.res.wide, variable %in% c("Estimate", "t.value"))
    df.res <- temp[, -2]
    df.res[, 1] <- as.vector(rbind(names(l.raw), ""))
    colnames(df.res)[1] <- "regressor"
    return(df.res)
}

Which produces data frame:

   regressor (Intercept)          d          e
1          a    0.393996  -0.666721   0.159508
2             (0.573926) (0.422125) (0.526446)
5          b    0.611077  0.0288942   -0.70033
6        开发者_如何学Python      (0.32696)  (0.24048) (0.299911)
9          c   -0.101033  -0.287821    0.14581
10            (0.203193) (0.149449) (0.186383)

Given the amazing plotting packages for R, I feel like google and rseek are hiding something from me.


A while ago I stumbled across the outreg function by Paul Johnson.

You can directly apply outreg to your lm object and combine several lm outputs into one, nice latex table.

Here you find an example .pdf

outreg examples

and the code for the function

outreg code

general homepage by Paul Johnson

Paul Johnson


Your code threw errors for me at the dcast call, so I simply read in the output that you offered and adjusted the colnames to match up. This code produces a well formed pdf file on my system after passing it through my LaTex processor. (I assume you have an appropriate LaTeX installation if you are already using Sweave.)

require(Hmisc)
latex(df.res)

When I passed the example in help(lmList) in the lme4 package latex() also produce a fairly large and unweildy 4 page display that would need some adjustments to widen the page on my machine but may also be worth examination.

require(lme4)
(fm1 <- lmList(Reaction ~ Days | Subject, sleepstudy))
latex(fm1)


The outreg link in the accepted answer is broken now. The new link is

http://pj.freefaculty.org/stat/ps706/outreg-worked.R

There is also an accompanying PDF in the parent folder.


Check out the apsrtable table package in CRAN Which creates American Political Science Review style tables which look roughly like what I think you want. It takes lm and glm models as well some non-linear models from some packages. It also has a long series of options for generating stars at particular levels and such. There's a nice vingette and it generates both LaTeX and HTML tables which can then be added to OpenOffice/Word.

I've been using it for 2-3 years now and it continues to be under active development. I've just looked quickly but it seems much more solid/featureful than outreg.


Here's one more options I saw go by CRANberries that reminded me of this question:

texreg — Looking at the vingette, it seems to do a really nice job of generating pretty standard regression tables in R and which is under very active development at time of writing.

Update: I've been playing around with a few of these and I think that over time, texreg has become the leader leader in this space in terms of it maturity, stability, and featurefulness. Most important to me, it works with a variety of models and does wonderfully handy things like bootstrap standard errors to build stars for lme4() models — something that the package itself makes a little tricky to get (for good reasons) but that reviewers and journals often demand nonetheless. I would strongly recommend using texreg. It seems to be actively maintained.


The package stargazer would be another option for HTML, LaTeX, or ASCII tables, side-by-side. See also my answer to Table of multiple lm() models using apsrtable in Rmarkdown that includes a screenshot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜