Chapter 18 Logistic Regression

18.1 What is logistic regression used for?

Logistic regression is useful when we have a response variable which is categorical with only two categories. This might seem like it wouldn’t be especially useful, however with a little thought we can see that this is actually a very useful thing to know how to do. Here are some examples where we might use logistic regression.

  • Predict whether a customer will visit your website again using browsing data
  • Predict whether a voter will vote for the democratic candidate in an upcoming election using demographic and polling data
  • Predict whether a patient given a surgery will survive for 5+ years after the surgery using health data
  • Given the history of a stock, market trends predict if the closing price tomorrow will be higher or lower than today?

With many other possible examples. We can often phrase important questions as yes/no or (0-1) answers where we want to use some data to better predict the outcome. This is a simple case of what is called a classification problem in the machine learning/data science community. Given some information we want to use a computer to decide make a prediction which can be sorted into some finite number of outcomes.

18.2 GLM: Generalized Linear Models

Our linear regression techniques thus far have focused on cases where the response (\(Y\)) variable is continuous in nature. Recall, they take the form: \[ Y_i=\alpha+ \sum_{j=1}^N \beta_j X_{ij} \] Where \(alpha\) is the intercept and \(\{\beta_1, \beta_2, ... \beta_N\}\) are the slope parameters for the explanatory variables (\(\{X_1, X_2, ...X_N\}\)). However, our outputs \(Y_i\) should give the probability that \(Y_i\) takes the value 1 given the \(X_j\) values. The right hand side of our model above will produce values in \(\mathbb{R}=(-\infty, \infty)\) while the left hand side should live in \([0,1]\).

Therefore to use a model like this we need to transform our outputs from [0,1] to the whole real line \(\mathbb{R}\).

The logit function is useful for this purpose as it maps \(logit: [0,1] \rightarrow \mathbb{R}\). The logit function takes the form: \[logit(x)=\ln \left(\frac{x}{1-x}\right)\] Thus, we can use our multiple regression techniques if treat our outputs as \(Y_i=logit(p_i)\). This is the basic idea of logistic regression: \[ \begin{aligned} & Y_i=logit(p_i)=\alpha+ \sum_{j=1}^N \beta_j X_{ij} \end{aligned} \] Usually, we want to know \(p_i\) and not \(logit(p_i)\) and we can find this using the inverse logit \(logit^{-1}\). \[ \begin{aligned} & p_i=logit^{-1} \left( \alpha+ \sum_{j=1}^N \beta_j X_{ij} \right) \\ & logit^{-1}(\gamma)=\frac{1}{1+e^{-\gamma}} \end{aligned} \] A logistic regression is one example in a family of techniques called generalized linear models (GLM). GLMs involve a linear predictor function \(\alpha+ \sum_{j=1}^N \beta_j X_{ij}\) and a link function \(g()\) which maps the linear predictor to the response variable.

\[y_i=g \left( \alpha+ \sum_{j=1}^N \beta_j X_{ij} \right)\]

18.3 A Starting Example

Let’s consider the shot logs data set again. We will use the shot distance column SHOT_DIST and the FGM columns for a logistic regression. The FGM column is 1 if the shot was made and 0 otherwise (perfect candidate for the response variable in a logistic regression). We expect that the further the shot is from the basket (SHOT_DIST) the less likely it will be that the shot is made (FGM=1).

To build this model in R we will use the glm() command and specify the link function we are using a the logit function.

logistic.nba<-glm(FGM~SHOT_DIST, data=shot_logs_2014, family=binomial(link="logit"))
logistic.nba$coefficients
##  (Intercept)    SHOT_DIST 
##  0.029247645 -0.001846314

\[logit(p)=0.392-0.04 \times SD \implies p=logit^{-1}(0.392-0.04 \times SD)\] So we can find the probability of a shot going in 12 feet from the basket as:

invlogit(0.392-0.04*12)
## [1] 0.4780142

Here is a plot of the probability of a shot going in as a function of the distance from the basket using our best fit coefficients.

x<-seq(0, 400, 40.0)
p<-invlogit(0.392-0.04*x)
slplot=dplyr::sample_n(shot_logs_2014, 1000)
plot(slplot$SHOT_DIST, slplot$FGM+rnorm(dim(slplot)[1], sd=0.01), cex=0.15, xlab='Shot Distance (Feet)', ylab='Probability of Shot Being Made')
lines(x,p, type = 'l', col='red')

