Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
920 lines (803 sloc) 47.8 KB
library(shiny)
Round <- function(x){
if(ceiling(max(x)/10)*10 - x <= x - floor(max(x)/10)*10){
ceiling(max(x)/10)*10
}else {floor(max(x)/10)*10}
}
Cumsum.df <- paste("https://www.dropbox.com/s/6in6lgmmv2mcqwz/cumsum.df.csv?dl=0")
Cumsum.df <- repmis::source_data(Cumsum.df,
sep = ",",
header = TRUE)
Cumsum.df$X <- as.character(Cumsum.df$X)
df <- as.data.frame(c(0))
shinyServer(
function(input,output){
output$odds <- renderTable({
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
State <- paste(First,Second,Third,Outs,sep='')
if(State=='0000'){row.num=1
}else if (State=='0001'){row.num=2
}else if (State=='0002'){row.num=3
}else if (State=='1000'){row.num=4
}else if (State=='1001'){row.num=5
}else if (State=='1002'){row.num=6
}else if (State=='0100'){row.num=7
}else if (State=='0101'){row.num=8
}else if (State=='0102'){row.num=9
}else if (State=='0010'){row.num=10
}else if (State=='0011'){row.num=11
}else if (State=='0012'){row.num=12
}else if (State=='1100'){row.num=13
}else if (State=='1101'){row.num=14
}else if (State=='1102'){row.num=15
}else if (State=='1010'){row.num=16
}else if (State=='1011'){row.num=17
}else if (State=='1012'){row.num=18
}else if (State=='0110'){row.num=19
}else if (State=='0111'){row.num=20
}else if (State=='0112'){row.num=21
}else if (State=='1110'){row.num=22
}else if (State=='1111'){row.num=23
}else {row.num=24}
score <- runif(15000)
spread <- rep(0,15000)
home.score <- rep(as.numeric(input$home.score),15000)
vis.score <- rep(as.numeric(input$vis.score),15000)
game.df <- as.data.frame(cbind(score,vis.score,home.score,spread))
if(Half.inning %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$vis.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$vis.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$vis.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$vis.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$vis.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$vis.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$vis.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$vis.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$vis.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$vis.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$vis.score+10,game.df$vis.score+11)))))))))))
}
if(Half.inning %% 2 == 0){
game.df$home.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$home.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$home.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$home.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$home.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$home.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$home.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$home.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$home.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$home.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$home.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$home.score+10,game.df$home.score+11)))))))))))
}
if(Half.inning<18){
for (i in (Half.inning+1):18){
game.df$score <- runif(15000)
if(i %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<=0.7598346,game.df$vis.score,
ifelse(game.df$score <= 0.889886,game.df$vis.score+1,
ifelse(game.df$score <= 0.9522798,game.df$vis.score+2,
ifelse(game.df$score <= 0.9803643,game.df$vis.score+3,
ifelse(game.df$score <= 0.9920653,game.df$vis.score+4,
ifelse(game.df$score <= 0.9965020,game.df$vis.score+5,
ifelse(game.df$score <= 0.9986701,game.df$vis.score+6,
ifelse(game.df$score <= 0.9994300,game.df$vis.score+7,
ifelse(game.df$score <= 0.9997765,game.df$vis.score+8,
ifelse(game.df$score <= 0.9999553,game.df$vis.score+9,game.df$vis.score+10))))))))))
}
if(i %% 2 == 0){
game.df$home.score <- ifelse(game.df$score <= 0.7438722,game.df$home.score,
ifelse(game.df$score <= 0.8818919,game.df$home.score+1,
ifelse(game.df$score <= 0.9487152,game.df$home.score+2,
ifelse(game.df$score <= 0.9781759,game.df$home.score+3,
ifelse(game.df$score <= 0.9914581,game.df$home.score+4,
ifelse(game.df$score <= 0.9965387,game.df$home.score+5,
ifelse(game.df$score <= 0.9986624,game.df$home.score+6,
ifelse(game.df$score <= 0.9993547,game.df$home.score+7,
ifelse(game.df$score <= 0.9997888,game.df$home.score+8,
ifelse(game.df$score <= 0.9999296,game.df$home.score+9,
ifelse(game.df$score <= 0.99997531,game.df$home.score+10,
ifelse(game.df$score <= 0.9999765,game.df$home.score+11,game.df$home.score+12))))))))))))
}
}
}
game.df$score <- runif(15000)
game.df$spread <- ifelse(game.df$home.score==game.df$vis.score & game.df$score < 0.5146396,1,game.df$spread)
game.df$spread <- ifelse(game.df$home.score>game.df$vis.score,1,game.df$spread)
df[1] <- sum(15000-sum(game.df$spread))
df[1] <- Round(df[1])
colnames(df)[1] <- 'start.state'
df$prob.home.team.winning <- 15000-df[1]
df$prob.home.team.winning <- paste(substr(df$prob.home.team.winning/150,1,4),'%',sep='')
df$start.state <- paste(substr(df$start.state/150,1,4),'%',sep='')
#df
colnames(df) <- c('prob.visitors.winning','prob.home.team.winning')
df$prob.visitors.winning <- ifelse(nchar(df$prob.visitors.winning)==3,paste(substr(df$prob.visitors.winning,1,2),'.0%',sep=''),df$prob.visitors.winning)
df$prob.home.team.winning <- ifelse(nchar(df$prob.home.team.winning)==3,paste(substr(df$prob.home.team.winning,1,2),'.0%',sep=''),df$prob.home.team.winning)
df
})
output$steal <- renderTable({
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
potential.steal=0
if(First==1 & (Second==0 | Third==0) | (Second==1 & Third==0)){potential.steal=1}
State <- c('','','')
State[1] <- paste(First,Second,Third,Outs,sep='')
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
State <- c('','','')
State[1] <- paste(First,Second,Third,Outs,sep='')
n=1
half.inn.flag=0
if(First==0 & Second==1 & Third==0){Third=1;Second=0;First=0}
if(First==1 & Second==1 & Third==0 & input$double.steal=='double'){Third=1;Second=1;First=0}
if(First==1 & Second==1 & Third==0 & input$double.steal=='single'){Third=1;Second=0;First=1}
if(First==1 & Second==0 & Third==0){Third=0;Second=1;First=0}
if(First==1 & Second==0 & Third==1){Third=1;Second=1;First=0}
Outs <- as.character(Outs)
State[2] <- paste(First,Second,Third,Outs,sep='')
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
Outs <- as.numeric(Outs)
if(First==1 & Second==0 & Third==1){
if(Outs<2){First=0;Second=0;Third=1;Outs=Outs+1
}else{First=0;Second=0;Third=0;Outs=0;half.inn.flag=1}
}
if(First==0 & Second==1 & Third==0){
if(Outs<2){First=0;Second=0;Third=0;Outs=Outs+1
}else {First=0;Second=0;Third=0;Outs=0;half.inn.flag=1}
}
if(First==1 & Second==0 & Third==0){
if(Outs<2){First=0;Second=0;Third=0;Outs=Outs+1
}else{First=0;Second=0;Third=0;Outs=0;half.inn.flag=1}
}
if(First==1 & Second==1 & Third==0 & input$double.steal=='double' & Outs<2){First=0;Second=1;Third=0;Outs=Outs+1}
if(First==1 & Second==1 & Third==0 & input$double.steal=='double' & Outs==2){First=0;Second=0;Third=0;Outs=0;half.inning.flag=1}
if(First==1 & Second==1 & Third==0 & input$double.steal=='single' & Outs<2){First=1;Second=0;Third=0;Outs=Outs+1}
if(First==1 & Second==1 & Third==0 & input$double.steal=='single' & Outs==2){First=0;Second=0;Third=0;Outs=0;half.inning.flag=1}
Outs <- as.character(Outs)
State[3] <- paste(First,Second,Third,Outs,sep='')
for(attempt in 1:3){
if(attempt==3 & half.inn.flag==1){Half.inning=Half.inning+1}
#State <- paste(First,Second,Third,Outs,sep='')
if(State[attempt]=='0000'){row.num=1
}else if (State[attempt]=='0001'){row.num=2
}else if (State[attempt]=='0002'){row.num=3
}else if (State[attempt]=='1000'){row.num=4
}else if (State[attempt]=='1001'){row.num=5
}else if (State[attempt]=='1002'){row.num=6
}else if (State[attempt]=='0100'){row.num=7
}else if (State[attempt]=='0101'){row.num=8
}else if (State[attempt]=='0102'){row.num=9
}else if (State[attempt]=='0010'){row.num=10
}else if (State[attempt]=='0011'){row.num=11
}else if (State[attempt]=='0012'){row.num=12
}else if (State[attempt]=='1100'){row.num=13
}else if (State[attempt]=='1101'){row.num=14
}else if (State[attempt]=='1102'){row.num=15
}else if (State[attempt]=='1010'){row.num=16
}else if (State[attempt]=='1011'){row.num=17
}else if (State[attempt]=='1012'){row.num=18
}else if (State[attempt]=='0110'){row.num=19
}else if (State[attempt]=='0111'){row.num=20
}else if (State[attempt]=='0112'){row.num=21
}else if (State[attempt]=='1110'){row.num=22
}else if (State[attempt]=='1111'){row.num=23
}else {row.num=24}
score <- runif(15000)
spread <- rep(0,15000)
home.score <- rep(as.numeric(input$home.score),15000)
vis.score <- rep(as.numeric(input$vis.score),15000)
game.df <- as.data.frame(cbind(score,vis.score,home.score,spread))
if(Half.inning %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$vis.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$vis.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$vis.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$vis.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$vis.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$vis.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$vis.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$vis.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$vis.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$vis.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$vis.score+10,game.df$vis.score+11)))))))))))
}
if(Half.inning %% 2 == 0){
game.df$home.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$home.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$home.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$home.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$home.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$home.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$home.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$home.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$home.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$home.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$home.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$home.score+10,game.df$home.score+11)))))))))))
}
if(Half.inning<18){
for (i in (Half.inning+1):18){
game.df$score <- runif(15000)
if(i %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<=0.7598346,game.df$vis.score,
ifelse(game.df$score <= 0.889886,game.df$vis.score+1,
ifelse(game.df$score <= 0.9522798,game.df$vis.score+2,
ifelse(game.df$score <= 0.9803643,game.df$vis.score+3,
ifelse(game.df$score <= 0.9920653,game.df$vis.score+4,
ifelse(game.df$score <= 0.9965020,game.df$vis.score+5,
ifelse(game.df$score <= 0.9986701,game.df$vis.score+6,
ifelse(game.df$score <= 0.9994300,game.df$vis.score+7,
ifelse(game.df$score <= 0.9997765,game.df$vis.score+8,
ifelse(game.df$score <= 0.9999553,game.df$vis.score+9,game.df$vis.score+10))))))))))
}
if(i %% 2 == 0){
game.df$home.score <- ifelse(game.df$score <= 0.7438722,game.df$home.score,
ifelse(game.df$score <= 0.8818919,game.df$home.score+1,
ifelse(game.df$score <= 0.9487152,game.df$home.score+2,
ifelse(game.df$score <= 0.9781759,game.df$home.score+3,
ifelse(game.df$score <= 0.9914581,game.df$home.score+4,
ifelse(game.df$score <= 0.9965387,game.df$home.score+5,
ifelse(game.df$score <= 0.9986624,game.df$home.score+6,
ifelse(game.df$score <= 0.9993547,game.df$home.score+7,
ifelse(game.df$score <= 0.9997888,game.df$home.score+8,
ifelse(game.df$score <= 0.9999296,game.df$home.score+9,
ifelse(game.df$score <= 0.99997531,game.df$home.score+10,
ifelse(game.df$score <= 0.9999765,game.df$home.score+11,game.df$home.score+12))))))))))))
}
}
}
game.df$score <- runif(15000)
game.df$spread <- ifelse(game.df$home.score==game.df$vis.score & game.df$score < 0.5146396,1,game.df$spread)
game.df$spread <- ifelse(game.df$home.score>game.df$vis.score,1,game.df$spread)
if(attempt==1)
{df[1] <- sum(15000-sum(game.df$spread))
df[1] <- Round(df[1])
colnames(df)[1] <- 'start.state'
df$prob.home.team.winning <- 15000-df[1]
df$prob.home.team.winning <- paste(substr(df$prob.home.team.winning/150,1,4),'%',sep='')
df$start.state <- paste(substr(df$start.state/150,1,4),'%',sep='')
#df
}
if(attempt==2){
df$vis.win.successful.play <- round(Round(sum(15000-sum(game.df$spread))),2)
df$home.win.successful.play <- round(15000-df$vis.win.successful.play,2)
vis.steal <- round(Round(sum(15000-sum(game.df$spread)))/150,2)
home.steal <- round(100-vis.steal,2)#15000-df$vis.win.successful.play/150
df$home.win.successful.play <- paste(substr(df$home.win.successful.play/150,1,4),'%',sep='')
df$vis.win.successful.play <- paste(substr(df$vis.win.successful.play/150,1,4),'%',sep='')
}
if (attempt==3){
df$caught.stealing <- Round(sum(15000-sum(game.df$spread)))
df$prob.home.team.winning.out <- 15000-df$caught.stealing
vis.out <- Round(sum(15000-sum(game.df$spread)))/150
home.out <- 100-vis.out#df$caught.stealing/150
df$prob.home.team.winning.out <- paste(substr(df$prob.home.team.winning.out/150,1,4),'%',sep='')
df$caught.stealing <- paste(substr(df$caught.stealing/150,1,4),'%',sep='')
}
}
if(potential.steal==0) {
colnames(df) <- c('prob.visitors.winning','prob.home.team.winning')
df$prob.visitors.winning <- ifelse(nchar(df$prob.visitors.winning)==3,paste(substr(df$prob.visitors.winning,1,2),'.0%',sep=''),df$prob.visitors.winning)
df$prob.home.team.winning <- ifelse(nchar(df$prob.home.team.winning)==3,paste(substr(df$prob.home.team.winning,1,2),'.0%',sep=''),df$prob.home.team.winning)
df <- df[1:2]
df <- rbind(c('No steal situation exists',''),df)
df
}else{
df$expected.end.state <- paste(Success*vis.steal + (1-Success)*vis.out,'%',sep='')
df$expected.home.win <- paste(Success*home.steal + (1-Success)*home.out,'%',sep='')
df.t <- as.data.frame(t(df))
colnames(df.t) <- 'prob.visitors.win'
df.t$prob.home.win <- 0
df.t$prob.visitors.win <- as.character(df.t$prob.visitors.win)
df.t$prob.home.win <- as.character(df.t$prob.home.win)
df.t$prob.home.win[1] <- df.t$prob.visitors.win[2]
df.t$prob.home.win[3] <- df.t$prob.visitors.win[4]
df.t$prob.home.win[5] <- df.t$prob.visitors.win[6]
df.t$prob.home.win[7] <- df.t$prob.visitors.win[8]
df.t <- df.t[c(1,3,5,7),]
df.t$prob.visitors.win[4] <- substr(df.t$prob.visitors.win[4],1,4)
df.t$prob.home.win[4] <- 100 - as.numeric(df.t$prob.visitors.win[4])
df.t$prob.visitors.win[4] <- paste(df.t$prob.visitors.win[4],'%',sep='')
df.t$prob.home.win[4] <- paste(df.t$prob.home.win[4],'%',sep='')
df.t$prob.visitors.win <- ifelse(nchar(df.t$prob.visitors.win)==3,paste(substr(df.t$prob.visitors.win,1,2),'.0%',sep=''),df.t$prob.visitors.win)
df.t$prob.home.win <- ifelse(nchar(df.t$prob.home.win)==3,paste(substr(df.t$prob.home.win,1,2),'.0%',sep=''),df.t$prob.home.win)
row.names(df.t) <- c('start state','successful stolen base','caught stealing','expected end state')
df.t
}
})
output$sac <- renderTable({
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
potential.bunt=0
if(Outs < 2 & ((First==1|Second==1) & Third==0)){potential.bunt=1}
State <- c('','','')
State[1] <- paste(First,Second,Third,Outs,sep='')
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
State <- c('','','')
State[1] <- paste(First,Second,Third,Outs,sep='')
n=1
half.inn.flag=0
if( Outs<2){
half.inn.flag==0
Outs <- as.numeric(input$outs)
Outs <- Outs + 1
Outs <- as.character(Outs)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
if(First==1 & Second==0){First=0;Second=1;
}else if(First==0 & Second==1 & Third==0){First=0;Second=0;Third=1
}else if(First==1 & Second==1 & Third==0){First=0;Second=1;Third=1
}else{First=First;Second=Second;Third=Third}
#print(paste('state 2',State[2],sep=' '))
State[2] <- paste(First,Second,Third,Outs,sep='')
#print(paste('state 2',State[2],sep=' '))
#print(Half.inning)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
if(First==1 & Second==0){First=1;Second=0
}else if(First==0 & Second==1 & Third==0){First=1;Second=0;Third=0
}else if(First==1 & Second==1 & Third==0){First=1;Second=1;Third=0
}else{First=First;Second=Second;Third=Third}
State[3] <- paste(First,Second,Third,Outs,sep='')
Outs <- input$outs
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
}
for(attempt in 1:3){
if(attempt==3 & half.inn.flag==1){Half.inning=Half.inning+1}
#State <- paste(First,Second,Third,Outs,sep='')
if(State[attempt]=='0000'){row.num=1
}else if (State[attempt]=='0001'){row.num=2
}else if (State[attempt]=='0002'){row.num=3
}else if (State[attempt]=='1000'){row.num=4
}else if (State[attempt]=='1001'){row.num=5
}else if (State[attempt]=='1002'){row.num=6
}else if (State[attempt]=='0100'){row.num=7
}else if (State[attempt]=='0101'){row.num=8
}else if (State[attempt]=='0102'){row.num=9
}else if (State[attempt]=='0010'){row.num=10
}else if (State[attempt]=='0011'){row.num=11
}else if (State[attempt]=='0012'){row.num=12
}else if (State[attempt]=='1100'){row.num=13
}else if (State[attempt]=='1101'){row.num=14
}else if (State[attempt]=='1102'){row.num=15
}else if (State[attempt]=='1010'){row.num=16
}else if (State[attempt]=='1011'){row.num=17
}else if (State[attempt]=='1012'){row.num=18
}else if (State[attempt]=='0110'){row.num=19
}else if (State[attempt]=='0111'){row.num=20
}else if (State[attempt]=='0112'){row.num=21
}else if (State[attempt]=='1110'){row.num=22
}else if (State[attempt]=='1111'){row.num=23
}else {row.num=24}
score <- runif(15000)
spread <- rep(0,15000)
home.score <- rep(as.numeric(input$home.score),15000)
vis.score <- rep(as.numeric(input$vis.score),15000)
game.df <- as.data.frame(cbind(score,vis.score,home.score,spread))
if(Half.inning %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$vis.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$vis.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$vis.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$vis.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$vis.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$vis.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$vis.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$vis.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$vis.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$vis.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$vis.score+10,game.df$vis.score+11)))))))))))
}
if(Half.inning %% 2 == 0){
game.df$home.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$home.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$home.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$home.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$home.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$home.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$home.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$home.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$home.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$home.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$home.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$home.score+10,game.df$home.score+11)))))))))))
}
if(Half.inning<18){
for (i in (Half.inning+1):18){
game.df$score <- runif(15000)
if(i %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<=0.7598346,game.df$vis.score,
ifelse(game.df$score <= 0.889886,game.df$vis.score+1,
ifelse(game.df$score <= 0.9522798,game.df$vis.score+2,
ifelse(game.df$score <= 0.9803643,game.df$vis.score+3,
ifelse(game.df$score <= 0.9920653,game.df$vis.score+4,
ifelse(game.df$score <= 0.9965020,game.df$vis.score+5,
ifelse(game.df$score <= 0.9986701,game.df$vis.score+6,
ifelse(game.df$score <= 0.9994300,game.df$vis.score+7,
ifelse(game.df$score <= 0.9997765,game.df$vis.score+8,
ifelse(game.df$score <= 0.9999553,game.df$vis.score+9,game.df$vis.score+10))))))))))
}
if(i %% 2 == 0){
game.df$home.score <- ifelse(game.df$score <= 0.7438722,game.df$home.score,
ifelse(game.df$score <= 0.8818919,game.df$home.score+1,
ifelse(game.df$score <= 0.9487152,game.df$home.score+2,
ifelse(game.df$score <= 0.9781759,game.df$home.score+3,
ifelse(game.df$score <= 0.9914581,game.df$home.score+4,
ifelse(game.df$score <= 0.9965387,game.df$home.score+5,
ifelse(game.df$score <= 0.9986624,game.df$home.score+6,
ifelse(game.df$score <= 0.9993547,game.df$home.score+7,
ifelse(game.df$score <= 0.9997888,game.df$home.score+8,
ifelse(game.df$score <= 0.9999296,game.df$home.score+9,
ifelse(game.df$score <= 0.99997531,game.df$home.score+10,
ifelse(game.df$score <= 0.9999765,game.df$home.score+11,game.df$home.score+12))))))))))))
}
}
}
game.df$score <- runif(15000)
game.df$spread <- ifelse(game.df$home.score==game.df$vis.score & game.df$score < 0.5146396,1,game.df$spread)
game.df$spread <- ifelse(game.df$home.score>game.df$vis.score,1,game.df$spread)
if(attempt==1)
{df[1] <- sum(15000-sum(game.df$spread))
df[1] <- Round(df[1])
colnames(df)[1] <- 'start.state'
df$prob.home.team.winning <- 15000-df[1]
df$prob.home.team.winning <- paste(substr(df$prob.home.team.winning/150,1,4),'%',sep='')
df$start.state <- paste(substr(df$start.state/150,1,4),'%',sep='')
#df
}
if(attempt==2){
df$vis.win.successful.play <- round(Round(sum(15000-sum(game.df$spread))),2)
df$home.win.successful.play <- round(15000-df$vis.win.successful.play,2)
vis.steal <- round(Round(sum(15000-sum(game.df$spread)))/150,2)
home.steal <- round(100-vis.steal,2)#15000-df$vis.win.successful.play/150
df$home.win.successful.play <- paste(substr(df$home.win.successful.play/150,1,4),'%',sep='')
df$vis.win.successful.play <- paste(substr(df$vis.win.successful.play/150,1,4),'%',sep='')
}
if (attempt==3){
df$caught.stealing <- Round(sum(15000-sum(game.df$spread)))
df$prob.home.team.winning.out <- 15000-df$caught.stealing
vis.out <- Round(sum(15000-sum(game.df$spread)))/150
home.out <- 100-vis.out#df$caught.stealing/150
df$prob.home.team.winning.out <- paste(substr(df$prob.home.team.winning.out/150,1,4),'%',sep='')
df$caught.stealing <- paste(substr(df$caught.stealing/150,1,4),'%',sep='')
}
}
if(potential.bunt==0) {
colnames(df) <- c('prob.visitors.winning','prob.home.team.winning')
df$prob.visitors.winning <- ifelse(nchar(df$prob.visitors.winning)==3,paste(substr(df$prob.visitors.winning,1,2),'.0%',sep=''),df$prob.visitors.winning)
df$prob.home.team.winning <- ifelse(nchar(df$prob.home.team.winning)==3,paste(substr(df$prob.home.team.winning,1,2),'.0%',sep=''),df$prob.home.team.winning)
df <- df[1:2]
df <- rbind(c('No sac bunt situation exists',''),df)
df
}else{
df$expected.end.state <- paste(Success*vis.steal + (1-Success)*vis.out,'%',sep='')
df$expected.home.win <- paste(Success*home.steal + (1-Success)*home.out,'%',sep='')
df.t <- as.data.frame(t(df))
colnames(df.t) <- 'prob.visitors.win'
df.t$prob.home.win <- 0
df.t$prob.visitors.win <- as.character(df.t$prob.visitors.win)
df.t$prob.home.win <- as.character(df.t$prob.home.win)
df.t$prob.home.win[1] <- df.t$prob.visitors.win[2]
df.t$prob.home.win[3] <- df.t$prob.visitors.win[4]
df.t$prob.home.win[5] <- df.t$prob.visitors.win[6]
df.t$prob.home.win[7] <- df.t$prob.visitors.win[8]
df.t <- df.t[c(1,3,5,7),]
df.t$prob.visitors.win[4] <- substr(df.t$prob.visitors.win[4],1,4)
df.t$prob.home.win[4] <- 100 - as.numeric(df.t$prob.visitors.win[4])
df.t$prob.visitors.win[4] <- paste(df.t$prob.visitors.win[4],'%',sep='')
df.t$prob.home.win[4] <- paste(df.t$prob.home.win[4],'%',sep='')
df.t$prob.visitors.win <- ifelse(nchar(df.t$prob.visitors.win)==3,paste(substr(df.t$prob.visitors.win,1,2),'.0%',sep=''),df.t$prob.visitors.win)
df.t$prob.home.win <- ifelse(nchar(df.t$prob.home.win)==3,paste(substr(df.t$prob.home.win,1,2),'.0%',sep=''),df.t$prob.home.win)
row.names(df.t) <- c('start state','successful sac bunt','lead runner forced out','expected end state')
df.t
}
})
output$tag <- renderTable({
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
potential.tag=0
if(Outs != 0 & Third==1){potential.tag=1}
State <- c('','','')
State[1] <- paste(First,Second,Third,Outs,sep='')
cumsum.df <- Cumsum.df
Inning <- as.numeric(input$inning)
Inning <- min(9,Inning)
Success <- as.numeric(input$success)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
spread <- rep(as.numeric(input$vis.score)-as.numeric(input$home.score),15000)
First=Second=Third=0
Outs <- as.numeric(input$outs)
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
State <- c('','','')
State[1] <- paste(First,Second,Third,Outs,sep='')
n=1
half.inn.flag=0
if( Outs>0){
half.inn.flag==0
Outs <- as.numeric(input$outs)
Outs <- Outs + 1
Outs <- as.character(Outs)
if(input$half.inning=='top') {Half.inning <- Inning*2-1}
if(input$half.inning=='bottom') {Half.inning <- Inning*2}
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
Outs=input$outs
Third=0
State[2] <- paste(First,Second,Third,Outs,sep='')
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
Outs=input$outs
if(Third == 1 & Outs==2){Outs=0;First=0;Second=0;Third=0;half.inn.flag=1}
if(Third == 1 & Outs==1){Outs=2;First=0;Second=0;Third=0;half.inn.flag=0}
State[3] <- paste(First,Second,Third,Outs,sep='')
#Outs <- input$outs
if (input$first=='no'){First=0}
if (input$first=='yes'){First=1}
if (input$second=='no'){Second=0}
if (input$second=='yes'){Second=1}
if (input$third=='no'){Third=0}
if (input$third=='yes'){Third=1}
}
for(attempt in 1:3){
if(attempt==3 & half.inn.flag==1){Half.inning=Half.inning+1}
if(State[attempt]=='0000'){row.num=1
}else if (State[attempt]=='0001'){row.num=2
}else if (State[attempt]=='0002'){row.num=3
}else if (State[attempt]=='1000'){row.num=4
}else if (State[attempt]=='1001'){row.num=5
}else if (State[attempt]=='1002'){row.num=6
}else if (State[attempt]=='0100'){row.num=7
}else if (State[attempt]=='0101'){row.num=8
}else if (State[attempt]=='0102'){row.num=9
}else if (State[attempt]=='0010'){row.num=10
}else if (State[attempt]=='0011'){row.num=11
}else if (State[attempt]=='0012'){row.num=12
}else if (State[attempt]=='1100'){row.num=13
}else if (State[attempt]=='1101'){row.num=14
}else if (State[attempt]=='1102'){row.num=15
}else if (State[attempt]=='1010'){row.num=16
}else if (State[attempt]=='1011'){row.num=17
}else if (State[attempt]=='1012'){row.num=18
}else if (State[attempt]=='0110'){row.num=19
}else if (State[attempt]=='0111'){row.num=20
}else if (State[attempt]=='0112'){row.num=21
}else if (State[attempt]=='1110'){row.num=22
}else if (State[attempt]=='1111'){row.num=23
}else {row.num=24}
score <- runif(15000)
spread <- rep(0,15000)
if(attempt !=2){home.score <- rep(as.numeric(input$home.score),15000)
vis.score <- rep(as.numeric(input$vis.score),15000)}
if(attempt==2 & input$half.inning=='top'){vis.score <- rep((as.numeric(input$vis.score)+1),15000)
home.score <- rep(as.numeric(input$home.score),15000)}
if(attempt==2 & input$half.inning=='bottom'){vis.score <- rep(as.numeric(input$vis.score),15000)
home.score <- rep((as.numeric(input$home.score)+1),15000)}
game.df <- as.data.frame(cbind(score,vis.score,home.score,spread))
if(Half.inning %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$vis.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$vis.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$vis.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$vis.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$vis.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$vis.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$vis.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$vis.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$vis.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$vis.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$vis.score+10,game.df$vis.score+11)))))))))))
}
if(Half.inning %% 2 == 0){
game.df$home.score <- ifelse(game.df$score<cumsum.df[row.num,2],game.df$home.score,
ifelse(game.df$score<cumsum.df[row.num,3],game.df$home.score+1,
ifelse(game.df$score<cumsum.df[row.num,4],game.df$home.score+2,
ifelse(game.df$score<cumsum.df[row.num,5],game.df$home.score+3,
ifelse(game.df$score<cumsum.df[row.num,6],game.df$home.score+4,
ifelse(game.df$score<cumsum.df[row.num,7],game.df$home.score+5,
ifelse(game.df$score<cumsum.df[row.num,8],game.df$home.score+6,
ifelse(game.df$score<cumsum.df[row.num,9],game.df$home.score+7,
ifelse(game.df$score<cumsum.df[row.num,10],game.df$home.score+8,
ifelse(game.df$score<cumsum.df[row.num,11],game.df$home.score+9,
ifelse(game.df$score<cumsum.df[row.num,12],game.df$home.score+10,game.df$home.score+11)))))))))))
}
if(Half.inning<18){
for (i in (Half.inning+1):18){
game.df$score <- runif(15000)
if(i %% 2 == 1){
game.df$vis.score <- ifelse(game.df$score<=0.7598346,game.df$vis.score,
ifelse(game.df$score <= 0.889886,game.df$vis.score+1,
ifelse(game.df$score <= 0.9522798,game.df$vis.score+2,
ifelse(game.df$score <= 0.9803643,game.df$vis.score+3,
ifelse(game.df$score <= 0.9920653,game.df$vis.score+4,
ifelse(game.df$score <= 0.9965020,game.df$vis.score+5,
ifelse(game.df$score <= 0.9986701,game.df$vis.score+6,
ifelse(game.df$score <= 0.9994300,game.df$vis.score+7,
ifelse(game.df$score <= 0.9997765,game.df$vis.score+8,
ifelse(game.df$score <= 0.9999553,game.df$vis.score+9,game.df$vis.score+10))))))))))
}
if(i %% 2 == 0){
game.df$home.score <- ifelse(game.df$score <= 0.7438722,game.df$home.score,
ifelse(game.df$score <= 0.8818919,game.df$home.score+1,
ifelse(game.df$score <= 0.9487152,game.df$home.score+2,
ifelse(game.df$score <= 0.9781759,game.df$home.score+3,
ifelse(game.df$score <= 0.9914581,game.df$home.score+4,
ifelse(game.df$score <= 0.9965387,game.df$home.score+5,
ifelse(game.df$score <= 0.9986624,game.df$home.score+6,
ifelse(game.df$score <= 0.9993547,game.df$home.score+7,
ifelse(game.df$score <= 0.9997888,game.df$home.score+8,
ifelse(game.df$score <= 0.9999296,game.df$home.score+9,
ifelse(game.df$score <= 0.99997531,game.df$home.score+10,
ifelse(game.df$score <= 0.9999765,game.df$home.score+11,game.df$home.score+12))))))))))))
}
}
}
game.df$score <- runif(15000)
game.df$spread <- ifelse(game.df$home.score==game.df$vis.score & game.df$score < 0.5146396,1,game.df$spread)
game.df$spread <- ifelse(game.df$home.score>game.df$vis.score,1,game.df$spread)
if(attempt==1)
{df[1] <- sum(15000-sum(game.df$spread))
df[1] <- Round(df[1])
colnames(df)[1] <- 'start.state'
df$prob.home.team.winning <- 15000-df[1]
df$prob.home.team.winning <- paste(substr(df$prob.home.team.winning/150,1,4),'%',sep='')
df$start.state <- paste(substr(df$start.state/150,1,4),'%',sep='')
#df
}
if(attempt==2){
df$vis.win.successful.play <- round(Round(sum(15000-sum(game.df$spread))),2)
df$home.win.successful.play <- round(15000-df$vis.win.successful.play,2)
vis.steal <- round(Round(sum(15000-sum(game.df$spread)))/150,2)
home.steal <- round(100-vis.steal,2)#15000-df$vis.win.successful.play/150
df$home.win.successful.play <- paste(substr(df$home.win.successful.play/150,1,4),'%',sep='')
df$vis.win.successful.play <- paste(substr(df$vis.win.successful.play/150,1,4),'%',sep='')
}
if (attempt==3){
df$caught.stealing <- Round(sum(15000-sum(game.df$spread)))
df$prob.home.team.winning.out <- 15000-df$caught.stealing
vis.out <- Round(sum(15000-sum(game.df$spread)))/150
home.out <- 100-vis.out#df$caught.stealing/150
df$prob.home.team.winning.out <- paste(substr(df$prob.home.team.winning.out/150,1,4),'%',sep='')
df$caught.stealing <- paste(substr(df$caught.stealing/150,1,4),'%',sep='')
}
}
if(potential.tag==0) {
colnames(df) <- c('prob.visitors.winning','prob.home.team.winning')
df$prob.visitors.winning <- ifelse(nchar(df$prob.visitors.winning)==3,paste(substr(df$prob.visitors.winning,1,2),'.0%',sep=''),df$prob.visitors.winning)
df$prob.home.team.winning <- ifelse(nchar(df$prob.home.team.winning)==3,paste(substr(df$prob.home.team.winning,1,2),'.0%',sep=''),df$prob.home.team.winning)
df <- df[1:2]
df <- rbind(c('No tag situation exists',''),df)
df
}else{
df$expected.end.state <- paste(Success*vis.steal + (1-Success)*vis.out,'%',sep='')
df$expected.home.win <- paste(Success*home.steal + (1-Success)*home.out,'%',sep='')
df.t <- as.data.frame(t(df))
colnames(df.t) <- 'prob.visitors.win'
df.t$prob.home.win <- 0
df.t$prob.visitors.win <- as.character(df.t$prob.visitors.win)
df.t$prob.home.win <- as.character(df.t$prob.home.win)
df.t$prob.home.win[1] <- df.t$prob.visitors.win[2]
df.t$prob.home.win[3] <- df.t$prob.visitors.win[4]
df.t$prob.home.win[5] <- df.t$prob.visitors.win[6]
df.t$prob.home.win[7] <- df.t$prob.visitors.win[8]
df.t <- df.t[c(1,3,5,7),]
df.t$prob.visitors.win[4] <- substr(df.t$prob.visitors.win[4],1,4)
df.t$prob.home.win[4] <- 100 - as.numeric(df.t$prob.visitors.win[4])
df.t$prob.visitors.win[4] <- paste(df.t$prob.visitors.win[4],'%',sep='')
df.t$prob.home.win[4] <- paste(df.t$prob.home.win[4],'%',sep='')
df.t$prob.visitors.win <- ifelse(nchar(df.t$prob.visitors.win)==3,paste(substr(df.t$prob.visitors.win,1,2),'.0%',sep=''),df.t$prob.visitors.win)
df.t$prob.home.win <- ifelse(nchar(df.t$prob.home.win)==3,paste(substr(df.t$prob.home.win,1,2),'.0%',sep=''),df.t$prob.home.win)
row.names(df.t) <- c('start state','runner on third scores','runner out at the plate','expected end state')
df.t
}
})
})