Creating Kernel Density Plots in R and SPSS

I discuss kernel density plots in Chapter two of Statistical Methods for Psychology, 8ed, but Ido not show there how to use software to generate them. Below are two programs, one for SPSS and one for R that will do what you want.

SPSS Syntax

SPSS does a very nice job of computing kernel density plots, but you can not simply call them up using the drop down menus. Instead you have to use SPSS syntax to issue the instructions, but that is not particularly difficult. For those not familiar with SPSS syntax, it is just a bunch of lines of code that tell SPSS what to do. When you do anything from a drop down menu, SPSS creates its own syntax file, though you don't usually see it. If you get all set to run and analysis and click "Paste" instead of "OK," the program will create a file containing the syntax you have created through the menus. You can then save this to be used later, or you can run it immediately by clicking on the Run button. What is different here is that you won't even try using drop down menus. Assuming that you have already loaded the data file, you will simply copy the syntax below, tell SPSS to create a new syntax file, and paste that syntax in. Then click Run.

The resulting screen will look like the following. Notice the "Run" button in the top line.

I have probably skipped a bit that I need to cover. You will see that the syntax contains variable names. You will obviously have to change those to match the names of your variable.s You might also want to variable labels. I think that this is all you will need to change. The results follow.

The web has a number of sources that may be helpful, and a quick Google search will be profitable. Two sources that I recommend are http://www.ats.ucla.edu/stat/spss/examples/ara/foxch4.html
and
http://school.maths.uwa.edu.au/~duongt/seminars/intro2kde/
I suspect that my syntax may have come from a modification of the first site. The second contains a bit of theory about plots and some of the alternative choices you have. Also, typing "Kernel Density" into the SPSS help window search box will give you a list of possible options you might choose.

R Syntax

The complete syntax to generate a kernel density plot in R is shown below, along with the source. The only thing that you will need to change is the source of the data file and the final line of syntax, where "RxTime" needs to be replaced by your own variable name. If you don't want a normal distribution superimposed on the plot, comment out the line beginning "curve(dnorm....)." If you type ?density in the R command window you will see a variety of options that you can enter in the line that currently says " dens = density(variable)." The resulting output follows.

SAS

I just rebuilt my laptop and have not yet reinstalled SAS on it. However there is a kernel density procedure in SAS named PROX KDE, and a small bit of searching on the web or the SAS help files will show you how to use it. I may add that later.