18.3.1 Confidence Intervals for the Parameters

A major point of this book is that you should never be satisfied with a single number summary in statistics. Rather than just considering a single best fit for our coefficients we should really form some confidence intervals for their values.

As we saw for simple regression we can look at the confidence intervals for our intercepts and slopes using the confint command.

library(MASS)
ci1<-confint(logistic.nba)
print(ci1)
##                    2.5 %      97.5 %
## (Intercept)  0.029201282  0.02928679
## SHOT_DIST   -0.001849244 -0.00184327

Note, these values are still in the logit transformed scale.

18.4 Equivalence of Logistic Regression and Proportion Tests

Suppose we want to use the categorical variable of the individual player in our analysis. In the interest of keeping our tables and graphs visible we will limit our players to just those who took more than 820 shots in the data set.

library(dplyr)
lotsShots<-shot_logs_2014 %>% group_by(player_name) %>% summarise(num.attempts=n()) %>% filter(num.attempts>=820) %>% arrange(player_name)
knitr::kable(lotsShots, col.names = c('Name', 'Number of Shots'))
Name Number of Shots
blake griffin 878
chris paul 851
damian lillard 925
gordon hayward 833
james harden 1006
klay thompson 953
kyle lowry 832
kyrie irving 919
lamarcus aldridge 1010
lebron james 947
mnta ellis 1004
nikola vucevic 889
rudy gay 861
russell westbrook 943
stephen curry 941
tyreke evans 875

Now we can get a reduced data set with just these players.

sl2<-shot_logs_2014 %>% filter(player_name %in% lotsShots$player_name) %>% arrange(player_name)

Let’s form a logistic regression using just a categorical variable as the explanatory variable. \[ logit(p)=\beta Player \]

logistic.nba.player<-glm(FGM~player_name+0, data=sl2, family=binomial(link="logit"))
summary(logistic.nba.player)
## 
## Call:
## glm(formula = FGM ~ player_name + 0, family = binomial(link = "logit"), 
##     data = sl2)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
##  -8.49   -8.49    0.00    0.00    8.49  
## 
## Coefficients:
##                                 Estimate  Std. Error     z value Pr(>|z|)
## player_nameblake griffin      -5.306e+73   2.265e+06  -2.343e+67   <2e-16
## player_namechris paul          2.154e+75   2.300e+06   9.364e+68   <2e-16
## player_namedamian lillard      4.414e+90   2.207e+06   2.000e+84   <2e-16
## player_namegordon hayward      1.343e+76   2.325e+06   5.778e+69   <2e-16
## player_namejames harden       1.540e+116   2.116e+06  7.277e+109   <2e-16
## player_nameklay thompson       4.420e+76   2.174e+06   2.033e+70   <2e-16
## player_namekyle lowry        -6.631e+110   2.327e+06 -2.850e+104   <2e-16
## player_namekyrie irving       1.007e+116   2.214e+06  4.547e+109   <2e-16
## player_namelamarcus aldridge -2.915e+110   2.112e+06 -1.380e+104   <2e-16
## player_namelebron james        1.429e+78   2.181e+06   6.554e+71   <2e-16
## player_namemnta ellis        -3.981e+104   2.118e+06  -1.880e+98   <2e-16
## player_namenikola vucevic     1.478e+112   2.251e+06  6.565e+105   <2e-16
## player_namerudy gay           1.969e+105   2.287e+06   8.607e+98   <2e-16
## player_namerussell westbrook   6.740e+96   2.185e+06   3.084e+90   <2e-16
## player_namestephen curry     -1.748e+105   2.188e+06  -7.991e+98   <2e-16
## player_nametyreke evans      -4.751e+105   2.269e+06  -2.094e+99   <2e-16
##                                 
## player_nameblake griffin     ***
## player_namechris paul        ***
## player_namedamian lillard    ***
## player_namegordon hayward    ***
## player_namejames harden      ***
## player_nameklay thompson     ***
## player_namekyle lowry        ***
## player_namekyrie irving      ***
## player_namelamarcus aldridge ***
## player_namelebron james      ***
## player_namemnta ellis        ***
## player_namenikola vucevic    ***
## player_namerudy gay          ***
## player_namerussell westbrook ***
## player_namestephen curry     ***
## player_nametyreke evans      ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance:  20333  on 14667  degrees of freedom
## Residual deviance: 534095  on 14651  degrees of freedom
## AIC: 534127
## 
## Number of Fisher Scoring iterations: 7

If we take the inverse logit of the coefficients we get the field goal percentage of the players in our data set.

invlogit(logistic.nba.player$coefficients)
##     player_nameblake griffin        player_namechris paul 
##                            0                            1 
##    player_namedamian lillard    player_namegordon hayward 
##                            1                            1 
##      player_namejames harden     player_nameklay thompson 
##                            1                            1 
##        player_namekyle lowry      player_namekyrie irving 
##                            0                            1 
## player_namelamarcus aldridge      player_namelebron james 
##                            0                            1 
##        player_namemnta ellis    player_namenikola vucevic 
##                            0                            1 
##          player_namerudy gay player_namerussell westbrook 
##                            1                            1 
##     player_namestephen curry      player_nametyreke evans 
##                            0                            0

Now suppose we want to see if the players in our data set truly differ in their field goal percentages or whether the differences we observe could just be caused by random effects. To do this we want to compare a model without the players information included with one that includes this information. Let’s create a null model to compare against our player model.

null.player.model<-glm(FGM~1, data=sl2, family=binomial(link="logit"))

This null model contains no explanatory variables and takes the form: \[logit(p_i)=\alpha\]

Thus, the shooting percentage is not allowed to vary between the players. We find based on this data an overall field goal percentage of:

invlogit(null.player.model$coefficients)
## (Intercept) 
##   0.5199527

Now we may compare logistic regression models using the anova command in R.

anova(null.player.model, logistic.nba.player, test='LRT')
## Analysis of Deviance Table
## 
## Model 1: FGM ~ 1
## Model 2: FGM ~ player_name + 0
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1     14666      20439                     
## 2     14651     534095 15  -513656

The second line contains a p value of 2.33e-5 telling us to reject the null hypothesis that the two models are equivalent. So we found that knowledge of the player does matter in calculating the probability of a shot being made.

Notice we could have performed this analysis as a proportion test using the null that all players shooting percentages are the same \(p_1=p_2=...p_{15}\)

prop.test(table(sl2$player_name, sl2$FGM))
## 
##  16-sample test for equality of proportions without continuity
##  correction
## 
## data:  table(sl2$player_name, sl2$FGM)
## X-squared = 48.247, df = 15, p-value = 2.32e-05
## alternative hypothesis: two.sided
## sample estimates:
##    prop 1    prop 2    prop 3    prop 4    prop 5    prop 6    prop 7 
## 0.4977221 0.5158637 0.5664865 0.5462185 0.5516899 0.5362015 0.5829327 
##    prop 8    prop 9   prop 10   prop 11   prop 12   prop 13   prop 14 
## 0.5277476 0.5504950 0.5058078 0.5448207 0.4668166 0.5481998 0.5577943 
##   prop 15   prop 16 
## 0.5069075 0.5600000

Notice the p-value obtained matches the logistic regression ANOVA almost exactly. Thus, a proportion test can be viewed as a special case of a logistic regression.

18.5 Example: Building a More Accurate Model

Now we can form a model for the shooting percentages using the individual players data:

\[ logit(p_i)=\alpha+\beta_1 SF+\beta_{2} DD+\beta_3 (player_dummy) \]

