Announcement

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

  • Overlaying marginsplot line derived from npregress on top of a scatter plot

    Good Afternoon,

    Context: We ran several RCT's looking at the impact of computer assisted learning on student's test scores. The working paper is out and we are updating our figures.

    I have estimated a nonparametric line with confidence intervals and then used margins and marginsplot to overlay a scatterplot over the marginsplot to show that we believe that our results follow a non linear pattern. I have done this using the addplot command to add the scatterplot after using marginsplot. My issue is pretty simple but I cannot find a fix for it, I want the marginsplot line to be on top of the scatter plot (image below), not below (yes I know the weighted circles are messy but our primary author wants them). We have a version of this graph with a regular line of best fit but do not think that it properly visualizes what is going in the data.

    I have tried saving the marginsplot and overlaying it on the scatterplot, but saving is not allowed with marginsplot following the use of npregress (it throws an error saying this). As far as I know, the only way to create a figure like this using marginsplot is to use addplot afterward, where there does not seem to be an option to select which plot is on top. Below is the code I am using to create the figure below and an example of the data being used for the figure. I have cut down on the bootstrap numbers in the code example. The mean residuals and practice minutes variables here are purely used for visuals for the scatterplot. non_zmss_22_class_size is the number of students in a classroom who took our post intervention test (used for weighting). Is there a way to overlay the red marginsplot line on top of the scatterplot rather than beneath it?

    Code:
    // Fitting a non-parametric kernel regression model using all the data
    npregress kernel Residual_zmss_22 classminutes, reps(100)  seed(12) bwidth(10 1,copy) 
    npgraph
    
    // Compute margins with specific intervals
    margins, at(classminutes=(0(8)110)) reps(20) seed(12) 
    
    // Create the margins plot with custom axis labels and other desired options
    marginsplot, ///
        plotopts(lcolor(cranberry) mcolor(cranberry) msymbol(point)) ///
        ciopts(lcolor(cranberry) lpattern(solid) msymbol(point)) ///
        addplot(scatter mean_Residual_zmss_22 mean_classminutes [weight = non_zmss_22_class_size] if classminutes >= 0 & classminutes <= 110, mcolor(ebblue*0.5) ylabel(-1.25(0.25)1,nogrid) xlabel(0(10)110,nogrid) msymbol(circle_hollow)  ) ///
        yline(0, lpattern(dash)) ///
        title("") ///
        ytitle("Residual Standardized Math STARR Score") ///
        xtitle("Weekly Average Practice Time") legend(off)
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(Residual_zmss_22 classminutes mean_Residual_zmss_22 mean_classminutes non_zmss_22_class_size)
     2.2202148  58.6278   .8615404 59.82357  19
     1.8866357 59.33506   .8615404 59.82357  19
     1.1808496  59.6869   .8615404 59.82357  19
             . 59.74548   .8615404 59.82357  19
     .14575522 60.68965   .8615404 59.82357  19
             . 59.57655   .8615404 59.82357  19
             . 59.59894   .8615404 59.82357  19
      .8431989 60.36934   .8615404 59.82357  19
     .11537106 59.88672   .8615404 59.82357  19
             . 60.82843   .8615404 59.82357  19
      .9684724 59.92774   .8615404 59.82357  19
     2.6919434 58.05226   .8615404 59.82357  19
      -.344446 59.82043   .8615404 59.82357  19
     1.0639145  60.5715   .8615404 59.82357  19
             . 58.84902   .8615404 59.82357  19
     -.4093983 60.19437   .8615404 59.82357  19
     1.8103932 60.25547   .8615404 59.82357  19
     .20052236 60.65535   .8615404 59.82357  19
     -.3118613 59.97682   .8615404 59.82357  19
    -.51702493  98.1909  .26301917 98.12198  33
      .5651296 98.37404  .26301917 98.12198  33
      .5400366 97.75206  .26301917 98.12198  33
     .29956305 97.39747  .26301917 98.12198  33
       .554035 99.70355  .26301917 98.12198  33
     -.4282148 97.66106  .26301917 98.12198  33
     -.3664183 98.97102  .26301917 98.12198  33
     -.2961583 98.48431  .26301917 98.12198  33
     .13967054 98.09097  .26301917 98.12198  33
     -.1962264 97.47748  .26301917 98.12198  33
       1.52508 98.06164  .26301917 98.12198  33
      .8772518 98.11316  .26301917 98.12198  33
      .8633727 98.42539  .26301917 98.12198  33
      .9008282  97.5853  .26301917 98.12198  33
     .56787264  99.4714  .26301917 98.12198  33
             . 98.35497  .26301917 98.12198  33
      .4612411 97.51111  .26301917 98.12198  33
     -.3128726 96.97287  .26301917 98.12198  33
     1.3726445 96.91226  .26301917 98.12198  33
     .24085757 97.78868  .26301917 98.12198  33
     .05638966 98.62801  .26301917 98.12198  33
             .  97.8459  .26301917 98.12198  33
    -.24976495   98.498  .26301917 98.12198  33
      1.054589 97.75097  .26301917 98.12198  33
     -.4958166 98.13387  .26301917 98.12198  33
             . 98.33012  .26301917 98.12198  33
      1.842746 97.95932  .26301917 98.12198  33
    -.13921443 98.94305  .26301917 98.12198  33
     .11102656 97.99664  .26301917 98.12198  33
     -.6733521 97.69231  .26301917 98.12198  33
    -.56628215 99.10337  .26301917 98.12198  33
    -.10343245 97.53562  .26301917 98.12198  33
             . 98.30843  .26301917 98.12198  33
      .3056947 5.075164 -1.6276754   5.0532 108
      .4732611 5.088404 -1.6276754   5.0532 108
     -2.891395  5.05681 -1.6276754   5.0532 108
     -.6832248 5.090286 -1.6276754   5.0532 108
     -3.988117 5.093191 -1.6276754   5.0532 108
     -3.796348  5.05681 -1.6276754   5.0532 108
    -2.1425853 5.093191 -1.6276754   5.0532 108
    -2.1425853  5.05681 -1.6276754   5.0532 108
       1.09795 5.059165 -1.6276754   5.0532 108
     -.7110434 5.000212 -1.6276754   5.0532 108
     -2.813258  5.05681 -1.6276754   5.0532 108
      -.646506  5.08414 -1.6276754   5.0532 108
     1.4205372 4.933375 -1.6276754   5.0532 108
     -3.080225  5.05681 -1.6276754   5.0532 108
     -.2518726 5.067123 -1.6276754   5.0532 108
             . 5.093191 -1.6276754   5.0532 108
    -.08437244 4.956368 -1.6276754   5.0532 108
     -.5032555 5.091768 -1.6276754   5.0532 108
     -4.127036  5.05681 -1.6276754   5.0532 108
             .  5.05681 -1.6276754   5.0532 108
    -.08437244 5.066453 -1.6276754   5.0532 108
     .40672895 5.054163 -1.6276754   5.0532 108
    -4.2025356  5.05681 -1.6276754   5.0532 108
      -.646506  5.02914 -1.6276754   5.0532 108
      .3759781 5.092449 -1.6276754   5.0532 108
     -3.051575  5.05681 -1.6276754   5.0532 108
     -3.988117  5.05681 -1.6276754   5.0532 108
     -3.030738  5.05681 -1.6276754   5.0532 108
      .4732611 5.069326 -1.6276754   5.0532 108
    -2.1425853  5.05681 -1.6276754   5.0532 108
    -4.0711665 5.093191 -1.6276754   5.0532 108
     -3.080225 5.093191 -1.6276754   5.0532 108
    -2.7637715 5.060437 -1.6276754   5.0532 108
     -3.009902 5.093191 -1.6276754   5.0532 108
     -3.009902  5.05681 -1.6276754   5.0532 108
      .4767636  4.93821 -1.6276754   5.0532 108
             . 5.092688 -1.6276754   5.0532 108
    -2.1425853 5.093191 -1.6276754   5.0532 108
             .  5.08059 -1.6276754   5.0532 108
     1.1908159 5.093191 -1.6276754   5.0532 108
     -3.051575 5.093191 -1.6276754   5.0532 108
     .21866404 4.955354 -1.6276754   5.0532 108
     -3.009902 5.093191 -1.6276754   5.0532 108
    -4.0258665  5.05681 -1.6276754   5.0532 108
     2.0852623 5.021742 -1.6276754   5.0532 108
     .25238043 5.080891 -1.6276754   5.0532 108
     -3.125805 5.093191 -1.6276754   5.0532 108
    -4.0711665  5.05681 -1.6276754   5.0532 108
    end


    Click image for larger version

Name:	residscore_mins_46treated_CI.png
Views:	1
Size:	101.8 KB
ID:	1753584


  • #2
    not a solution that i know of, but a more gneralized approach: we need an 'overlay' option for 'graph combine'

    Comment


    • #3
      Originally posted by Michael Andrew Kemp View Post
      Is there a way to overlay the red marginsplot line on top of the scatterplot rather than beneath it?
      See the -below- option of -addplot()-

      addplot(plots [, below]) specifies the rest of the graph twoway subcommands to
      be added to the graph twoway command issued by command.

      below is a suboption of the addplot() option and specifies that the added
      plots be drawn before the plots drawn by the command. Thus the added
      plots will appear below the plots drawn by command. The default is to
      draw the added plots after the command's plots so that they appear
      above the command's plots. below affects only the added plots that are
      drawn on the same x and y axes as the command's plots.

      Comment


      • #4
        Thank you! I obviously didn't look hard enough. This solves the issue. The marginsplot line is now laid on top of the scatterplot

        Comment

        Working...
        X