Day One: R Programming

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the MD toolbar button for help on Markdown).

When you click the Knit HTML button a web page 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:

summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120  

You can also embed plots, for example:

numsim = 1000 ; doors = 1:3

opendoor = function(x) {            #what door will monty open?
 if (x[1]==x[2])                                    #if winner==choice (contestant chose right) then...
    return(sample(doors[-c(x[1])], 1))              #monty chooses a random wrong door.
 else return(doors[-c(x[1],x[2])])                      #if winner dne choice ..monty chooses the other bad door..so monty is helping you by showing you a bad door.
}
swapdoor = function(x) { return(doors[-c(x[1], x[2])]) }    #drop the doors corresponding to x[1] and x[2]...those were cbind(open, choice)

winner = sample(doors, numsim, replace=TRUE)            #the good door is "winner"
choice = sample(doors, numsim, replace=TRUE)            #contestant chooses "choice"
open = apply(cbind(winner, choice), 1, opendoor)        #monty then picks a door via opendoor()
newchoice = apply(cbind(open, choice), 1, swapdoor)     #contestant swaps doors via swapdoor()

head(cbind(winner,choice,open,newchoice))               #these things collected.
##      winner choice open newchoice
## [1,]      1      2    3         1
## [2,]      2      3    1         2
## [3,]      2      1    3         2
## [4,]      3      1    2         3
## [5,]      3      2    1         3
## [6,]      1      2    3         1
sum(winner==choice) / numsim                            #% correct via not swapping
## [1] 0.343
sum(winner==newchoice) / numsim                     #% correct via swapping
## [1] 0.657

Now the random number generation. We begin with the linear congruential generator.

d = 53  #modulus
a = 20  #multiplier
b = 0  #increment / shift
s = 21  #seed
m = 60  #length of run(counting seed as #1)
r = numeric(m)  #initalize the vector for random integers

r[1] = s  #set seed
for (i in 1:(m - 1)) r[i + 1] = (a * r[i] + b)%%d  #RNG
r
##  [1] 21 49 26 43 12 28 30 17 22 16  2 40  5 47 39 38 18 42 45 52 33 24  3
## [24]  7 34 44 32  4 27 10 41 25 23 36 31 37 51 13 48  6 14 15 35 11  8  1
## [47] 20 29 50 46 19  9 21 49 26 43 12 28 30 17
d = 53  #modulus
a = 23  #multiplier
b = 0  #increment / shift
s = 21  #seed
m = 60  #length of run(counting seed as #1)
r = numeric(m)  #initalize the vector for random integers

r[1] = s  #set seed
for (i in 1:(m - 1)) r[i + 1] = (a * r[i] + b)%%d  #RNG
r
##  [1] 21  6 32 47 21  6 32 47 21  6 32 47 21  6 32 47 21  6 32 47 21  6 32
## [24] 47 21  6 32 47 21  6 32 47 21  6 32 47 21  6 32 47 21  6 32 47 21  6
## [47] 32 47 21  6 32 47 21  6 32 47 21  6 32 47