Dear all,
I am trying to translate a SAS code into Stata using Mata. Part of the code implements a probit model where the gradient and Hessian are calculated by hand. I am using Stata 14.2, and the following is my code used on the auto dataset. I know I shouldn't be hardcoding a command that is always available in Stata, but this is part of a bigger code that I want to change from SAS to Stata, and I am new to Mata.
"
Executing the code ends with "3200 conformability error". Help appreciated.
The formulas for the gradient and Hessian are based on Wooldridge (2002, p. 393).
Wooldridge, J.M. (2002). Econometric Analysis of Cross Section and Panel Data. The MIT Press
I am trying to translate a SAS code into Stata using Mata. Part of the code implements a probit model where the gradient and Hessian are calculated by hand. I am using Stata 14.2, and the following is my code used on the auto dataset. I know I shouldn't be hardcoding a command that is always available in Stata, but this is part of a bigger code that I want to change from SAS to Stata, and I am new to Mata.
"
Executing the code ends with "3200 conformability error". Help appreciated.
Code:
sysuse auto, clear probit foreign price mpg weight *De fine y and x generate cons = 1 local y foreign local xlist price mpg weight cons mata st_view(y=., ., "`y'") // read in stata data to y and X st_view(X=., ., tokens("`xlist'")) b = J(cols(X),1,0) // compute starting values n = rows(X) iter = 1 // initialize number of iterations cha = 1 // initialize stopping criterion do { //real vector r, f, xb //real matrix df xb = X*b f = normal(xb) d = normalden(xb) r1 = y - f r2 = 1 - f df = normalden(xb):*X' grad_numer = df:*r1 grad_denom = f:*r2 grad = grad_numer/grad_denom // kx1 gradient vector hes = (d:^2)*cross(X',*X)/grad_denom // negative of the kxk hessian matrix bold = b b = bold + cholinv(hes)*(grad) cha = (bold-b)'(bold-b)/(b'b) iter = iter + 1 } while (cha > 1e-16) // end of iteration loops end
Wooldridge, J.M. (2002). Econometric Analysis of Cross Section and Panel Data. The MIT Press
Comment