I have run across a problem when using moptimize() with a subview that contains factor variables. Here's a quick program that can be used as as an example:
If I run the following code, Stata will completely crash -- killing the Stata.exe process in Windows. This is not simply a matter of Stata returning an error code. It actually crashes.
This code is supposed to reproduce the coefficients from logit. The invsym() line produces the coefficients from regress, and demonstrates that the subviews (X and Y) are fine. That is, there is no problem with the way the subviews are created, but instead is a problem with how moptimize() uses these subviews.
However, I don't have any problem if X is a view (instead of being a subview):
Furthermore, I don't have any problem if X is a subview without factor variables:
Does anyone have any idea on how to make this work? Is the problem on my end, or a problem with the compiler? Thanks so much for the help!
Best,
Keith
In case it's helpful, I'm using the latest version of Stata 15.1 MP for Windows:
Code:
mata: void mylogit(real colvector Ymat, real matrix Xmat) { transmorphic S S=moptimize_init() moptimize_init_evaluator(S, &logit_eval()) moptimize_init_evaluatortype(S,"lf") moptimize_init_depvar(S,1,Ymat) moptimize_init_eq_indepvars(S,1,Xmat) moptimize_init_eq_colnames(S, 1, (J(1,cols(Xmat),"x") + strofreal((1..cols(Xmat))))) // (another problem, which might be related, is that moptimize throws an error message if X includes factor variables; this is a quick hack) moptimize(S) moptimize_result_display(S) } void logit_eval(transmorphic S, real rowvector beta, real colvector lnf) { real colvector Y, pm, xb, lj Y = moptimize_util_depvar(S, 1) xb = moptimize_util_xb(S, beta, 1) pm = 2*(Y :!= 0) :- 1 lj = invlogit(pm:*xb) lnf = ln(lj) } end
Code:
sysuse auto gen vlong = (length>150) mata: Y=X=dta=. st_view(dta, ., "foreign price weight 0b0.vlong 1b0.vlong") st_subview(Y, dta, ., 1 ) st_subview(X, dta, ., 2\. ) invsym(quadcross(X, 1, X, 1))*quadcross(X, 1, Y, 0) mylogit(Y,X) "If you can see this, Stata did not crash" end
However, I don't have any problem if X is a view (instead of being a subview):
Code:
mata: Y=X=. st_view(Y, ., "foreign") st_view(X, ., "price weight 0b0.vlong 1b0.vlong") mylogit(Y,X) end
Code:
mata: Y=X=dta=. st_view(dta, ., "foreign price weight") st_subview(Y, dta, ., 1 ) st_subview(X, dta, ., 2\. ) mylogit(Y,X) end
Best,
Keith
In case it's helpful, I'm using the latest version of Stata 15.1 MP for Windows:
Code:
c(stata_version) = 15.1 c(version) = 15.1 (version) c(userversion) = 15.1 (version) c(dyndoc_version) = 1 (dyndoc) ----------------------------------------------------------------------------------------------------------- c(born_date) = "30 Jan 2018" c(flavor) = "IC" c(bit) = 64 c(SE) = 1 c(MP) = 1 c(processors) = 4 (Stata/MP, set processors) c(processors_lic) = 4 c(processors_mach) = 8 c(processors_max) = 4 c(mode) = "" c(console) = "" ----------------------------------------------------------------------------------------------------------- c(os) = "Windows" c(osdtl) = "64-bit" c(hostname) = "KKranker" c(machine_type) = "PC (64-bit x86-64)" c(byteorder) = "lohi"
Comment