################## Example 1 ###################### # R code to approximate sampling distribution of X-bar # when taking sample of size 4 from an exponential(1) distn: my.m <- 1000 my.n <- 4 sample.matrix<-matrix(rexp(my.m*my.n, 1), nrow=my.m, ncol=my.n) # Each row will be a sample of size 4 # There will be 1000 of these simulated "samples" my.xbar.values <- apply(sample.matrix, 1, mean) par(mfrow=c(2,2)) hist(my.xbar.values) plot(density(my.xbar.values)) # 95% of sample mean values are between these two numbers: lower.upper <- quantile(my.xbar.values, probs=c(0.025, 0.975)) print(lower.upper) ################## Example 2 ###################### # R code to approximate sampling distribution of sample midrange # when taking sample of size 4 from an exponential(1) distn: find.midrange<-function(x){ range.vector <- range(x) ## Note midrange formula: #The following is correct: my.midrange <- sum(range.vector)/2 return(my.midrange) } my.midrange.values <- apply(sample.matrix, 1, find.midrange) hist(my.midrange.values) plot(density(my.midrange.values)) # 95% of midrange values are between these two numbers: lower.upper <- quantile(my.midrange.values, probs=c(0.025, 0.975)) print(lower.upper) par(mfrow=c(1,1))