0

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

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")

在下一步中,我为period变量创建不同的类别

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_periodonindicator的边际效应factor_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数据进行分类是个问题。没有at参数,margins命令工作得很好......有谁知道如何解决这个问题?

4

1 回答 1

0

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

test$factor_period<-relevel(as.factor(test$factor_period), ref="8")
于 2020-12-03T17:31:56.947 回答