### ### ### file: SamplingDist1.R ### ### estimates sampling distribution discussed in class using a simulation ### ### Instructions for use: ### 1) Copy and paste all the code into an R session (then observe what happens) ### 2) Play around with the parameters to the function to see what happens ### 3) Don't forget to compare the results of your simulation to the theoretical ### ones derived in class. simulate.sampling.distribution <- function(n,barplot=TRUE,epdf=TRUE){ U1 <- runif(n) # for simulating X1 U2 <- runif(n) # for simulating X2 X1 <- matrix(0,n,1) X2 <- matrix(0,n,1) X1[U1 <= 0.1] <- 0 X1[(0.1 < U1) & (U1 <= 0.3)] <- 1 X1[(0.3 < U1) & (U1 <= 0.7)] <- 2 X1[U1 > 0.7] <- 3 X2[U2 <= 0.1] <- 0 X2[(0.1 < U2) & (U2 <= 0.3)] <- 1 X2[(0.3 < U2) & (U2 <= 0.7)] <- 2 X2[U2 > 0.7] <- 3 Total = X1 + X2 if (barplot){ barplot(table(Total)/n) } if (epdf){ return(table(Total)/n) } else { return (Total) } } simulate.sampling.distribution(1000)