UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Teaching Tools and Macros
Correlation Demo Macros

The correlation coefficient measures the strength of relations between two variables. Sometime, we may want to visualize relationship between two variables with a given correlation coefficient. This page shows two SAS macros that we developed for these purposes. 

This link will take you to a SAS macro called %corrdemo.  This link will take you to the help file for the macro %corrdemo.  This link will take you to a SAS macro called %corrdemoa.  This link will take you to the help file for the macro %corrdemoa.

Using %corrdemo and %corrdemoa

The two macros %corrdemo and %corrdemoa are used to demonstrate the correlation coefficient using scatter plots. Basically, the macros draw random samples from a population with given correlation coefficient and then plot the scatter plots. It is quite natural that the correlation coefficient of the sample may not be exactly the same as the population because of random errors. The default title for the plots uses the population correlation coefficient.

%corrdemo

Given a correlation coefficient and a sample size,  the macro will draw a sample of the given size from a population of two variables with the given correlation coefficient and plot the scatter plot of sample. Here are some examples.

As shown below, you can call the %corrdemo macro and it will default to a correlation of .7 and a sample size of 100, for example if we run

%corrdemo

the macro creates a graph like the one shown below.

We can explicitly indicate sample size and correlation coefficient via the size= and corr= options, such as: 

%corrdemo(size=500, corr=0.9)

and we would get a graph like the one shown below in the SAS graph window.

You can also redirect the output to a gif file, say to a file called corr9.gif in your C:\temp directory by doing the following. 
%corrdemo(size=500, corr=0.9, outfile="c:\temp\corr9.gif")

You can also add a regression line to the plot by adding a symbol option shown below.

%corrdemo(size=500, corr=0.7, outfile="c:\temp\corr7.gif", symbol = i=r )

The background color or the symbol color can be changed by adding a plot option or a symbol option. Here we used symbol = c=red to change the symbol color to red from yellow and used plot = cframe = liy to change the background color to light yellow.

%corrdemo(size=500, corr=-.8, outfile="c:\temp\corr8.gif", symbol = c=red, plot=cframe=liy)

We can also add reference lines by adding a plot option such as plot=vref=(-3 0 3) if we want to add three reference lines at y=-3, y=0 and y=3.

%corrdemo(size=200, corr=0.2, outfile="c:\temp\corr2.gif", plot= vref=(-3 0 3))

Basically, any legal symbol options that SAS allows for proc gplot can be used with our macro. For example, if we want to add confidence limits for mean predicted values, we can use the SAS option symbol i = rclm95 as follows. 

%corrdemo(size=200, corr=-0.75, outfile="c:\temp\corr_2.gif", symbol=i=rlclm95)

Another choice is to use symbol option i=rlcli95 to display confidence limits for individual predicted values as shown below.

%corrdemo(size=200, corr=-0.75, outfile="c:\temp\corr_2.gif", symbol=i=rlcli95)

Occasionally, you may not want the correlation coefficient appear on the plot or you simply want to change the title of the plot. You can just add a title= option to the macro as shown below. 

%corrdemo(size=200, corr=-0.70, outfile="c:\temp\whatcorr.gif", 
          title="What is the Correlation Coefficient?")

 

corrdemoa

This macro creates animated gif files of scatter plots with different correlation coefficients. For example, without any argument, simply call the macro will create a gif file called corrdemoa.gif in your C:\temp directory. The correlation coefficients will change from -1 to +1 by 0.1. The delay is set to be 25ms that controls how fast the gif file changes each of its frame. 
%corrdemoa

You can also use most of the proc gplot options similar to the way with the macro %corrdemo to obtain the desired settings. For example, you can change the range of correlation coefficients and the background color as shown below. We set the lowest correlation coefficient to be -.9 and the highest to be .9. We also set the outer background color to be light green. 

%corrdemoa(size=200, lo=-.9, hi=.9, gopt=cback=lig, out="c:\temp\corr9a.gif")

In the following example, we change the delay from 25ms to 50 ms, so it is easier to see the changes. We also change the increment from the default 0.1 to .05 to have more frames. We will restrict the correlation coefficients from -1 to 0. 

%corrdemoa(size=200, lo=-1, hi=0, gopt=delay=50, inc=0.05, out="c:\temp\corr10a.gif")

The following example shows how to change the colors of different parts of the plot. Notice, we also set the lowest correlation coefficient to be -1 and highest to be 0.

%corrdemoa(lo=-1, hi=0, gopt=delay=50 cback=blue ctext=white, 
          plot=cframe=snow, symbol=c=black,
          inc=0.05, out="c:\temp\corr11a.gif")


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California