Exploratory Data Analysis – Kernel Density Estimation and Rug Plots in R on Ozone Data in New York and Ozonopolis

Thanks to Harlan Nelson for noting on AnalyticBridge that the ozone concentrations for both New York and Ozonopolis are non-negative quantities, so their kernel density plot should have non-negative support sets. This has been corrected in this post by

– defining new variables called max.ozone and max.ozone2

– using the options “from = 0″ and “to = max.ozone” or “to = max.ozone2″ in the density() function when defining density.ozone and density.ozone2 in the R code.

Update on February 2, 2014:

Harlan also noted in the above comment that any truncated kernel density estimator (KDE) from density() in R does not integrate to 1 over its support set. Thanks to Julian Richer Daily for suggesting on AnalyticBridge to scale any truncated kernel density estimator (KDE) from density() by its integral to get a KDE that integrates to 1 over its support set. I have used my own function for trapezoidal integrationto do so, and this has been added below.

I thank everyone for your patience while I took the time to write a post about numerical integration before posting this correction. I was in the process of moving between jobs and cities when Harlan first brought this issue to my attention, and I had also been planning a major expansion of this blog since then. I am glad that I have finally started a series on numerical integration to provide the conceptual background for the correction of this error, and I hope that they are helpful. I recognize that this is a rather late correction, and I apologize for any confusion.

Introduction

This post follows the recent introduction of the conceptual foundations of kernel density estimation. It uses the “Ozone” data from the built-in “airquality” data set in R and the previously simulated ozone data for the fictitious city of “Ozonopolis” to illustrate how to construct kernel density plots in R. It also introduces rug plots, shows how they can complement kernel density plots, and shows how to construct them in R.

Read the rest of this post to learn how to create the above combination of a kernel density plot and a rug plot!

Example: Ozone Pollution Data from New York and Ozonopolis

Recall that I used 2 sets of ozone data in my last post about box plots. One came from the “airquality” data set that is built into R. I simulated the other one and named its city of origin “Ozonopolis”. Here are the code and the plot of the kernel density estimates (KDEs) of the 2 ozone pollution data sets. I used the default settings in density() – specifically, I used the normal (Gaussian) kernel and the “nrd0” method of choosing the bandwidth. I encourage you to try the other settings. I have used the set.seed() function so that you can replicate my random numbers.

Thanks to Harlan Nelson and Julian Richer Daily, I have learned that the KDE from density() does not integrate to 1 if the support set is truncated with the “from = ” or “to = ” options. To correct this problem, I have used trapezoidal integration to integrate the resulting KDE and divide the KDE by that integral; this scaled KDE will integrate to 1. In my correction below, I saved the function in an R script called “trapezoidal integration.R” in my working directory, and I then called it via the source() function.

It is clear that Ozonopolis has more ozone pollution than New York. The right-skewed shapes of both curves also suggest that the normal distribution may not be suitable. (If you read my blog post carefully, you will already see evidence of a different distribution!) In a later post, I will use quantile-quantile plots to illustrate this. Stay tuned!

To give you a better sense of why the density plots have higher “bumps” at certain places, take a look at the following plot of the ozone pollution just in New York. Below the density plot, you will find a rug plot – a plot of tick marks along the horizontal axis indicating where the data are located. Clearly, there are more data in the neighbourhood between 0 and 50, where the highest “bump” is located. Use the rug() function to get the rug plot in R.