3

Possible Duplicate:
how to succinctly write a formula with many variables from a data frame?
omit some coefficients from mtable/outreg-type table

I'm using write.mtable() from the memisc package to display results for several regressions in a (tab-separated) table that can be easily pasted into Word/Excel/etc.

Because these tables tend to get big pretty quickly, there are times when I want to restrict the output to an arbitrary subset of the variables included in the regressions.

Is there a simply way to not include results for certain explanatory variables in regression tables without actually removing these variables from the regressions themselves? The easy way is to just delete the rows once they've been pasted into Word/Excel/etc, of course, but perhaps there's a more elegant solution.


For example, starting with an example from the documentation for mtable:

lm0 <- lm(sr ~ pop15 + pop75,              data = LifeCycleSavings)
lm1 <- lm(sr ~                 dpi + ddpi, data = LifeCycleSavings)
lm2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
mtable123 <- mtable("Model 1"=lm0,"Model 2"=lm1,"Model 3"=lm2, summary.stats=c("sigma","R-squared","F","p","N"))
mtable123

Is it possible to remove, say, pop75 from the output as below (not that there's any reason to do this here, but as an example of the type of output I'm trying to achieve):

Calls:
Model 1: lm(formula = sr ~ pop15 + pop75, data = LifeCycleSavings)
Model 2: lm(formula = sr ~ dpi + ddpi, data = LifeCycleSavings)
Model 3: lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)

==========================================
              Model 1   Model 2   Model 3 
------------------------------------------
(Intercept)  30.628***  6.360*** 28.566***
             (7.409)   (1.252)   (7.355)  
pop15        -0.471**            -0.461** 
             (0.147)             (0.145)   
dpi                     0.001    -0.000   
                       (0.001)   (0.001)  
ddpi                    0.529*    0.410*  
                       (0.210)   (0.196)  
------------------------------------------
sigma          3.931     4.189     3.803  
R-squared      0.262     0.162     0.338  
F              8.332     4.528     5.756  
p              0.001     0.016     0.001  
N             50        50        50      
==========================================

Thanks!

Community
  • 1
  • 1
  • 4
    Can you give a small reproducible data set with illustration of what you would like to see? – Dason Nov 19 '12 at 17:20
  • A suggestion: `mtable` appears to collect all the model information via `getSummary`. If you look at the docs for that function, it specifies in a fair bit of detail what custom methods should return. You could write your own version of `getSummary` that omits certain variable and pass that function to `mtable`. – joran Nov 19 '12 at 17:48
  • [Here is another SO question](http://stackoverflow.com/questions/11040380/omit-some-coefficients-from-mtable-outreg-type-table/11041119#11041119) that addressed the same issue. – Josh O'Brien Nov 19 '12 at 18:01

2 Answers2

2

As far as I know you can't do this directly from mtable.

However, you can remove coefficients from the model itself. For example, with lm0

lm0$coefficients

Will list the pieces of the model. To remove "pop75", you can do:

lm0$coefficients <- lm0$coefficients[names(lm0$coefficients) != "pop75"]

And you're good to go. To remove a lot of coefficients:

lm0$coefficients <- lm0$coefficients[!names(lm0$coefficients) %in% c("pop75","pop15")]

If you have a bunch of models, you can write a loop or use by/apply to make it more efficient.

*Note: lm0$coefficients[names(lm0$coefficients)=="pop75"] <- NULL (i.e., trying to remove one element within the list) will not work.

Señor O
  • 17,049
  • 2
  • 45
  • 47
  • Hmm...this code seems to work fine for summary(), but when I run mtable() I get "Error in eval(expr, envir, enclos) : subscript out of bounds". Useful to know, though! – Morris Cornell-Morgan Nov 19 '12 at 18:08
  • Interesting - I would assume then that the way `mtable` gathers it's data (`getSummary`) is doing something weird - it may be helpful to look directly at that as joran was suggesting? – Señor O Nov 19 '12 at 18:11
  • If you do `lm0[1:13]` (or check the `lm` help page), it shows that lm0 stores a lot of information about the model that it normally does not display. `getSummary(lm0)` returns a lot of 'tests' for each coefficient, and so it presumably uses a lot of that hidden data. I think the easiest solution to your problem, unfortunately, would be creating your own function like `mtable` where you can omit coefficients. – Señor O Nov 19 '12 at 18:37
0

OK, based on the question linked to by Josh O'Brien in the comments and the code from Señor O, I think this is the simplest and most flexible way to do what I want:

excluded_vars <- c("pop75")
mtable123$coefficients <- mtable123$coefficients[,,!dimnames(mtable123$coefficients)[[3]] %in% excluded_vars,,drop=F]
mtable123

This solution should also make it fairly easy to add to the list of variables to be excluded in the display.

Thanks everyone!!!