Assignment_week03_June Yao_500316995
1 Parametric density estimation
2 Parametrics and Nonparametric density estimation
2.1 Basic exploratory data analysis
height = read.delim("height-1.txt")
par(mfrow=c(2,2))
hist(height[,2], probability = TRUE, breaks = 5,main = "Histogram of The Heights", xlab = "Heights (m)")
hist(height[,2], probability = TRUE, breaks = 10,main = "Histogram of The Heights", xlab = "Heights (m)")
hist(height[,2], probability = TRUE, breaks = 15,main = "Histogram of The Heights", xlab = "Heights (m)")
hist(height[,2], probability = TRUE, breaks = 20,main = "Histogram of The Heights", xlab = "Heights (m)")2.2 Kernel density estimation
d1 <- density(height[,2], bw = 0.01, kernel = "gaussian")
d2 <- density(height[,2], bw = 0.01, kernel = "epanechnikov")
d3 <- density(height[,2], bw = 0.01, kernel = "triangular")
hist(height[,2], freq = FALSE, breaks = 20, main = "Histogram of The Heights", xlab = "Heights (m)")
lines(d1$x, d1$y, col = "red")
lines(d2$x, d2$y, col = "green2")
lines(d3$x, d3$y, col = "purple")
legend("topright",c("Gaussian","Epanechnikov", "Triangular"),
lty = c(1, 1, 1), col = c("red", "green2", "purple"))3 Bandwidth selection
3.1 Integrated square error
StandardNorm <- rnorm(100, mean = 0, sd = 1)
ISE <- function(bw) {
ds <- density(x = StandardNorm, from = -3, to = 3, n = 512,bw=bw)
result <- suppressWarnings((ds$y - dnorm(ds$x))^2)
return(sum(result))
}
bw=seq(0.01, 1, length=10)
ISER=sapply(bw, ISE)
plot(bw,ISER,type="l",ylab="ISE")ISERmin=min(ISER)
optimalbw=bw[which(ISER==ISERmin)]
#list(bw,ISER)
print(cat('min ISE',ISERmin,'\noptimal bandwidth:',as.double(optimalbw)))## min ISE 0.1638532
## optimal bandwidth: 0.34NULL
optimalds=density(x = StandardNorm, from = -3, to = 3, n = 512,bw=optimalbw)
plot(optimalds$x,dnorm(optimalds$x),col='red',main="Density Estimation vs Normal Distribution Density",xlab="",ylab="density")
lines(optimalds$x,optimalds$y,type="l",xlab="")APP. Student Info
Course: STAT5003_Computational Statistical Methods
Assignment: Lab Week 3
Student Name: Yujun Yao(June Yao)
SID: 500316995
Email: yyao2983@uni.sydney.edu.au