所以我的示例数据如下所示:
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_period
onindicator
的边际效应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
命令工作得很好......有谁知道如何解决这个问题?