2020-11-18

Kernel Density Estimation

Sampling distribution

data(mtcars)
hist(mtcars$mpg)

Colored Histogram with Different Number of Bins

hist(mtcars$mpg, breaks=12, col="red")

Kernel Density Plot

d <- density(mtcars$mpg) # returns the density data 
plot(d) # plots the results

Kernel Density Plot with 1/3rd bandwith

d <- density(mtcars$mpg,adjust = 1/3) # returns the density data 
plot(d) # plots the results

Kernel Density Plot with 2x bandwith

d <- density(mtcars$mpg,adjust = 2) # returns the density data 
plot(d) # plots the results

Kernel Density Plot with a rectangular kernel

d <- density(mtcars$mpg, kernel = "rectangular") # returns the density data 
plot(d) # plots the results

Kernel Density Plot with the Epanechnikov kernel

d <- density(mtcars$mpg, kernel = "epanechnikov") # returns the density data 
plot(d) # plots the results