logistic.nba2<-glm(FGM~SHOT_DIST+Team.Defending, data=shot_logs_2014, family=binomial(link="logit"))
summary(logistic.nba2)
## 
## Call:
## glm(formula = FGM ~ SHOT_DIST + Team.Defending, family = binomial(link = "logit"), 
##     data = shot_logs_2014)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
##  -8.49   -8.49   -8.49    0.00    0.00  
## 
## Coefficients:
##                     Estimate Std. Error    z value Pr(>|z|)    
## (Intercept)       1.555e+184  1.917e+05 8.109e+178   <2e-16 ***
## SHOT_DIST          2.454e+33 5.043e-147 4.865e+179   <2e-16 ***
## Team.DefendingBKN  5.853e+35 2.406e-144 2.433e+179   <2e-16 ***
## Team.DefendingBOS  8.563e+35 1.760e-144 4.865e+179   <2e-16 ***
## Team.DefendingCHA  3.215e+35 1.982e-144 1.622e+179   <2e-16 ***
## Team.DefendingCHI  8.114e+35 2.001e-144 4.054e+179   <2e-16 ***
## Team.DefendingCLE  1.324e+36 2.041e-144 6.487e+179   <2e-16 ***
## Team.DefendingDAL  1.125e+36 1.735e-144 6.487e+179   <2e-16 ***
## Team.DefendingDEN  3.335e+35 1.763e-144 1.892e+179   <2e-16 ***
## Team.DefendingDET  1.221e+36 2.581e-144 4.730e+179   <2e-16 ***
## Team.DefendingGSW  1.359e+36 2.052e-144 6.622e+179   <2e-16 ***
## Team.DefendingHOU  1.050e+36 1.851e-144 5.676e+179   <2e-16 ***
## Team.DefendingIND  4.348e+35 2.298e-144 1.892e+179   <2e-16 ***
## Team.DefendingLAC  1.271e+36 1.919e-144 6.622e+179   <2e-16 ***
## Team.DefendingLAL  1.994e+36 2.342e-144 8.514e+179   <2e-16 ***
## Team.DefendingMEM  2.322e+36 2.455e-144 9.460e+179   <2e-16 ***
## Team.DefendingMIA  1.756e+36 2.166e-144 8.109e+179   <2e-16 ***
## Team.DefendingMIL  2.252e+36 2.380e-144 9.460e+179   <2e-16 ***
## Team.DefendingMIN  1.910e+36 2.826e-144 6.757e+179   <2e-16 ***
## Team.DefendingNOP  9.558e+35 2.358e-144 4.054e+179   <2e-16 ***
## Team.DefendingNYK  1.209e+36 2.236e-144 5.406e+179   <2e-16 ***
## Team.DefendingOKC  6.746e+35 1.664e-144 4.054e+179   <2e-16 ***
## Team.DefendingORL  2.134e+36 2.632e-144 8.109e+179   <2e-16 ***
## Team.DefendingPHI  1.933e+36 2.044e-144 9.460e+179   <2e-16 ***
## Team.DefendingPHX  1.316e+36 2.164e-144 6.081e+179   <2e-16 ***
## Team.DefendingPOR  2.028e+36 2.382e-144 8.514e+179   <2e-16 ***
## Team.DefendingSAC  6.167e+35 1.690e-144 3.649e+179   <2e-16 ***
## Team.DefendingSAS  1.860e+36 1.911e-144 9.730e+179   <2e-16 ***
## Team.DefendingTOR  1.806e+36 2.969e-144 6.081e+179   <2e-16 ***
## Team.DefendingUTA  1.374e+36 2.259e-144 6.081e+179   <2e-16 ***
## Team.DefendingWAS  1.747e+36 1.795e-144 9.730e+179   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance:  168881  on 122501  degrees of freedom
## Residual deviance: 4802601  on 122471  degrees of freedom
## AIC: 4802663
## 
## Number of Fisher Scoring iterations: 5

18.6 Example: Measuring Team Defense Using Logistic Regression

\[ logit(p_i)=\alpha+\beta_1 SD+\beta_2 (Team)+\beta_3 (Team) (SD) \] Since the team defending is a categorical variable R will store it as a dummy variable when forming the regression. Thus the first level of this variable will not appear in our regression (or more precisely it will be included in the intercept \(\alpha\) and slope \(\beta_1\)). Before we run the model we can see which team will be missing.

