by B.K. Sandercock » Sun Dec 02, 2012 5:15 pm
Evan's comments and cautions are well taken. Still, it might be useful for folks to know that the function deltavar() works for all the empirical examples given in Appendix 2 of the Mark Manual on Delta Method. The following R scripts complete the calculations and give exactly the same answers reported for the three examples on pages B-11 through B-19.
I get different answers for example 3 on pages B-20 to B-22, but suspect there are errors in the Appendix. On page B-20, values of the covariate mass are reported as mean = 109.97 with a SD = 24.79. The values for mass-squared are given as mean = 12707.46 and SD = 5532.03, but I think the correct values should be mean = 12093.4 and SD = 614.54. If these corrections are made, the estimates of the parameter and variance come out a little differently.
Appendix 2 was a great set of materials for testing the delta method function, thanks again for putting this resource together. Feel free to add the R scripts to the Appendix if you think they'd be helpful. If you decide to add some parameter estimates to example 2 on reporting rates on page B-14, I can prepare a script for that one too.
Cheers, Brett.
# delta method
# bookkeeping
rm(list = ls())
# need to install package emdbook
library(emdbook)
# Page B-11 Example 1 variance of product of survival probabilities
# period = phi1*phi2*phi3
par.name = c("phi1", "phi2", "phi3")
par.mean = c(phi1=0.6109350, phi2=0.458263, phi3=0.4960239)
par.varcovar = matrix(c(0.0224330125, -0.0003945405, 0.0000654469,
-0.0003945405, 0.0099722201, -0.0002361998,
0.0000654469, -0.0002361998, 0.0072418858), nrow = 3, byrow = T)
(period=prod(par.mean))
(period.var = deltavar(phi1*phi2*phi3, meanval=par.mean, vars=par.name, Sigma=par.varcovar, verbose=T))
# check answers, period=0.138871, var(period) = 0.0025565
# Page B-14 Example 2
# No parameter estimates reported
# Page B-15 Example 3a variance of back-transformed estimates - simple
# phihat = exp(beta)/(1+exp(beta))
par.name = c("beta")
par.mean = c(beta=0.2648275)
par.var = (0.1446688)^2
(phihat=plogis(as.numeric(par.mean)))
(phihat.var = deltavar(exp(beta)/(1+exp(beta)), meanval=par.mean, vars=par.name, Sigma=par.var, verbose=T))
# check answers, phihat=0.5658226, var(phihat) = 0.001263
# Page B-18 Example 3b variance of back-transformed estimates - somewhat harder
# phihat = exp(-1*(int+beta))/(1+exp(-1*(int+beta)))
par.name = c("int", "beta")
par.mean = c(int= 0.4267863, beta=-0.5066372)
par.varcovar = matrix(c(0.0321405326, -0.0321581167,
-0.0321581167, 0.0975720877), nrow = 2, byrow = T)
(phihat=plogis(as.numeric(sum(par.mean))))
(phihat.var = deltavar(exp(-1*(int+beta))/(1+exp(-1*(int+beta))), meanval=par.mean, vars=par.name, Sigma=par.varcovar, verbose=T))
# check answers, phihat=0.48005, var(phihat) = 0.0040742678
# Page B-20 Example 4 variance of back-transformed estimates - a bit harder still
# phihat = exp(-1*(int+beta1+beta2))/(1+exp(-1*(int+beta1+beta2)))
(mass = 110)
(mass.mean = 109.97)
(mass.SD = 24.79)
(mass2 = mass^2)
(mass2.mean = mass.mean^2)
(mass2.SD = mass.SD^2)
(mass.z = (mass - mass.mean)/mass.SD)
(mass2.z = (mass2 - mass2.mean)/mass2.SD)
par.name = c("int", "beta1", "beta2")
par.mean = c(int= 0.256732, beta1=1.1750358*mass.z, beta2=-1.0554864*mass2.z)
par.varcovar = matrix(c(0.0009006921, -0.0004109710, 0.0003662359,
-0.0004109710, 0.0373887267, -0.0364250288,
0.0003662359, -0.0364250288, 0.0362776933), nrow = 3, byrow = T)
(phihat=plogis(as.numeric(sum(par.mean))))
(phihat.var = deltavar(exp(-1*(int+beta1+beta2))/(1+exp(-1*(int+beta1+beta2))), meanval=par.mean, vars=par.name, Sigma=par.varcovar, verbose=T))
# values below may be incorrect
# check answers, mass=110, phihat=0.5925, var(phihat) = 0.00007387
# check answers, mass=120, phihat=(not given), var(phihat) = 0.000074214