Announcement

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

  • Using Matrix command

    Dear Users,
    I have two freq tables as follows:

    Table 1
    Code:
                pml |      Freq.     Percent        Cum.
    ----------------+-----------------------------------
          poor_poor |     10,916       54.90       54.90
       poor_nonpoor |        962        4.84       59.74
       nonpoor_poor |      1,810        9.10       68.84
    nonpoor_nonpoor |      6,196       31.16      100.00
    ----------------+-----------------------------------
              Total |     19,884      100.00
    and

    Table 2
    Code:
                pmu |      Freq.     Percent        Cum.
    ----------------+-----------------------------------
          poor_poor |  8,827.596       44.40       44.40
       poor_nonpoor | 2,809.7888       14.13       58.53
       nonpoor_poor | 5,488.3671       27.60       86.13
    nonpoor_nonpoor |  2,758.248       13.87      100.00
    ----------------+-----------------------------------
              Total |     19,884      100.00
    I want to construct a 4 x 2 matrix from the two tables.
    The two columns to be Percent column of Table 1 (54.90, 4.84, ...) and the corresponding Percent column of Table 2 (44.40, 14.13,...). The four rows to be as already defined.

    I will appreciate your suggestions.

    Thanks,
    Dapel

  • #2
    Code

    Code:
    tempname P1 P2
    tabulate pml , matcell(`P1')
    matrix `P1' = `P1'/r(N)*100
    tabulate pmu , matcell(`P2')
    matrix `P2' = `P2'/r(N)*100
    matrix P = (`P1', `P2')
    Best
    Daniel

    Comment


    • #3
      Not sure it's the simplest way, but anyway, here is a possibility.

      Some fictitious data:
      Code:
      clear
      set seed 17760704
      set obs 250
      gen pml=runiformint(1,4)
      gen pmu=runiformint(1,4)
      lab def poor 1 "poor_poor" 2 "poor_nonpoor" 3 "nonpoor_poor" 4 "nonpoor_nonpoor"
      lab val * poor
      Now, the matcell option of -tab1- stores frequencies in a matrix. You just have to concatenate the columns.
      Code:
      tab1 pml, matcell(pml)
      tab1 pmu, matcell(pmu)
      mat a=pml,pmu
      mat list a
      Output:
      Code:
      a[4,2]
          c1  c1
      r1  61  58
      r2  68  73
      r3  62  59
      r4  59  60
      Now 'a' has frequencies, not row percents. And it does not have row names
      Code:
      loc n :rowsof a
      loc p :colsof a
      
      * Compute rows percents
      forv j=1/`p' {
          mat s=J(1,`n',1)*a[1...,`j']
          mat a[1,`j']=a[1...,`j']/s[1,1]*100
      }
      mat list a
      
      * Get and assign row names
      loc s
      forv i=1/`n' {
          loc k=el(row,`i',1)
          loc s `"`s' "`:label poor `k''""'
      }
      mat rownames a = `s'
      
      mat list a

      Output:
      Code:
      a[4,2]
                      c1    c1
         poor_poor  24.4  23.2
      poor_nonpoor  27.2  29.2
      nonpoor_poor  24.8  23.6
      nonpoor_no~r  23.6    24
      Last edited by Jean-Claude Arbaut; 21 Jul 2019, 02:36.

      Comment


      • #4
        Thanks a million, Jean-Claude! I'm using Stata 13. I encountered
        rowsof not allowed
        . Any way around this?

        Comment


        • #5
          According to the manual, Stata 13 does not have the rowsof and colsof macro extended functions (apparently they appeared in Stata 15). You can replace the two lines with:

          Code:
          loc n=rowsof(a)
          loc p=colsof(a)
          Last edited by Jean-Claude Arbaut; 21 Jul 2019, 12:33.

          Comment


          • #6
            Thanks, Jean-Claude! That went through but found another error:
            Code:
            forv i=1/`n' {
              2.     loc k=el(row,`i',1)
              3.     loc s `"`s' "`:label poor `k''""'
              4. }
            row not found
            r(111);

            Comment


            • #7
              Thanks, @daniel klein! This solved the problem. However, it doesn't seem to accept the application of weights, e.g
              Code:
              tabulate pml [aw=popwt], matcell(`P1')
              I will also like to round the result to 2 decimal places.

              Thanks again,
              Dapel

              Comment


              • #8
                It worked. What's left is rounding to 2 decimal places

                Comment


                • #9
                  Rounding is a matter of presenting not calculating the results. The matlist command has a format() option. There are other ways of presenting the results but I have no idea what your ultimate goal is so I do not know whether matlist is the best suited tool.

                  Best
                  Daniel

                  Comment


                  • #10
                    Zuhumnan Dapel Regarding #6, it's on me: I modified the answer, but I failed to change the tab1 call accordingly. The first one should read:

                    Code:
                    tab1 pml, matcell(pml) matrow(row)
                    This create a second matrix holding the unique values read (the program then reads the corresponding label to build the row names of the matrix).

                    Comment


                    • #11
                      Perfect! Thank you both!

                      Comment

                      Working...
                      X