deltamethod is a function to get a first-order approximate standard error for a nonlinear function of a vector of random variables with known or estimated covariance matrix. deltamethod emulates the behavior of the excellent and well-established car::deltaMethod and car::linearHypothesis functions, but it supports more models, requires fewer dependencies, and offers some convenience features like shortcuts for robust standard errors.

deltamethod(
  model,
  hypothesis = NULL,
  FUN = NULL,
  vcov = NULL,
  conf_level = 0.95,
  ...
)

Arguments

model

Model object

hypothesis

specify a hypothesis test or custom contrast using a vector, matrix, string, or string formula.

  • String:

    • "pairwise": pairwise differences between estimates in each row.

    • "reference": differences between the estimates in each row and the estimate in the first row.

  • String formula to specify linear or non-linear hypothesis tests. If the term column uniquely identifies rows, terms can be used in the formula. Otherwise, use b1, b2, etc. to identify the position of each parameter. Examples:

    • hp = drat

    • hp + drat = 12

    • b1 + b2 + b3 = 0

  • Numeric vector: Weights to compute a linear combination of (custom contrast between) estimates. Length equal to the number of rows generated by the same function call, but without the hypothesis argument.

  • Numeric matrix: Each column is a vector of weights, as describe above, used to compute a distinct linear combination of (contrast between) estimates. The column names of the matrix are used as labels in the output.

  • See the Examples section below and the vignette: https://vincentarelbundock.github.io/marginaleffects/articles/hypothesis.html

FUN

a function which accepts a model object and returns a numeric vector or a data.frame with two columns called term and estimate.

vcov

Type of uncertainty estimates to report (e.g., for robust standard errors). Acceptable values:

  • FALSE: Do not compute standard errors. This can speed up computation considerably.

  • TRUE: Unit-level standard errors using the default vcov(model) variance-covariance matrix.

  • String which indicates the kind of uncertainty estimates to return.

    • Heteroskedasticity-consistent: "HC", "HC0", "HC1", "HC2", "HC3", "HC4", "HC4m", "HC5". See ?sandwich::vcovHC

    • Heteroskedasticity and autocorrelation consistent: "HAC"

    • Mixed-Models degrees of freedom: "satterthwaite", "kenward-roger"

    • Other: "NeweyWest", "KernHAC", "OPG". See the sandwich package documentation.

  • One-sided formula which indicates the name of cluster variables (e.g., ~unit_id). This formula is passed to the cluster argument of the sandwich::vcovCL function.

  • Square covariance matrix

  • Function which returns a covariance matrix (e.g., stats::vcov(model))

conf_level

numeric value between 0 and 1. Confidence level to use to build a confidence interval.

...

Additional arguments are passed to the predict() method supplied by the modeling package.These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?marginaleffects documentation for a non-exhaustive list of available arguments.

Examples

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)
#>           term    estimate
#> 1  (Intercept) 35.84599532
#> 2           hp -0.02311981
#> 3           wt -3.18140405
#> 4 factor(cyl)6 -3.35902490
#> 5 factor(cyl)8 -3.18588444

# Test of equality between coefficients
deltamethod(mod, "hp = wt")
#>      term estimate std.error statistic      p.value conf.low conf.high
#> 1 hp = wt 3.158284 0.7199081  4.387066 1.148899e-05  1.74729  4.569278

# Non-linear function
deltamethod(mod, "exp(hp + wt) = 0.1")
#>                 term    estimate  std.error statistic    p.value   conf.low
#> 1 exp(hp + wt) = 0.1 -0.05942178 0.02919718 -2.035189 0.04183184 -0.1166472
#>      conf.high
#> 1 -0.002196363

# Robust standard errors
deltamethod(mod, "hp = wt", vcov = "HC3")
#>      term estimate std.error statistic      p.value conf.low conf.high
#> 1 hp = wt 3.158284 0.8051929  3.922394 8.767334e-05 1.580135  4.736433

# b1, b2, ... shortcuts can be used to identify the position of the
# parameters of interest in the output of FUN
deltamethod(mod, "b2 = b3")
#>      term estimate std.error statistic      p.value conf.low conf.high
#> 1 b2 = b3 3.158284 0.7199081  4.387066 1.148899e-05  1.74729  4.569278

# term names with special characters have to be enclosed in backticks
deltamethod(mod, "`factor(cyl)6` = `factor(cyl)8`")
#>                              term   estimate std.error  statistic  p.value
#> 1 `factor(cyl)6` = `factor(cyl)8` -0.1731405  1.653923 -0.1046847 0.916626
#>   conf.low conf.high
#> 1 -3.41477   3.06849

# 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)
#>   term   estimate std.error  statistic    p.value  conf.low  conf.high
#> 1   b1 -1.0983601 0.7160423 -1.5339319 0.12504640 -2.501777  0.3050570
#> 2   b2 -1.0983601 0.7160423 -1.5339319 0.12504640 -2.501777  0.3050570
#> 3   b3  0.2331884 0.7808207  0.2986452 0.76521076 -1.297192  1.7635688
#> 4   b4 -0.5945143 0.6471012 -0.9187346 0.35823441 -1.862809  0.6737808
#> 5   b5 -0.4175761 0.6474633 -0.6449417 0.51896494 -1.686581  0.8514287
#> 6   b6 -5.0264654 2.1949096 -2.2900558 0.02201808 -9.328409 -0.7245217

f <- function(x) predict(x, type = "response", newdata = mtcars)
p <- deltamethod(mod, FUN = f)
head(p)
#>   term    estimate  std.error statistic     p.value    conf.low  conf.high
#> 1   b1 0.250047286 0.13293667 1.8809504 0.059978669 -0.01050381 0.51059838
#> 2   b2 0.250047286 0.13293667 1.8809504 0.059978669 -0.01050381 0.51059838
#> 3   b3 0.558034354 0.19239540 2.9004558 0.003726204  0.18094630 0.93512241
#> 4   b4 0.355599745 0.14785649 2.4050330 0.016171004  0.06580635 0.64539314
#> 5   b5 0.397096909 0.15541311 2.5551057 0.010615556  0.09249282 0.70170100
#> 6   b6 0.006519185 0.01407361 0.4632204 0.643206374 -0.02106459 0.03410296