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!

4

2 回答 2

2

据我所知,您不能直接从mtable.

但是,您可以从模型本身中删除系数。例如,使用 lm0

lm0$coefficients

将列出模型的各个部分。要删除“pop75”,您可以执行以下操作:

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

你可以走了。要删除很多系数:

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

如果你有一堆模型,你可以写一个循环或者使用by/apply来提高效率。

*注意:(lm0$coefficients[names(lm0$coefficients)=="pop75"] <- NULL即尝试删除 中的一个元素list)将不起作用。

于 2012-11-19T17:51:44.813 回答
0

好的,根据 Josh O'Brien 在评论中链接的问题和 Señor O 的代码,我认为这是做我想做的最简单和最灵活的方法:

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

此解决方案还应该使添加到要在显示中排除的变量列表变得相当容易。

感谢大家!!!

于 2012-11-20T07:28:56.043 回答