# when using lag/lead functions, gotta double check data is in correct order
## main dataframe can get reordered during "merge" functions, so we reorder by id_master
master <- master[order(master$id_master),]
# input situations - possibility 1
## use the attack code to set expectations
a <- aggregate(rally_eff ~ attack_code, subset(master, skill=="Attack" & enoughdata=="yes"), mean)
b <- aggregate(count ~ attack_code, subset(master, skill=="Attack" & enoughdata=="yes"), sum)
c <- merge(a,b)
write.csv(c, "step8_attackcode.csv")
# input situations - possibility 2
## use the 1st touch + quality to set expectations
master$firsttouch <- NA
master$firsttouch <- ifelse(lag(master$skill=="Set") & lag(master$skill, 2) %in% c("Receive", "Dig", "Freeball"), lag(master$skq,2), master$firsttouch)
d <- aggregate(rally_eff ~ firsttouch, subset(master, skill=="Attack" & enoughdata=="yes"), mean)
e <- aggregate(count ~ firsttouch, subset(master, skill=="Attack" & enoughdata=="yes"), sum)
f <- merge(d,e)
write.csv(f, "step8_firsttouch.csv")
# input situations - possibility 3
## use a combination of in-sys/OOS & type of block the attacker faces to set expectations
### yes, this could be cleaner, but I pulled it from where I have input/outputs for ALL skills so this method was easier for that task
master$attack_type <- NA
master$attack_type <- ifelse(master$add_cover %in% c("Reception", "Dig", "Cover", "Freeball") & lead(master$team, 1)==master$team & lead(master$team,2)==master$team & lead(master$add_cover,1)=="Set",
substr(lead(master$attack_code,2),1,1), master$attack_type)
master$in_or_oos <- NA
master$in_or_oos <- ifelse(master$attack_type=="V", "OOS", master$in_or_oos)
master$in_or_oos <- ifelse(master$attack_type %in% c("C", "X", "P"), "In Sys", master$in_or_oos)
# create the 2nd touch situation labels
## if good set, grab In/OOS and combine w/ blockers
### if poor or error, label as such
master$second_touch_result <- NA
master$second_touch_result <- ifelse(master$skq=="Set #" & lead(master$skill=="Attack",1), paste0(lag(master$in_or_oos,1), " - ", lead(master$num_players)), master$second_touch_result)
master$second_touch_result <- ifelse(master$skq=="Set -", "Poor Set", master$second_touch_result)
master$second_touch_result <- ifelse(master$skq=="Set =", "Ball Handling Error", master$second_touch_result)
# same deal, create inputs based on second touch result, unless overpass...in which case, call it overpass
master$input <- NA
master$input <- ifelse(master$skill=="Attack", lag(master$second_touch_result,1), master$input)
overpass <- c("Reception /", "Dig -", "Cover -", "Freeball /")
master$input <- ifelse(master$skill=="Attack" & master$one_touch_ago %in% overpass, "overpass", master$input)
g <- aggregate(rally_eff ~ input, subset(master, skill=="Attack" & enoughdata=="yes"), mean)
h <- aggregate(count ~ input, subset(master, skill=="Attack" & enoughdata=="yes"), sum)
i <- merge(g,h)
write.csv(i, "step8_fullinputs.csv")
# pull National Champ specific data
a <- aggregate(input_eff ~ input*team*player_name, subset(master, skill=="Attack" & match_id=="7a7773f6bed6251f00b1ab12056fc2b6"), mean)
b <- aggregate(count ~ input*team*player_name, subset(master, skill=="Attack" & match_id=="7a7773f6bed6251f00b1ab12056fc2b6"), sum)
c <- merge(a,b)
d <- aggregate(poss_eff ~ input*team*player_name, subset(master, skill=="Attack" & match_id=="7a7773f6bed6251f00b1ab12056fc2b6"), mean)
e <- merge(c,d)
write.csv(e, "step8_Stanford.csv")