Announcement

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

  • Getting name labels variables from 2x2 table using tabulate or tabstat

    Hi all

    I Wonder if its possible get name labels variables from tabulate or tabstat to export to matrix.

    I did try with below example but lamentably i had half success: tabulate dont show labels variable names and tabstat dont show results like a 2x2 cross tabulation:

    Code:
    sysuse auto, clear
    tab make rep78   in 1/5, matcell(x)
    
    
                       |  Repair Record 1978
        Make and Model |         3          4 |     Total
    -------------------+----------------------+----------
           AMC Concord |         1          0 |         1
             AMC Pacer |         1          0 |         1
         Buick Century |         1          0 |         1
         Buick Electra |         0          1 |         1
    -------------------+----------------------+----------
                 Total |         3          1 |         4
    
    
    mat list x
    
    x[4,2]
        c1  c2
    r1   1   0
    r2   1   0
    r3   1   0
    r4   0   1
    
    tabstat rep78 in 1/5, by(make) s(count) save
                make |         N
    -----------------+----------
         AMC Concord |         1
           AMC Pacer |         1
          AMC Spirit |         0
       Buick Century |         1
       Buick Electra |         1
    -----------------+----------
               Total |         4
    ----------------------------
    
    
    tabstatmat mpgstat
    tabstatmat mpgstat, nototal
    
    
                     rep78
      AMC Concord:N      1
        AMC Pacer:N      1
       AMC Spirit:N      0
    Buick Century:N      1
    Buick Electra:N      1
    Any suggestion I would grateful
    Last edited by Rodrigo Badilla; 18 Nov 2018, 12:49.

  • #2
    I dont know of immediate solution, but cant you first go with -tabulate-, and then fiddle with the matrix until its row and column names are more appropriately labelled?

    Something like this:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . keep in 1/5
    (69 observations deleted)
    
    . keep if !missing(rep)
    (1 observation deleted)
    
    . tabulate make rep, matcell(x)
    
                       |  Repair Record 1978
        Make and Model |         3          4 |     Total
    -------------------+----------------------+----------
           AMC Concord |         1          0 |         1 
             AMC Pacer |         1          0 |         1 
         Buick Century |         1          0 |         1 
         Buick Electra |         0          1 |         1 
    -------------------+----------------------+----------
                 Total |         3          1 |         4 
    
    . levels make, local(makes)
    `"AMC Concord"' `"AMC Pacer"' `"Buick Century"' `"Buick Electra"'
    
    . mat rownames x = `makes'
    
    . levels rep, local(repses)
    3 4
    
    . mat colnames x = `repses'
    
    . matlist x
    
                 |         3          4 
    -------------+----------------------
     AMC Concord |         1          0 
       AMC Pacer |         1          0 
    Buick Cent~y |         1          0 
    Buick Elec~a |         0          1 
    
    .

    Comment


    • #3
      Thanks Joro for you reply!

      I used your commands and used decode command to make string variable with labels,.

      In my last example dont work properly but work fine with my data. Just add two lines before:

      decode make, gen(make_2)
      decode rep78, gen(rep78_2)
      sysuse auto, clear tab make_2 rep78_2 in 1/5, matcell(x)

      Comment

      Working...
      X