Announcement

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

  • Out of sample calculations

    Hello,

    I am looking for a solution to expand my matrices in Mata for some out of sample calculations. For example, we have population data for different 5 age-groups in 10 regions over 2000-2020 (matrix is 200x5). I would like to expand the matrix to include 20 additional years for each region (400x5).

    Currently, I import the data to Stata and use xtset and tsappend commands and then import the data to Mata. I was hoping that I can do this directly in Mata.

    Thank you.

  • #2
    Your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Create and show a small example of your initial matrix - say 2 age groups, 3 regions, 4 years, a 2x12 matrix - and then show what you hope it to be when expanded by 4 additional years into a 2x24 matrix.

    The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output.

    Comment


    • #3
      Sure, here is an example, assuming regions 1 and 2, years 2000 and 2001 and one column of data on population, for example:
      mata: I = (1,2000,20\1,2001,22\2,2000,15\2,2001,25)
      1 2 3
      +----------------------+
      1 | 1 2000 20 |
      2 | 1 2001 22 |
      3 | 2 2000 15 |
      4 | 2 2001 25 |
      +----------------------+

      I want to expand this matrix by adding two more years for each region and have the following:
      mata: I = (1,2000,20\1,2001,22\1,2002,.\1,2003,.\2,2000,15\2 ,2001,25\2,2002,.\2,2003,.)


      1 2 3
      +----------------------+
      1 | 1 2000 20 |
      2 | 1 2001 22 |
      3 | 1 2002 . |
      4 | 1 2003 . |
      5 | 2 2000 15 |
      6 | 2 2001 25 |
      7 | 2 2002 . |
      8 | 2 2003 . |
      +----------------------+

      Thank you.

      Comment


      • #4
        If you install the community-contributed moremata package from SSC, the following seems to do what you want. I added three years to distinguish the number of added years (3) from the number of regions (2) and the number of columns of data (1).
        Code:
        /* one time only highlight and run the following:
        ssc install moremata
        */
        
        mata:
        I = (1,2000,20\1,2001,22\2,2000,15\2,2001,25)
        C1 = mm_expand(range(1,2,1),3,1,1)
        C2 = mm_expand(range(2002,2004,1),2,1,0)
        C3 = J(2*3,1,.)
        J = I \ (C1,C2,C3)
        J = sort(J,(1,2))
        J
        end
        Code:
        : J
                   1      2      3
             +----------------------+
           1 |     1   2000     20  |
           2 |     1   2001     22  |
           3 |     1   2002      .  |
           4 |     1   2003      .  |
           5 |     1   2004      .  |
           6 |     2   2000     15  |
           7 |     2   2001     25  |
           8 |     2   2002      .  |
           9 |     2   2003      .  |
          10 |     2   2004      .  |
             +----------------------+

        Comment


        • #5
          Perfect. Thanks for this, William. Great to learn about moremata.

          Comment

          Working...
          X