delta_method | R Documentation |
Transform means a (co)variances using the delta method
delta_method(..., .means, .V, return = c("means", "cov", "stddev", "cor"))
... |
Unquoted transformations. See example. |
.means |
A named vector of means. |
.V |
A covariance matrix. |
return |
What should be returned? |
A list with one of (optionally named):
means
of the transformed variables.
cov
(co) variance matrix of the the transformed variables.
stddev
standard deviations of the transformed variables (sqrt(diag(cov))
).
cor
correlation matrix of the transformed variables. (cov2cor(cov)
)
Most of this function is mostly copied from msm::deltamethod()
.
M <- sapply(mtcars, mean)
V <- cov(mtcars)
delta_method(
(mpg^2) / hp,
log_am = log1p(am),
.means = M, .V = V,
return = "cor"
)
# Sobel Test ----
data("mtcars")
mod.y <- lm(mpg ~ hp + cyl, mtcars[1:5, ])
mod.m <- lm(hp ~ cyl, mtcars[1:5, ])
bhat <- c(coef(mod.y), coef(mod.m))[c(2, 5)]
Vhat <- dbind(vcov(mod.y), vcov(mod.m))[c(2, 5), c(2, 5)]
res <- delta_method(
hp * cyl,
.means = bhat, .V = Vhat,
return = c("means", "stddev")
)
res$means / res$stddev
# Compare:
(bhat[1] * bhat[2]) /
sqrt(bhat[1]^2 * Vhat[2, 2] + bhat[2]^2 * Vhat[1, 1])
# Special character will give you a bad time...
m <- lm(mpg ~ factor(cyl), mtcars[1:5, ])
bhat <- coef(m)
names(bhat) <- c("cyl4", "cyl6", "cyl8")
V <- vcov(m)
delta_method(cyl4, cyl4 + cyl6, cyl4 + cyl8,
.means = bhat,
.V = V
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.