4.R

s_rathwell — Apr 3, 2014, 6:58 PM

set.seed(12345)
samp   <- matrix(0,5000,20) #create sample matrix for 5000 samples of 20 permutations
sim    <- rep(0,5000)       #set up simulation
sim.sc <- c(NA)             #create similarity score vector
N      <- 5000

for(i in 1:5000){     #find similarity score of each permutation  
  samp[i,]  <- sample(1:20, 20)
  sim.sc[i] <- sum(c(1:20)*samp[i,])
}

exp <- sum(sim.sc[which(sim.sc > 2700)])/length(sum(sim.sc[which(sim.sc > 2700)]))
#expected sim score given score is greater than 2700
exp                        
[1] 2703

sav <- mean(sim.sc)                           #sample average sim score 
sub <- c(1,seq(10,5000,10))                   #sequence of simulation to be plotted (1:5000 for every 10)
ts.plot(sim.sc[sub], ylab="similarity score") #plot sim scores with sample average
abline(h = sav)

plot of chunk unnamed-chunk-1