- /*Producing a similar plot to the density plot at:
- http://www.survey-design.com.au/Usergraphs.html
- using Stata 12's new contour plot
- Requires: spgrid, spkde & mylabels
- to download these programs type the following
- on the Stata command line (if not already loaded):
- 耗时约3分钟
- */
- ssc install spgrid
- ssc install spkde
- ssc install mylabels
- sysuse "auto.dta", clear
- set more off
- summarize price mpg
- clonevar x = mpg
- clonevar y = price
- replace x = (x-0) / (50-0)
- replace y = (y-0) / (20000-0)
- mylabels 0(10)50, myscale((@-0) / (50-0)) local(XLAB)
- mylabels 0(5000)20000, myscale((@-0) / (20000-0)) local(YLAB)
- keep x y
- save "xy.dta", replace
- * 2. Generate a 100x100 grid
- spgrid, shape(hexagonal) xdim(100) ///
- xrange(0 1) yrange(0 1) ///
- dots replace ///
- cells("2D-GridCells.dta") ///
- points("2D-GridPoints.dta")
- * 3. Estimate the bivariate probability density function
- spkde using "2D-GridPoints.dta", ///
- xcoord(x) ycoord(y) ///
- bandwidth(fbw) fbw(0.1) dots ///
- saving("2D-Kde.dta", replace)
- use "2D-Kde.dta", clear
- merge 1:1 _n using xy.dta
- twoway (contour p spgrid_ycoord spgrid_xcoord if p!=0 , ///
- levels(15)) ///
- (scatter y x, mcolor(black) msize(small) ) ///
- ,xlab(`XLAB', nogrid) xtitle("Mileage (mpg)") ///
- ylab(`YLAB', nogrid) ///
- ytitle("Price $US") plotregion(color(blue))


雷达卡


京公网安备 11010802022788号







