Announcement

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

  • Forest Plot after Logistic Regression

    Hello everyone

    How can I make a forest plot type graph after a logistic regression that in the left sector shows the graph and on the right side shows the odd ratio values, confidence intervals and p-values?

    For example, here I have a regression with disability as the dependent variable and sex, age (decennial) and education as covariates. When running the regression I use the following commands


    logistic disability sex i.agre_group i.education


    then to make the forest plot I use this command

    coefplot, drop(_cons) xline(1, lcolor(red) lpattern(dash)) title("Odds Ratios con Intervalos de Confianza 95%") xlabel(,angle(45)) mcolor(blue) ciopts(recast(rcap) lcolor(black))


    But, I would like the OR, CI and P-values to appear on the side of the plot.

    Is it possible to do that? any guidance on this?



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str25 id byte sex float(education disability agre_group)
    "1091002"  1 2 1 1
    "1091003"  2 3 1 1
    "1091004"  2 3 0 1
    "1091006"  1 2 0 2
    "1091007"  1 1 1 3
    "1091008"  1 1 1 4
    "1091013"  1 3 1 2
    "1091016"  2 2 1 2
    "1091019"  2 2 1 1
    "1091020"  2 3 1 3
    "1091021"  1 3 0 3
    "1091022"  2 1 1 3
    "1091023"  2 2 0 2
    "1091024"  2 1 1 3
    "1091025"  2 1 0 2
    "1091026"  2 2 0 1
    "1091027"  2 3 0 2
    "1091028"  1 3 1 3
    "1091029"  1 2 0 1
    "1091030"  1 1 1 2
    "1091031"  2 2 1 1
    "1091032"  1 1 1 1
    "1091033"  1 1 1 1
    "1091034"  2 1 1 2
    "1091035"  2 2 1 2
    "1091037"  1 2 1 2
    "1091039"  2 2 1 2
    "1091040"  2 1 1 3
    "1091041"  2 3 1 1
    "1091042"  2 2 0 1
    "1091045"  1 2 1 1
    "1091047"  2 2 1 1
    "1091049"  2 2 0 1
    "1091050"  1 1 0 3
    "1091051"  1 1 1 3
    "1091053"  2 3 0 1
    "1091059"  1 2 0 3
    "1091060"  1 2 1 3
    "1091061"  1 2 1 1
    "1091062"  2 1 1 3
    "1091063"  2 1 1 4
    "1091064"  1 1 1 3
    "1091065"  1 2 1 3
    "1091068"  1 1 0 1
    "1091069"  2 1 1 3
    "1091070"  2 1 1 2
    "1091074"  1 1 0 1
    "1091076"  2 1 0 1
    "1091079"  2 1 1 2
    "1091081"  1 3 0 1
    "1091083"  2 1 0 2
    "1091089"  1 2 0 2
    "1091091"  1 3 0 3
    "1091094"  2 2 0 2
    "1091105"  1 1 1 1
    "1091106"  1 1 1 1
    "1091107"  1 2 1 3
    "1091108"  1 2 1 1
    "1091111"  1 2 1 1
    "1091113"  2 1 0 1
    "1091114"  1 2 0 3
    "1091115"  1 1 0 1
    "1091116"  1 1 1 3
    "1091117"  2 2 0 2
    "1091118"  1 2 1 2
    "1091122"  1 2 0 2
    "1091123"  1 2 1 4
    "1091124"  2 2 0 1
    "1091125"  1 3 1 1
    "1091126"  1 3 1 1
    "1091127"  1 2 0 1
    "1091128"  1 2 1 2
    "1091129"  1 3 1 2
    "1091130"  1 2 1 1
    "10911302" 2 2 0 1
    "1091131"  2 1 1 4
    "1091132"  2 1 1 3
    "1091133"  2 3 0 1
    "1091134"  1 2 1 1
    "10911342" 2 3 1 1
    "1091135"  2 3 1 2
    "1091136"  2 2 0 2
    "1091137"  1 1 1 1
    "1091141"  1 1 1 3
    "1091143"  1 3 1 3
    "1091144"  2 3 1 1
    "1091173"  2 1 0 1
    "1091174"  2 1 1 1
    "1091176"  1 2 1 1
    "1092001"  2 2 1 3
    "1092002"  1 2 0 2
    "1092003"  1 3 0 2
    "1092004"  1 3 0 1
    "1092005"  2 2 1 1
    "1092006"  1 2 0 1
    "1092007"  1 2 1 3
    "1092008"  1 1 1 1
    "1092009"  2 1 1 2
    "1092010"  1 3 1 1
    "1092011"  1 1 1 2
    end
    label values sex a2l
    label def a2l 1 "Hombre", modify
    label def a2l 2 "Mujer", modify
    label values education esc2
    label def esc2 1 "0-3 años", modify
    label def esc2 2 "4-8 años", modify
    label def esc2 3 "9+ años", modify
    label values disability dis
    label def dis 0 "No limitado", modify
    label def dis 1 "Limitado", modify
    label values agre_group edd
    label def edd 1 "60-69 años", modify
    label def edd 2 "70-79 años", modify
    label def edd 3 "80-89 años", modify
    label def edd 4 "90+ años", modify
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 885 observations
    Use the count() option to list more








  • #2
    Ben Jann provides an example here: https://repec.sowi.unibe.ch/stata/coefplot/markers.html

    Code:
    logistic disability sex i.agre_group i.education, or
    coefplot, xline(0) mlabposition(1) mlabgap(*2) ///
        mlabel("{it:p} = " + string(@pval,"%9.3f")) eform
    Best wishes

    (Stata 16.1 MP)

    Comment


    • #3
      Thanks Felix, for guidance. However, I am looking for a graph similar to this image... I don't know if it is possible in stata to come up with something similar.






      Click image for larger version

