Created
August 17, 2017 09:43
-
-
Save twolodzko/98223b205bf5561b568021dea0d38c2c to your computer and use it in GitHub Desktop.
Examples of bandwidth choice in kernel density estimation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ptmp <- par(mfrow=c(2,2), mar = c(3,3,3,3)) | |
set.seed(123) | |
n <- 7 | |
x <- rnorm(n, sd = 3) | |
K <- function(x) dnorm(x) | |
kde <- function(x, data, h, K) { | |
n <- length(data) | |
out <- outer(x, data, function(xi,yi) K((xi-yi)/h)) | |
rowSums(out)/(n*h) | |
} | |
xx = seq(-8, 8, by = 0.001) | |
for (h in c(0.25, 0.5, 0.75, 1)) { | |
plot(NA, xlim = c(-4, 8), ylim = c(0, 0.5), xlab = "", ylab = "", | |
main = paste0("h = ", h)) | |
for (i in 1:n) { | |
lines(xx, K((xx-x[i])/h)/n, type = "l", col = rainbow(n)[i]) | |
rug(x[i], lwd = 2, col = rainbow(n)[i], side = 3, ticksize = 0.075) | |
} | |
lines(xx, kde(xx, x, h, K), col = "darkgray") | |
} | |
par(ptmp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment