# R code for Exercise 3.3. ############################################################ # # x is an array of numbers # areDifferent returns 1 if all elements of x are unique # areDifferent returns 0 if some element of x is duplicated # ############################################################ areDifferent = function(x) { if(length(unique(x)) == length(x)) return(1) else return(0) } ############################################################ # # nsample is the number of samples, by default, 10,000. # n is the sample size, by default, 5 # x is an nsample by n matrix where each row can be thought # of as a random sample of n digits from 0 to 9 # ############################################################ sampleDigits = function(nsample=10000,n=5) { x = matrix(sample(0:9,size=nsample*n,replace=T),nrow=nsample,ncol=n) return(x) } ############################################################ # # x is a matrix where each row is a sample of random digits # results is the return value, an array of length nrow(x) # which is 1 if the entire row is different # and 0 if there is a duplicate on the corresponding row # ############################################################ getResults = function(x) { results = apply(x,1,areDifferent) return(results) } ############################################################ # # x is an array of 0s and 1s indicating the outcome # of each trial (0 is failure, 1 is success) # p is the theoretical probability # # plotResults makes a plot similar to those on page 82 # to show how the relative frequency changes as the # number of samples increases. # # cumsum computes the cumulative sum, so the ith element of cumsum(x) # equals the total number of successes in the first i trials # cumsum(x) / 1:n is the relative frequency of success for each i from 1 to n # # invisible() instructs R to not priont anything when the function is complete. # ############################################################ plotResults = function(x,p) { n = length(x) plot(1:n,cumsum(x)/1:n,type="l",ylim=c(0,1),xlab="Sample number",ylab="Relative Frequency") abline(h=p,lty=2,col=2) invisible() }