This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
### In class, 2016-01-19
sample(1:6)
## [1] 6 3 2 5 1 4
sample(x = 1:6, size = 2)
## [1] 3 4
sample(x = 1:6, size = 6)
## [1] 5 6 3 2 4 1
# sample(x = 1:6, size = 12) ## error because the size 12 is larger than the possible samples
sample(x = 1:6, size = 12, replace = T)
## [1] 4 6 5 2 3 2 6 5 6 3 2 2
factorial(6) ## There are 6! permutations.
## [1] 720
nSimulations = 10 # Let's test nSimulations=1000
permut = sapply(1:nSimulations, function(whocares) sample(x = 1:6, size = 6, replace = F))
str(permut) # List 6! permutation 1000 times in a structure
## int [1:6, 1:10] 2 3 1 5 4 6 1 4 3 6 ...
permut2 = apply(permut, 2, paste, collapse="")
str(permut2) # Print out each permutation as a chunk structure
## chr [1:10] "231546" "143652" "436521" "513426" "432615" ...
table(permut2) # Disply the only unique permuation in a table format
## permut2
## 143652 213564 231546 246351 326145 432615 436521 453621 513426 523614
## 1 1 1 1 1 1 1 1 1 1
length(table(permut2)) # It will be 720 or a little less than 270 because the number of unique permutation by nSiumlations will be 720 or less than the total permuation number, 720.
## [1] 10
factorial(6)
## [1] 720
## The probability to get a permuation is: p=1/720
## The probability to get a different permutation in nSimulations is: (1-p)^720
## The chance that you get them all in nSimulations tries is:
1-(1-1/720)^nSimulations
## [1] 0.0138024
## a great approximation (when nSimulations is large) is:
## The change is similar to above formular calculation
1-exp(-nSimulations/720)
## [1] 0.01379288