Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: trouble with passing a global macro to stata() in a Mata function


From   Limin Fang <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: trouble with passing a global macro to stata() in a Mata function
Date   Thu, 29 Aug 2013 16:07:42 +0000

Dear Statalist users,

I am writing a Stata program that involves automatically generating a list of temporary variable names, which I put in a global macro called "stubnames."   I then need to pass this "stubnames" and a number of matrices to a Mata function that I designed. In the Mata function, I also need to return to Stata environment in order to use its built-in command such as "reshape" and "asclogit."  I use the function "state()" to implement these commands. Since both "reshape" and "asclogit" require the input of variable names that I stored in the global macro "stubnames,"  I want to write something like stata("reshape long $stubnames, i(id) j(alternative)"), but stata() is not recognizing the global macro $stubnames, although once I finish running the program and type in "macro dir," the global macro "stubnames" is listed. 

I don't quite understand why state() within my Mata function cannot recognize the global macro "stubnames". I should also add that if I run the program again, state() does pick up the global macro "stubnames" that has already been defined in the first run of the program. Would anyone kindly explain to me why this is happening? Also could anyone help me solve this problem by any chance?

I am including an abridged version of my code if this helps:

program nplsing, rclass
	version 10
	syntax varlist [if] [in], NPAR(real) [ITERation(real 5) ID(varname numeric) NChoice(real 2) ///
		NUMX(real 2) Bdisc(real 0.99) NAMES(string)]
	…… 

	forvalues i = 1/`npar'{ 
		local stubnames "`stubnames' z`i'_ " 
		} 
	global stubnames "`stubnames' e"
	…… 
	
	mata: nplsing("`zevarlist'", "zmat", "pini", "fmat", `bdisc', `iteration', "`touse'")
	…… 
end

version 10
mata:
void nplsing(string scalar tempvarlist, ///
					 string scalar zmatname, ///
					 string scalar pininame, ///
					 string scalar fmatname, ///
					 real scalar bdisc, ///
					 real scalar iterlim, ///
					 string scalar touse)
{
	…….
	while (convcrit >= 1e-6){
		…… 
		stata("preserve")
		…… 
		stata("reshape long $stubnames, i(id) j(alternative)")
		……
		stata("asclogit chosen $stubnames, case(id) alternative(alternative) noconstant constraint(1) shownrtolerance tol(1e-10)")
		…… 
		stata("restore")
		…… 
		convcrit = colmax(abs(tetaest1 - tetaest0))
		…… 
	}

}
end

In case you suggest me breaking the mata function into pieces and return back to Stata environment to implement "reshape " and "asclogit," I would say that I'd rather keep this nplsing Mata function as one piece because of the loop "while (convcrit >= 1e-6)." I find it rather tedious to send matrices back and forth between Mata and Stata. More importantly I would have to create a local macro "convcrit" in Stata for the stopping rule in "while"and update it constantly with a value of a scalar that I receive from the Mata function, and I don't quite know how to do that just yet in Stata.    

Nevertheless, any advice in helping me solve my problem is welcome. Thank you very much in advance!

Limin Fang
Department of Economics 
University of Toronto



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index