levels(shot_logs_2014$Team.Defending)[1]
## [1] "ATL"
logistic.nba.team<-glm(FGM~SHOT_DIST+Team.Defending+Team.Defending:SHOT_DIST, data=shot_logs_2014, family=binomial(link="logit"))
summary(logistic.nba.team)
## 
## Call:
## glm(formula = FGM ~ SHOT_DIST + Team.Defending + Team.Defending:SHOT_DIST, 
##     family = binomial(link = "logit"), data = shot_logs_2014)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
##  -8.49   -8.49   -8.49    0.00    0.00  
## 
## Coefficients:
##                               Estimate Std. Error    z value Pr(>|z|)    
## (Intercept)                 2.146e+208  1.917e+05 1.119e+203   <2e-16 ***
## SHOT_DIST                    9.809e+58 8.763e-146 1.119e+204   <2e-16 ***
## Team.DefendingBKN            9.871e+61 1.470e-142 6.717e+203   <2e-16 ***
## Team.DefendingBOS            1.197e+61 3.564e-143 3.358e+203   <2e-16 ***
## Team.DefendingCHA            1.180e+61 3.512e-143 3.358e+203   <2e-16 ***
## Team.DefendingCHI            7.450e+60 2.773e-143 2.687e+203   <2e-16 ***
## Team.DefendingCLE            1.240e+61 3.693e-143 3.358e+203   <2e-16 ***
## Team.DefendingDAL            6.713e+60 3.332e-143 2.015e+203   <2e-16 ***
## Team.DefendingDEN            3.170e+66 3.077e-143 1.030e+209   <2e-16 ***
## Team.DefendingDET            1.457e+66 1.885e-143 7.727e+208   <2e-16 ***
## Team.DefendingGSW            1.064e+66 2.755e-143 3.863e+208   <2e-16 ***
## Team.DefendingHOU            2.784e+66 3.088e-143 9.015e+208   <2e-16 ***
## Team.DefendingIND            9.063e+66 7.819e-143 1.159e+209   <2e-16 ***
## Team.DefendingLAC            2.327e+66 3.011e-143 7.727e+208   <2e-16 ***
## Team.DefendingLAL            2.866e+66 7.417e-143 3.863e+208   <2e-16 ***
## Team.DefendingMEM            2.674e+66 2.966e-143 9.015e+208   <2e-16 ***
## Team.DefendingMIA            8.603e+65 2.227e-143 3.863e+208   <2e-16 ***
## Team.DefendingMIL            1.956e+66 1.898e-143 1.030e+209   <2e-16 ***
## Team.DefendingMIN            1.190e+66 4.619e-143 2.576e+208   <2e-16 ***
## Team.DefendingNOP            3.764e+66 7.307e-143 5.151e+208   <2e-16 ***
## Team.DefendingNYK            1.875e+66 2.427e-143 7.727e+208   <2e-16 ***
## Team.DefendingOKC            2.500e+66 2.427e-143 1.030e+209   <2e-16 ***
## Team.DefendingORL            1.142e+66 2.660e-143 4.293e+208   <2e-16 ***
## Team.DefendingPHI            7.342e+65 3.420e-143 2.146e+208   <2e-16 ***
## Team.DefendingPHX            1.068e+71 2.595e-143 4.115e+213   <2e-16 ***
## Team.DefendingPOR            6.241e+71 5.055e-143 1.235e+214   <2e-16 ***
## Team.DefendingSAC            2.971e+71 3.009e-143 9.877e+213   <2e-16 ***
## Team.DefendingSAS            4.097e+71 2.370e-143 1.728e+214   <2e-16 ***
## Team.DefendingTOR            4.690e+71 1.899e-143 2.469e+214   <2e-16 ***
## Team.DefendingUTA            6.966e+71 3.526e-143 1.975e+214   <2e-16 ***
## Team.DefendingWAS            2.498e+71 2.023e-143 1.235e+214   <2e-16 ***
## SHOT_DIST:Team.DefendingBKN  9.555e+70 9.674e-144 9.877e+213   <2e-16 ***
## SHOT_DIST:Team.DefendingBOS  1.272e+65 1.975e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingCHA  1.268e+65 1.969e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingCHI  1.176e+65 1.826e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingCLE  1.680e+65 1.957e-144 8.585e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingDAL  2.259e+65 2.631e-144 8.585e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingDEN  9.744e+64 2.270e-144 4.293e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingDET  7.925e+64 1.477e-144 5.366e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingGSW  1.812e+65 2.814e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingHOU  9.761e+64 2.274e-144 4.293e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingIND  2.962e+65 4.600e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingLAC  1.797e+65 1.994e-144 9.015e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingLAL  2.949e+65 4.580e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingMEM  2.582e+65 2.506e-144 1.030e+209   <2e-16 ***
## SHOT_DIST:Team.DefendingMIA  8.115e+64 1.575e-144 5.151e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingMIL  1.211e+65 1.612e-144 7.512e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingMIN  2.387e+65 3.708e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingNOP  2.505e+65 4.667e-144 5.366e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingNYK  1.519e+65 2.022e-144 7.512e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingOKC  7.522e+64 1.947e-144 3.863e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingORL  2.434e+65 2.100e-144 1.159e+209   <2e-16 ***
## SHOT_DIST:Team.DefendingPHI  1.562e+65 2.021e-144 7.727e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingPHX  1.226e+65 1.904e-144 6.439e+208   <2e-16 ***
## SHOT_DIST:Team.DefendingPOR  4.237e+65 4.113e-144 1.030e+209   <2e-16 ***
## SHOT_DIST:Team.DefendingSAC  2.488e+65 1.811e-144 1.374e+209   <2e-16 ***
## SHOT_DIST:Team.DefendingSAS  1.078e+60 1.337e-144 8.060e+203   <2e-16 ***
## SHOT_DIST:Team.DefendingTOR  1.052e+60 1.468e-144 7.164e+203   <2e-16 ***
## SHOT_DIST:Team.DefendingUTA  1.040e+60 2.582e-144 4.030e+203   <2e-16 ***
## SHOT_DIST:Team.DefendingWAS  8.728e+59 1.856e-144 4.702e+203   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance:  168881  on 122501  degrees of freedom
## Residual deviance: 4802601  on 122442  degrees of freedom
## AIC: 4802721
## 
## Number of Fisher Scoring iterations: 5

