Published on :
Statistics CREATION_INTERNE

PROC KDE Examples: Kernel Density Estimation

This code is also available in: Deutsch Español Français
Awaiting validation
This script generates simulated bivariate data and applies the KDE procedure to estimate densities. It explores the impact of the bandwidth multiplier (bwm), requests descriptive statistics and custom percentiles, and produces various univariate and bivariate plots via ODS Graphics.
Data Analysis

Type : CREATION_INTERNE


The 'bivnormal' data is generated by a Data Step using the rannor function to create 1000 observations of a bivariate normal distribution.

1 Code Block
DATA STEP Data
Explanation :
Creation of a 'bivnormal' dataset containing 1000 simulated observations. Variables x and y are constructed from random normal draws to introduce correlation.
Copied!
1DATA bivnormal;
2 seed = 1283470;
3 DO i = 1 to 1000;
4 z1 = rannor(seed);
5 z2 = rannor(seed);
6 z3 = rannor(seed);
7 x = 3*z1+z2;
8 y = 3*z1+z3;
9 OUTPUT;
10 END;
11 drop seed;
12RUN;
2 Code Block
PROC KDE
Explanation :
Execution of bivariate density estimation with bandwidth modification. The first execution uses a global multiplier (bwm=2), the second specifies different multipliers for x and y. ODS Graphics is enabled to visualize the results.
Copied!
1ods graphics on;
2PROC KDE DATA=bivnormal;
3 bivar x y / bwm=2;
4RUN;
5 
6PROC KDE DATA=bivnormal;
7 bivar (x y) (x (bwm=0.5) y (bwm=2));
8RUN;
9ods graphics off;
3 Code Block
PROC KDE
Explanation :
Request for additional output tables: bivariate statistics, density levels, percentiles, and univariate statistics.
Copied!
1 
2PROC KDE
3DATA=bivnormal;
4bivar x y / bivstats levels percentiles unistats;
5RUN;
6 
4 Code Block
PROC KDE
Explanation :
Bivariate estimation with explicit specification of contour levels and percentiles to calculate.
Copied!
1PROC KDE DATA=bivnormal;
2 bivar x y / levels=2.5, 50, 97.5
3 percentiles=2.5, 25, 50, 75, 97.5;
4RUN;
5 Code Block
PROC KDE
Explanation :
Generation of complete univariate plots: density alone, histogram alone, and histogram/density overlay for x. An overlay of x and y densities is also requested.
Copied!
1ods graphics on;
2PROC KDE DATA=bivnormal;
3 univar x / plots=(density histogram histdensity);
4 univar x y / plots=densityoverlay;
5RUN;
6ods graphics off;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : SAS SAMPLE LIBRARY, NAME: KDEX2, PRODUCT: SAS