Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: mata programming - input type


From   Tomáš Houška <xbender@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: mata programming - input type
Date   Thu, 21 Mar 2013 19:53:39 +0100

Hello Nick,

thank you for your help. I have been surprised by your answer as I
have met several discussions on Statalist talking about how Mata
cannot be processed within Stata loop. I have tried your proposal but
I am running into a problem comparable to the problem described in the
above mentioned discussions.

Even in a very trivial example, in which I just want to list the macro
carried over from Stata to Mata:

levelsof time_period, local(period_id)
	
foreach period of local period_id {
	
	mata
	mata_period = st_local("period")
	mata_period			
        end
}

... I get an error "-Break-  r(1); " and the values of mata_period are
not shown. I expect this to be a comparable problem as e.g.
http://www.stata.com/statalist/archive/2007-07/msg00834.html
A solution to that would be establishing a mata function (taking
"time_period" as an argument) that I will call within the stata loop,
Or am I missing something?

Thank you ,
Tomas




2013/3/21 Nick Cox <njcoxstata@gmail.com>:
> It's just not true that Mata cannot be processed within a Stata loop,
> but that does not seem to be the problem here.
>
> The lack of macros in Mata poses challenges that are more evident than
> real to many programmers who know a lot of Stata. The question was
> addressed in
>
> SJ-11-2 pr0052  . . . . Stata tip 100: Mata and the case of the missing macros
>         . . . . . . . . . . . . . . . . . . . . . . . . W. Gould and N. J. Cox
>         Q2/11   SJ 11(2):323--324                                (no commands)
>         tip showing how to do the equivalent of Stata's macro
>         substitution in Mata
>
> and the gnomic answer is to solve the problem by avoiding it.
>
> In the Tip above the main contribution of the junior author (meaning,
> younger by just about 2 months) was to add a Perry Mason-type title to
> the work of the senior author.
>
> One good method is to pass macros once to Mata and thereafter to use
> Mata's string operators and functions.
>
> Try this:
>
> levelsof time_period, local(period_id)
>
> foreach period of local period_id {
>        mata
>        //import STATA matrices
>        period = st_local("period")
>        EPSILON = st_matrix("EPSILON_M" + period)
>        PRICE   = st_matrix("PRICE_M" + period)
>        ...
>        end
> }
>
> So,
>
> 1. -period- in Mata is just a string scalar containing the same same
> contents as a Stata local macro.
>
> 2. Adding that string scalar to given text is quite enough technique
> for what you want.
>
> 3. More complicated manipulations just need e.g. string functions in Mata.
>
> Nick
>
> On Wed, Mar 20, 2013 at 10:16 PM, Tomáš Houška <xbender@gmail.com> wrote:
>
>> I am trying to write this loop (here I give an example in "stata
>> language", but I am aware that this will never work as mata cannot be
>> processed within Stata loop):
>>
>> Example:
>>
>> levelsof time_period, local(period_id)
>>
>> foreach period of local period_id {
>> mata
>>         //import STATA matrices
>>         EPSILON = st_matrix("EPSILON_M`period'")
>>         PRICE   = st_matrix("PRICE_M`period'")
>>         SHARE   = st_matrix("SHARE_M`period'")
>>
>>         BETA = EPSILON :* ( (SHARE *(1:/PRICE)') )
>>
>>        st_matrix("BETA`period'",BETA)
>>
>> end
>> }
>>
>> Which means I have several matrices, each marked by period number and
>> I am trying to perform some operations on them (here I give a
>> simplified example of my code). I would like to create a mata function
>> that would take "period_id" as an input (period_id is always in
>> integer) and process the mata code, resulting in creating new matrix
>> BETA`period_id'.
>>
>> So far I have been able to come up with this:
>> Example 2:
>>
>> mata
>> mata clear
>> void mc_estim(string scalar period) {
>>         //import STATA matrices
>>         EPSILON = st_matrix("EPSILON_M`period'")
>>         PRICE   = st_matrix("PRICE_M`period'")
>>         SHARE   = st_matrix("SHARE_M`period'")
>>
>>         BETA = EPSILON :* ( (SHARE *(1:/PRICE)') )
>>
>>         st_matrix("BETA`period'",BETA)
>>         }
>> end
>> * run mata function with a selected input
>> mata mc_estim(534)
>>
>> But I am not sure how to use the "input" in the mata function code (I
>> expect that the notation is different from Statas local macros....) I
>> would appreciate any help on this.
>
> *
> *   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index