Announcement

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

  • Forest plot after running regression loop

    Hi,
    I would like to ask if it is possible to create a forest plot for the effect size after running a regression loop for multiple outcomes. I have used the command below, which I found in this question. https://www.statalist.org/forums/for...egression-loop.

    Code:
    frame create results_NProt_Pulse_Velocity
    frame results_NProt_Pulse_Velocity {
        set obs 1000
        gen outcome= ""
        gen coef=.
        gen SE=.
        gen pvalue=.
        gen ci_l=.
        gen ci_u=.
    }
    local counter 0
    foreach outcome of varlist CHIP-C34_HIV_F {
       regress `outcome' PulseWaveVelocity Age sq_age
       if r(table)[4, 1] < 0.05{
            local ++counter
            frame results_NProt_Pulse_Velocity {
                replace outcome= "`outcome'" in `counter'
                replace coef= `=r(table)[1, 1]' in `counter'
                replace SE= `=r(table)[2, 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_NProt_Pulse_Velocity
    drop if missing(outcome)
    browse


    And after this command I got something like this:
    Click image for larger version

Name:	Screen Shot 2023-02-18 at 8.28.12 PM.png
Views:	1
Size:	50.1 KB
ID:	1702366


    So how can I create a forest plot after getting this result?
    I want the forest plot to include the names of the outcomes and the coefficient value on the y-axis.

  • #2
    For your future posts, present data examples using the dataex command as recommended in FAQ Advice #12. Also, see why screenshots are not as helpful as you think.

    Code:
    sysuse auto, clear
    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 price mpg disp{
       regress `outcome' weight turn i.rep78
       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)
    
    *EXTRACT RESULTS TO MATRIX
     mkmat coef ci_l ci_u, mat(res) rownames(outcome)
    
    *GRAPH USING COEFPLOT FROM SSC
    *ssc install coefplot, replace
    set scheme s1color
    mat res= res'
    coefplot mat(res), ci((2 3))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.4 KB
ID:	1702429

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      For your future posts, present data examples using the dataex command as recommended in FAQ Advice #12. Also, see why screenshots are not as helpful as you think.

      Code:
      sysuse auto, clear
      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 price mpg disp{
      regress `outcome' weight turn i.rep78
      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)
      
      *EXTRACT RESULTS TO MATRIX
      mkmat coef ci_l ci_u, mat(res) rownames(outcome)
      
      *GRAPH USING COEFPLOT FROM SSC
      *ssc install coefplot, replace
      set scheme s1color
      mat res= res'
      coefplot mat(res), ci((2 3))
      [ATTACH=CONFIG]n1702429[/ATTACH]
      Thank you very much Mr. Andrew for your help. Actually, I found your response under this post https://www.statalist.org/forums/for...n-one-coefplot response #2 and I was looking to have a plot similar to that but I want to present values of coefficients besides their names (outside the graph).

      Comment


      • #4
        Change last line to:

        Code:
        coefplot mat(res), ci((2 3)) mlab(@b) mlabpos(1)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	15.7 KB
ID:	1702443

        Comment


        • #5
          To place them on the margins, see Ben's posts at https://www.statalist.org/forums/for...-on-right-axis

          Comment


          • #6
            Cross-posted at https://stackoverflow.com/questions/...est-plot-stata

            Please note our policy on cross-posting, which is that you are asked to tell us about it.

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              To place them on the margins, see Ben's posts at https://www.statalist.org/forums/for...-on-right-axis
              Thank you very much for your help

              Comment


              • #8

                Sorry Mr. Nick, I was unaware of that because I am a new user. Next time, I will consider that.

                Comment

                Working...
                X