[R] variance of combinations of means - off topic
Kjetil Brinchmann Halvorsen
kjetil at acelerate.com
Thu Jan 6 01:41:06 CET 2005
Liaw, Andy wrote:
>You could try googling for "delta method". I believe MASS even has code for
>that...
>
>Andy
>
>
>
also,
help.search("delta")
does give nothing usefull, so if it is in MASS it would be hidden, and
need a \concept
entry in the .Rd file.
The delta method is really nothimg more (or less) than a linear
Taylor approximation.
The following is a very fast implementation, which surely can be bettered:
se.delta <- function(expr, namevec, theta, sigma) {
# theta is means, or more generally, estimates,
# sigma is vector or matrix containing (standard errors)^2 and (if
matrix)
# covariances. If vector must be same length as theta, and we assume no
# correlation between estimates theta.
# expr must be a formula defining a function
# taking as many arguments as length of theta.
# namevec is a character vector containing the symbols used in the
formula.
p <- length(theta)
if (!is.matrix(sigma)) sigma <- diag(sigma)
if(!(NROW(sigma)==p)) stop("sigma and theta must have appropiate
dimension")
fun <- deriv(expr, namevec, TRUE)
derivs <- do.call("fun", as.list(theta))
derivs <- as.vector(attr(derivs, "gradient"))
vartheta <- derivs %*% sigma %*% derivs
setheta <- sqrt(vartheta)
setheta
}
example:
se.delta(~ x^2 + y^2, c("x", "y"), c(0.5, 1), c(0.001, 0.001))
[,1]
[1,] 0.07071068
(there must be possible to calculate the namevec argument from the
formula argument, but I have no time
for that now)
Kjetil
>>From: Bill Shipley
>>
>>Hello, and please excuse this off-topic question, but I have not been
>>able to find an answer elsewhere. Consider a value Z that is
>>calculated
>>using the product (or ratio) of two means X_mean and Y_mean:
>>Z=X_mean*Y_mean. More generally, Z=f(X_mean, Y_mean). The standard
>>error of Z will be a function of the standard errors of the means of X
>>and Y. I want to calculate this se of Z. Can someone direct me to a
>>reference (text book or other) that gives the solution to
>>this *general*
>>problem?
>>
>>Thanks.
>>
>>
>>
>>Bill Shipley
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide!
>>http://www.R-project.org/posting-guide.html
>>
>>
>>
>>
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
>
>
--
Kjetil Halvorsen.
Peace is the most effective weapon of mass construction.
-- Mahdi Elmandjra
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
More information about the R-help
mailing list