Week 10: Generalized Linear Models

This data dive into our ‘Pokemon’ data set The Complete Pokemon Dataset involves creating a generalized linear model (GLM). GLMs are generalizations of the standard linear model, and are helpful when our dependent variable has a non-normal error distribution (residuals do not follow a normal, or Gaussian, distribution). In particular, we will be investigating the independent variables, percentage_male, type1, and type2 and how they model the behavior of the dependent variable, is_legendary.

About Logistic Regression

Logistic regression is a type of GLM used for binary outcomes. It tests whether each predictor (our independent variables) has no effect on the log-odds of the binary outcome. In other words, it states that none of the predictors will improve prediction compared to a model with no predictors.

\[ H_0:\beta_j = 0 \]

Where H is our outcome, beta is our coefficient, and j is for each predictor.

Because logistic regression is a linear model, probabilities are required to be transformed into numbers that range from negative to positive infinity so that we can apply different techniques. Hence, log-odds are the logarithmic product of the conversion of probability to odds (\(p/1-p)\)). Specifically, then, logistic regression is a type of linear model that predicts log-odds instead of raw probabilities. In the context of our Pokemon data, we are using logistic regression to model the odds that a Pokemon is legendary given if it’s single typing and missing a gender assignment.

Pokemon with Legendary Status

In the Pokemon franchise, there are exclusive species of Pokemon that assume the ‘legendary’ status (often referred to as ‘legendaries’). These Pokemon grace the covers of game cartridges and movies in the franchise and often play a pivotal role in the overarching story of each game. There are typically only one or two legendary Pokemon available for capture in each game and players have only one opportunity to catch them (not including resets or techniques for additional encounters). Along with their rarity, legendaries are highly coveted among players given their special abilities or move-pools that give them an added advantage in battle. Trainers seeking to improve their battle prowess or those interested in the story of the games would likely take an interest in understanding if there are any predictors for legendary status.

Our response variable is the binary column is_legendary that directly conveys legendary status. Our hypothesized predictors, percentage_male, type1 and type2 have contextual qualities that could relate to legendaries. Percentage_male is a continuous column that communicates three things: male (direct), female (indirectly), and a missing gender (represented by NA). The unknown gender is a point of interest as it leads to questions such as “What is the meaning of NA as a gender in the context of the Pokemon universe?” and “Could this be in relation to legendary status?”. Similarly, there are Pokemon with single typing and those with two types. Could it be possible that Pokemon with single typing are more likely to be legendary? If so, what might the context of this be?

Preparing the Data

Prior to plotting the data, we need to factor our data for type and gender given that our outcome is a binomial.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
pokemon$missing_binary <- as.integer(is.na(pokemon$percentage_male))
pokemon$type_binary <- as.integer(is.na(pokemon$type2))
View(pokemon)

Binarizing continuous columns typically reduces statistical power due to weaker coefficients, inflated standard errors and higher type 1 (false positives) errors. However, because our glm only needs to account for missing vs. non-missing values, this is one of the few situations where it remains optimal given we are checking for a ‘missing-ness’ pattern.

Modeling the Data

m <- glm(is_legendary ~ type_binary + missing_binary, data=pokemon, family=binomial)
summary(m)
## 
## Call:
## glm(formula = is_legendary ~ type_binary + missing_binary, family = binomial, 
##     data = pokemon)
## 
## Coefficients: (1 not defined because of singularities)
##                Estimate Std. Error z value Pr(>|z|)    
## (Intercept)     -4.5994     0.3799  -12.11   <2e-16 ***
## type_binary          NA         NA      NA       NA    
## missing_binary   5.1872     0.4344   11.94   <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: 474.93  on 800  degrees of freedom
## Residual deviance: 206.21  on 799  degrees of freedom
## AIC: 210.21
## 
## Number of Fisher Scoring iterations: 7
confint(m, "type_binary")
## Waiting for profiling to be done...
##  2.5 % 97.5 % 
##     NA     NA
confint(m, "missing_binary")
## Waiting for profiling to be done...
##    2.5 %   97.5 % 
## 4.397540 6.122023

The call displays our model that a Pokemon is legendary using gender and type. For our coefficients, the intercept presents the log-odds of legendary status for each variables’ reference category. Specifically, the intercept being -4.4199 (p <2e-16) represents the log-odds of being legendary when type_binary = 0 and missing_binary = 0. Converted to probability, the baseline probability is approximately 1.2%

\[ p = 1/1+e^4.4199 \approx 0.012 \]

The type_binary variable is -.4053 (p=0.281). These values are not statistically significant and have a small effect size. Furthermore, the 95% confidence interval for this coefficient is [-1.151, 0.332], meaning that our interval includes 0. We would fail to reject the null hypothesis for this variable given this evidence.

The missing_binary variable is 5.1702 (p <2e-16). This is incredibly statistically significant. The p-value is tiny due to the fact that when missing_binary is true, the odds of legendary status increase by ~176 (\(e^5.1702 \approx175.7\)). Lastly, the confidence interval for this variable excludes 0. This indicates that we should reject the null hypothesis for this variable.

Deviance conveys model fit. The null deviance is the deviance of our model with no predictors, while the residual deviance is the full model. There is a massive 269.89 point difference between models, with the full model being much lower than the null deviance, indicating that our predictors (primarily missing_binary) explain a huge amount of variation in legendary status.

Final Thoughts

The results show that singular typing does not increase the odds of a Pokemon being legendary. Yet, a missing gender assignment is a strong predictor of legendary status. The model performs better than the model with no predictors, so we can reject the null hypothesis.

While this analysis was relative clean, it should be mentioned that this data set is slightly outdated. As shown in the generation column, it covers Pokemon generations 1-7. As of 2026, there are 9 generations of Pokemon. The more correct version of our analysis would be to specify that ‘missing gender assignment is a strong predictor of legendary status for Pokemon generations 1-7’.