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

我有三个有序回归模型,其中有序因变量的范围从 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() 的输出不会生成相同类型的表,我也无法在每个级别生成表: 观星者(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

最佳答案

Github 上的新开发包对此有所帮助。作者 Thomas Leeper,他经常在这里发帖。

https://github.com/leeper/margins

尽情享受吧!

https://stackoverflow.com/questions/38845477/

相关文章:

f# - 使用寓言中的节点模块

python - 查找文本是否突出显示

java - 从另一个项目注入(inject) FeignClient 时出错

python - 在 Python 中查找对称矩阵的 SVD

android - 键盘打开android时布局正在缩小

reactjs - 每个 child 在 React 中应该有一个唯一的关键错误

c# - 使用 Entity Framework 定义关系/FK 的两端是否重要?

google-maps - 谷歌地图 -> 根据给定距离制作环形路线?

c# - WCF 服务在多服务器环境中不起作用

c++ - 如何从 MAC 中的/Library/Framework 文件夹中包含 C++ 中的文件