Announcement

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

  • Adding both 90% and 95% confidence intervals to a marginsplot of average marginal effects

    Dear Statalist,

    I am using Stata 17 and am analysing an interaction effect of how the relationship between length of time spent in one's neighbourhood is associated with one's mental health depending on how attached one is to their neighbourhood. Mental health is a continuous variable, time in the neighbourhood is continuous and attachment is a 3-category categorical variable. The data is 4 waves of panel data.

    Code:
    xtreg mental c.time_in_neighbourhood##i.nei_attachment, fe
    I have been creating a marginsplot showing the average marginal effects of the impact of time spent in the neighbourhood at three levels of attachment, with 95% confidence intervals around the estimates.

    Code:
    margins, dydx(time_in_neighbourhood) at(nei_attachment=(1 2 3))  level(95)
    marginsplot, recast(scatter) yline(0, lwidth(vthin))
    I get this marginsplot as a result:


    However, I was wondering if it is possible to plot both the 95% confidence intervals and the 90% confidence for these average marginal effects on the same marginsplot? Below is an example from an article that seems to do this. It shows average marginal effects of having had COVID-19 on trust in strangers depending on their perceived economic condition (an interaction effect). The thin/thick vertical bars are 95/90% CIs.


    However, I can't seem to find anyway of doing this myself. When specifying the
    Code:
    level(95)
    in calculating margins it only allows me to set one at a time. I have seen that you can do this using coefplot e.g.,
    Code:
    coefplot, levels(99.9 99 95)
    However, I can't seem to be able to do something similar using marginsplot.

    I did find a way of plotting average marginal effects using coefplot:

    Code:
    xtreg mental c.time_in_neighbourhood##i.nei_attachment, fe
    margins, dydx(time_in_neighbourhood) at(nei_attachment=(1 2 3))  level(95) post
    coefplot, xline(0) xtitle(Average marginal effects)
    But again, I can't seem to find a way of getting both the 90% and 95% confidence intervals on to the same plot.

    Any help would be much appreciated.

    Best wishes,

    James
    Last edited by James Lawrence; 14 Mar 2024, 05:14.

  • #2
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.8 KB
ID:	1746669
    Click image for larger version

Name:	Example.PNG
Views:	1
Size:	39.1 KB
ID:	1746670


    Apologies but the images did not attach the first time around so here are the images

    Comment


    • #3
      As recommended in FAQ Advice #12, include a reproducible example to increase your chances of obtaining a helpful reply.

      Comment


      • #4
        Hi Andrew,

        Thanks so much. I created an example using the sysuse auto.dta

        Code:
        sysuse auto.dta
        reg headroom c.mpg##i.foreign
        margins, dydx(mpg) at(foreign=(0 1)) level(95)
        marginsplot, recast(scatter)
        This marginsplot has the 95% confidence intervals around the point estimate. I would like to include both the 95% and 90% confidence intervals on it. Akin to the example figure I showed above where 'trust in strangers' is on the y-axis.

        Any help would be much appreciated.

        Best,

        James

        Comment


        • #5
          Check to see whether -coefplot- (Ben Jann, SSC, with links to SJ article and online resources) will help you. It can definitely show a range of CI widths; so I tihnk the issue is whether you can get it to display the estimates you wish

          Comment


          • #6
            Hi Stephen,

            Thanks for the reply. I had been investigating this possibility as well. From what I can tell, you are able to plot marginal effects (calculated using the margins command) in coefplot (https://repec.sowi.unibe.ch/stata/co...estimates.html). However, when you're calculating the marginal effects to go into the coefplot, you can still only specify one level for the confidence intervals. The situation where you can show a range of CI widths using coefplot comes when you're running it straight after a model. So I haven't yet found a way of accomplishing what I was hoping to do.

            Comment


            • #7
              Originally posted by James Lawrence View Post
              I would like to include both the 95% and 90% confidence intervals on it.
              You can save the margins and plot with twoway.

              Code:
              sysuse auto.dta, clear
              reg headroom c.mpg##i.foreign
              margins, dydx(mpg) at(foreign=(0 1)) level(95) saving(output1, replace)
              margins, dydx(mpg) at(foreign=(0 1)) level(90) saving(output2, replace)
              use output2, clear
              rename (_ci_lb _ci_ub) (_ci_lb1 _ci_ub1)
              keep _ci*
              save output2, replace
              use output1, clear
              merge 1:1 _n using output2, nogen
              
              *MARGINSPLOT USING TWOWAY
              twoway (rspike _ci_lb _ci_ub _at2 if _at2==0, sort pstyle(ci) color(blue%50) lw(thick)) ///
              (rspike _ci_lb _ci_ub _at2 if _at2==1, sort pstyle(ci) color(blue%50) lw(thick)) ///
              (rspike _ci_lb1 _ci_ub1 _at2 if _at2==0, sort pstyle(ci) color(red%50) lw(thick)) ///
              (rspike _ci_lb1 _ci_ub1 _at2 if _at2==1, sort pstyle(ci) color(red%50) lw(thick)), ///
              legend(order(1 "95% CI" 3 "90% CI")) xlab(0 1, val) yline(0, lwidth(vthin)) ///
              ytitle("Effects on linear prediction")
              Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.3 KB
ID:	1746737

              Last edited by Andrew Musau; 14 Mar 2024, 12:48.

              Comment


              • #8
                Hi Andrew,

                This is great! It works perfectly.

                Thanks so much for your help,

                James

                Comment

                Working...
                X