Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the kernel bandwidth used in a density plot in R

Tags:

r

ggplot2

How do I see what bandwidth gets used for kernels in a density plot and how do I specify a bandwidth to be used? I tried

ggplot(mtcars,aes(mpg))+geom_density(bw=1)

with no luck.

like image 894
Ben Avatar asked Jul 19 '13 20:07

Ben


People also ask

How does changing the bandwidth affect the density estimation curve?

Changing the bandwidth changes the shape of the kernel: a lower bandwidth means only points very close to the current position are given any weight, which leads to the estimate looking squiggly; a higher bandwidth means a shallow kernel where distant points can contribute.

How do you plot kernel density estimation in R?

The density() function in R computes the values of the kernel density estimate. Applying the plot() function to an object created by density() will plot the estimate. Applying the summary() function to the object will reveal useful statistics about the estimate.

What is bandwidth in density plot?

The bandwidth defines how close to r the distance between two points must be to influence the estimation of the density at r. A small bandwidth only considers the closest values so the estimation is close to the data. A large bandwidth considers more points and gives a smoother estimation.

What is adjust in geom_density?

You can set multiple properties within the geom_density layer. One of them is the adjust property – multiplicative bandwidth adjustment. Default value is set to 1. The following figure shows how the density plot changes when the adjust property is reduced.


1 Answers

stat_geom utilises the adjust argument to apply a multiplier to the optimal bandwidth that ggplot calculates see documentation for density(). Try:

ggplot(mtcars,aes(mpg))+geom_density() + stat_density(adjust = 2)

I gather to determine the calculated optimal bandwidth - based on "the standard deviation of the smoothing kernel" - you'll need to interrogate Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. New York: Springer.

like image 75
geotheory Avatar answered Oct 04 '22 14:10

geotheory