Announcement

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

  • Matrix to Table

    I know we can use esttab to make tables from matrices. And that's all well and good. But, what I want, is to use Stata's table command to accomplish this.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    collect clear
    clear
    input str39 pops double Totals
    "Alcohol users"                            5
    "Auto insured"                             2
    "Cannabis users"                          17
    "Children in need of protective services"  3
    "College students"                        21
    "Crash-involved individuals"              10
    "Crime victims"                            1
    "Criminal justice involved"               63
    "Decedents"                               36
    "Disability claimants"                     2
    "Drug treatment clients"                  15
    "Elementary school students"               1
    "Females"                                  1
    "Festival/nightlife participants"          2
    "General population"                      87
    "High school students"                    41
    "Hospital patients"                       24
    "Internet/social media users"             19
    "Law enforcement"                          2
    "Medical professionals"                    2
    "Medically insured"                       14
    "Middle school students"                  21
    "Newborns"                                 5
    "Other groups"                            19
    "Outpatients"                              7
    "Parents"                                  3
    "People with health conditions"           15
    "Poison center patients"                  12
    "Pregnant/postpartum women"                9
    "Prescription drug users"                 11
    "Property buyers/renters"                  9
    "Proprietary survey panelist"             10
    "Tobacco users"                            6
    "Tourists/travelers"                       3
    "Urban population"                        13
    "Vehicle drivers"                         24
    "Workforce participants"                  11
    end
    
    tempvar id
    
    egen `id' = group(pops)
    cls
    
    g sort = 1 if inlist(`id',9,13,15,23,24,26,32,35,37) //gen pop
    
    replace sort = 2 if inlist(`id',1,3,30,33) // drug users
    
    replace sort = 3 if inlist(`id',5,12,16,22) // education
    
    replace sort = 4 if inlist(`id',2,14,18,31,32,34) // consumers
    
    replace sort = 5 if inlist(`id',7,8,19) // criminal justice
    
    replace sort = 6 if inlist(`id',17,20,21,25,28,29) // medical
    
    replace sort = 7 if inlist(`id',4,10,11,27) // at-risk
    
    replace sort = 8 if inlist(`id',6,36) // cars
    
    as !mi(sort)
    
    gsort sort -Totals
    
    mkmat Totals, rownames(pops) mat(Populations)
    
    cls
    loc a: display "`: colname Populations'"
    
    loc b: display "`: rowname Populations'"
    
    mat l Populations
    
    collect style autolevels `a' Totals
    collect layout (`b') (colname)
    All I want is for the rows to be what they are, and the numbers in those rows to be the single column of interest. At present, the preview section of the table editor says "Your layout specification does not identify any items."

    Is there a straightforward way to simply collect a matrix into a table and continue editing from there? At present, the row names are there, but the column of totals isn't there.

  • #2
    Though you have asked for help using table command (available in Stata 17), I am posting a solution using asdoc (that works on versions of Stata 13 and above).
    Code:
    ssc install asdoc
    asdoc wmat, mat(Populations) replace abb(.)
    asdocx offers more flexibility to send the output to Excel, Word, LaTeX or HTML.

    Code:
    asdocx wmat, mat(Populations) replace abb(.)
    Here is the output from asdocx
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	37.7 KB
ID:	1668567
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Thanks so much for this. It was important to preserve the sort order of the matrix. Presumably there's a way to do this with table, but this certainly works. Thanks so much!! Attaullah Shah

      Comment


      • #4
        Here is a way to do that with table. The trick is to create a new numeric variable that represents the sort order in which you want the variables to appear, with value labels that represent the original variable. This general strategy was already useful before the new table command, and is discussed in this Stata Journal article by Nick Cox: https://www.stata-journal.com/articl...article=gr0034 . To get the labmask command I find it easiest to just type search labmask and follow the links.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        collect clear
        clear
        input str39 pops double Totals
        "Alcohol users"                            5
        "Auto insured"                             2
        "Cannabis users"                          17
        "Children in need of protective services"  3
        "College students"                        21
        "Crash-involved individuals"              10
        "Crime victims"                            1
        "Criminal justice involved"               63
        "Decedents"                               36
        "Disability claimants"                     2
        "Drug treatment clients"                  15
        "Elementary school students"               1
        "Females"                                  1
        "Festival/nightlife participants"          2
        "General population"                      87
        "High school students"                    41
        "Hospital patients"                       24
        "Internet/social media users"             19
        "Law enforcement"                          2
        "Medical professionals"                    2
        "Medically insured"                       14
        "Middle school students"                  21
        "Newborns"                                 5
        "Other groups"                            19
        "Outpatients"                              7
        "Parents"                                  3
        "People with health conditions"           15
        "Poison center patients"                  12
        "Pregnant/postpartum women"                9
        "Prescription drug users"                 11
        "Property buyers/renters"                  9
        "Proprietary survey panelist"             10
        "Tobacco users"                            6
        "Tourists/travelers"                       3
        "Urban population"                        13
        "Vehicle drivers"                         24
        "Workforce participants"                  11
        end
        
        tempvar id
        
        egen `id' = group(pops)
        
        g sort = 1 if inlist(`id',9,13,15,23,24,26,32,35,37) //gen pop
        replace sort = 2 if inlist(`id',1,3,30,33) // drug users
        replace sort = 3 if inlist(`id',5,12,16,22) // education
        replace sort = 4 if inlist(`id',2,14,18,31,32,34) // consumers
        replace sort = 5 if inlist(`id',7,8,19) // criminal justice
        replace sort = 6 if inlist(`id',17,20,21,25,28,29) // medical
        replace sort = 7 if inlist(`id',4,10,11,27) // at-risk
        replace sort = 8 if inlist(`id',6,36) // cars
        
        as !mi(sort)
        
        gsort sort -Totals
        
        // new
        
        gen Sort = _n
        labmask Sort, values(pops)
        table (Sort), stat(total Totals)
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          After moving/copying matrix Populations to r(Populations):
          Code:
          collect get r(Populations)
          collect layout (rowname)(colname)
          a copy can be made by:
          Code:
          mata : st_matrix("r(Populations)", st_matrix("Populations"))
          mata : st_matrixrowstripe("r(Populations)", st_matrixrowstripe("Populations"))
          mata : st_matrixcolstripe("r(Populations)", st_matrixcolstripe("Populations"))
          Last edited by Bjarte Aagnes; 10 Jun 2022, 14:48.

          Comment


          • #6
            Bjarte Aagnes Thanks so much, this was really what I was looking for. I didn't know it was stored in an r class result (even though I maybe should've). All this helped me so much, thank you all Maarten Buis

            Comment

            Working...
            X