R - Density

> Procedural Languages > R

1 - About

The density function

This function computes kernel density estimates.

The algorithm used in density.default disperses the mass of the empirical distribution function over a regular grid of at least 512 points and then uses the fast Fourier transform to convolve this approximation with a discretized version of the kernel and then uses linear approximation to evaluate the density at the specified points.

3 - Syntax

This function belongs to the stats package

density(x, ...)
## Default S3 method:
density(
   x, 
   bw = "nrd0", 
   adjust = 1,
   kernel = c("gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine"),
   weights = NULL, 
   window = kernel, 
   width,
   give.Rkern = FALSE,
   n = 512, 
   from, 
   to, 
   cut = 3, 
   na.rm = FALSE, 
   ...)

where:

  • kernel is the smoothing kernel to be used (default “gaussian”)
Advertising

4 - Example

4.1 - Code

data = c(78 ,79 ,84 ,85 ,85 ,86 ,87 ,89 ,89 ,90 ,90 ,92 ,92 ,93 ,95 ,95 ,96 ,97 ,97 ,98)
density(data)
Call:
	density.default(x = data)

Data: data (20 obs.);	Bandwidth 'bw' = 2.875

       x                y           
 Min.   : 69.38   Min.   :0.000104  
 1st Qu.: 78.69   1st Qu.:0.005420  
 Median : 88.00   Median :0.021823  
 Mean   : 88.00   Mean   :0.026815  
 3rd Qu.: 97.31   3rd Qu.:0.050371  
 Max.   :106.62   Max.   :0.057345  

4.2 - Plot

4.2.1 - Default

hist(data, freq=FALSE, main="Histogram with Density", xlab="")
lines(density(data))

where in the hist function:

  • freq= FALSE (or prob=TRUE) means that the histogram graphic is a representation of probability densities (not frequencies) ie the histogram has a total area of one

4.2.2 - Ggplot

R - Ggplot

ggplot(data=dataFrame, aes(dataFrame$Col1)) + 
geom_histogram(aes(y =..density..)) + 
geom_density(col=2) + 
labs(title="Title") +
labs(x="X", y="Frequency Count")

5 - Comparison

sm.density.compare(.., .., xlab = "X Axis Label")
Advertising
lang/r/density.txt · Last modified: 2017/06/23 14:13 by gerardnico