n = 500
phi<-c(.2,.7)
thet <- 0
sd <- .2
x <- matrix(arima.sim(n=n,list(order=c(2,0,1),ar=phi,ma=thet,sd=sd)),ncol=1)
g <- function(tet,x)
{
n <- nrow(x)
u <- (x[7:n] - tet[1] - tet[2]*x[6:(n-1)] - tet[3]*x[5:(n-2)])
f <- cbind(u,u*x[5:(n-2)],u*x[4:(n-3)],u*x[3:(n-4)])
#f <- cbind(u,u*x[5:(n-2)],u*x[4:(n-3)],u*x[3:(n-4)],u*x[2:(n-5)],u*x[1:(n-6)])
#f <- cbind(u,u*x[4:(n-3)],u*x[3:(n-4)],u*x[2:(n-5)],u*x[1:(n-6)])
return(f)
}
Dg <- function(tet,x)
{
n <- nrow(x)
xx <- cbind(rep(1,(n-6)),x[6:(n-1)],x[5:(n-2)])
H <- cbind(rep(1,(n-6)),x[5:(n-2)],x[4:(n-3)],x[3:(n-4)])
# H <- cbind(rep(1,(n-6)),x[5:(n-2)],x[4:(n-3)],x[3:(n-4)],x[2:(n-5)],x[1:(n-6)])
#H <- cbind(rep(1,(n-5)),x[4:(n-3)],x[3:(n-4)],x[2:(n-5)],x[1:(n-6)])
f <- -crossprod(H,xx)/(n-6)
return(f)
}
resgmm <- gmm(g,c(0,.3,.6),x,grad=Dg)
summary(resgmm)
Run the code above in your browser using DataLab