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!
data bivnormal;
seed = 1283470;
do i = 1 to 1000;
z1 = rannor(seed);
z2 = rannor(seed);
z3 = rannor(seed);
x = 3*z1+z2;
y = 3*z1+z3;
output;
end;
drop seed;
run;
1
DATA 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;
12
RUN;
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!
ods graphics on;
proc kde data=bivnormal;
bivar x y / bwm=2;
run;
proc kde data=bivnormal;
bivar (x y) (x (bwm=0.5) y (bwm=2));
run;
ods graphics off;
1
ods graphics on;
2
PROC KDEDATA=bivnormal;
3
bivar x y / bwm=2;
4
RUN;
5
6
PROC KDEDATA=bivnormal;
7
bivar (x y) (x (bwm=0.5) y (bwm=2));
8
RUN;
9
ods graphics off;
3 Code Block
PROC KDE
Explanation : Request for additional output tables: bivariate statistics, density levels, percentiles, and univariate statistics.
Copied!
proc kde data=bivnormal;
bivar x y / bivstats levels percentiles unistats;
run;
1
2
PROC KDE
3
DATA=bivnormal;
4
bivar x y / bivstats levels percentiles unistats;
5
RUN;
6
4 Code Block
PROC KDE
Explanation : Bivariate estimation with explicit specification of contour levels and percentiles to calculate.
Copied!
proc kde data=bivnormal;
bivar x y / levels=2.5, 50, 97.5
percentiles=2.5, 25, 50, 75, 97.5;
run;
1
PROC KDEDATA=bivnormal;
2
bivar x y / levels=2.5, 50, 97.5
3
percentiles=2.5, 25, 50, 75, 97.5;
4
RUN;
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!
ods graphics on;
proc kde data=bivnormal;
univar x / plots=(density histogram histdensity);
univar x y / plots=densityoverlay;
run;
ods graphics off;
1
ods graphics on;
2
PROC KDEDATA=bivnormal;
3
univar x / plots=(density histogram histdensity);
4
univar x y / plots=densityoverlay;
5
RUN;
6
ods 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
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.