Announcement

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

  • subset matrix by part of column names

    Dear Stata users,

    I have a large matrix that I'd like to subset it by its column equation names. My original matrix looks like the following :


    Code:
    . mat list cut
    
    cut[1,20]
           tau1_1:     tau2_1:     tau2_2:     tau2_3:     tau2_4:     tau2_5:     tau2_6:
            _cons       _cons       _cons       _cons       _cons       _cons       _cons
    b   .17826254   -1.041928   -.7895766  -.50704795  -.25593288   .13586857   .70962046
    
           tau2_7:     tau2_8:     tau2_9:     tau3_1:     tau3_2:     tau3_3:     tau4_1:
            _cons       _cons       _cons       _cons       _cons       _cons       _cons
    b   1.1027288   1.6016368   1.9813423  -.71304087   .19229588   1.5388753  -.55037033
    
           tau4_2:     tau4_3:     tau5_1:     tau5_2:     tau5_3:     tau5_4:
            _cons       _cons       _cons       _cons       _cons       _cons
    b   .34203077   1.5677015  -.28412486   .01651519   .72999177   1.4822046

    I'd like to subset it into five matrices based on the first part of its column equation name. I could do it manually as following :

    Code:
       mat e_tau1 = c[1, "tau1_1:"]
       mat e_tau2 = c[1, "tau2_1:" .. "tau2_9:"]'
       mat e_tau3 = c[1, "tau3_1:" .. "tau3_3:"]'
       mat e_tau4 = c[1, "tau4_1:" .. "tau4_3:"]'
       mat e_tau5 = c[1, "tau5_1:" .. "tau5_4:"]'
    However, given that the matrix columns will likely change based on my model specifications. I wonder if there's a way to automate this routine? Clearly syntax such as

    mat e_tau2 = c[1, "tau2_*:"]' won't work





  • #2
    Hi Donghui,

    There may be more elegant solutions, but one thing you could consider is converting the matrix to a dataset and subsetting that way. E.g.

    Code:
    preserve
    
    clear
    svmat cut, names(col)
    forvalues i = 1/5{
    mkmat tau`i'*, mat(e_tau`i')
    }
    
    restore

    Comment


    • #3
      You can extract the column names and define your condition. Something like

      Code:
      local colnms: coln cut
      forval i=1/5{
      foreach col in `colnms'{
           if regexm("`col'", "^tau`i'_"){
                local col`i' `col`i'' `col'
                }
           }
      }

      Comment


      • #4
        Thank you both !

        Comment

        Working...
        X