2

我想计算optim通过引导估计的参数的标准误差。目的是引导用作optim函数输入的数据。

初始优化函数为

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)

其中 C1 等是我希望通过引导创建的协方差矩阵。下面给出了引导使用的代码boot

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)

这将返回函数中未使用参数的错误optim。是否可以optim在引导功能中使用?我需要更改optim函数的定义方式吗?

编辑:正如文森特所建议的,附加参数必须通过初始函数传递。对于这个问题,重要的是,必须给出一个额外的索引以确保optim重复使用不同的数据。最后的解决办法是。

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)
4

0 回答 0