# The density function

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**The power of R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)*

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.