【问题标题】:Marginal effects from the multinomial model多项模型的边际效应
【发布时间】:2021-07-03 11:31:43
【问题描述】:

我试图从源自mlogit 包的多项模型中获得边际效应,但它显示错误。任何人都可以提供一些指导来解决问题吗?非常感谢!

# data
df1 <- structure(list(Y = c(3, 4, 1, 2, 3, 4, 1, 5, 2, 3, 4, 2, 1, 4, 
1, 5, 3, 3, 3, 5, 5, 4, 3, 5, 4, 2, 5, 4, 3, 2, 5, 3, 2, 5, 5, 
4, 5, 1, 2, 4, 3, 1, 2, 3, 1, 1, 3, 2, 4, 2, 2, 4, 1, 5, 3, 1, 
5, 2, 3, 4, 2, 4, 5, 2, 4, 1, 4, 2, 1, 5, 3, 2, 1, 4, 4, 1, 5, 
1, 1, 1, 4, 5, 5, 3, 2, 3, 3, 2, 4, 4, 5, 3, 5, 1, 2, 5, 5, 1, 
2, 3), D = c(12, 8, 6, 11, 5, 14, 0, 22, 15, 13, 18, 3, 5, 9, 
10, 28, 9, 16, 17, 14, 26, 18, 18, 23, 23, 12, 28, 14, 10, 15, 
26, 9, 2, 30, 18, 24, 27, 7, 6, 25, 13, 8, 4, 16, 1, 4, 5, 18, 
21, 1, 2, 19, 4, 2, 16, 17, 23, 15, 13, 21, 24, 14, 27, 6, 20, 
6, 19, 8, 7, 23, 11, 11, 1, 22, 21, 4, 27, 6, 2, 9, 18, 30, 26, 
22, 10, 1, 4, 7, 26, 15, 26, 18, 30, 1, 11, 29, 25, 3, 19, 15
), x1 = c(13, 12, 4, 3, 16, 16, 15, 13, 1, 15, 10, 16, 1, 17, 
7, 13, 12, 6, 8, 16, 16, 11, 7, 16, 5, 13, 12, 16, 17, 6, 16, 
9, 14, 16, 15, 5, 7, 2, 8, 2, 9, 9, 15, 13, 9, 4, 16, 2, 11, 
13, 11, 6, 4, 3, 7, 4, 12, 2, 16, 14, 3, 13, 10, 11, 10, 4, 11, 
16, 8, 12, 14, 9, 4, 16, 16, 12, 9, 10, 6, 1, 3, 8, 7, 7, 5, 
16, 17, 10, 4, 15, 10, 8, 3, 13, 9, 16, 12, 7, 4, 11), x2 = c(12, 
19, 18, 19, 15, 12, 15, 16, 15, 11, 12, 16, 17, 14, 12, 17, 17, 
16, 12, 20, 11, 11, 15, 14, 18, 10, 14, 13, 10, 14, 18, 18, 18, 
17, 18, 14, 16, 19, 18, 16, 18, 14, 17, 10, 16, 12, 16, 15, 11, 
18, 19, 15, 19, 11, 16, 10, 20, 14, 10, 12, 10, 15, 13, 15, 11, 
20, 11, 12, 16, 16, 11, 15, 11, 11, 10, 10, 16, 11, 20, 17, 20, 
17, 16, 11, 18, 19, 18, 14, 17, 11, 16, 11, 18, 14, 15, 16, 11, 
14, 11, 13)), class = "data.frame", row.names = c(NA, -100L))

library(mlogit)
mld <- mlogit.data(df1, choice="Y", shape="wide")  # shape data for `mlogit()`
mlfit <- mlogit(Y ~ 1 | D + x1 + x2, reflevel="1", data=ml.d)  # fit the model
effects(mlfit) # this shows the following error:
Error in if (rhs %in% c(1, 3)) { : argument is of length zero
Called from: effects.mlogit(mlfit)

【问题讨论】:

    标签: r error-handling multinomial mlogit marginal-effects


    【解决方案1】:

    我相信你错过了需要放在那里的协变量信息,所以如果你使用effects(mlfit, covariate = 'D'),它应该可以工作。现在错误来了,因为协变量的默认值是 NULL。 NULL 在 R 中是特殊的,它没有(零)长度,因此你得到长度为零的参数。如果它解决了您的问题,请告诉我。

    根据 effects.mlogit 的文档,它说:

    covariate 
    the name of the covariate for which the effect should be computed,
    

    我最后得到了这个输出:

    R>effects(mlfit, covariate = 'D')
                  1               2               3 
    -0.003585105992 -0.070921137682 -0.026032167377 
                  4               5 
     0.078295227196  0.022243183855 
    

    【讨论】:

    • 好的,太好了。谢谢@PKumar!
    • @cliu,万事如意
    • 嗨@PKumar,只是一个快速的后续问题:我试图提取模型中所有系数的边际效应,但现在即使我指定了协变量,它也给了我同样的错误。你知道它的方法吗?下面是代码:c.names &lt;- names(ml.fit$model)[- c(1, 5:6)] ME.mnl &lt;- sapply(c.names, function(x) stats::effects(mlfit, covariate='x', data=mld), simplify=FALSE)谢谢你的帮助!
    • @cliu 运行这个:sapply(c('D', 'x1', 'x2'), function(x)effects(mlfit, covariate = x)) 您的模型名称也是 mlfit 而不是 ml.fit,c.names 给您错误的结果,因为您的方程中没有名为 linpred 的协变量,c。名字应该是这个c.names &lt;- names(mlfit$model)[- c(1, 5:7)]
    • 太棒了。非常感谢@Pkumar!
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2022-08-20
    相关资源
    最近更新 更多