Hello everybody,
It’s Michael, and today’s post will be the first to cover data modeling in R. The model I will be discussing is the logistic regression model. For those that don’t know, logistic regression models explore the relationship between a binary* dependent variable and one or more independent variables.
*refers to variable with only 2 possible values, like yes/no, wrong/right, healthy/sick etc.
The data set I will be using is-TV shows-which gives a list of 85 random TV shows of various genres that were currently airing during the 2017-18 TV season and whether or not each show was renewed for the 2018-19 TV season. So, like any good data scientist, let’s first load the file and read (as well as understand) the data.

The variables include
- TV Show-the name of the TV show
- Genre-the genre of the TV show
- Premiere Year-the year the TV show premiered (for reboots like Roseanne, I included the premiere date of the original, not the revival)
- X..of.seasons..17.18. (I’ll refer to it as season count)-how many seasons the show had aired at the conclusion of the 2017-18 TV season (in the case of revived shows like American Idol, I counted both the original run and revival, which added up to 16 seasons)
- Network-the network the show was airing on at the end of the 2017-18 TV season
- X2018.19.renewal. (my binary variable)-Whether or not the show was renewed for the 2018-19 TV season
- You’ll notice I used 0 and 1 for this variable; this is because it is a good idea to use dummy variables (the 0 and 1) for your binary dependent variable to help quantify qualitative data.
- The qualitative data in this case being whether a show was renewed for the 2018-19 TV season (shown by 1) or not (shown by 0)
- You’ll notice I used 0 and 1 for this variable; this is because it is a good idea to use dummy variables (the 0 and 1) for your binary dependent variable to help quantify qualitative data.
Now that we know the variables in our data set, let’s figure out what we want to analyze.
- Let’s analyze the factors (eg. network, genre) that affected a certain TV show’s renewal or cancellation (the binary variable represented by 0/1)
So here’s the code to build the model, using the binary dependent variable and two of the independent variables (I’ll use genre and premiere year)
What does all of this output mean?
- The call just reprints the model we created.
- The estimate represents the change in log odds (or logarithm of the odds) for the dependent variable should a certain independent variable variable be increased by 1.
- Log odds function–>log(p/(1-p))
- For instance, if the premiere year increases by 1 (let’s say from 2009 to 2010), the odds that it was renewed for the 18-19 TV season decrease by 6.73% (as evidenced by the -0.06763 as the premiere year estimate)
- Standard error represents how far the sample mean is from the population mean. In the case of premiere year, the two means are close together. In the case of genre however, the two means are mostly far apart (then again, genre isn’t numerical).
- Z-value is the ratio of the estimate to the standard error
- P-value (denoted by Pr(|>z|)) helps you determine the significance of your results by giving you a number between 0 and 1
- P-values are used to either prove or disprove your null hypothesis (a claim you are making about your data)
- Let’s say you think a show’s genre and premiere year affected its chances of renewal; this would be your null hypothesis.
- Your alternative hypothesis would be the opposite of your null hypothesis; that is, genre and premiere year don’t affect a shows chances of renewal
- Small p-values (those <=0.05) indicate strong evidence against the null hypothesis, so in these cases, you can reject the null hypothesis. For p-values larger than 0.05, you should accept the null hypothesis.
- Since all the p-values are well above 0.05, you can accept the null hypothesis
- P-values are used to either prove or disprove your null hypothesis (a claim you are making about your data)
- Null deviance shows how well our dependent variable (whether or not a show got renewed) is predicted by a model that includes only the intercept
- Residual deviance shows how well our dependent variable (whether or not a show got renewed) is predicted by a model that includes the intercept as well as any independent variables
- As you can see here, the residual deviance is 89.496 on 71 degrees of freedom, a decrease of 20.876 from null deviance (as well as a decrease of 13 degrees of freedom).
- AIC (or Akaike Information Criterion) is a way to gauge the quality of your model through comparison of related models; the point of the AIC is to prevent you from using irrelevant independent variables.
- The AIC itself is meaningless unless we have another model to compare it to, which I will include in this post.
- The number of Fisher scoring iterations shows how many times the model ran to attain maximum likelihood, 17 in this case. This number isn’t too significant.
Now let’s create another model, this time including season count in place of genre.
How does this compare to the previous model?
- There is a smaller difference between null & residual deviance (12.753 and 2 degrees of freedom, as opposed to 20.876 and 13 degrees of freedom)
- The AIC is 13.88 smaller than that of the previous model, which indicates a better quality of the model
- The number of Fisher scoring iterations is also lower than the previous model (5 as opposed to 17), which means it took less tries to attain maximum likelihood (that a show was renewed)
- The estimate for premiere year also increased
- This time, if premiere year increases by 1, the odds that a show was renewed for the 2018-19 TV season increased by 14.81%, rather than decreased.
- If season count increased by 1 (say from 4 to 5 seasons), then the odds a show was renewed increased by 31.45%
- The asterisk by season count just gives an idea of the range of p-values of season count (denoted by Pr(|>z|))
- The p-value of season count is >0.01 but <0.05 (which makes perfect sense as Pr(|>z|) is 0.027
- Let’s create two null hypotheses-premiere year and season count affects a show’s chances of renewal (we are treating these as separate hypotheses).
- Premiere year is greater than 0.05, so accept this null hypothesis.
- In other words, premiere year did affect a show’s chances for renewal.
- Season count is less than 0.05, so reject this null hypothesis.
- In other words, season count didn’t affect a show’s chances for renewal.
- Premiere year is greater than 0.05, so accept this null hypothesis.
That’s all for now. Thanks for reading.
Michael
4 thoughts on “R Lesson 4: Logistic Regression Models”