Search code examples
rconfidence-intervalmargins

How to change the confidence interval for marginal effects at levels of a moderator variable in R


I'm trying to estimate the marginal effects of a treatment variable at different level of a moderator variable in an interactive linear model. The "margins" package says you can change the confidence intervals using the "level" argument, but that doesn't seem to work for this purpose (note the identical outputs below).

# Required Package
library(margins)

# Regression
model<-lm(mpg ~ vs + am + vs*am, data=mtcars)

# Marginal Effects with 95% confidence intervals
margins95 <- summary(margins(model,at=list(am=0:1),variables='vs',vce="delta",level=.95))
print(margins95)

# Marginal Effects with 90% confidence intervals
margins90 <- summary(margins(model,at=list(am=0:1),variables='vs',vce="delta",level=.9))
print(margins90)

Thanks for your help!


Solution

  • It's the margins_summary method that takes the level argument, or alternatively the summary method. In themargins call itself the value is silently discarded within ..., because that argument is not known/used.

    Either of this will work:

    margins_summary(model,at=list(am=0:1),variables='vs',vce="delta", level=.90)
    summary(margins(model,at=list(am=0:1),variables='vs',vce="delta"), level=.90)