-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulate-with-short-passes-run.R
93 lines (70 loc) · 2.74 KB
/
simulate-with-short-passes-run.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#source to prepare data, then here can adjust file
####SPECIFY STRATEGY#####
source("simulate-with-short-passes-functions.R")
sack.param<-2
#assume sack rate on long throws=twice that of short throws
if("percent.sack"%in% colnames(stateDF)){
total<-(stateDF$percent.short.pass+sack.param*stateDF$percent.long.pass)
stateDF$percent.short.pass<-stateDF$percent.short.pass+stateDF$percent.sack*stateDF$percent.short.pass/total
stateDF$percent.long.pass<-stateDF$percent.long.pass+stateDF$percent.sack*sack.param*stateDF$percent.long.pass/total
head(stateDF, 10)
stateDF$percent.sack<-NULL
stateDF[is.na(stateDF)]<-0
}
#example:
stateDF[1:5,]
head(stateDF)
summary(rowSums(stateDF[, grepl("percent", colnames(stateDF))&!grepl("sack",colnames(stateDF) )], na.rm=T)) #should be all 1's
stateDF[1:2, ]
adjust.row(strategy = stateDF[1:2, ], increase.percent = .1, increase.var = "percent.long.pass")
adjust.row(strategy = stateDF[1:2, ], increase.percent = .2, increase.var = "percent.short.pass")
#specify strategy, or leave default
strategyDF<-stateDF
# strategyDF[strategyDF$down%in% 1:2,]<-
# adjust.row(strategyDF[strategyDF$down%in% 1:2, ], increase.percent = .1, increase.var = "percent.short.pass")
#check strategy:
head(strategyDF)
head(stateDF)
table(strategyDF$ydstogo.bin)
####RUN SIMULATION#####
n.sims <- 10000 #how many drives to simulate
all.drives.store <- list();length(all.drives.store)<-n.sims
yfog.start <- 25
for (i in 1:n.sims){
#i<-i+1
#initialize drive
drive.store <- list()
length(drive.store)<-40 #no drive is ever gonna be more than 40 plays
new.down <- 1
new.distance <- 10
new.yfog <- yfog.start
end.of.drive <- FALSE
play.num <- 1
#simulate until absorbing state
while (!end.of.drive){
run.play <- sample.play(df.scrimmage=df.scrimmage,
stateDF=stateDF,
down = new.down,
yards.to.go = new.distance,
yards.from.own.goal = new.yfog,
strategyDF = strategyDF,
sack.param=sack.param
) #can specify play_type here
run.play$play.num <- play.num
drive.store[[play.num]] <- run.play #add each play
new.down <- run.play$new.down
new.distance <- run.play$new.distance
new.yfog <- run.play$new.yfog
end.of.drive <- run.play$end.drive
play.num <- play.num + 1
drive.store[1:(play.num-1)]
}
drive.store<-rbindlist(drive.store)
drive.store$sim.id <- i
drive.store
if (i %% 100 == 0){print(i)}
all.drives.store[[i]] <- drive.store
}
#combine and save
all.drives.store<-rbindlist(all.drives.store)
write.csv(all.drives.store, file="Data/alldrives_withshortpasses.csv", row.names = F)