I am minimizing this function below using the optim function, which works really well. My only problem is that I can't save the W matrix, I am computing inside the function when minimizing. Is there a way to save the W matrix somehow?
W<-c()
GMM_1_stage <- function(beta) {for (i in 1:(nrow(gmm_i))){
gmm_i[i,]=g_beta(i,beta)}
gmm_N=t(colSums(gmm_i))%*%colSums(gmm_i)
W<-solve((1/(nrow(A)/5))*t(gmm_i)%*%gmm_i)
return(gmm_N)
}
GMM_1<-optim(beta_MLE,GMM_1_stage)
Best regards