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(つまり、 内の 1 つの要素を削除しようとするlist) は機能しません。

于 2012-11-19T17:51:44.813 に答える
0

OK、コメントで 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 に答える