rm(list=ls())
table1<- read.csv("Prem4Jan15.csv", header = T) # Prem table from 4th Jan 2015 - from Google
total.replicate <- 10000 # good bootstrap number - reduce this just to play with the code
table3<- array(NA, dim=c(total.replicate,4)) # will write team names to this later
for(replicate in 1:total.replicate){ # set up unefficent bootstapping loop
for(team in 1:20){ # 20 teams in premiership
won<- table1$W[team] # pulls out the wins from the table
lost <- table1$L[team]
drawn <- table1$D[team]
played<- won+drawn+lost
points <- won*3 + drawn*1 # work out points
prob.win <- won/played # working out 'form' over year so far
prob.drawn <- drawn / played
prob.lost <- lost / played
remaining <- 38-played # how many more games
for(x in 1:remaining){ # a third nested loop??? Still, it works, and it's logical to me #
# runs through each team's remaining games
y <- runif(1)
if( y <=prob.lost){
lost <- lost+1 # stocastic loss, but based on probability given year so far
}
if (y > prob.lost & y <= prob.lost+prob.drawn){
drawn <- drawn+1
}
if(y > prob.lost+prob.drawn){
won <- won+1
}
}
points <- won*3 + drawn*1 # calculate points again
table1[team,replicate+10]<-points # add 'end' points to the table
}
table2 <- table1[order(table1[,replicate+10]), ] # rank by points (this does lowest to highest)
table3[replicate,1]<- as.character(table2$Team[20]) # pull out highest four teams
table3[replicate,2]<- as.character(table2$Team[19])
table3[replicate,3]<- as.character(table2$Team[18])
table3[replicate,4]<- as.character(table2$Team[17])
}
a <- table(table3) # make count of number of times each team makes top 4
a <- (a/replicate)*100 #convert to percentage
print(a)