Announcement

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

  • 'Hiding' one level of a dimension when creating tables using Collect

    Hello

    The Data: I have several binary variables indicating presence/absence of a particular health condition and I am comparing them between homeless and housed individuals. For example, the variables will be 1/0 indicating presence/absence of a diagnosis of CVD, Cancer, etc. and I am interested in the proportion with the disease in the homeless population vs the housed population.

    The Problem: When I create the table it gives me the proportion of people with the disease (variable = 1) as well as the proportion of people without the disease (variable = 0). I just want it to display the proportion of people with the disease in the table. So essentially, I will have a table with 3 columns - column1 = variable name (e.g. CVD, Cancer, etc.), column 2 = proportion with the disease in homeless population, column 3 = proportion with the disease in the housed population. It's a bit more complicated than this sounds, but if I can solve this simple problem, I can get the rest to work.

    Solution Needed: How can get Stata to only display the proportion when the variable = 1 using the Collect command? I can't just use an if statement in the Table command preceding it (e.g. if cancer == 1) as then it won't give me the correct proportion of people with the condition.

    I hope that makes sense and I'm grateful to anyone who can help. I have many variables and multiple years of data so I don't want to fiddle around in Excel deleting rows.

    Serena

    p.s. I have the Table/Collect commands working fine apart from this one issue. Unfortunately I can't copy my code as I am analysing my data in a secure data safe haven.
    Last edited by Serena Luchenski; 14 Feb 2023, 06:01.

  • #2
    So just to be clear, here's the problem and where I'm trying to get to to:

    Current Display
    Homeless % Housed %
    Cancer = 1
    Cancer = 0
    CVD = 1
    CVD = 0

    Desired Display
    Homeless % Housed %
    Cancer
    CVD

    Comment


    • #3
      your question is not very clear to me (e.g., what commands are you collecting from), but the following may help:
      Code:
      help collect_style_showbase

      Comment


      • #4
        just a simple table command (i.e. tab) for descriptive results. The showbase command works when I use it for regressions, but not for the descriptives/proportions.

        Collect refers to variables as 'dimensions' and each dimension has different levels (in this case, level 1 and level 0). So I'm trying to essentially 'hide' level 0. Sorry, I'm finding it hard to explain!
        Last edited by Serena Luchenski; 14 Feb 2023, 06:16.

        Comment


        • #5
          Code:
          . // open some example data
          . sysuse nlsw88, clear
          (NLSW, 1988 extract)
          
          .
          . // create a table
          . table () (union), stat(fvpercent south collgrad) nototal
          
          -----------------------------------------------------
                                            |    Union worker  
                                            |  Nonunion   Union
          ----------------------------------+------------------
          Lives in the south=Not south      |     53.21   70.50
          Lives in the south=South          |     46.79   29.50
          College graduate=Not college grad |     77.70   67.90
          College graduate=College grad     |     22.30   32.10
          -----------------------------------------------------
          
          .
          . // what dimensions exist in the table?
          . collect layout
          
          Collection: Table
                Rows: result#var
             Columns: union
             Table 1: 4 x 2
          
          -----------------------------------------------------
                                            |    Union worker  
                                            |  Nonunion   Union
          ----------------------------------+------------------
          Lives in the south=Not south      |     53.21   70.50
          Lives in the south=South          |     46.79   29.50
          College graduate=Not college grad |     77.70   67.90
          College graduate=College grad     |     22.30   32.10
          -----------------------------------------------------
          
          . // what levels are there for the two dimensions on the rows?
          . collect levelsof result
          
          Collection: Table
           Dimension: result
              Levels: fvpercent
          
          . collect levelsof var
          
          Collection: Table
           Dimension: var
              Levels: 0.south 1.south 0.collgrad 1.collgrad
          
          .
          . // now we can select the rows we want:
          . collect layout (result#var[1.south 1.collgrad]) (union)
          
          Collection: Table
                Rows: result#var[1.south 1.collgrad]
             Columns: union
             Table 1: 2 x 2
          
          -------------------------------------------------
                                        |    Union worker  
                                        |  Nonunion   Union
          ------------------------------+------------------
          Lives in the south=South      |     46.79   29.50
          College graduate=College grad |     22.30   32.10
          -------------------------------------------------
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Amazing! Thanks so much Maarten - this is exactly what I needed!

            Comment


            • #7
              Hello. I have 2 variables, pdrepeat and dlbrepeat. They are coded 0 and 1.
              I would like to make a table which shows only rows for value of 1.
              I would like frequency to be in 1 column and percentage in the 2nd column.

              table (var) (), statistic(fvfrequency pdrepeat ) statistic(fvpercent pdrepeat ) statistic(fvfrequency dlbrepeat ) statistic(fvpercent dlbrepeat )

              ------------------------------------------------------------------
              | Factor variable frequency Factor variable percent
              ------------+-----------------------------------------------------
              pdrepeat=0 | 808 71.50
              pdrepeat=1 | 322 28.50
              dlbrepeat=0 | 1,029 91.06
              dlbrepeat=1 | 101 8.94
              ------------------------------------------------------------------

              collect style row stack, nobinder spacer
              collect recode result fvfrequency = column1 fvpercent = column2
              (8 items recoded in collection Table)

              collect layout (var) (result[column1 column2])

              Collection: Table
              Rows: var
              Columns: result[column1 column2]
              Table 1: 6 x 2

              -------------------------------
              | column1 column2
              ----------+--------------------
              pdrepeat |
              0 | 808 71.50442
              1 | 322 28.49558
              dlbrepeat |
              0 | 1029 91.06195
              1 | 101 8.938053
              -------------------------------

              This is the table I would like, except I would like to remove the pdrepeat=0 and dlbrepeat=0 rows.

              collect levelsof result

              Collection: Table
              Dimension: result
              Levels: column1 column2 fvfrequency fvpercent

              collect levelsof var
              Collection: Table
              Dimension: var
              Levels: 0.pdrepeat 1.pdrepeat 0.dlbrepeat 1.dlbrepeat

              I then try the following code and it is not working, any input would be much appreciated.

              collect layout (result#var[1.pdrepeat 1.dlbrepeat])

              Collection: Table
              Rows: result#var[1.pdrepeat 1.dlbrepeat]
              Your layout specification does not identify any items.


              Comment


              • #8
                Code:
                . sysuse nlsw88, clear
                (NLSW, 1988 extract)
                
                .
                . gen byte touse  = !missing(collgrad, south)
                
                .
                . table (var) () if touse, ///
                >     statistic(fvfrequency collgrad ) statistic(fvpercent collgrad ) ///
                >     statistic(fvfrequency south ) statistic(fvpercent south )
                
                ----------------------------------------------------------------------------------------
                                                  |  Factor-variable frequency   Factor-variable percent
                ----------------------------------+-----------------------------------------------------
                College graduate=Not college grad |                      1,714                     76.31
                College graduate=College grad     |                        532                     23.69
                Lives in the south=Not south      |                      1,304                     58.06
                Lives in the south=South          |                        942                     41.94
                ----------------------------------------------------------------------------------------
                
                .     
                . collect style row stack, nobinder spacer
                
                . collect recode result fvfrequency = column1 fvpercent = column2
                (8 items recoded in collection Table)
                
                . collect layout (var) (result[column1 column2])
                
                Collection: Table
                      Rows: var
                   Columns: result[column1 column2]
                   Table 1: 7 x 2
                
                ----------------------------------------
                                   |  column1    column2
                -------------------+--------------------
                College graduate   |                    
                  Not college grad |     1714   76.31345
                  College grad     |      532   23.68655
                                   |                    
                Lives in the south |                    
                  Not south        |     1304   58.05877
                  South            |      942   41.94123
                ----------------------------------------
                
                .     
                . collect layout (var[1.collgrad 1.south]) (result[column1 column2])    
                
                Collection: Table
                      Rows: var[1.collgrad 1.south]
                   Columns: result[column1 column2]
                   Table 1: 5 x 2
                
                ----------------------------------------
                                   |  column1    column2
                -------------------+--------------------
                College graduate   |                    
                  College grad     |      532   23.68655
                                   |                    
                Lives in the south |                    
                  South            |      942   41.94123
                ----------------------------------------
                ---------------------------------
                Maarten L. Buis
                University of Konstanz
                Department of history and sociology
                box 40
                78457 Konstanz
                Germany
                http://www.maartenbuis.nl
                ---------------------------------

                Comment

                Working...
                X