Here is the function. There are other custom functions that this function calls, but all the dependencies run 100% good all the time. Here is the function. I apologize for the length.
ShortSim=function(SimmedSchedule,Year,runs){
rm(FinalR)
Schedule=SimmedSchedule
rm(SimmedSchedule)
NotPlayed=Schedule %>%
filter(Simmed==1)
Schedule=Schedule %>%
filter(Simmed==0)
NotPlayed$VisitingTeam=str_trim(NotPlayed$VisitingTeam,side = "both")
NotPlayed$HomeTeam=str_trim(NotPlayed$HomeTeam,side = "both")
for(i in 1:nrow(NotPlayed)){
tryCatch({
message("Simulating ",paste(NotPlayed[i,3]," @ ",NotPlayed[i,2]," on ",NotPlayed[i,1]," *** ",round((i/nrow(NotPlayed))*100,digits = 1),"% Complete ***",sep = ""))
hold=SimGame(NotPlayed[i,3],NotPlayed[i,2],runs)
#Check to make sure aggregate results from sims to not come out equal which would result in a tie. If they do, chose randomly
if(hold[[1]] == hold[[2]]){
if(runif(1)>=.5){
hold[[1]]=1
hold[[2]]=0
}else{
hold[[1]]=0
hold[[2]]=1
}
}
NotPlayed[i,5]=hold[[1]]
NotPlayed[i,4]=hold[[2]]
},error=function(e){})
}
beep(2)
NotPlayed=NotPlayed[complete.cases(NotPlayed),]
NotPlayed=NotPlayed %>%
select("date","HomeTeam","VisitingTeam","HomeScore","VisitingScore","Neutral")
NotPlayed$Simmed=1
Schedule=Schedule %>%
select("date","HomeTeam","VisitingTeam","HomeScore","VisitingScore","Neutral")
Schedule$Simmed=0
Schedule=Schedule[complete.cases(Schedule),]
SimmedSchedule=rbind.data.frame(Schedule,NotPlayed)
rm(NotPlayed)
SimmedSchedule=SimmedSchedule %>%
arrange(date)
beep(1)
#FinalR=ApplyModels(CalculateConfChamp(CalculateRPI(SimmedSchedule)))
SimmedSchedule=CalculateRPI(SimmedSchedule)
#write.csv(Result,"F:\\Result.csv")
rm(SimmedSchedule)
gc()
beep(2)
#Result=read.csv("F:\\Result.csv")
SimmedSchedule=CalculateConfChamp(SimmedSchedule)
#rm(Result)
FinalR=ApplyModels(SimmedSchedule)
#rm(ConfR)
#rm(ConfR)
#rm(Result)
#rm(Schedule)
beep(2)
return(FinalR)
}