Name:	forest plot.png
Views:	1
Size:	235.4 KB
ID:	1774505








      Comment


      • #4
        coefplot is from SSC, as you are asked to explain (FAQ Advice #12). Thanks for the reproducible example.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str25 id byte sex float(education disability agre_group)
        "1091002"  1 2 1 1
        "1091003"  2 3 1 1
        "1091004"  2 3 0 1
        "1091006"  1 2 0 2
        "1091007"  1 1 1 3
        "1091008"  1 1 1 4
        "1091013"  1 3 1 2
        "1091016"  2 2 1 2
        "1091019"  2 2 1 1
        "1091020"  2 3 1 3
        "1091021"  1 3 0 3
        "1091022"  2 1 1 3
        "1091023"  2 2 0 2
        "1091024"  2 1 1 3
        "1091025"  2 1 0 2
        "1091026"  2 2 0 1
        "1091027"  2 3 0 2
        "1091028"  1 3 1 3
        "1091029"  1 2 0 1
        "1091030"  1 1 1 2
        "1091031"  2 2 1 1
        "1091032"  1 1 1 1
        "1091033"  1 1 1 1
        "1091034"  2 1 1 2
        "1091035"  2 2 1 2
        "1091037"  1 2 1 2
        "1091039"  2 2 1 2
        "1091040"  2 1 1 3
        "1091041"  2 3 1 1
        "1091042"  2 2 0 1
        "1091045"  1 2 1 1
        "1091047"  2 2 1 1
        "1091049"  2 2 0 1
        "1091050"  1 1 0 3
        "1091051"  1 1 1 3
        "1091053"  2 3 0 1
        "1091059"  1 2 0 3
        "1091060"  1 2 1 3
        "1091061"  1 2 1 1
        "1091062"  2 1 1 3
        "1091063"  2 1 1 4
        "1091064"  1 1 1 3
        "1091065"  1 2 1 3
        "1091068"  1 1 0 1
        "1091069"  2 1 1 3
        "1091070"  2 1 1 2
        "1091074"  1 1 0 1
        "1091076"  2 1 0 1
        "1091079"  2 1 1 2
        "1091081"  1 3 0 1
        "1091083"  2 1 0 2
        "1091089"  1 2 0 2
        "1091091"  1 3 0 3
        "1091094"  2 2 0 2
        "1091105"  1 1 1 1
        "1091106"  1 1 1 1
        "1091107"  1 2 1 3
        "1091108"  1 2 1 1
        "1091111"  1 2 1 1
        "1091113"  2 1 0 1
        "1091114"  1 2 0 3
        "1091115"  1 1 0 1
        "1091116"  1 1 1 3
        "1091117"  2 2 0 2
        "1091118"  1 2 1 2
        "1091122"  1 2 0 2
        "1091123"  1 2 1 4
        "1091124"  2 2 0 1
        "1091125"  1 3 1 1
        "1091126"  1 3 1 1
        "1091127"  1 2 0 1
        "1091128"  1 2 1 2
        "1091129"  1 3 1 2
        "1091130"  1 2 1 1
        "10911302" 2 2 0 1
        "1091131"  2 1 1 4
        "1091132"  2 1 1 3
        "1091133"  2 3 0 1
        "1091134"  1 2 1 1
        "10911342" 2 3 1 1
        "1091135"  2 3 1 2
        "1091136"  2 2 0 2
        "1091137"  1 1 1 1
        "1091141"  1 1 1 3
        "1091143"  1 3 1 3
        "1091144"  2 3 1 1
        "1091173"  2 1 0 1
        "1091174"  2 1 1 1
        "1091176"  1 2 1 1
        "1092001"  2 2 1 3
        "1092002"  1 2 0 2
        "1092003"  1 3 0 2
        "1092004"  1 3 0 1
        "1092005"  2 2 1 1
        "1092006"  1 2 0 1
        "1092007"  1 2 1 3
        "1092008"  1 1 1 1
        "1092009"  2 1 1 2
        "1092010"  1 3 1 1
        "1092011"  1 1 1 2
        end
        label values sex a2l
        label def a2l 1 "Hombre", modify
        label def a2l 2 "Mujer", modify
        label values education esc2
        label def esc2 1 "0-3 años", modify
        label def esc2 2 "4-8 años", modify
        label def esc2 3 "9+ años", modify
        label values disability dis
        label def dis 0 "No limitado", modify
        label def dis 1 "Limitado", modify
        label values agre_group edd
        label def edd 1 "60-69 años", modify
        label def edd 2 "70-79 años", modify
        label def edd 3 "80-89 años", modify
        label def edd 4 "90+ años", modify
        
        logistic disability sex i.agre_group i.education
        
        capt program drop coefplot_mlbl
        *! version 1.0.0  10jun2021  Ben Jann
        program coefplot_mlbl, sclass
            _parse comma plots 0 : 0
            syntax [, MLabel(passthru) * ]
            if `"`mlabel'"'=="" local mlabel mlabel(string(@b))
            preserve
            qui coefplot `plots', `options' `mlabel' generate replace nodraw
            sreturn clear
            tempvar touse
            qui gen byte `touse' = __at<.
            mata: st_global("s(mlbl)", ///
                invtokens((strofreal(st_data(.,"__at","`touse'")) :+ " " :+ ///
                "`" :+ `"""' :+ st_sdata(.,"__mlbl","`touse'") :+ `"""' :+ "'")'))
            sreturn local plots `"`plots'"'
            sreturn local options `"`options'"'
        end
        
        
        coefplot_mlbl, drop(_cons)  xline(1, lcolor(red) lpattern(dash)) ///
        title("Odds Ratios con Intervalos de Confianza 95%") ///
        xlabel(,angle(45)) mcolor(blue) ciopts(recast(rcap) lcolor(black)) ///
        mlabel(string(exp(@b),"%9.3f") + " (" + string(exp(@ll),"%9.3f") ///
        + " - " + string(exp(@ul),"%9.3f") + ")")
        
        coefplot `s(plots)', `s(options)' ///
            ymlabel(`s(mlbl)', angle(0) notick axis(2)) ///
            yaxis(1 2) yscale(alt) yscale(axis(2) alt) ///
            ylabel(none, axis(2)) yti("", axis(2))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	26.1 KB
ID:	1774526

        Last edited by Andrew Musau; 17 Mar 2025, 20:44.

        Comment


        • #5
          Dear Andrew


          Thank you very much, the programming works very well.
          Thanks for your help




          Comment

          Working...
          X