[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: 3D surface plot of joint density

From   "maurizio.pisati" <>
To   <>
Subject   Re: st: 3D surface plot of joint density
Date   Thu, 03 Sep 2009 09:19:05 +0200

In Stata, you can draw 2D surface plots using a combination of user-written programs. Here's an example taken from the help file of the user-written program -spkde-, available from the Boston SSC Archive:

-spkde- can be used also for estimating the joint probability density function of any pair of quantitative variables (for an alternative, see Stata program -kdens2-, written by Christopher F. Baum and available from the Boston SSC Archive). The user-written program -spmap- (available from the Boston SSC Archive) can then be used to draw the corresponding 2D density plot. To this purpose, it is advised to make use of -mylabels-, a Stata program written by Nicholas J. Cox and available from the Boston SSC Archive.

As an example, let us estimate and plot the bivariate probability density function for two of the variables included in the auto dataset: -mpg- and -price-. This can be done in four steps as follows:

* 1. Normalize variables in the range [0,1]

sysuse "auto.dta", clear
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")          ///

* 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)

* 4. Draw the density plot

use "2D-Kde.dta", clear
recode lambda (.=0)
spmap lambda using "2D-GridCells.dta",      ///
 id(spgrid_id) clnum(20) fcolor(Rainbow)   ///
 ocolor(none ..) legend(off)               ///
 point(data("xy.dta") x(x) y(y))           ///
 freestyle aspectratio(1)                  ///
 xtitle(" " "Mileage (mpg)")               ///
 xlab(`XLAB')                              ///
 ytitle("Price" " ")                       ///
 ylab(`YLAB', angle(0))

Best wishes,
Maurizio Pisati

dgill wrote:
I am trying to generate a graph of the joint density of
two variables (bmi and age).
I am aware of the 3 variable graphing packages such
as scat3 and gr39, but those ask for 3 separate variables.
I want my 3rd dimension to be the joint density, not
another variable.

My data are individuals followed over time. So I have
their age and their body mass for each year of their

Can anyone point me in the right direction?

-Donna Gilleskie

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index