## ---------------------------------------------------------------------------- ## R scripts for Haerdle/Mueller/Sperlich/Werwatz: "Nonparametric and ## Semiparametric Modelling", Springer Series in Statistics, 2004 ## ---------------------------------------------------------------------------- ## Script SPMkdeconstruct ## ---------------------------------------------------------------------------- ## Description visualizes the construction of a kernel density ## estimate ## ---------------------------------------------------------------------------- ## Author Marlene Mueller, 2008/11/21 ## ---------------------------------------------------------------------------- h <- 0.22 n <- 10 x <- rnorm(n) xr <- diff(range(x)) ng <- 100 xg <- (xr+7*h)*(0:(ng-1))/(ng-1) + min(x)-3.5*h fk <- matrix(0,nrow=ng,ncol=n) for (j in 1:n){ fk[,j] <- dnorm((xg-x[j])/h)/(n*h) } fh <- rowSums(fk) ylim <- c(-0.1*max(fh),max(fh)) plot(xg,fh,type="l",lwd=2,ylim=ylim, main="Construction of Density Estimate",xlab="Data x",ylab="KDE fh, Weights Kh") points(cbind(x,rep(-0.05*max(fh),n)),col="red",pch=3) for (j in 1:n){ lines(xg,fk[,j],col="blue") }