deltamethod {metafor} | R Documentation |
Apply the (Multivariate) Delta Method
Description
Function to apply the (multivariate) delta method to a set of estimates.
Usage
deltamethod(x, vcov, fun, level, H0=0, digits)
Arguments
x |
either a vector of estimates or a model object from which model coefficients can be extracted via |
vcov |
when |
fun |
a function to apply to the estimates. |
level |
numeric value between 0 and 100 to specify the confidence interval level (see here for details). If unspecified, this either defaults to 95 or, if possible, the corresponding value from the model object. |
H0 |
numeric value to specify the value under the null hypothesis for the Wald-type test(s) (the default is 0). Can also be a vector. |
digits |
optional integer to specify the number of decimal places to which the printed results should be rounded. |
Details
Let \(\hat{\theta}\) denote a vector of \(p\) estimates which can be specified via the x
argument and let \(\Sigma\) denote the corresponding \(p \times p\) variance-covariance matrix, which can be specified via the vcov
argument. If x
is not an vector with estimates, then the function assumes that x
is a model object and will try to use coef(x)
and vcov(x)
to extract the model coefficients and the corresponding variance-covariance matrix (in this case, the vcov
argument is ignored).
Let \(f(\cdot)\) be a function, specified via the fun
argument, with \(p\) inputs/arguments (or with a single argument that is assumed to be a vector of length \(p\)), which returns a numeric (and atomic) vector of \(q\) transformed estimates. Then the function computes \(f(\hat{\theta})\) and the corresponding variance-covariance matrix of the transformed estimates using the multivariate delta method (e.g., van der Vaart, 1998) with \[\text{Var}[f(\hat{\theta})] = \nabla f(\hat{\theta})' \cdot \Sigma \cdot \nabla f(\hat{\theta})\] where \(\nabla f(\hat{\theta})\) denotes the gradient of \(f(\cdot)\) evaluated at \(\hat{\theta}\). The function computes the gradient numerically using the derivative
function from the calculus
package.
The function also computes Wald-type tests and confidence intervals for the \(q\) transformed estimates. The level
argument can be used to control the confidence interval level.
Value
An object of class "deltamethod"
. The object is a list containing the following components:
tab |
a data frame with the transformed estimates, standard errors, test statistics, p-values, and lower/upper confidence interval bounds. |
vcov |
the variance-covariance matrix of the transformed estimates. |
... |
some additional elements/values. |
The results are formatted and printed with the print
function. Extractor functions include coef
and vcov
.
Author(s)
Wolfgang Viechtbauer (wvb@metafor-project.org, https://www.metafor-project.org).
References
van der Vaart, A. W. (1998). Asymptotic statistics. Cambridge, UK: Cambridge University Press.
Viechtbauer, W. (2010). Conducting meta-analyses in R with the metafor package. Journal of Statistical Software, 36(3), 1–48. https://doi.org/10.18637/jss.v036.i03
See Also
conv.delta
for a function to apply the (univariate) delta method to observed effect sizes or outcomes and their sampling variances.
Examples
############################################################################
### copy data into 'dat'
dat <- dat.craft2003
### construct dataset and var-cov matrix of the correlations
tmp <- rcalc(ri ~ var1 + var2 | study, ni=ni, data=dat)
V <- tmp$V
dat <- tmp$dat
### turn var1.var2 into a factor with the desired order of levels
dat$var1.var2 <- factor(dat$var1.var2,
levels=c("acog.perf", "asom.perf", "conf.perf", "acog.asom", "acog.conf", "asom.conf"))
### multivariate random-effects model
res <- rma.mv(yi, V, mods = ~ 0 + var1.var2, random = ~ var1.var2 | study, struct="UN", data=dat)
res
### restructure estimated mean correlations into a 4x4 matrix
R <- vec2mat(coef(res))
rownames(R) <- colnames(R) <- c("perf", "acog", "asom", "conf")
round(R, digits=3)
### check that order in vcov(res) corresponds to order in R
round(vcov(res), digits=4)
### fit regression model with 'perf' as outcome and 'acog', 'asom', and 'conf' as predictors
matreg(1, 2:4, R=R, V=vcov(res))
### same analysis but using the deltamethod() function
deltamethod(coef(res), vcov(res), fun=function(r1,r2,r3,r4,r5,r6) {
R <- vec2mat(c(r1,r2,r3,r4,r5,r6))
setNames(c(solve(R[-1,-1]) %*% R[2:4,1]), c("acog","asom","conf"))
})
### using a function that takes a vector as input
deltamethod(coef(res), vcov(res), fun=function(r) {
R <- vec2mat(r)
setNames(c(solve(R[-1,-1]) %*% R[2:4,1]), c("acog","asom","conf"))
})
############################################################################
### construct dataset and var-cov matrix of the r-to-z transformed correlations
dat <- dat.craft2003
tmp <- rcalc(ri ~ var1 + var2 | study, ni=ni, data=dat, rtoz=TRUE)
V <- tmp$V
dat <- tmp$dat
### turn var1.var2 into a factor with the desired order of levels
dat$var1.var2 <- factor(dat$var1.var2,
levels=c("acog.perf", "asom.perf", "conf.perf", "acog.asom", "acog.conf", "asom.conf"))
### multivariate random-effects model
res <- rma.mv(yi, V, mods = ~ 0 + var1.var2, random = ~ var1.var2 | study, struct="UN", data=dat)
res
### estimate the difference between r(acog,perf) and r(asom,perf)
deltamethod(res, fun=function(z1,z2,z3,z4,z5,z6) {
transf.ztor(z1) - transf.ztor(z2)
})
### using a function that takes a vector as input
deltamethod(res, fun=function(z) {
transf.ztor(z[1]) - transf.ztor(z[2])
})
############################################################################