# create overall PointScore% for every individual team
## for every time that team served, how many times did they win the rally?
### and give me the mean, so we take (won rallies / total)
#### we use a 2nd dataframe that we'll use for the opponent (team2)
a <- aggregate(rally_winner ~ team, subset(master, skill=="Serve" & enoughdata=="yes"), mean)
b <- a
colnames(a) <- c("team1", "PSperc_t1")
colnames(b) <- c("team2", "PSperc_t2")
c <- merge(a,b)
# find out per match, per set, which team won the set
## then get rid of the count, it doesn't matter
d <- aggregate(count ~ team*opponent*match_id*set_id*teamwonset, subset(master, enoughdata=="yes"), sum)
colnames(d) <- c("team1", "team2", "match_id", "set_id","won_the_set","count")
d$count <- NULL
# merge the 2 dataframes (by match and set id)
## then create the PS diff between the two teams in the match
e <- merge(c,d)
e$PS_diff <- e$PSperc_t1-e$PSperc_t2
cor(e$PS_diff, e$won_the_set)
## [1] 0.4017074
# load libraries - pscl for the pR2 function to get psuedo-R2 values
## caret for the createdatapartition function we use to split train/test sets
library(pscl)
library(caret)
# set seed to be able to replicate this run
## create training and testing sets using a 80/20 split
### train the model on the 80% of the data
#### show the model the 20% it hasn't seen and ask it to predict who won the set
##### if model predicts 50%+ chance of winning, classify it as a predicted win
set.seed(123)
intrain <- createDataPartition(e$won_the_set, p = 0.8, list = FALSE)
training_set <- e[intrain,]
testing_set <- e[-intrain,]
glm.fit <- glm(won_the_set ~ PS_diff, family = binomial, data = training_set)
glm.probs <- predict(glm.fit, newdata = testing_set, type = "response")
glm.pred <- ifelse(glm.probs < 0.5, 1, 0)
# overall summary of the model
summary(glm.fit)
##
## Call:
## glm(formula = won_the_set ~ PS_diff, family = binomial, data = training_set)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.6359 -1.0201 -0.2603 1.0204 2.8720
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.005388 0.011097 -0.486 0.627
## PS_diff 22.849551 0.313465 72.893 <0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 53949 on 38915 degrees of freedom
## Residual deviance: 46989 on 38914 degrees of freedom
## AIC: 46993
##
## Number of Fisher Scoring iterations: 3
# pseudo-R2 value to measure "variance explained by model"
pR2(glm.fit)
## fitting null model for pseudo-r2
## llh llhNull G2 McFadden r2ML r2CU
## -23494.5102669 -26974.2918115 6959.5630892 0.1290036 0.1637566 0.2183429
# confusion matrix to test accuracy of model at prediction
cm <- table(glm.pred, testing_set$won_the_set)
cm
##
## glm.pred 0 1
## 0 1611 3256
## 1 3188 1674
# how often is the model correct at predicting the future?
accuracy <- (cm[1,1] + cm[2,2])/sum(cm)
accuracy
## [1] 0.3376503
# load library and draw a logistic plot w/ histograms of how data is distributed for W vs. L
library(popbio)
logi.hist.plot(e$PS_diff, e$won_the_set, boxp=FALSE, type = "hist", col = "gray")