So my example data looks like this:
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")
In the next step I create different categories for the period
variable
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'")
I am now calculating a probit model.
model_time <- glm(indicator ~ factor_period, family = binomial(link = "probit"), data = test)
So far so good. Now I am trying do determine the marginal effect of factor_period
on indicator
at factor_period = "8"
like this:
res_time <- summary(margins(model = model_time, at = list(factor_period = "8"), data = test))
This returns the error message:
Error in attributes(.Data) <- c(attributes(.Data), attrib) :
'names'[1] attribute must be the same length as the vector[0]
It seems like categorizing the period
data is the problem. Without the at
argurment, the margins
command works just fine...Does anyone know how to fix this?
Solution: By releveling
factor_period
to "8" you can avoid theat
command in themargins
operator and therefore calculate all marginal effects.