【问题标题】:Calculate marginal effects of probit model with categorial data使用分类数据计算概率模型的边际效应
【发布时间】:2020-12-03 12:41:42
【问题描述】:

所以我的示例数据如下所示:

library(dplyr)
library(plm)
library(car)
library(margins)
test <- structure(list(period = c(1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 
5, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 
10, 1, 2, 2, 2, 3, 3, 3, 3, 5, 6, 6, 7, 7, 7, 7, 8, 8), indicator = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L)), row.names = c(NA, -50L), class = "data.frame")

在下一步中,我为periodvariable 创建不同的类别

test$factor_period <-car::recode(test$period,"0:2='<2';3:5='3-5';6='6';7='7';8='8';9='9';10='10';11='11';12:44='>12'")

我现在正在计算一个概率模型。

model_time <- glm(indicator ~ factor_period, family = binomial(link = "probit"), data = test)

到目前为止一切顺利。现在我正在尝试确定factor_periodindicatorfactor_period = "8" 上的边际效应,如下所示:

res_time <- summary(margins(model = model_time, at = list(factor_period = "8"), data = test))

这会返回错误信息:

Error in attributes(.Data) <- c(attributes(.Data), attrib) : 
      'names'[1] attribute must be the same length as the vector[0]

似乎对period 数据进行分类是个问题。如果没有 atargurment,marginscommand 就可以正常工作...有人知道如何解决这个问题吗?

【问题讨论】:

    标签: r glm margins marginal-effects


    【解决方案1】:

    解决方案:通过将factor_period 重新调整为“8”,您可以避免margins 运算符中的at 命令,从而计算所有边际效应。

    test$factor_period<-relevel(as.factor(test$factor_period), ref="8")
    

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 2021-05-31
      • 2011-08-13
      • 2020-05-07
      • 1970-01-01
      • 2020-08-16
      • 2014-12-16
      • 2019-10-10
      • 2013-04-27
      相关资源
      最近更新 更多