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]

Re: st: do loops and mata


From   "Thomas, Anthony" <[email protected]>
To   [email protected]
Subject   Re: st: do loops and mata
Date   Thu, 13 Mar 2014 11:59:40 -0400

It seemed like the "Pi" were the matrices Jonathan wanted to store
(since he wanted them named P1- P39) and I thought "Pi" was an attempt
to do P`i' as one would in Stata, but the principle is the same either
way.

Thanks,

Anthony

On Thu, Mar 13, 2014 at 11:38 AM, Schaffer, Mark E
<[email protected]> wrote:
> Jonathan, Anthony,
>
> I think there might be a typo or 2 in Anthony's code (e.g., "M[i] = &P") but I agree with Anthony that this is the way to go.
>
> Below his code but with (I think) the typos fixed.  I also changed M to a column vector (easier to read).
>
> --Mark
>
> mata
>
> P = st_data( ., ("ee", "eu" ,"ue", "uu") )
>
> M = J(rows(P), 1, NULL)
>
>  for ( i=1; i<=rows(P) ;i++) {
>
>  Pi = P[i,.]
>  xi = rowshape(Pi,2)
> // store xi in M
>  M[i] = &xi
>
>  }
>
> // look at M
> M
> // not useful - just a bunch of memory junk
>
> // get 10th matrix
> *M[10]
>
> end
>
>> -----Original Message-----
>> From: [email protected] [mailto:owner-
>> [email protected]] On Behalf Of Thomas, Anthony
>> Sent: 13 March 2014 15:18
>> To: [email protected]
>> Subject: Re: st: do loops and mata
>>
>> Hi Jonathan,
>>
>> You could also try putting each matrix in an array in Mata. They
>> wouldn't have a nice naming system like Stata matrices do, but then
>> your code could be contained to Mata. Note that as Nick points out,
>> this may be needlessly complicated, but if you're interested in a Mata
>> only solution this is what I would do.
>>
>> **************** CODE ****************
>>
>> P = st_data( ., ("ee", "eu" ,"ue", "uu") ) P
>>
>> // assuming P has 39 rows...?
>> M = J(1, 39, NULL)
>>
>>  for ( i=1; i<=rows(P) ;i++) {
>>
>>  P= P[i,.]
>>  P
>> // store P in M
>>  M[i] = &P
>>
>>  xi=rowshape( Pi, 2)
>>  xi
>>
>>  }
>>
>> ****************** END ****************
>>
>> The matrix M holds pointers to the 39 smaller matrices P1 - P39. These
>> pointers tell the computer where the 39 small matrices are stored in
>> the system's memory. Then you can ask Mata to retrieve one of the
>> matrices by doing the following:
>>
>> // look at M
>> M
>> // not useful - just a bunch of memory junk
>>
>> // get 34th matrix
>> *M[34]
>>
>> Others may have a better solution, but this is just what came to mind.
>> Arrays are cool in general and come in handy in a variety of
>> situations.
>>
>> Anthony
>>
>>
>> On Thu, Mar 13, 2014 at 10:36 AM,  <[email protected]> wrote:
>> > Thanks again Nick. I'll persevere using the old command structure
>> >
>> > The reason I need the sequence of matrices is that my colleagues and I are
>> writing a paper that involves decomposing the contribution of labour market
>> transition probabilities to changes in the labour market stocks. After some
>> algebra this essentially involves multiplying the sequence of transition
>> probability matrices from t= 1 to t=t. So I need to refer specifically to each
>> transition matrix at each period t and loop this because the calculation will be
>> different at each t (involve a different set of matrices)
>> >
>> > Hope that makes things a little more transparent. Let me know if mata ever
>> gets macros
>> >
>> > Jonathan
>> >
>> > -----Original Message-----
>> > From: [email protected] [mailto:owner-
>> [email protected]] On Behalf Of Nick Cox
>> > Sent: 13 March 2014 14:10
>> > To: [email protected]
>> > Subject: Re: st: do loops and mata
>> >
>> > The short answer is that there is no exact Stata equivalent, because Mata as
>> such does not have macros. But although you did not specify an exact Mata
>> equivalent, I guess the longer answer would depend on what you want to do
>> with these matrices. It could be that you need something deeper, e.g. pointers
>> or structures.
>> >
>> > Indeed, although it's always fair just to ask about language details as a matter
>> of curiosity, I remain very puzzled why you want to do this. I've never wanted to
>> do anything like this in Mata: that means no more than it says, and can be put
>> down to narrowness of my experience, but it's why I am puzzled.
>> > Nick
>> > [email protected]
>> >
>> >
>> > On 13 March 2014 13:48,  <[email protected]> wrote:
>> >> Dear Nick
>> >>
>> >> Thanks for the quick response. The commands is indeed almost
>> >> equivalent to vec (except I need to go from a 39X4 matrix to 39  2 by
>> >> 2 matrices rather than a column vector ) but I still need a loop to
>> >> extract the 39 separate matrices and label them differently
>> >>
>> >> I can do this in the old matrix sequence using the commands
>> >>
>> >> mkmat ee eu ue uu, matrix(P)
>> >> matrix list P
>> >>
>> >> local i=1
>> >> while `i'<=rowsof(P) {
>> >> matrix P`i'= P[`i',1...]        /* loops to give 39 different 1 by 4 matrices of
>> transition probabilities */
>> >> local i=`i'+1
>> >> }
>> >>
>> >> matrix list P1
>> >> matrix list P39
>> >>
>> >> but I was interested in doing the mata equivalent
>> >>
>> >> thanks again
>> >>
>> >> Jonathan Wadsworth
>> >>
>> >>
>> >> -----Original Message-----
>> >> From: [email protected]
>> >> [mailto:[email protected]] On Behalf Of Nick Cox
>> >> Sent: 13 March 2014 12:49
>> >> To: [email protected]
>> >> Subject: Re: st: do loops and mata
>> >>
>> >> This looks like a way of reinventing -stack-. Am I wrong?
>> >>
>> >> Nick
>> >> [email protected]
>> >>
>> >>
>> >> On 13 March 2014 12:34,  <[email protected]> wrote:
>> >>
>> >>> As a novice to mata I'm trying to generate a set of 39 matrices using
>> >>> a loop - essentially extracting each row of a larger matrix P
>> >>> sequentially and then transforming the vector into a matrix using the
>> >>> rowshape command
>> >>>
>> >>> I've got as far as this - which does indeed scroll through a matrix P
>> >>> 39 times and extract a row at a time and writes to a mtrix Pi, but
>> >>> then it overwrites the matrix Pi each time so that I'm left with just
>> >>> one matrix called Pi at the end of the loop instead of 39 matrics
>> >>> called P1--P39
>> >>>
>> >>> Does anyone know how to adapt the code below to generate and store 39
>> different matrices named p1, p2, ..p39 ?
>> >>
>> >>> mata
>> >>>
>> >>>
>> >>> P = st_data( ., ("ee", "eu" ,"ue", "uu") ) P
>> >>>
>> >>> for ( i=1; i<=rows(P) ;i++) {
>> >>>
>> >>> Pi= P[i,.]
>> >>> Pi
>> >>>
>> >>> xi=rowshape( Pi, 2)
>> >>> xi
>> >>>
>> >>> }
>> >>>
>> >>> end
>> >>
>> >> *
>> >> *   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/
>> >>
>> >> Please access the attached hyperlink for an important electronic
>> >> communications disclaimer: http://lse.ac.uk/emailDisclaimer
>> >>
>> >> *
>> >> *   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/
>> >
>> > Please access the attached hyperlink for an important electronic
>> communications disclaimer: http://lse.ac.uk/emailDisclaimer
>> >
>> > *
>> > *   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/
>
>
> -----
> Sunday Times Scottish University of the Year 2011-2013
> Top in the UK for student experience
> Fourth university in the UK and top in Scotland (National Student Survey 2012)
>
> We invite research leaders and ambitious early career researchers to
> join us in leading and driving research in key inter-disciplinary themes.
> Please see www.hw.ac.uk/researchleaders for further information and how
> to apply.
>
> Heriot-Watt University is a Scottish charity
> registered under charity number SC000278.
>
>
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index