如何在R中创建边际效应表?

时间:2016-08-09 08:17:14

标签: r graphics latex logistic-regression glm

我有三个有序回归模型,其中有序因变量的范围从0到2.我想要做的是在所有三个模型的每个级别(0,1和2)创建边际效应表(不是图) 。因此,三个表格分别显示0,1和2级的边际效应。

    ## create a random data that is similar to my dataset
    set.seed(100)
    mydata <- data.frame(
      x1    = sample(c(0, 1, 2), 100, replace = TRUE),
      x2    = sample(c(0, 1, 2, 3, 4), 100, replace = TRUE),
      x3    = sample(c(0, 1, 2, 3, 5), 100, replace = TRUE),
      x4    = sample(c(1:100), 100, replace = TRUE),
      x5    = sample(c(10:1000), 100, replace = TRUE),
      Z1 = sample(c(0, 1, 2), 100, replace = TRUE)
    )


    ## makeit factor
    mydata$Z1 <- as.factor(mydata$Z1)

    ## My models
    require(MASS)

    M1<- polr(Z1 ~x1+x2+x3+x4, data=mydata, Hess = TRUE,  method="logistic")

    M2<- polr(Z1 ~x2+x3+x4+x5, data=mydata, Hess = TRUE,  method="logistic")

    M3<- polr(Z1 ~x1+x2+x3+x4+x5, data=mydata, Hess = TRUE,  method="logistic")

    ## Calculate marginal effects using the erer package
    require(erer)

    M1ME<- ocME(M1)

    M2ME <- ocME(M2)

    M3ME <- ocME(M3)

通常我会使用包stargazer来创建适当的表,例如使用:

stargazer(M1,M2, M3, type = ”text”)  

但是,OcME()的输出不会生成相同类型的表,也不能在每个级别生成表:  stargazer(M1ME$out,M2ME$out, M3ME$out, type = "text" )

  • 您对如何生成这些类型有任何建议吗? 表?手动执行此操作非常耗时。

修改 因此,理想的输出是创建下面指出的三个表(注意:数字不正确,只是一个插图)

边际效应 - 等级0(Z)

 ==========================================
                      Dependent variable:     
                 -----------------------------
                              Z1              
                    (1)       (2)       (3)   
    ------------------------------------------
    x1             0.301               0.302  
                  (0.250)             (0.250) 

    x2             0.143     0.174     0.142  
                  (0.131)   (0.128)   (0.132) 

    x3             0.121     0.106     0.122  
                  (0.117)   (0.116)   (0.117) 

    x4            -0.008    -0.008    -0.008  
                  (0.007)   (0.007)   (0.007) 

    x5                     -0.00004   -0.0001 
                            (0.001)   (0.001) 

    ------------------------------------------
    Observations    100       100       100   
    ==========================================
    Note:          *p<0.1; **p<0.05; ***p<0.01

边际效应 - 第1级(Z)

==========================================
                  Dependent variable:     
             -----------------------------
                          Z1              
                (1)       (2)       (3)   
------------------------------------------
x1             0.301               0.302  
              (0.250)             (0.250) 

x2             0.143     0.174     0.142  
              (0.131)   (0.128)   (0.132) 

x3             0.121     0.106     0.122  
              (0.117)   (0.116)   (0.117) 

x4            -0.008    -0.008    -0.008  
              (0.007)   (0.007)   (0.007) 

x5                     -0.00004   -0.0001 
                        (0.001)   (0.001) 

------------------------------------------
Observations    100       100       100   
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01

边际效应 - 级别3(Z)

==========================================
                  Dependent variable:     
             -----------------------------
                          Z1              
                (1)       (2)       (3)   
------------------------------------------
x1             0.301               0.302  
              (0.250)             (0.250) 

x2             0.143     0.174     0.142  
              (0.131)   (0.128)   (0.132) 

x3             0.121     0.106     0.122  
              (0.117)   (0.116)   (0.117) 

x4            -0.008    -0.008    -0.008  
              (0.007)   (0.007)   (0.007) 

x5                     -0.00004   -0.0001 
                        (0.001)   (0.001) 

------------------------------------------
Observations    100       100       100   
==========================================
Note:          *p<0.1; **p<0.05; ***p<0.01

1 个答案:

答案 0 :(得分:0)

Github上的新开发包得到了一点。托马斯·李珀(Thomas Leeper)在这里发帖很多。

https://github.com/leeper/margins

享受!