# Dotplot x=c(-2.2,-.7,-.3,.1,1.4,2.0) plot(x,rep(0,6),ylim=c(-0.1,2),ylab="",yaxs=NULL,xlim=c(-3,3),main="Dotplot") #Density estimate at a single point #Weighting function for density estimate at x=-1 plot(x,rep(0,6),ylim=c(-0.1,2),ylab="",yaxs=NULL,xlim=c(-3,3),main=expression(paste("Normal kernel with ", sigma,"=.5 at X=-1"))) curve(dnorm(x,mean=-1,sd=.5),from=-3,to=3,add=TRUE) lines(c(-1,-1),c(0,2),lwd=2,col="red") #Weight assignments plot(x,rep(0,6),ylim=c(-0.1,2),ylab="",yaxs=NULL,xlim=c(-3,3),main="Density Estimate at X=-1",sub="(for probability histogram, scale by 1/n)") curve(dnorm(x,mean=-1,sd=.5),from=-3,to=3,add=TRUE) for(i in 1:6){ lines(c(x[i],x[i]),c(0,dnorm(x[i],mean=-1,sd=.5)),col=i,lwd=2) } text(1.6,.2,"Density too small to see lines \n for these two points",cex=.8) dxm1=c(0,cumsum(dnorm(x,mean=-1,sd=.5))) for(i in 1:6){ lines(c(-1,-1),c(dxm1[i],dxm1[i+1]),col=i,lwd=2) } #Histogram with density estimates computed over a grid hist(x,freq=FALSE,main="Histogram with Density Estimates Computed Over a Grid") nj=length(seq(-3,3,by=.2)) for(j in seq(-3,3,by=.2)){ dxm1=sum(dnorm(x,mean=j,sd=.5))/6 lines(c(j,j),c(0,dxm1),lwd=2,col="red") } #Finer grid hist(x,freq=FALSE,main="Histogram with Two Density Estimates Over a Fine Grid") nj=length(seq(-3,2,by=.01)) dx=rep(0,nj) ict=1 for(j in seq(-3,2,by=.01)){ dx[ict]=sum(dnorm(x,mean=j,sd=.5))/6 ict=ict+1 } lines(seq(-3,2,by=.01),dx,col="red",lwd=2) nj=length(seq(-3,2,by=.01)) dx=rep(0,nj) ict=1 for(j in seq(-3,2,by=.01)){ dx[ict]=sum(dnorm(x,mean=j,sd=1))/6 ict=ict+1 } lines(seq(-3,2,by=.01),dx,col="blue",lwd=2) points(x,rep(0,6)) legend(-2.5,.25,c(expression(paste(sigma,"=.5")),expression(paste(sigma,"=1"))),lwd=c(2,2),col=c("red","blue"))