deltamethod | R Documentation |
Function to apply the (multivariate) delta method to a set of estimates. \loadmathjax
deltamethod(x, vcov, fun, level, H0=0, digits)
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. |
Let \mjeqn\hat\theta\theta denote a vector of \mjseqnp estimates which can be specified via the x
argument and let \mjseqn\Sigma denote the corresponding \mjeqnp \times ppxp 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 \mjeqnf(\cdot)f(.) be a function, specified via the fun
argument, with \mjseqnp inputs/arguments (or with a single argument that is assumed to be a vector of length \mjseqnp), which returns a numeric (and atomic) vector of \mjseqnq transformed estimates. Then the function computes \mjeqnf(\hat\theta)f(\theta) and the corresponding variance-covariance matrix of the transformed estimates using the multivariate delta method (e.g., van der Vaart, 1998) with \mjdeqn\textVar[f(\hat\theta)] = \nabla f(\hat\theta)' \cdot \Sigma \cdot \nabla f(\hat\theta)Var[f(\theta)] = ▽f(\theta)' \Sigma ▽f(\theta) where \mjeqn\nabla f(\hat\theta)▽f(\theta) denotes the gradient of \mjeqnf(\cdot)f(.) evaluated at \mjeqn\hat\theta\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 \mjseqnq transformed estimates. The level
argument can be used to control the confidence interval level.
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
.
Wolfgang Viechtbauer (wvb@metafor-project.org, https://www.metafor-project.org).
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
conv.delta
for a function to apply the (univariate) delta method to observed effect sizes or outcomes and their sampling variances.
############################################################################
### 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])
})
############################################################################
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.