Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm wanting to compute the standard errors of parameters estimated through optim via bootstrapping. The aim is to bootstrap the data used as the input for the optim function.

The initial optim function is given as

f = function (theta) {

A = rbind (c(1,theta[1], theta[2]), c(theta[3],1,theta[4]), c(theta[5],theta[6],1))
S1 = diag ( c(theta[7],theta[8],theta[9]) )
S2 = diag ( c(theta[10],theta[11],theta[12]) )
S3 = diag ( c(theta[13],theta[14],theta[15]) )
S4 = diag ( c(theta[16],theta[17],theta[18]) )  

M1 <-   A %*% C1 %*% t(A) - S1 
M2 <-   A %*% C2 %*% t(A) - S2 
M3 <-   A %*% C3 %*% t(A) - S3 
M4 <-   A %*% C4 %*% t(A) - S4 

mm =  sum(as.vector(M1)*(T1/T), as.vector(M2)*(T2/T),
     as.vector(M3)*(T3/T), as.vector(M4)*(T4/T))^2
 return (mm)     
}
result = optim (rep(0,18), fn=f)

Where C1 etc is a covariance matrix i wish to create via bootstrapping. The code for bootstrapping using boot is given below.

bootresid=cbindX(steady.resid,dairy.resid,nzdusd.resid,interest.resid)

bootfun = function(bootresid, i) {

C1 = cov(bootresid[i,1:3],use = "na.or.complete")
C2 = cov(bootresid[i,4:6],use = "na.or.complete")
C3 = cov(bootresid[i,7:9],use = "na.or.complete")
C4 = cov(bootresid[i,10:12],use = "na.or.complete")

ans = optim (rep(0,18), fn = f)
return(ans$par)
}

bootres = boot(bootresid, statistic = bootfun, 500)

This returns an error of unused arguments within the optim function. Is it possible to use optim within the boot function? Do i need to make changes to the way the optim function is defined?

EDIT: As Vincent suggested, additional arguments must be passed through the initial function. Importantly for this problem, an additional indices must be given to ensure optim is repeated with different data. The final solution is.

bootfun = function(bootresid, i, d, C1, C2, C3, C4) {

C1 = cov(bootresid[i,1:3],use = "na.or.complete")
C2 = cov(bootresid[i,4:6],use = "na.or.complete")
C3 = cov(bootresid[i,7:9],use = "na.or.complete")
C4 = cov(bootresid[i,10:12],use = "na.or.complete")

ans = optim (rep(0,18), fn=f)

return(ans$par [d])
}

bootres = boot(bootresid, statistic = bootfun, 500)
share|improve this question
C1, C2, C3, C4 should be passed as arguments -- otherwise, f will look for them in the environment in which it was defined, i.e., the top-level environment, not the body of the bootfun function. – Vincent Zoonekynd May 24 at 6:33
Thanks @Vincent. This and including another argument for repeating the optim function has given the desired result. – LoganH May 25 at 2:57

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.