## OFC simulated data
mu = 34.5; s=1.5
x <- round(rnorm(30000,mu,s))
x0 = seq(min(x)-sd(x),max(x)+sd(x), length=100)
f0 = dnorm(x0, mu,s)
est0 = density(x); est0$bw
plot(est0, type="l")
lines(f0~x0, col=1, lty=2)
## rule-of-thumb
est1 <- wkde(x)
lines(est1, col=2)
est2 <- wkde(x, bandwidth='wnrd')
lines(est2, col=4)
est3 <- wkde(x, bandwidth='wmise')
lines(est3, col=5)
est4 <- wkde(x, bandwidth='awmise')
lines(est4, col=6)
legend(max(est0$x), max(est0$y), xjust=1,yjust=1,
legend=c("density", "wnrd0","wnrd","wmise","awmise","True"),
lty=c(1,1,1,1,1,2), col=c(1,2,4,5,6,1))
### Faithful data
data(faithful)
x <- faithful$eruptions
est0 = density(x); est0$bw
est <- wkde(x, bandwidth=0.25)
plot(est0, type="l")
## rule-of-thumb
est1 <- wkde(x)
lines(est1, col=2)
est2 <- wkde(x, bandwidth='wnrd')
lines(est2, col=4)
est3 <- wkde(x, bandwidth='wmise')
lines(est3, col=5)
est4 <- wkde(x, bandwidth='awmise')
lines(est4, col=6)
legend(max(est0$x), max(est0$y), xjust=1,yjust=1,
legend=c("density", "wnrd0","wnrd","wmise","awmise"),
lty=c(1,1,1,1,1), col=c(1,2,4,5,6))
## biased sampling
n = 1000; mu = 34.5; s = 5
x = rnorm(n,mu,s); x = sort(x)
fx = function(x,mu,s) dnorm(x,mu,s)*x/mu # length biasing
x0 = seq(min(x)-s,max(x)+s,length=100)
f0 = fx(x0,mu,s)
fn = dnorm(x0, mu, s)
plot(f0~x0, type='l',col=2, lwd=2)
lines(fn~x0, lty=2, col=2, lwd=2)
lines(density(x), col=4, lwd=2)
f1 = wkde(x,w=x)
lines(f1, col=1, lwd=2)
f2 = wkde(x,w=x, bandwidth='wmise')
lines(f2, col=1, lty=2, lwd=2)
f3 = wkde(x,w=x, bandwidth='awmise')
lines(f3, col=1, lty=3, lwd=2)
legend(max(x0), max(f0), xjust=1,yjust=1,
legend=c("true", "biased","density(...)","f.rot","wmise","awmise"),
lty=c(1,2,1,1,2,3), col=c(2,2,4,1,1,1),lwd=c(2,2,2,2,2,2))
Run the code above in your browser using DataLab