Announcement

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

  • Saving results as a variable using loops

    Hello,

    I am trying to bring a program that I have written in to loops to make it easier to change the variables it is using. I am trying to get a list of odds ratio for a list of different risk factors (This form is required for another program).

    So the program should loop through a list of variables, perform a logistic regression and then save the odds ratios into a variable. So far i have got really close, the first variable does exactly what i want it to, however when you add the second variable the results of the first don't seem to be saved. I was hoping someone would be able to help me retain the previous results so i have one variable that lists all of the odds ratios for all of the variables.

    Here is the code i have so far:


    Code:
    local variables a b c
    gen output = .
    
    foreach var of local variables {
    
       by `var', sort: gen `var'counter = 1 if _n == 1
       replace `var'counter = sum(`var'counter)
       local num = `var'counter[_N]
    
       foreach i of numlist 2/`num' {
    
          replace output = exp(_b[`i'.`var']) in `j++'
       }
    }

    Thanks
    Cydney

  • #2
    You need to show more, please. The code doesn't include the logistic regression. The local macro j is never initialised.

    Code:
    by `var', sort: gen `var'counter = 1 if _n == 1
    replace `var'counter = sum(`var'counter)
    local num = `var'counter[_N]
    looks to me like

    Code:
    qui tab `var'
    local num = r(r)
    although there are many other ways to do it without a loop and without new variables.
    Last edited by Nick Cox; 28 Apr 2020, 05:56.

    Comment


    • #3
      Ahh yes sorry, I missed a few lines:

      Code:
       local variables a b c
      
       local j = 1
       gen output = .
      
       foreach var of local variables {  
       
         by `var', sort: gen `var'counter = 1 if _n == 1
         replace `var'counter = sum(`var'counter)
         local num = `var'counter[_N]
      
         logistic event ib1.`var', baselevel
         est store `var'
      
         foreach i of numlist 2/`num' {
          
              replace output = exp(_b[`i'.`var']) in `j++'
         }
      }
      Last edited by Cydney Bruce; 28 Apr 2020, 06:50.

      Comment


      • #4
        Thanks for the extra detail. This extends my simplification of your code as started in #2. At the same time, I don't ,see why there should be problem as reported in #1. I think you need a reproducible example here.

        Code:
        local variables a b c
        
        local j = 1
        gen output = .
        
        quietly foreach var of local variables {  
            noisily logistic event ib1.`var', baselevel
            est store `var'
        
            tab `var'
            forval i = 2/`r(r)' {
                replace output = exp(_b[`i'.`var']) in `j++'
            }
        }

        Comment


        • #5
          Hi Nick,

          Okay so I figured out the problem is that this part of the code:
          Code:
          by `var', sort: gen `var'counter = 1 if _n == 1  
          replace `var'counter = sum(`var'counter)
          local num = `var'counter[_N]
          Was reordering the data meaning the previous loops values were moving within the data. (The dataset i was working on was so large I hadn't noticed they were lower down).
          The piece of code you gave me above fixed the problem perfectly thank you!

          Cydney

          Comment


          • #6
            Well spotted. I have to confess that I didn't think about side-effects of that code once I had figured how to replace it.

            Comment

            Working...
            X