I have function like
<code>gmmparametric <- function(theta){
s1 <- etahatparametricmat[,2] - theta[1]*logk[,2] - theta[2]*logm[,2]
s2 <- etahatparametricmat[,1] - theta[1]*logk[,1] - theta[2]*logm[,1]
olspar <- olsmodel$coefficients
implcte <- olspar[1]/(1-2*implomega)
matmoment1 <- logy[,2] - implcte - theta[1]*logk[,2] - theta[2]*logm[,2] -implomega*etahatparametricmat[,1] + implomega*(implcte + theta[1]*logk[,1] + theta[2]*logm[,1])
matmoment2 <- logy[,3] - implcte - theta[1]*logk[,3] - theta[2]*logm[,3] - implomega*etahatparametricmat[,2] + implomega*(implcte + theta[1]*logk[,2] + theta[2]*logm[,2])
matmoment <- cbind(matinst1*matmoment1, matinst2*matmoment2)
meanmatmoment <- colSums(matmoment)/n
objectfunc <- t(meanmatmoment)%*%diag(8)%*%meanmatmoment
<code>gmmparametric <- function(theta){
s1 <- etahatparametricmat[,2] - theta[1]*logk[,2] - theta[2]*logm[,2]
s2 <- etahatparametricmat[,1] - theta[1]*logk[,1] - theta[2]*logm[,1]
olsmodel <- lm(s1 ~ s2)
olspar <- olsmodel$coefficients
implomega <- olspar[2]
implcte <- olspar[1]/(1-2*implomega)
matmoment1 <- logy[,2] - implcte - theta[1]*logk[,2] - theta[2]*logm[,2] -implomega*etahatparametricmat[,1] + implomega*(implcte + theta[1]*logk[,1] + theta[2]*logm[,1])
matmoment2 <- logy[,3] - implcte - theta[1]*logk[,3] - theta[2]*logm[,3] - implomega*etahatparametricmat[,2] + implomega*(implcte + theta[1]*logk[,2] + theta[2]*logm[,2])
matmoment <- cbind(matinst1*matmoment1, matinst2*matmoment2)
meanmatmoment <- colSums(matmoment)/n
objectfunc <- t(meanmatmoment)%*%diag(8)%*%meanmatmoment
objectfunc
}
</code>
gmmparametric <- function(theta){
s1 <- etahatparametricmat[,2] - theta[1]*logk[,2] - theta[2]*logm[,2]
s2 <- etahatparametricmat[,1] - theta[1]*logk[,1] - theta[2]*logm[,1]
olsmodel <- lm(s1 ~ s2)
olspar <- olsmodel$coefficients
implomega <- olspar[2]
implcte <- olspar[1]/(1-2*implomega)
matmoment1 <- logy[,2] - implcte - theta[1]*logk[,2] - theta[2]*logm[,2] -implomega*etahatparametricmat[,1] + implomega*(implcte + theta[1]*logk[,1] + theta[2]*logm[,1])
matmoment2 <- logy[,3] - implcte - theta[1]*logk[,3] - theta[2]*logm[,3] - implomega*etahatparametricmat[,2] + implomega*(implcte + theta[1]*logk[,2] + theta[2]*logm[,2])
matmoment <- cbind(matinst1*matmoment1, matinst2*matmoment2)
meanmatmoment <- colSums(matmoment)/n
objectfunc <- t(meanmatmoment)%*%diag(8)%*%meanmatmoment
objectfunc
}
The function is essentially computing a quadratic expression or a GMM objective function-like, but before do it, it “concentrates out” two parameters that can be obtained from an OLS problem, which depends on the variable theta
, the unknown that I want to determine by minimizing gmmparametric
. I want to minimize gmmparametric
using CVXR.
My intuition was to do something like
<code>theta <- Variable(2)
object <- Minimize(gmmparametric(theta))
<code>theta <- Variable(2)
object <- Minimize(gmmparametric(theta))
prob <- Problem(object)
prob.solve()
</code>
theta <- Variable(2)
object <- Minimize(gmmparametric(theta))
prob <- Problem(object)
prob.solve()
But I get the error
<code>Error in model.frame.default(formula = s1 ~ s2, drop.unused.levels = TRUE) :
<code>Error in model.frame.default(formula = s1 ~ s2, drop.unused.levels = TRUE) :
object is not a matrix
</code>
Error in model.frame.default(formula = s1 ~ s2, drop.unused.levels = TRUE) :
object is not a matrix
which I believe comes from the fact that OLS cannot be performed inside gmmparametric
as theta
is not an actual vector. So I was wondering if there is a way such that I can minimize gmmparametric
using CVXR in this setting. I can use optim
straightforwardly, but I do not how to proceed with CVXR.