I would like to estimate the parameter 'r' using GMM, based on a rolling data set [1:2567],[2:2568]... and so on. Finally, the results are to be filled in a matrix. I had already tested the core codes as follows, which yield valid answers.
x<-Source[1 : 2567,"AAA"]
z<-as.matrix(x)
e<-function(r,x){
+ m<-exp(-x/r)-1
+ return(m)
+ }
coef(gmm(e,z,t0=1,method="BFGS",control=1e-12))
Theta[1]
1.096466
x<-Source[2 : 2568,"AAA"]
z<-as.matrix(x)
e<-function(r,x){
+ m<-exp(-x/r)-1
+ return(m)
+ }
coef(gmm(e,z,t0=1,method="BFGS",control=1e-12))
Theta[1]
1.102329
However, when I want to do so using a loop structure, it returns with an error message.
n <- 2
result <- matrix(rep(0, n), nrow = n)
for(i in c(1 :n)){
+ x<-Source[i : i + 2566,"AAA"]
+ z<-as.matrix(x)
+ e<-function(r,x){
+ m<-exp(-x/r)-1
+ return(m)
+ }
+
+ result[i,1] <- coef(gmm(e,z,t0=1,method="BFGS",control=1e-12))
+ }
Error in ar.ols(x, aic = aic, order.max = order.max, na.action = na.action, : 'order.max' must be < 'n.used'
Could anyone help me figure out what had gone wrong? Many thanks~!
(*The data set "Source" is a 5200*4 matrix, with actual data shown in the link below: https://docs.google.com/spreadsheets/d/1AnTErQd2jm9ttKDZa7On3DLzEZUWaz5Km3nKaB7K18o/edit#gid=0)