Chapter 12 - Probability Density Estimation
Marquette University MATH 4750 - Spring 2025
set.seed(12345) n <- 25 x <- rnorm(n) # Calculate breaks according to Sturges' Rule nclass <- ceiling(1 + log2(n)) cwidth <- diff(range(x) / nclass) breaks <- min(x) + cwidth * 0:nclass # Default histogram h.default <- hist(x, freq = FALSE, xlab = "default", main = "Histogram: Default")
# Kernel density estimate dens <- density(x) plot(dens, main = "Kernel Density Estimate", col = "blue", lwd = 2)
# Overlay histogram and KDE hist(x, freq = FALSE, col = "lightgray", border = "white", main = "Histogram with KDE") lines(dens, col = "red", lwd = 2)