使用memisc包中的mtable()更改回归表中自变量的出现顺序

时间:2018-11-09 14:05:22

标签: r lm

我使用软件包memisc将我的回归表导出到LaTeX。我现在有了一个三个模型,每个模型中的自变量(IV)和控制变量(CV)都不同。

使用

[5]

,我得到以下输出:

mtable("Modell 1"=lm(DV ~ IV1 + CV1 + CV2),
       "Modell 2"=lm(DV ~ IV2 + CV1 + CV2),
       "Modell 3"=lm(DV ~ IV3 + CV1 + CV2))

(我将摘要统计信息截短了)。我希望首先出现IV1,IV2和IV3,然后出现所有控制变量。有自动化的解决方案吗?

1 个答案:

答案 0 :(得分:1)

这是一个有趣的问题。请考虑以下示例。

library(memisc)
# independent variables (IV) are dpi, ddpi, log(dpi) and log(ddpi)
# control variables (CV) are pop15 and pop75
mtable("Model 1" = lm(sr ~ dpi + pop15 + pop75, LifeCycleSavings),
       "Model 2" = lm(sr ~ ddpi + pop15 + pop75, LifeCycleSavings),
       "Model 3" = lm(sr ~ log(dpi) + pop15 + pop75, LifeCycleSavings),
       "Model 4" = lm(sr ~ log(ddpi) + pop15 + pop75, LifeCycleSavings))
# looks like mtable appends new coefficients to the list of coefficients defined in the first model
# output
Calls:
Model 1: lm(formula = sr ~ dpi + pop15 + pop75, data = LifeCycleSavings)
Model 2: lm(formula = sr ~ ddpi + pop15 + pop75, data = LifeCycleSavings)
Model 3: lm(formula = sr ~ log(dpi) + pop15 + pop75, data = LifeCycleSavings)
Model 4: lm(formula = sr ~ log(ddpi) + pop15 + pop75, data = LifeCycleSavings)

=====================================================================
                    Model 1      Model 2     Model 3      Model 4    
---------------------------------------------------------------------
  (Intercept)       31.457***    28.125***    36.304**    26.118***  
                    (7.482)      (7.184)     (10.511)     (7.416)    
  dpi               -0.001                                           
                    (0.001)                                          
  pop15             -0.492**     -0.452**     -0.506**    -0.408**   
                    (0.149)      (0.141)      (0.154)     (0.144)    
  pop75             -1.568       -1.835       -1.649      -1.663     
                    (1.121)      (0.998)      (1.110)     (1.009)    
  ddpi                            0.428*                             
                                 (0.188)                             
  log(dpi)                                    -0.779                 
                                              (1.018)                
  log(ddpi)                                                1.584*    
                                                          (0.722)    
---------------------------------------------------------------------
  R-squared          0.274        0.337        0.271       0.332     
  adj. R-squared     0.227        0.293        0.223       0.288     
  sigma              3.939        3.767        3.948       3.780     
  F                  5.797        7.778        5.700       7.608     
  p                  0.002        0.000        0.002       0.000     
  Log-likelihood  -137.410     -135.171     -137.525    -135.355     
  Deviance         713.767      652.606      717.054     657.424     
  AIC              284.821      280.341      285.050     280.709     
  BIC              294.381      289.902      294.610     290.269     
  N                 50           50           50          50         
=====================================================================

我们现在希望IV(即dpiddpilog(dpi)log(ddpi))首先出现。

选项1

# we will define a fake model which includes all the IVs
mtables <- mtable("Model fake" = lm(sr ~ dpi + ddpi + log(dpi) + log(ddpi), LifeCycleSavings),
                  "Model 1" = lm(sr ~ dpi + pop15 + pop75, LifeCycleSavings),
                  "Model 2" = lm(sr ~ ddpi + pop15 + pop75, LifeCycleSavings),
                  "Model 3" = lm(sr ~ log(dpi) + pop15 + pop75, LifeCycleSavings),
                  "Model 4" = lm(sr ~ log(ddpi) + pop15 + pop75, LifeCycleSavings))
