You can do it this way:
gmmobjnoinst.make <- function(delta.ini){
delta0 <- delta.ini
gmmobjnoints <- function(theta2){
deltanew <- innerloop(theta2,delta0) #the thing using the input (delta0)
delta0 <<- deltanew #the thing I want to be saved for next iteration
theta1hat <- solve(t(x1)%*%x1)%*%t(X)%*%deltanew
w <- deltanew-x1%*%theta1hat
gmmobj <- t(w)%*%w
return(gmmobj)
}
return(gmmobjnoints)
}
gmmobjnoinst <- mgmmobjnoinst.make(0)
This way delta0
becames an static
variable.