The below plot shows the expected shooting percentages at each distance for the teams in the data set.

#Make a ribbon plot including the errors in the predictions

plotTeamsSP<- function(teamList) {
  teamdf %>% filter(Team %in% teamList) %>% mutate(upper.prob=invlogit(logit(Results)+2*se),lower.prob=invlogit(logit(Results)-2*se))%>% ggplot(aes(x=xvalues, y=Results, color=Team))+geom_line()+geom_ribbon(aes(ymin=lower.prob, ymax=upper.prob, color=Team, fill=Team), alpha=0.2)+xlab('Shot Distance (FT)')+ylab('Probability of Made Shot')+labs(title='Team Defense')
}

plotTeamsSP(c("GSW", "SAS"))

#Better Approach

inTraining<-createDataPartition(shot_logs_2014$FGM, p=0.80, list=FALSE)
sl_train<-shot_logs_2014[inTraining,]
sl_test <-shot_logs_2014[-inTraining,]
#Build a logistic regression model using the training data
lr_sd<-glm(FGM~SHOT_DIST+as.factor(player_name), data=sl_train, family = binomial(link="logit"))

predicted.train<-ifelse(invlogit(predict.glm(lr_sd, sl_train))<0.5, "Made", "Missed")
actual.train<-ifelse(sl_train$FGM==1, "Made", "Missed")


confusionMatrix(as.factor(predicted.train), as.factor(actual.train))
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  Made Missed
##     Made       0      0
##     Missed 44729  53273
##                                           
##                Accuracy : 0.5436          
##                  95% CI : (0.5405, 0.5467)
##     No Information Rate : 0.5436          
##     P-Value [Acc > NIR] : 0.5013          
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.0000          
##             Specificity : 1.0000          
##          Pos Pred Value :    NaN          
##          Neg Pred Value : 0.5436          
##              Prevalence : 0.4564          
##          Detection Rate : 0.0000          
##    Detection Prevalence : 0.0000          
##       Balanced Accuracy : 0.5000          
##                                           
##        'Positive' Class : Made            
## 

Kahneman, Daniel. 2011. Thinking, Fast and Slow. Macmillan.

Wickham, Hadley, and Garrett Grolemund. 2016. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. " O’Reilly Media, Inc.".

Xie, Yihui. 2018. Bookdown: Authoring Books and Technical Documents with R Markdown. https://CRAN.R-project.org/package=bookdown.