library(marginaleffects)
mod <- lm(mpg ~ hp + wt + factor(cyl), data = mtcars)
# When `FUN` and `hypothesis` are `NULL`, `deltamethod()` returns a data.frame of parameters
deltamethod(mod)
# Test of equality between coefficients
deltamethod(mod, hypothesis = "hp = wt")
# Non-linear function
deltamethod(mod, hypothesis = "exp(hp + wt) = 0.1")
# Robust standard errors
deltamethod(mod, hypothesis = "hp = wt", vcov = "HC3")
# b1, b2, ... shortcuts can be used to identify the position of the
# parameters of interest in the output of FUN
deltamethod(mod, hypothesis = "b2 = b3")
# term names with special characters have to be enclosed in backticks
deltamethod(mod, hypothesis = "`factor(cyl)6` = `factor(cyl)8`")
mod2 <- lm(mpg ~ hp * drat, data = mtcars)
deltamethod(mod2, hypothesis = "`hp:drat` = drat")
# predictions(), comparisons(), and marginaleffects()
mod <- glm(am ~ hp + mpg, data = mtcars, family = binomial)
cmp <- comparisons(mod, newdata = "mean")
deltamethod(cmp, hypothesis = "b1 = b2")
mfx <- marginaleffects(mod, newdata = "mean")
deltamethod(cmp, hypothesis = "b2 = 0.2")
pre <- predictions(mod, newdata = datagrid(hp = 110, mpg = c(30, 35)))
deltamethod(pre, hypothesis = "b1 = b2")
# The `FUN` argument can be used to compute standard errors for fitted values
mod <- glm(am ~ hp + mpg, data = mtcars, family = binomial)
f <- function(x) predict(x, type = "link", newdata = mtcars)
p <- deltamethod(mod, FUN = f)
head(p)
f <- function(x) predict(x, type = "response", newdata = mtcars)
p <- deltamethod(mod, FUN = f)
head(p)
Run the code above in your browser using DataLab