Announcement

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

  • Contour plot problem

    Dear All,

    I have encountered the following problem, seemingly the same plot is sometimes drawn incorrectly, and sometimes correctly. Consider the following code:
    Code:
    // Stata/MP 16.0 for Windows (64-bit x86-64) Revision 02 Jul 2019
    // Compile number 797
    
    version 16.0
    
    clear all
    input x y z
    10 10 100
    20 20 100
    10 20 100
    20 10 100
    15 15 50
    end
    twoway contour z x y, name(correct) title(correct) interp(shepard)
    
    
    clear
    input x y z
    10 10 100
    20 20 100
    10 20 100
    20 10 100
    15 15 50
    end
    set obs 10000
    twoway contour z x y, name(incorrect) title(incorrect) interp(shepard)
    
    graph combine correct incorrect, xsize(12) ysize(5)
    
    // end of file
    Which produces the image below.

    Why do the missing observations matter in this case?
    Aren't they supposed to be ignored by the contour plot command?
    If they are not ignored, what is the logic for treating {x=.,y=.,z=.} ?
    Where does the asymmetry arise from?

    Thank you, Sergiy Radyakin

    PS: this is not the most recent version of Stata, (I know), but I have to stick to this version for a while.
    If this issue has been already found/fixed, sorry for the alert.


    Click image for larger version

Name:	contour-problem.png
Views:	1
Size:	132.1 KB
ID:	1563780


  • #2
    Dear Sergiy,

    I agree with your observation that contour cannot compute correctly with missings, and the same applies to contourline, but that this is limited to the variables x and y (not z) in:
    Code:
    twoway contour z y x [if] [in] [, options]
    My example:
    Code:
    sysuse auto, clear
    
    reg weight c.length##c.displacement##c.length##c.displacement
    predict yhat
    
    twoway contour yhat length displacement, clegend(off) crule(intensity)  ///
        scolor(gold*.1) ecolor(gold) levels(50) ///
        || contourline yhat length displacement,  plegend(off) scolor(gs12) ecolor(gs8) ///
         crule(linear) colorlines levels(8) clw(thin thin thin thin thin) /// 
        || scatter length displacement, mc(black) xlabel(minmax) ylabel(minmax) xsize(10) ysize(10) name(correct, replace)
    
    forvalue i = 55(1)74{
        replace displacement=. in `i'
    }
    
    twoway contour yhat length displacement, clegend(off) crule(intensity)  ///
        scolor(gold*.1) ecolor(gold) levels(50) ///
        || contourline yhat length displacement,  plegend(off) scolor(gs12) ecolor(gs8) ///
         crule(linear) colorlines levels(8) clw(thin thin thin thin thin) /// 
        || scatter length displacement, mc(black) xlabel(minmax) ylabel(minmax) xsize(10) ysize(10) name(incorrect, replace)
    
    graph combine correct incorrect, xsize(10) ysize(5)
    Which renders:
    Click image for larger version

Name:	Contour_Plot_auto_price_error.png
Views:	1
Size:	135.2 KB
ID:	1563876


    I am using Stata 16.1 (Revision May, 20), so, I think this is a flaw that has to be corrected by Stata Corp.
    Untill that is solved, my workaround is to include an if constraint on either the x or y variable, or both, for data sets with missings. Note that this must be repeated both for contour and contourline should each be used (but, in this example, not for scatter):

    Code:
    * Workaround
    twoway contour yhat length displacement if displacement!=. , clegend(off) crule(intensity)  ///
        scolor(gold*.1) ecolor(gold) levels(50) ///
        || contourline yhat length displacement if displacement!=. ,  plegend(off) scolor(gs12) ecolor(gs8) ///
         crule(linear) colorlines levels(8) clw(thin thin thin thin thin) /// 
        || scatter length displacement, mc(black) xlabel(minmax) ylabel(minmax) xsize(10) ysize(10) name(workaround, replace)
    Best,
    Eric
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Dear Eric,

      thank you very much for checking and confirming with the updated version.Yes, the workaround is clear when the reason for the problem is identified, but I was originally puzzled when I encountered that earlier in a real project, where it was not so obvious:
      https://www.statalist.org/forums/for...-contour-plots

      So, also hope this will be fixed later by the developers.

      Best, Sergiy Radyakin

      Comment

      Working...
      X