Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop for selecting rolling rows of matrix

    Hi all,

    I am working on a program that bootstrapping some estimations for 100 times. This program generates a matrix with 100 rows and 2 columns.

    pc_w_total =

    1 2
    4 5
    7 8
    10 11
    13 14
    16 17
    ...
    ...

    I want to extract 3 rows and 1 column at a time to form a new matrix – each with a different name. The expected result would be the following matrices:

    pc_w_total_1 =

    1
    4
    7

    pc_w_total_2 =

    10
    13
    16

    and so on.

    Instead of writing 33 matselrc commands, such as:

    matselrc pc_w_total pc_w_total_1, r(1/3) c(1)
    matselrc pc_w_total pc_w_total_2, r(4/6) c(1)
    matselrc pc_w_total pc_w_total_3, r(7/9) c(1)
    matselrc pc_w_total pc_w_total_4, r(10/12) c(1)
    ….
    matselrc pc_w_total pc_w_total_32, r(94/96) c(1)
    matselrc pc_w_total pc_w_total_33, r(97/100) c(1)

    I need to create a loop that will do the task.

    Anyway I can do this? Thanks.



    Amy Liu
    Australian National University
    [email protected]


  • #2
    Hi Amy,

    In future do please share data on Statalist using dataex (ssc install dataex). It provides a convenient way for other members to work on your problem and helps us offer you much better solutions.

    I've tested this code, which broadly does what you want

    Code:
    local start 1
    
    forvalues suff = 1/33{
        forvalues i = `start''(3)`=`start'+2'{
            di in red "loop or suffix equals `suff'"
            di in text "start equals `start'"
            di in text "start + 2 equals `=`start'+2'"
            local start `=`start'+3'
        }
    }
    I have tried to adapt it for your specific problem below, but as you have not shared any data that I can easily bring in to Stata, I have not been able to test it (read: it may have a typo / be a bit bug-y)

    Code:
    local start 1
    
    forvalues suff = 1/33{
        forvalues i = `start'(3)`=`start'+2'{
            matselrc pc_w_total pc_w_total_1, r(`start'/`=`start'+2') c(`suff')
            local start `=`start'+3'
        }
    } //of course change the 33 on the outer loop if in fact there are more matrices you end up wanting to make
    Last edited by Chris Larkin; 07 Feb 2017, 18:33.

    Comment


    • #3
      I don't think Chris Larkin's code does what you want. Try this:

      Code:
      forvalues i = 1/32 {
          local low = 3*`i'-2
          local hi = 3*`i'
          matserlc pc_w_total pc_w_total_`i', r(`low'/`hi') c(1)
      }
      masterlc pc_w_total pc_w_total_33, r(97/100) c(1)
      The final -masterlc- has to be done separately because it does not fit the general pattern: the last one goes from 97 to 100, which is four rows, not three. While it would be possible to contrive a more complicated loop that handles that case differently, the code would be longer and it would be very opaque. So better to just take that one exceptional case outside the loop.

      A note about posting: -masterlc- is not an official Stata command. In this case, it isn't necessary to know anything about it to answer your question. But in general, when referring to a user-written Stata command you should point out that it is user-written and tell the reader where it comes from.

      Comment


      • #4
        Yep, my previous code was bug-y. I've just edited it, and it should do the job now though. It would run into the same problem as Clyde highlights though, so should really only loop up to i = 1/32, and then deal with the final matrix separately. Considering Clyde's solution will (with a high degree of certainty) work much better than mine, i will not go back and make these edits
        Last edited by Chris Larkin; 07 Feb 2017, 18:40.

        Comment


        • #5
          Hi Clyde and Chris,

          Thanks for your help and your suggestions how to post a question to the forum more effectively.

          The code works beautifully. Now I can easily change the # of the bootstrap replications.

          Just note that my last line should be "matselrc pc_w_total pc_w_total_33,r(97/99) c(1)" rather than "matselrc pc_w_total pc_w_total_33,r(97/100) c(1)" as posted earlier.


          Thanks again.

          Amy Liu
          Australian National University
          [email protected]

          Comment

          Working...
          X