using "at" specification in Margins (problem with factors)

824 views Asked by At

Does anybody know if (or when?) we can use the “at” specification for factor variables in the Margins Package?

According to the Margins vignette (dated 2018-05-22), this is not always possible "yet". I quote from the vignette below:

“A really nice feature of Stata’s margins command is that it handles factor variables gracefully. This functionality is difficult to emulate in R, but the margins() function does its best. …. margins() recognizes the factor and displays the marginal effect for each level of the factor separately. (Caveat: this may not work with certain “at” specifications, yet.)”

Indeed, I get an error when I try with my logistic regression that relies entirely on independent variables that are factors (e.g., "LOC" in code below).

margins(me.model.glm, at = list(LOC = 0:1))

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

1

There are 1 answers

1
James Jaccard On

Try

margins(me.model.glm, at = list(LOC = c("0","1"))

That worked for me in an analogous situation. Note that if I just put in one value for the factor variable, like

margins(me.model.glm, at = list(LOC = "0"))

I get an error. I must provide values for each level of the factor variable