Tal,
I ran the following normal data through the macro and drew a density ..
you can adjust the size and position of the graph with options on the axis
statements.
data indat;
do i = 1 to 500; y = rannor(1929); output; end;
run;
GOPTIONS reset=all gunit=pct FTEXT=swissb ROTATE=landscape
htitle=3 htext=3 CBACK=white ctext=black PAPERSIZE=(11,8.5);
axis1 label=(angle=90) length=6.5 in order=(0 to .5 by 0.1) label=(a=90
'Density') value=(h=2) minor=none;
axis2 order=(-4 to 4 by 0.5) length=8 in offset=(.5 cm) value=(h=1.5)
minor=none origin=(1.5 in, 1.5 in);
pattern1 v=solid c=grey;
symbol1 v=none i=join c=black;
* I eliminated the call to boxaxis, since not found in this macro;
%*boxaxis(data=data, out=boxanno, var=NOX, pos=96, boxwidth=5);
%density(data=indat, var=y,
out=densplot111,
vaxis=axis1,
haxis=axis2,
symbol=symbol1,
window=.7, /* Bandwidth (H) -- determines how smooth the density
is plotted (smaller ==> more jagged ) */
showobs=YES, /* = NO to remove small vertical bars at bottom */
plotopt=%str(areas=0 href=0 lhref=1)); * area=1 --> fill with
color;
The data plotted on the graph is in the out= dataset.
Robin High
UNMC
tal < XXXX@XXXXX.COM >
Sent by: "SAS(r) Discussion" < XXXX@XXXXX.COM >
06/09/2009 07:56 AM
Please respond to
tal < XXXX@XXXXX.COM >
To
XXXX@XXXXX.COM
cc
Subject
density function (I wanted to add an attachment- but couldn't figure out
how, can I send a post to the group's mail address? what is
is?)
Hi all,
I'm trying to plot the density function of several variables in my
data set.
I found a macro which I dont understand completely, and I tried to use
it.
1- Can I count on the density out file that it's the density function
(when i plot it with G Plot at the end)?
2- The grap I get is very small comparing to the window. I tried to
change the window parametter ,but it didn't help. I also get Black
lines that I don't need- how do i get rid of them?
/*-------------------------------------------------------------------*
* Name: density.sas *
* Title: Nonparametric density estimates from a sample. *
* Doc: http://www.math.yorku.ca/SCS/sssg/density.html *
* *
* User chooses a bandwidth parameter to balance smoothness and bias *
* and the range of the data over which the density is to be fit. *
*-------------------------------------------------------------------*
* Author: Michael Friendly < XXXX@XXXXX.COM > *
* Created: 23 Mar 1989 16:21:12 *
* Revised: 28 May 2004 16:21:271 *
* Version: 1.1 *
* -handle special missing values (.A-.Z) *
* -fixed variable names for V7+ (thx: Dietrich Alte) *
* From ``SAS System for Statistical Graphics, First Edition'' *
* Copyright(c) 1991 by SAS Institute Inc., Cary, NC, USA *
* *
*-------------------------------------------------------------------*
* Original program by: C. ROGER LONGBOTHAM *
* while at Rockwell International, Rocky Flats Plant *
* From: SAS SUGI 12, 1987, 907-909. *
*-------------------------------------------------------------------*/
%macro density(
data=_LAST_, /* Name of input data set */
out=DENSPLOT, /* Name of output data set */
var=X, /* Input variable (numeric) */
window=20, /* Bandwidth (H) */
xfirst=., /* . or any real; smallest X value */
xlast=., /* . or any real; largest X value */
xinc=., /* . or value>0; X-value increment */
/* Default: (XLAST-XFIRST)/60 */
gplot=YES,
symbol=,
haxis=, /* AXIS statement for horizontal axis */
vaxis=, /* and for vertical axis- use to equate axes */
plotopt=, /* other plot options, eg %str(areas=1) to fill */
anno=,
showobs=YES,
name=density
);
%*-- Remove missing data;
%local abort;
%let abort=0;
data _in_;
set &data;
keep &var;
if &var > .Z;
run;
%if &syserr > 4 %then %let abort=1; %if &abort %then %goto DONE;
%*-- Get the variable label;
proc contents data=_in_ out=_work_ noprint;
%if &syserr > 4 %then %let abort=1; %if &abort %then %goto DONE;
data _null_;
set _work_(keep=name type label);
if upcase(name) = upcase("&var") then do;
if label=' ' then label="&var";
call symput('vlabel', label);
end;
run;
proc sort data=_in_;
by &var;
proc iml;
start WINDOW; *-- Calculate default window width;
mean = xa[+,]/n;
css = ssq(xa - mean);
stddev = sqrt(css/(n-1));
q1 = floor(((n+3)/4) || ((n+6)/4));
q1 = (xa[q1,]) [+,]/2;
q3 = ceil(((3*n+1)/4) || ((3*n-2)/4));
q3 = (xa[q3,]) [+,]/2;
quartsig = (q3 - q1)/1.349;
h = .9*min(stddev,quartsig) * n##(-.2); * Silvermans formula;
file log;
put 'NOTE: Using calculated default window width, window=' h;
finish;
start INITIAL; *-- Translate parameter options;
if xf=. then xf=xa[1,1];
if xl=. then xl=xa[n,1];
if xl <= xf then do;
file log;
put 'ERROR: Either largest X value chosen is too small';
put ' or all data values are the same';
stop;
end;
if dx=. | dx <= 0 then do;
inc = (xl-xf)/60;
rinc = 10 ## (floor(log10(inc))-1);
dx = round(inc,rinc);
end;
if xf=xa[1,1] then xf=xf-dx;
nx = int((xl-xf)/dx) + 3;
finish;
*-- calculate density at specified x values;
start DENSITY;
fnx = j(nx,3,0);
vars = {"DENSITY" "&VAR" "WINDOW"};
create &out from fnx [colname=vars];
sigmasqr = .32653; * scale constant for kernel ;
gconst = sqrt(2*3.14159*sigmasqr);
nuh = n*h;
x = xf - dx;
do i = 1 to nx;
x = x + dx;
y = (j(n,1,x) - xa)/h;
ky = exp(-.5*y#y / sigmasqr) / gconst; * Gaussian kernel;
fnx[i,1] = sum(ky)/(nuh);
fnx[i,2] = x;
end;
fnx[,3] = round(h,.001);
append from fnx;
finish;
*-- Main routine ;
use _in_;
read all var "&var" into xa [colname=invar];
n = nrow(xa);
%if &window=%str() %then %do;
run window;
%end;
%else %do;
h = &window ;
%end;
xf = &xfirst;
xl = &xlast;
dx = &xinc;
run initial;
run density;
close &out;
quit;
%*-- Restore variable label;
data &out;
label &var = "&vlabel";
set &out;
%if &gplot = YES %then %do;
%if %length(&symbol)=0 %then %do;
symbol1 v=star i=join c=black;
%end;
%let annoopt=;
%if %length(&anno) > 0 %then %let annoopt = anno=&anno;
%if &showobs = YES %then %do;
data annoobs;
set &data;
retain xsys '2' ysys '1' when 'A';
x = &var;
y = 0; function='MOVE '; output;
y = 3; function='DRAW '; output;
%let annoopt=anno=annoobs;
%if %length(&anno) > 0 %then %do;
data annoobs;
set annoobs &anno;
when='A';
%end;
%end;
proc gplot data=&out ;
plot density * &var / frame &plotopt &annoopt
name="&name"
des="Density plot of &var"
%if %length(&haxis)>0 %then %do;
haxis = &haxis
%end;
%if %length(&vaxis)>0 %then %do;
vaxis = &vaxis
%end;
;
run ;
%end;
%done:
%mend;
axis1 order=(0 to .5 by 0.01) label=(a=90 'Density')
offset=(3,)
;
pattern1 v=solid c=red;
symbol1 v=none i=join c=black;
%boxaxis(data=data.data, out=boxanno, var=NOX, pos=96, boxwidth=5);
%density(data=data.data, var=O3, out=densplot111,vaxis=axis1,
haxis=axis2,
symbol=symbol1,
anno=boxanno,
plotopt=%str(areas=1));
proc gplot data=densplot111;
plot density * O3;
symbol1 i=join v=none;