Statistical Analysis: an Introduction using R/R/Random sampling
Appearance
Random sampling
[edit | edit source]From the help page, we have seen that the sample function can take a number of different arguments. x must be a vector of items, size must be a number. Since 1:6 gives a vector of the numbers from 1 to 6, we can set x=1:6 and size=5. Here are 5 examples (note that the first 4 are equivalent, although the actual result will differ due to chance effects when sampling[1]).
###The next 4 lines are equivalent, 5 numbers are selected from a list of 1..6 sample(x=1:6, size=5, replace=FALSE) #when sampling WITHOUT replacement, each number only appears once sample(replace=FALSE, size=5, x=1:6) #you can change the order of the arguments sample(x=1:6, size=5) #the same, because replace=FALSE by default sample(1:6, 5) #we don't need x= and size= if arguments are in the same order as in the help file ### The next line is a different model sample(1:6, 5, TRUE) #sampling WITH replacement (the same number can appear twice) ###The next 4 lines are equivalent, 5 numbers are selected from a list of 1..6 sample(x=1:6, size=5, replace=FALSE) #when sampling WITHOUT replacement, each number only appears once [1] 1 5 4 3 6 sample(replace=FALSE, size=5, x=1:6) #you can change the order of the arguments [1] 5 6 4 2 1 sample(x=1:6, size=5) #the same, because replace=FALSE by default [1] 2 3 4 6 5 sample(1:6, 5) #we don't need x= and size= if arguments are in the same order as in the help file [1] 1 6 3 5 4 ### Now simulate a different model sample(1:6, 5, TRUE) #sampling WITH replacement (the same number can appear twice) [1] 3 6 2 1 3
As noted, our "fair die" model is one of sampling with replacement: the same number can appear twice (and indeed does in our data). So our simulation model in R is simply
sample(1:6, 5, TRUE)
References
[edit | edit source]- ↑ call set.seed(1) before each chapter to get exactly the same results