Announcement

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

  • Extracting levels of variable from a regression output

    Dear Statalisters,

    I am trying to automate the process of creating a table using Stata 15. I want to extract odds ratios for multiple regressions and list them in a table, including the labels of the values. Currently this is what I've got:

    Code:
     
    webuse lbw, clear
    
    foreach var in smoke race {
    
    qui {
    logistic low i.`var'
    matrix _or = r(table)
    
    levelsof `var' 
    scalar _count = r(r)
    }
    forval x = 1/`=_count' {
    
    if `var' == smoke & `x' == 1 {
    di "Variable" _col(15) "Odds Ratio"
    }
    di "`var'" _col(15) _or[1,`x']
    }
    }
    This returns:

    Code:
    Variable      Odds Ratio
    smoke         1
    smoke         2.0219436
    race          1
    race          2.3275362
    race          1.8892339
    What I would like is a third column which extracts the value label of the variable, like so:

    Code:
    Variable      Level               Odds Ratio
    smoke          non-smoker         1
    smoke          smoker             2.0219436
    race           white              1
    race           black              2.3275362
    race           other              1.8892339

    I have tried another line of code, but it doesn't extract the odds ratios properly:

    Code:
    foreach var in smoke race {
    qui logistic low i.`var'
    matrix _or = r(table)
    
    qui levelsof `var', local(levels)
    local vlname: value label `var'
    foreach L of local levels {
       local vl: label `vlname' `L'
    
       display "`vl'" _col(15) _or[1,`L']
    
    }
    }
    This returns:

    Code:
    nonsmoker     .
    smoker        1
    white         1
    black         2.3275362
    other         1.8892339
    As you can see, the values for smoker are incorrect.

    I have also tried installing elabel, but my institution won't let me install it. Any advice would be greatly appreciated.


  • #2
    Try running this (not -do-)

    Code:
    webuse lbw, clear
    noi display "Variable" _col(15) "Level" _col(30) "Odds Ratio"
    
    foreach var in smoke race {
        logistic low i.`var'
        matrix _or = r(table)
        levelsof `var', local(levels)
        local numlevels: word count `levels'
        local vlname: value label `var'
        forval x = 1/`numlevels' {
            local L: word `x' of `levels'
            local vl: label `vlname' `L'
            noi display "`var'" _col(15) "`vl'" _col(30) "`:dis %4.3f _or[1,`x']'"
        }
    }
    which produces:

    Code:
    Variable      Level          Odds Ratio
    smoke         Nonsmoker      1.000
    smoke         Smoker         2.022
    race          White          1.000
    race          Black          2.328
    race          Other          1.889

    Comment


    • #3
      That's said, there are already many community-contributed commands that automate the process of creating tables of regression results. Two examples you could try:
      Code:
      ssc install estout
      or

      Code:
      net install outreg2.pkg

      Comment


      • #4
        Ahh, so
        Code:
        local numlevels : word count `levels'
        stores the number of unique observations in a variable, which is the number I needed to loop on, rather than the actual values of the variable.

        Thanks a lot Hermanshu!

        This matter is now closed.

        Comment


        • #5
          It appears you want the number of distinct values of a variable, not the number of unique observations. If you genuinely want the number of unique observations, see duplicates report YourVar.

          Presuming you do want distinct *values*: Check out return list after issuing a levelsof command. It will show you that levelsof returns the number of distinct values of the variable in r(r), which is apparently what you want in the local numlevels. What you are doing will work, but is the hard way. Note that you could also avoid some extra lines and possible mistakes with:
          Code:
          levelsof `var'
          foreach v in `r(levels {
            ... do something with `v'
          }
          Also: If you want to install a program but are not permitted to do that on your institution's machine, you can install community-contributed programs like elabel or estout in a directory of your choice instead of the default location to which you apparently don't have access. You do this by telling Stata a different location for your PERSONAL directory (e.g., your own removable storage device). See help sysdir to learn about this.

          Comment


          • #6
            Should be
            foreach v in `r(levels)'

            Comment


            • #7
              Thanks Mike! That's such a useful command. I needed both the unique values of the variables, as they are linked to the label I wanted to display in the table. I also needed a count of the number of unique values of the variables, as I need to call on them to extract the correct coefficient from the regression table. The command
              Code:
              local numlevels : word count `levels'
              solved it for me. Thanks again.

              Also: If you want to install a program but are not permitted to do that on your institution's machine, you can install community-contributed programs like elabel or estout in a directory of your choice instead of the default location to which you apparently don't have access. You do this by telling Stata a different location for your PERSONAL directory (e.g., your own removable storage device). See help sysdir to learn about this.
              Great, I'll try that.

              Comment

              Working...
              X