Announcement

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

  • Coefplot: arrow on confidence intervals

    Hi all,

    I am performing a logistic regression analysis using the xtlogit command. I am trying to plot the results using coefplot on Stata 15.0, comparing the results of two different models, for two different age groups. The problem is some of the confidence intervals are too large, making it difficult to compare all the results, so I am trying to reduce the x axis scale and trim the confidence intervals, in the same way as can easily be achieved in forestplot using “force”: forestplot (xlab (0 5 10, force). I would also like the large CI to have an arrow at the end indicating they are extended beyond the x axis scale.

    Code:
    coefplot A, bylabel(18-49 years) || B, bylabel(50-64 years) ||, eform

    Click image for larger version

Name:	Graph.png
Views:	2
Size:	42.0 KB
ID:	1560701



    I have tried various combinations of pstyle and ciopts, and none worked, for example:

    Code:
    coefplot A, bylabel(18-49 years) || B, bylabel(50-64 years) || ///
        (., pstyle(p1) if(@ll>0|@ul<8) ciopts(recast(rcap) lcolor(gs1))) (., pstyle(p1) if(@ul>=8)  ciopts(recast(pcarrow) lcolor(gs1))) ///
        (., pstyle(p1) if(@ul>=8) ciopts(recast(pcbarrow) lcolor(gs1))), ///
        eform base drop(_cons) byopts(compact rows(1)) ///
            nooffsets transform(*= min(max(@,0),8))
    Click image for larger version

Name:	Graph_2.png
Views:	1
Size:	58.5 KB
ID:	1560702


    This works trimming my x axis and CI, but
    - fails to put an arrow at the end of them, and
    - creates a new third column that I do not know what it is.

    Any help would be appreciated! Thanking you in advance!
    Attached Files

  • #2
    Dear Stata users - I am working with Nieves on this project and we are banging heads against the wall. It feels like there may be a simple solution, but we have not been able to find it. I wonder if anyone has come across this problem before?
    Please let us know if more information is required and thank you in advance.
    Josh.

    Comment


    • #3
      The advice as always is to post a reproducible example and to identify where coefplot is from. See FAQ #12.

      Comment


      • #4
        Hi! Thanks for the message and the advice. I hope this clarifies:

        I am performing a logistic regression analysis using the xtlogit command. I am trying to plot the results using coefplot on Stata 15.0 (Windows 10), comparing the results of two different models, using the code below.

        Code:
        sysuse auto.dta
        
        recode weight min/2200 = 1 2200/3000 = 2 3000/4000 = 3 4000/max = 4, gen (cweight)
        
        xtlogit foreign price mpg cweight if length <180, pa corr(exch) i(headroom) vce(robust) base eform
        estimates store xy
        xtlogit foreign price mpg cweight if length >180, pa corr(exch) i(rep78) vce(robust) base eform
        estimates store yx
        
        coefplot xy, || yx,  ||, eform
        Click image for larger version

Name:	Graph-1.png
Views:	1
Size:	26.2 KB
ID:	1562588


        The problem is some of the confidence intervals are too large, making it difficult to compare all the results, so I am trying to reduce the x axis scale and trim the confidence intervals (in the example above, to 5). I would also like the large CI to have an arrow at the end indicating they are extended beyond the x axis scale.

        I have tried various combinations of pstyle and ciopts, and none worked, for example:

        Code:
        coefplot xy, || yx, || ///
            (., pstyle(p1) if(@ll>0&@ul<5)) (., pstyle(p1) if(@ul>=5) ciopts(recast(pcarrow))) ||, ///
            eform transform(*= min(max(@,0),5))
        Click image for larger version

Name:	Graph-2.png
Views:	1
Size:	35.3 KB
ID:	1562589


        This approach fails to put the effect sizes and the CI that exceed the threshold in the second plot, and furthermore, creates a third subgraph.

        Thanks again for the time and attention!

        Comment


        • #5
          Thanks for the data example. What is missing is specifying that coefplot is from Stata Journal. You can add the arrow using the -addplot- option. Note in the example, price is coefficient 1 on the y axis, mileage is 2, and so on. So identify the coefficients that have extended CIs and create a variable to identify their positions. With two plots and four coefficients including constant, the possible values for this variable are 1 1 2 2 3 3 4 4, where the first number in a pair represents the left-hand side graph. In this case, we have the last coefficient on the right-hand side graph, so the coordinates for the pcarrow addplot are simply 4 0 4 5. We specify the start and end variables with constant values 0 and 5, respectively (although you should replace start with the minimum coefficient value in the plot).

          Code:
          sysuse auto.dta
          recode weight min/2200 = 1 2200/3000 = 2 3000/4000 = 3 4000/max = 4, gen (cweight)
          xtlogit foreign price mpg cweight if length <180, pa corr(exch) i(headroom) vce(robust) base eform
          estimates store xy
          xtlogit foreign price mpg cweight if length >180, pa corr(exch) i(rep78) vce(robust) base eform
          estimates store yx
          gen start=0
          gen end=5
          gen which= ceil(_n/2) if _n<9
          *TAG COEFFICIENT OF CONSTANT IN RHS GRAPH
          gen tag= which==4 &_n==8
          coefplot xy, || yx,  ||, eform transform(*= min(max(@,0),5)) ///
          addplot(pcarrow which start which end if tag, lcolor(navy) mlcolor(navy))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	29.9 KB
ID:	1562631

          Last edited by Andrew Musau; 09 Jul 2020, 11:25.

          Comment


          • #6
            Dear Andrew Musau, would you be able to figure out a way to automatize this? For example, scanning all stored CIs before coefplot is called, picking those above a certain threshold, and then being able to switch on selectively one or more strings with the correct addplot() specification?
            I'm using Stata/MP 17

            Comment


            • #7
              You can, but I cannot devote the time to write such a program at the moment.

              Comment


              • #8
                Understandable, that's perhaps an effort worth making for an update of the command itself.
                I'm using Stata/MP 17

                Comment

                Working...
                X