Today I found such an interesting function called “density”, this function computes kernel density estimates, that’s why I found it pretty interesting, all you need is:

- the the data from which the estimate is to be computed
- the smoothing kernel to be used (This must be one of
`"gaussian"`

, `"rectangular"`

, `"triangular"`

, `"epanechnikov"`

, `"biweight"`

, `"cosine"`

or `"optcosine"`

, with default `"gaussian"`

, and may be abbreviated to a unique prefix -single letter.)

For example, I used some of the datasets included in R to use this function with different kernels, my first example was using the data set called ‘UKgas’, which contains the Quarterly UK gas consumption from 1960Q1 to 1986Q4, in millions of therms. The 1st image shows the histogram of given data set using a gaussian kernel, while the second image shows the same but using a rectangular kernel, where the diference between both estimations is obvious.

For the 2nd example I used a dataset called ‘Treering’, which contains normalized tree-ring widths in dimensionless units, here the 2nd image uses a gaussian kernel, and the image on the left uses a rectangular kernel, where the difference between both estimations again is obvious.

Now, from the statistical point of view, if we type on R density(treering), we will get the next:

Which shows the basic statistics for the density estimation, another reason why I found this function pretty interesting and useful.

To finish with this post, I will add the code used for the examples, have a great day!

*par(mfrow=c(1,2))*

*hist(treering,prob=1,breaks=20)*

*lines(density(treering,kernel=”gaussian”),col=2)*

*hist(treering,prob=1,breaks=20)*

*lines(density(treering,kernel=”rectangular”),col=2)*

*density(treering)*

*par(mfrow=c(1,2))*

*hist(UKgas,prob=1,breaks=20)*

*lines(density(UKgas,kernel=”gaussian”),col=2)*

*hist(UKgas,prob=1,breaks=20)*

*lines(density(UKgas,kernel=”rectangular”),col=2)*

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** The power of R**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...