juliah0494
juliah0494

Reputation: 175

using "at" argument of margins function in R for logit model

I want to be able to analyze the marginal effect of continuous and binary variables in a logit model. I am hoping for R to provide what the independent marginal effect of hp is at its mean (in this example that is at 200), while also finding the marginal effect of the vs variable equaling 1. I am hoping the output table also includes the SE, p value, and z score. I am having trouble with the table and when I have gotten it to run it doesn't evaluate the two variables independently. Here is an MRE below. Thank you!

mod2 <- glm(am ~ hp + factor(vs), data=mtcars, family=binomial)

margins(mod2)
#> Average marginal effects
#> glm(formula = am ~ hp + factor(vs), family = binomial, data = mtcars)
#>        hp      vs1
#>  -0.00203 -0.03154

#code where I am trying to evaluate at the desired values. 
margins(mod2, at=list(hp=200, vs=1))

Upvotes: 2

Views: 702

Answers (1)

duckmayr
duckmayr

Reputation: 16940

This is because you've changed vs to a factor. Consider the following

library(margins)
mod3 <- glm(am ~ hp + vs, data=mtcars, family=binomial)
margins(mod3, at=list(hp=200, vs=1))

# Average marginal effects at specified values
# glm(formula = am ~ hp + vs, family = binomial, data = mtcars)
# 
#  at(hp) at(vs)        hp       vs
#     200      1 -0.001783 -0.02803

There is no real reason to turn vs into a factor here; it's dichotomous.

Upvotes: 1

Related Questions