Example 1: What does the following R code do? Predict what the output will be and then run the code to check your prediction.
box <- c(10,10,10,5,5) n <- 10000 x <- sample(box,n,replace=T) sum(x) sum(x)/n table(x)Example 2: Now consider the two lines of R code below. Figure out what each does, predict results, and verify by running the code. After you figure it out, increase the number of reps for each and write down the results.
table(replicate(100,sum(sample(box,2,replace=T)))) table(replicate(100,sum(sample(box,2))))Example 3: Now run the code below. Comment on what is different between the two experiments being simulated and what is the same.
x <- replicate(100,sum(sample(box,2,replace=T))) mean(x) y <- replicate(100,sum(sample(box,2))) mean(y) par(mfrow=c(2,1)) # see pages 67-73 of Intro to R doc hist(x) hist(y)