[R] Presentation of multiple models in one table using xtable

Ajay Narottam Shah ajayshah at mayin.org
Mon Aug 14 15:08:27 CEST 2006


Consider this situation:
> x1 <- runif(100); x2 <- runif(100); y <- 2 + 3*x1 - 4*x2 + rnorm(100)
> m1 <- summary(lm(y ~ x1))
> m2 <- summary(lm(y ~ x2))
> m3 <- summary(lm(y ~ x1 + x2))

Now you have estimated 3 different "competing" models, and suppose you
want to present the set of models in one table. xtable(m1) is cool,
but doing that thrice would give us 3 different tables.

What I want is this one table:

-----------------------------------------------------------
                    M1             M2              M3
-----------------------------------------------------------
Intercept         0.0816         3.6292         2.2272
                 (0.5533)       (0.2316)***    (0.2385)***

x1                2.8151                        2.7606
                 (0.5533)***                   (0.3193)***

x2                              -4.2899        -4.2580
                                (0.401)***     (0.3031)***

$\sigma_e$        1.538          1.175          0.8873
$R^2$             0.2089         0.5385         0.7393
-----------------------------------------------------------

How would one set about doing this? I am hoping that it's possible to
write a function xtable.multi.lm where one would say
xtable.multi.lm(m1,m2,m3) and get the above table.

My sense is there are three challenges:

1. How to write a general R function which eats a unpredictable number
   of summary(lm()) objects, and fill out a matrix with results such
   as the above.

2. How to get a good xtable(), with decimal alignment and with the ***
   stuff (actually, $^{***}$). Will there be any catch in dropping
   into mathmode for $R^2$? After each pair of lines, I'd like to have
   a \\[2mm] so as to get a nice spacing in the table.

3. This style of presentation seems relevant to a whole host of models
   - whether ARCH models or survival models - not just OLS
   regressions. It would be very nice if one supported all manner of
   model objects and not just what comes out of lm().

I'm happy to take a crack at writing this function, which should
ideally go back into the xtable library. But I don't have an idea on
how to go about these two questions. If you will guide me, I am happy
to work on it. :-)

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.



More information about the R-help mailing list