deltaMethod
is a generic function that uses the delta method 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(object, ...)# S3 method for default
deltaMethod(object, g, vcov., func=g, constants, level=0.95, ...)
# S3 method for lm
deltaMethod (object, g, vcov.=vcov(object, complete=FALSE),
parameterNames=names(coef(object)), ...)
# S3 method for nls
deltaMethod(object, g, vcov.=vcov(object, complete=FALSE), ...)
# S3 method for multinom
deltaMethod (object, g, vcov. = vcov(object, complete=FALSE),
parameterNames = if (is.matrix(coef(object)))
colnames(coef(object)) else names(coef(object)), ...)
# S3 method for polr
deltaMethod (object, g, vcov.=vcov(object, complete=FALSE), ...)
# S3 method for survreg
deltaMethod (object, g, vcov. = vcov(object, complete=FALSE),
parameterNames = names(coef(object)), ...)
# S3 method for coxph
deltaMethod (object, g, vcov. = vcov(object, complete=FALSE),
parameterNames = names(coef(object)), ...)
# S3 method for mer
deltaMethod (object, g, vcov. = vcov(object, complete=FALSE),
parameterNames = names(fixef(object)), ...)
# S3 method for merMod
deltaMethod (object, g, vcov. = vcov(object, complete=FALSE),
parameterNames = names(fixef(object)), ...)
# S3 method for lme
deltaMethod (object, g, vcov. = vcov(object, complete=FALSE),
parameterNames = names(fixef(object)), ...)
# S3 method for lmList
deltaMethod (object, g, ...)
For the default method, object
is either (1) a vector of p
named elements, so names(object)
returns a list
of p
character strings that are the names of the elements of
object
; or (2) a model object for which there are coef
and vcov
methods,
and for which the named coefficient vector returned by coef
is asymptotically normally distributed with asymptotic
covariance matrix returned by vcov
.
For the other methods, object
is a
regression object for which coef(object)
or fixef(object)
returns a vector of parameter
estimates.
A quoted string that is the function of the parameter estimates to be evaluated; see the details below.
The (estimated) covariance matrix of the coefficient
estimates. For the default method, this argument is required. For all
other methods, this argument must either provide the estimated covariance
matrix or a function that when applied
to object
returns a covariance matrix. The default is
to use the function vcov
.
A quoted string used to annotate output. The default of
func = g
is usually appropriate.
A character vector of length p
that gives the
names of the parameters in the same order as they appear in the vector of
estimates. This argument will be useful if some of the names in the
vector of estimates include special characters, like I(x2^2)
, or
x1:x2
that will confuse the numerical differentiation function. See
details below.
This argument is a named vector whose elements are constants that
are used in the f
argument. This is needed only when the function is
called from within another function to comply to R scoping rules. See an
example below.
level for confidence interval, default 0.95
.
Used to pass arguments to the generic method.
A data.frame with two components
named Estimate
for the estimate, SE
for its standard error.
The value of g
is given as a row label.
Suppose \(x\) is a random vector of length \(p\) that is at least approximately normally distributed with mean \(\beta\) and estimated covariance matrix \(C\). Then any function \(g(\beta)\) of \(\beta\), is estimated by \(g(x)\), which is in large samples normally distributed with mean \(g(\beta)\) and estimated variance \(h'Ch\), where \(h\) is the first derivative of \(g(\beta)\) with respect to \(\beta\) evaluated at \(x\). This function returns both \(g(x)\) and its standard error, the square root of the estimated variance.
The default method requires that you provide \(x\) in the argument
object
, \(C\) in the argument vcov.
, and a text expression
in argument g
that when evaluated gives the function \(g\). The
call names(object)
must return the names of the elements of x
that are used in the expression g
.
Since
the delta method is often applied to functions of regression parameter
estimates, the argument object
may be the name of a regression
object from which the the estimates and their estimated variance matrix can
be extracted. In most regression models, estimates are returned by the
coef(object)
and the variance matrix from vcov(object)
.
You can provide an alternative function for computing the sample variance
matrix, for example to use a sandwich estimator.
For mixed models using lme4
or nlme
, the coefficient estimates
are returned by the fixef
function, while for multinom
,
lmList
and nlsList
coefficient estimates are returned by
coef
as a matrix. Methods for these models are provided to get the
correct estimates and variance matrix.
The argument g
must be a quoted character string
that gives the function of interest. For example, if you set
m2 <- lm(Y ~ X1 + X2 + X1:X2)
, then deltaMethod(m2,"X1/X2")
applies the
delta method to the ratio of the coefficient estimates for X1
and
X2
. The argument g
can consist of constants and names
associated with the elements of the vector of coefficient estimates.
In some cases the names may include characters including such as the colon
:
used in interactions, or mathematical symbols like +
or
-
signs that would confuse the
function that computes numerical derivatives, and for this case you can replace
the names of the estimates with the parameterNames
argument. For
example, the ratio of the
X2
main effect to the interaction term could be computed using
deltaMethod(m2, "b1/b3", parameterNames=c("b0", "b1", "b2", "b3"))
.
The name “(Intercept)
” used for the intercept in linear and generalized
linear models is an exception, and it will be correctly interpreted by
deltaMethod
.
For multinom
objects, the coef
function returns a matrix of
coefficients, with each row giving the estimates for comparisons of one category
to the baseline. The deltaMethod
function applies the delta method to
each row of this matrix. Similarly, for lmList
and nlsList
objects, the delta method is computed for each element of the list of
models fit.
For nonlinear regression objects of type nls, the call coef(object)
returns the estimated
coefficient vectors with names corresponding to parameter names.
For example,
m2 <- nls(y ~ theta/(1 + gamma * x), start = list(theta=2, gamma=3))
will
have parameters named c("theta", "gamma")
.
In many other familiar regression methods, such as lm and glm, the names of
the coefficient estimates are the corresponding regressor names, not
parameter names.
For mixed-effects models fit with lmer
and nlmer
from the
lme4
package or lme
and nlme
from the nlme
package,
only fixed-effect coefficients are considered.
For regression models for which methods are not provided, you can extract
the named vector of coefficient estimates and and estimate of its covariance
matrix and then apply the default deltaMethod
function.
Earlier versions of deltaMethod
included an argument
parameterPrefix
that implemented the same functionality as the
parameterNames
argument, but it caused several unintended bugs that
were not easily fixed without the change in syntax.
Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.
Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.
Weisberg, S. (2014) Applied Linear Regression, Fourth Edition, Wiley, Section 6.1.2.
First derivatives of g
are computed using symbolic differentiation
by the function D
.
# NOT RUN {
m1 <- lm(time ~ t1 + t2, data = Transact)
deltaMethod(m1, "b1/b2", parameterNames= paste("b", 0:2, sep=""))
deltaMethod(m1, "t1/t2") # use names of preds. rather than coefs.
deltaMethod(m1, "t1/t2", vcov=hccm) # use hccm function to est. vars.
# to get the SE of 1/intercept, rename coefficients
deltaMethod(m1, "1/b0", parameterNames= paste("b", 0:2, sep=""))
# The next example calls the default method by extracting the
# vector of estimates and covariance matrix explicitly
deltaMethod(coef(m1), "t1/t2", vcov.=vcov(m1))
# the following works:
a <- 5
deltaMethod(m1, "(t1 + a)/t2")
# ...but embedded in a function this will fail
f1 <- function(mod, ...) {
z <- 3
deltaMethod(m1, "(t1+z)/t2", ...)
}
# }
# NOT RUN {
f1(m1)
# }
# NOT RUN {
# if z is defined globally f1 could even return the wrong answer.
# the following function works
f2 <- function(mod, ...) {
deltaMethod(m1, "(t1+z)/t2", ...)
}
f2(m1, constants=c(z=3))
# as does
f3 <- function(mod) {
a <- 3
deltaMethod(m1, "(t1+z)/t2", constants=c(z=a))
}
f3(m1)
# }
Run the code above in your browser using DataLab