## simulate some random data from the Binomial n.samples <- 100000 x <- rbinom(n.samples,15,0.5) # compute the emprical pdf my.epdf <- matrix(0,16,2) my.epdf[,1] <- 0:15 my.epdf[ as.numeric(names(table(x)))+1,2] <- table(x)/n.samples my.epdf # the theoretical pdf my.theoreticalpdf <- dbinom(0:15,15,0.5) #error in estimate my.epdf[,2]- my.theoreticalpdf #relative error (my.epdf[,2] - my.theoreticalpdf )/my.theoreticalpdf # compute the empirical cdf my.ecdf <- matrix(0,16,2) my.ecdf[,1] <- 0:15 my.ecdf[ as.numeric(names(table(x)))+1,2] <- cumsum(table(x)/n.samples) my.ecdf[(max(as.numeric(names(table(x))))+1):16,2] <- 1 my.ecdf # the theoretical cdf my.theoreticalcdf <- pbinom(0:15,15,0.5) #error in estimate my.ecdf[,2]- my.theoreticalcdf #relative error (my.ecdf[,2] - my.theoreticalcdf )/my.theoreticalcdf #plot the empirical pdf barplot(table(x)/n.samples ,xlim=c(0,15)) #plot the empirical cdf plot(ecdf(x),ylim=c(0,15)) ## simulate data from a continuos distribution y <- runif(1000) #plot empirical pdf with a smoother hist(y,prob=TRUE) lines(density(y)) #plot the empirical cdf plot(ecdf(y))