Announcement

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

  • overlay/combine mcp graphs

    Hello everyone,
    I use Patrick Roystons mcp command to plot conditional marginal effects for an interaction of two continuous variables (c_loggdp1000 and c_religiosity). Using the at-option to restrict the command to 6 values of c_loggdp1000 and 3 of c_religiosity gives me three separate graphs (that do not have the same scale on the y-axis). Interpreting them is therefore difficult. See below:


    Click image for larger version

Name:	mcp-graph.png
Views:	2
Size:	12.3 KB
ID:	1343428


    Is there any option to overlay these graphs in a single graph? (I know this works without plotting confidence intervals)

    Thank you very much in advance,
    Sebastian
    Attached Files

  • #2
    I'm looking for something similar as well

    Comment


    • #3
      Usually when the author of a program does not provide a particular option, there is a good reason for it. If you view the source code for mcp, you will notice that the plots are no more than a twoway graph consisting of a combination of a line graph and a range plot with area shading (rarea).

      Code:
      *TYPE findit marginscontplot AND FOLLOW INSTRUCTIONS TO INSTALL 
      viewsource marginscontplot.ado

      A series of twoway graphs are sequential plots in the sense that they are drawn in the order that you specify them. Therefore, if graphs intersect at any point, the last drawn graphs (in order) will obscure part or all of those drawn previously. The author of mcp anticipates this with confidence interval plots and therefore explicitly states in help


      ci displays pointwise confidence intervals for the fitted values on the margins plot. For legibility, if more than one
      line is specified, each line is plotted on a separate graph.
      Now, it may be the case that you do not have an intersection and you want to display both lines and confidence intervals in one graph. In such a case, you can save the calculated margins and their confidence interval, using the -saving- option and then use graph twoway to plot the graphs. Personally, I would not go through the trouble since Stata's inbuilt marginsplot command can achieve what you want. You could just use the -showmarginscmd- option in mcp which displays the equivalent margins command. I will use the auto dataset to present both approaches.

      Code:
      *APPROACH #1
      sysuse auto
      regress mpg i.rep78 weight
      *MCP plot choosing 2 levels of rep78 (2 graphs)
      marginscontplot weight rep78, at2(2 5) ci
      *SAVE CALCULATED MARGINS AND CONFIDENCE INTERVALS
      quietly marginscontplot weight rep78, at2(2 5) ci saving(graph)
      clear
      use graph
      * PLOT GRAPHS USING DEFAULT NAMES AS SAVED BY MCP (1 overlaid graph). NOTE THAT GRAPHS
      INTERSECT, SO THIS APPROACH IS NOT OPTIMAL
      twoway (rarea _ci_lb1 _ci_ub1 weight, sort pstyle(ci))  (line _margin1 weight, sort lstyle(refline))||(rarea _ci_lb2 _ci_ub2 weight, sort pstyle(ci))  (line _margin2 weight, sort lstyle(refline))
      *THIS SUGGESTS REVERSING THE PLOT ORDER BY PLOTTING THE CONFIDENCE INTERVALS FIRST. BUT WHERE
      DOES ONE CI START AND STOP? PERHAPS USE DIFFERENT COLORS FOR CI SHADING.
      twoway (rarea _ci_lb1 _ci_ub1 weight, sort pstyle(ci)) (rarea _ci_lb2 _ci_ub2 weight, sort pstyle(ci)) || (line _margin1 weight, sort lstyle(refline)) (line _margin2 weight, sort lstyle(refline))
      Code:
      *APPROACH #2
      sysuse auto
      regress mpg i.rep78 weight
      *DISPLAY MARGINS COMMAND
      marginscontplot weight rep78, at2(2 5) ci
      marginscontplot weight rep78, at2(2 5) ci nograph showmarginscmd
      So taking the margins command displayed, use marginsplot to draw the graph

      Code:
      . marginscontplot weight rep78, at2(2 5) ci nograph showmarginscmd
      margins , at( weight=( 1760 1800 1830 1930 1980 1990 2020 2040 2050 2070 2110 2120 2130 2160 2200 2230 2240 2280 2370 2410 2520 2580 2640 2
      > 650 2670 2690 2730 2750 2830 2930 3170 3180 3200 3210 3220 3250 3260 3280 3300 3310 3330 3350 3370 3400 3420 3430 3470 3600 3670 3690 370
      > 0 3720 3740 3830 3880 3900 4030 4060 4080 4130 4290 4330 4720 4840) rep78=( 2 5)) 
      
      . quietly margins , at( weight=( 1760 1800 1830 1930 1980 1990 2020 2040 2050 2070 2110 2120 2130 2160 2200 2230 2240 2280 2370 2410 2520 2
      > 580 2640 2650 2670 2690 2730 2750 2830 2930 3170 3180 3200 3210 3220 3250 3260 3280 3300 3310 3330 3350 3370 3400 3420 3430 3470 3600 367
      > 0 3690 3700 3720 3740 3830 3880 3900 4030 4060 4080 4130 4290 4330 4720 4840) rep78=( 2 5))
      
      . marginsplot
      Click image for larger version

Name:	graph_margins.png
Views:	1
Size:	20.8 KB
ID:	1359064

      Comment


      • #4
        Thanks very much Andrew Musau for your very useful post on how to manipulate the MCP graphs - a wonderful package from Patrick Royston. I used your approach and it worked well. I didn't realise you could save graphical output as you demonstrated in Stata. That issue you mention about overlapping shaded areas is easy these days with the colour transparency option, for example,
        Code:
         twoway (rarea _ci_lb1 _ci_ub1 weight, sort pstyle(ci) bcolor(blue%20))

        Comment


        • #5
          For those who read this post and are unfamiliar with the mcp package, here is the paper availabele for downlloading:
          Royston, P. (2013). Marginscontplot: Plotting the Marginal Effects of Continuous Predictors. The Stata Journal, 13(3), 510–527.

          And here is another paper availabele for downlloading on using Stata's margins command that might be of interest:
          Williams, R. (2012). Using the Margins Command to Estimate and Interpret Adjusted Predictions and Marginal Effects. The Stata Journal, 12(2), 308–331.
          http://publicationslist.org/eric.melse

          Comment


          • #6
            Also note that an extension of marginscontplot, mcp, is available from scc:
            Code:
            ssc install marginscontplot2 , replace
            h mcp2    // Graph margins for continuous predictors, This routine is an extension of marginscontplot.
            which mcp2
            *! version 1.2.0 PR 20mar2017
            http://publicationslist.org/eric.melse

            Comment

            Working...
            X