# we will display mtables without the fake model
mtables[2:5]
# output
Calls:
Model 1: lm(formula = sr ~ dpi + pop15 + pop75, data = LifeCycleSavings)
Model 2: lm(formula = sr ~ ddpi + pop15 + pop75, data = LifeCycleSavings)
Model 3: lm(formula = sr ~ log(dpi) + pop15 + pop75, data = LifeCycleSavings)
Model 4: lm(formula = sr ~ log(ddpi) + pop15 + pop75, data = LifeCycleSavings)

=====================================================================
                    Model 1      Model 2     Model 3      Model 4    
---------------------------------------------------------------------
  (Intercept)       31.457***    28.125***    36.304**    26.118***  
                    (7.482)      (7.184)     (10.511)     (7.416)    
  dpi               -0.001                                           
                    (0.001)                                          
  ddpi                            0.428*                             
                                 (0.188)                             
  log(dpi)                                    -0.779                 
                                              (1.018)                
  log(ddpi)                                                1.584*    
                                                          (0.722)    
  pop15             -0.492**     -0.452**     -0.506**    -0.408**   
                    (0.149)      (0.141)      (0.154)     (0.144)    
  pop75             -1.568       -1.835       -1.649      -1.663     
                    (1.121)      (0.998)      (1.110)     (1.009)    
---------------------------------------------------------------------
  R-squared          0.274        0.337        0.271       0.332     
  adj. R-squared     0.227        0.293        0.223       0.288     
  sigma              3.939        3.767        3.948       3.780     
  F                  5.797        7.778        5.700       7.608     
  p                  0.002        0.000        0.002       0.000     
  Log-likelihood  -137.410     -135.171     -137.525    -135.355     
  Deviance         713.767      652.606      717.054     657.424     
  AIC              284.821      280.341      285.050     280.709     
  BIC              294.381      289.902      294.610     290.269     
  N                 50           50           50          50         
=====================================================================

选项2

# stargazer can be an option
library(stargazer)
stargazer(lm(sr ~ dpi + pop15 + pop75, LifeCycleSavings),
          lm(sr ~ ddpi + pop15 + pop75, LifeCycleSavings),
          lm(sr ~ log(dpi) + pop15 + pop75, LifeCycleSavings),
          lm(sr ~ log(ddpi) + pop15 + pop75, LifeCycleSavings),
          type = "text", column.labels = c("Model 1", "Model 2", "Model 3", "Model 4"),
          model.numbers = FALSE)
# output
=====================================================================
                                        Dependent variable:          
                              ---------------------------------------
                                                sr                   
                               Model 1   Model 2   Model 3   Model 4 
---------------------------------------------------------------------
dpi                            -0.001                                
                               (0.001)                               

ddpi                                     0.428**                     
                                         (0.188)                     

log(dpi)                                           -0.779            
                                                   (1.018)           

log(ddpi)                                                    1.584** 
                                                             (0.722) 

pop15                         -0.492*** -0.452*** -0.506*** -0.408***
                               (0.149)   (0.141)   (0.154)   (0.144) 

pop75                          -1.568    -1.835*   -1.649    -1.663  
                               (1.121)   (0.998)   (1.110)   (1.009) 

Constant                      31.457*** 28.125*** 36.304*** 26.118***
                               (7.482)   (7.184)  (10.511)   (7.416) 

---------------------------------------------------------------------
Observations                     50        50        50        50    
R2                              0.274     0.337     0.271     0.332  
Adjusted R2                     0.227     0.293     0.223     0.288  
Residual Std. Error (df = 46)   3.939     3.767     3.948     3.780  
F Statistic (df = 3; 46)      5.797***  7.778***  5.700***  7.608*** 
=====================================================================
Note:                                     *p<0.1; **p<0.05; ***p<0.01

基于您的数据的解决方案

mtables <- mtable("Modell 0"=lm(DV ~ IV1 + IV2 + IV3),
                  "Modell 1"=lm(DV ~ IV1 + CV1 + CV2),
                  "Modell 2"=lm(DV ~ IV2 + CV1 + CV2),
                  "Modell 3"=lm(DV ~ IV3 + CV1 + CV2))
mtables[2:4]
# or use stargazer() as above