Empirical Distribution Function and Confidence Interval

The following function will graph the empirical distribution function and confidence interval if you give it a sorted data set and the appropriate value that goes with the sample size and a level according to the chapter handed out in class.

edf<-function(x,d=0){

plot(c(min(x)-1,max(x)+1),c(0,1),type="n",xlab="x",ylab="p")

lines(c(x[1]-1,x[1]),c(0,0),lty=2)

for (i in 1:(length(x)-1)){

lines(c(x[i],x[i+1]),c(i/length(x),i/length(x)),lty=2)}

lines(c(x[length(x)],x[length(x)]+1),c(1,1),lty=2)

lines(c(x[1]-1,x[1]),c(0,0),lwd=3)

for (i in 1:(length(x)-1)){

lines(c(x[i],x[i+1]),

c(max(i/length(x)-d,0),max(i/length(x)-d,0)),lwd=3)}

lines(c(x[length(x)],x[length(x)]+1),c(1-d,1-d),lwd=3)

lines(c(x[1]-1,x[1]),c(d,d),lwd=3)

for (i in 1:(length(x)-1)){

lines(c(x[i],x[i+1]),

c(min(i/length(x)+d,1),min(i/length(x)+d,1)),lwd=3)}

lines(c(x[length(x)],x[length(x)]+1),c(1,1),lwd=3)

}

 

The following code will generate the data and plot the confidence interval for a (c2(1)-1)/sqrt(2), and then draw the cumulative distribution function for a normal over it.

x<-sort((rchisq(45,1)-1)/sqrt(2))

 

edf(x,.2)

x2<-(((trunc(x[1])-1)*20):((trunc(x[length(x)])+1)*20))/20

y2<-pnorm(x2,0,1)

par(new=T)

plot(x2,y2,xlab="",ylab="",type="l",xlim=c(min(x)-1,max(x)+1),

ylim=c(0,1))