Announcement

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

  • Extract coefficient and p-value for certain variable from regression loop

    Hi,

    I have I applied a regression loop to 1000 metabolites (outcome), and the exposure variable is BMI. I also have other variables in the model. I would like to know how I can extract only the coefficient, p-value, and 95% CI for BMI only and when BMI is significant. And then I want to extract them into an Excel file.


    This is the code I have used. Finally, it informed me that there were, for example, 100 significant results. So I'm trying to figure out which 100 are those and extract them for BMI only, without other variables in the model.

    PHP Code:
    local counter 0
    local counter_pos 
    0
    local counter_neg 
    0

    foreach outcome of varlist B {
       
    regress `outcome' bmi Age i.sex i.smoking i.lpa2c i.cholestrol
        matrix M = r(table)
        if M[4, 1] < 0.05 {
            local ++counter
            if _b[bmi] < 0 {
                local ++counter_neg
            }
            else {
                local ++counter_pos
            }
        }
    }


    display as text "Number of positive significant: " as result 
    `counter_pos'
    display as text "Number of negative significant: " as result `counter_neg'
    display as text "Total of significant results: " as result `counter' 

  • #2
    Code:
    frame create results
    frame results{
        set obs 1000
        gen outcome= ""
        gen coef=.
        gen pvalue=.
        gen ci_l=.
        gen ci_u=.
    }
    local counter 0
    foreach outcome of varlist B - Z {
       regress `outcome' bmi Age i.sex i.smoking i.lpa2c i.cholestrol
       if r(table)[4, 1] < 0.05{
            local ++counter
            frame results{
                replace outcome= "`outcome'" in `counter'
                replace coef= `=r(table)[1, 1]' in `counter'
                replace pvalue= `=r(table)[4, 1]' in `counter'
                replace ci_l= `=r(table)[5, 1]' in `counter'
                replace ci_u= `=r(table)[6, 1]' in `counter'
             }
         }
    }
    frame change results
    drop if missing(outcome)
    browse

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      Code:
      frame create results
      frame results{
      set obs 1000
      gen outcome= ""
      gen coef=.
      gen pvalue=.
      gen ci_l=.
      gen ci_u=.
      }
      local counter 0
      foreach outcome of varlist B - Z {
      regress `outcome' bmi Age i.sex i.smoking i.lpa2c i.cholestrol
      if r(table)[4, 1] < 0.05{
      local ++counter
      frame results{
      replace outcome= "`outcome'" in `counter'
      replace coef= `=r(table)[1, 1]' in `counter'
      replace pvalue= `=r(table)[4, 1]' in `counter'
      replace ci_l= `=r(table)[5, 1]' in `counter'
      replace ci_u= `=r(table)[6, 1]' in `counter'
      }
      }
      }
      frame change results
      drop if missing(outcome)
      browse
      Thank you very much. Finally, it's working.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Code:
        frame create results
        frame results{
        set obs 1000
        gen outcome= ""
        gen coef=.
        gen pvalue=.
        gen ci_l=.
        gen ci_u=.
        }
        local counter 0
        foreach outcome of varlist B - Z {
        regress `outcome' bmi Age i.sex i.smoking i.lpa2c i.cholestrol
        if r(table)[4, 1] < 0.05{
        local ++counter
        frame results{
        replace outcome= "`outcome'" in `counter'
        replace coef= `=r(table)[1, 1]' in `counter'
        replace pvalue= `=r(table)[4, 1]' in `counter'
        replace ci_l= `=r(table)[5, 1]' in `counter'
        replace ci_u= `=r(table)[6, 1]' in `counter'
        }
        }
        }
        frame change results
        drop if missing(outcome)
        browse
        Mr. Andrew I would like to ask if there is a way to draw the forest plot for the effect size after running this command.

        Comment


        • #5
          If interested, see https://www.statalist.org/forums/for...egression-loop

          Comment

          Working...
          X