Announcement

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

  • Combining two matrices in one Variable

    Dear all,

    my university is using the Stata version with max matsize = 800. I am working with a dataset which has 1300 observations so I splitted my matrices in 800 and 500 observations. So I have got a 800x5 (matrix a) and a 500x5 (matrix b) both containing the same 5 coefficients.

    As I am not able to do:
    Code:
    matrix total = matrix a \ matrix b
    I am able to put the coefficients in several variables:
    Code:
    svmat matrix a
    svmat matrix b
    So I receive 10 columns (5 of each matrix).

    And now my problem: Is there a command in stata to put the 5 Variables of Matrix B under the 5 Variables of Matrix A?

    Best regards,

    Ikn



  • #2
    -append-ing files does that.

    Probably your strategy should be

    1, turn matrix b into variables, save the dataset

    2. turn matrix a into variables, use the -append- command to append the data from matrix a below.

    Comment


    • #3
      There are a couple of other ways to do what Ikn wants, the second of which, using Mata, might be particularly attractive.

      1) See -help stack- for the not-commonly-used -stack- command, which stacks variables vertically.
      Code:
      clear all
      // Simulated data with some visual distinctions built in
      mat a = J(800,5,.)
      mat b = J(500,5,.)
      forval i = 1/800 {
         forval j = 1/5 {
            mat a[`i', `j'] =  10*`i' + `j'        
            if (`i' <=500) mat b[`i', `j'] = 10000*`i' + `j'        
         }   
      }   
      svmat a
      svmat b
      // 
      stack a* b*, into (ab1 ab2 ab3 ab4 ab5)
      list in 795/805 // visualize that b is under a
      drop if missing(ab1) //  b with _n > 500 was missing
      2) To my knowledge, even smaller versions of Stata, such as IC, do not have matrix size limitations in the Mata language. Generally speaking, working with matrices in Mata is actually easier than it is in Stata. Using the two matrices created above, one could just do:
      Code:
      mata: AB = st_matrix("a") \ st_matrix("b") // Put the matrices into Mata and stack them
      mata: AB[795..805,.]  // visualize again
      // Get the Mata matrix back into Stata as variables and observation, if this is needed.
      // -getmata- is the easiest but not the most robust way to do this
      clear // Stata, not Mata
      getmata (ab*) = AB

      Comment


      • #4
        Dear Joro and Mike,

        thank you very much for your help! I really appreciate this.

        Best
        Ikn

        Comment


        • #5
          You are welcome, Ikn.

          Mike Lacy 's solution based on -stack- is easy to implement, and so is my solution based on -append-. Here:

          Code:
          . mat a = J(8,5,1)
          
          . mat b = J(5,5,2)
          
          . mat dir
                      b[5,5]
                      a[8,5]
          
          . svmat b
          number of observations will be reset to 5
          Press any key to continue, or Break to abort
          number of observations (_N) was 0, now 5
          
          . save b, replace
          file b.dta saved
          
          . mat drop b
          
          . mat rename a b
          
          . clear
          
          . svmat b
          number of observations will be reset to 8
          Press any key to continue, or Break to abort
          number of observations (_N) was 0, now 8
          
          . append using b
          
          . list, clean
          
                 b1   b2   b3   b4   b5  
            1.    1    1    1    1    1  
            2.    1    1    1    1    1  
            3.    1    1    1    1    1  
            4.    1    1    1    1    1  
            5.    1    1    1    1    1  
            6.    1    1    1    1    1  
            7.    1    1    1    1    1  
            8.    1    1    1    1    1  
            9.    2    2    2    2    2  
           10.    2    2    2    2    2  
           11.    2    2    2    2    2  
           12.    2    2    2    2    2  
           13.    2    2    2    2    2  
          
          . ren b* a*
          
          . list, clean
          
                 a1   a2   a3   a4   a5  
            1.    1    1    1    1    1  
            2.    1    1    1    1    1  
            3.    1    1    1    1    1  
            4.    1    1    1    1    1  
            5.    1    1    1    1    1  
            6.    1    1    1    1    1  
            7.    1    1    1    1    1  
            8.    1    1    1    1    1  
            9.    2    2    2    2    2  
           10.    2    2    2    2    2  
           11.    2    2    2    2    2  
           12.    2    2    2    2    2  
           13.    2    2    2    2    2  
          
          .
          There is no need to revert to Mata for such simple problem.

          Comment

          Working...
          X