I am using the same dataset as last homework, which I found on kaggle, and is based on a survey about people’s opinions on gun control. The topic for this week is about support for more extensive background checks for potential gun buyers. There have been many discussions about having more extensive background checks for potential gun buyers, as this is currently a popular topic in the current political climate https://www.nraila.org/get-the-facts/background-checks-nics/.
I am interested in seeing the relationship between support for more extensive background checks on gun buyers (dependent binary variable), ones’ age group, gender, support for restrictions on who can buy a gun, and political affiliation (all independent variables). Since I personally feel that factors such as political affiliation and gender are very important when looking at surveys about the second amendment, I will be looking at them again.
#Loading the dataset
library(texreg)
library(tidyr)
library(list)
library(corpcor)
library(tidyverse)
library(knitr)
library (Zelig)
library(readr)
gun_control <- read_csv("C:/Users/abbys/Downloads/gun_control.csv")
Parsed with column specification:
cols(
gender = [31mcol_character()[39m,
age = [31mcol_character()[39m,
region = [31mcol_character()[39m,
political_affiliation = [31mcol_character()[39m,
`should_background_checks_on_gun_buyers_be_more_extensive<U+613C><U+3E30>` = [31mcol_character()[39m,
should_there_be_more_restrictions_on_who_can_buy_a_gun = [31mcol_character()[39m,
will_further_gun_control_reduce_the_possibilities_of_mass_shootings = [31mcol_character()[39m,
`should_civilians_be_allowed_to_own_guns<U+613C><U+3E30>` = [31mcol_character()[39m
)
head(gun_control)
guncontrol2=gun_control %>%
rename(`more_restrictions`=`should_there_be_more_restrictions_on_who_can_buy_a_gun`,
`background_checks`=`should_background_checks_on_gun_buyers_be_more_extensive `)%>%
mutate(background_checks = sjmisc::rec(background_checks, rec = "No=0; Yes=1"))
head(guncontrol2)
guncontrol2$age=recode(guncontrol2$age,"17 or younger"="Under 18", "18 to 24 years"="Young Adult", "25 to 34 years"="Young Adult", "35 to 44 years"="Adult/Middle Aged", "45 to 54 years"="Adult/Middle Aged", "55 to 64 years"="Adult/Middle Aged", "65 years and over"="Senior Citizen")
guncontrol2$political_affiliation=recode(guncontrol2$political_affiliation,"Green Party"="Liberals","Democratic Party"="Liberals", "Republican Party"="Conservatives","Constitution Party"="Conservatives")
head(guncontrol2)
Starting with the age variable.
glm1<-glm(background_checks ~ age,data = guncontrol2,family="binomial")
summary(glm1)
Call:
glm(formula = background_checks ~ age, family = "binomial", data = guncontrol2)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.1184 0.4735 0.4735 0.5448 0.5448
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.8326 0.1795 10.209 <2e-16 ***
ageSenior Citizen 15.7335 907.6100 0.017 0.986
ageUnder 18 0.1133 0.7769 0.146 0.884
ageYoung Adult 0.2990 0.2687 1.113 0.266
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 406.15 on 559 degrees of freedom
Residual deviance: 400.05 on 556 degrees of freedom
AIC: 408.05
Number of Fisher Scoring iterations: 16
Model 1: Intercept: when age=middle aged (ie when age=0, absorbed by the intercept), middle aged people have a log odds of supporting background checks of 1.8326. The other age variables (slopes) are not statistically significant.
Adding the variables gender and political_affiliation to the model.
glm2<-glm(background_checks ~ age+political_affiliation+gender,data = guncontrol2,family = "binomial")
summary(glm2)
Call:
glm(formula = background_checks ~ age + political_affiliation +
gender, family = "binomial", data = guncontrol2)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.4281 0.3283 0.4355 0.5288 0.8767
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.8969 0.2876 6.595 4.26e-11 ***
ageSenior Citizen 15.7304 894.2276 0.018 0.9860
ageUnder 18 0.2001 0.7877 0.254 0.7995
ageYoung Adult 0.3604 0.2773 1.300 0.1936
political_affiliationLiberals 0.6367 0.2935 2.169 0.0301 *
political_affiliationLibertarian Party -0.5527 0.3910 -1.413 0.1575
genderMale -0.5863 0.2805 -2.090 0.0366 *
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 406.15 on 559 degrees of freedom
Residual deviance: 384.18 on 553 degrees of freedom
AIC: 398.18
Number of Fisher Scoring iterations: 16
Model 2: Age does not seem to be statistically significant except for the intercept. However, adding political affiliation and gender are.
Liberals have higher log odds of supporting extensive background checks than other political ideologies by .6367. Compared to females, males have a decreased log odds for supporting extensive background checks by .5863.
Adding more_restrictions variable to the model, and adding an interaction between more_restrictions and gender
glm3<-glm(background_checks ~ age+political_affiliation+more_restrictions*gender,data =guncontrol2,family = "binomial")
summary(glm3)
Call:
glm(formula = background_checks ~ age + political_affiliation +
more_restrictions * gender, family = "binomial", data = guncontrol2)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.7816 0.2216 0.2562 0.2998 1.6703
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.8565 0.5044 -1.698 0.0895 .
ageSenior Citizen 15.6114 831.0393 0.019 0.9850
ageUnder 18 0.9851 0.9784 1.007 0.3140
ageYoung Adult 0.2943 0.3557 0.827 0.4081
political_affiliationLiberals -0.1529 0.3931 -0.389 0.6972
political_affiliationLibertarian Party -0.7132 0.5301 -1.345 0.1785
more_restrictionsYes 4.4097 0.5990 7.362 1.82e-13 ***
genderMale 0.4597 0.5493 0.837 0.4027
more_restrictionsYes:genderMale -1.0744 0.7312 -1.469 0.1418
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 406.15 on 559 degrees of freedom
Residual deviance: 248.71 on 551 degrees of freedom
AIC: 266.71
Number of Fisher Scoring iterations: 16
Model 3: For the 3rd model, gender and political affiliation are no longer statistically significant, and neither is the new interaction term. The only statistically significant terms are the intercept but only at the .1 level, and more_restictionsYes.
Those who support more restrictions on who can buy a gun have an increase in log odds by 4.410 of supporting more extensive background checks for gun purchasers compared to those who do not support more restrictions on who can buy a gun.
more_restrictionsYes:genderMale: Males who support more restrictions on who can buy a gun have a decrease in log odds by 1.074 of supporting more extensive background checks for gun purchasers compared to females, (which is not statistically significant).
The best fit model is Model 3 due to the AIC and BIC being the lowest.
library(texreg)
htmlreg(list(glm1, glm2, glm3), caption = "", digits = 3)
| Model 1 | Model 2 | Model 3 | ||
|---|---|---|---|---|
| (Intercept) | 1.833*** | 1.897*** | -0.857 | |
| (0.180) | (0.288) | (0.504) | ||
| ageSenior Citizen | 15.733 | 15.730 | 15.611 | |
| (907.610) | (894.228) | (831.039) | ||
| ageUnder 18 | 0.113 | 0.200 | 0.985 | |
| (0.777) | (0.788) | (0.978) | ||
| ageYoung Adult | 0.299 | 0.360 | 0.294 | |
| (0.269) | (0.277) | (0.356) | ||
| political_affiliationLiberals | 0.637* | -0.153 | ||
| (0.294) | (0.393) | |||
| political_affiliationLibertarian Party | -0.553 | -0.713 | ||
| (0.391) | (0.530) | |||
| genderMale | -0.586* | 0.460 | ||
| (0.280) | (0.549) | |||
| more_restrictionsYes | 4.410*** | |||
| (0.599) | ||||
| more_restrictionsYes:genderMale | -1.074 | |||
| (0.731) | ||||
| AIC | 408.047 | 398.182 | 266.712 | |
| BIC | 425.359 | 428.477 | 305.663 | |
| Log Likelihood | -200.023 | -192.091 | -124.356 | |
| Deviance | 400.047 | 384.182 | 248.712 | |
| Num. obs. | 560 | 560 | 560 | |
| p < 0.001, p < 0.01, p < 0.05 | ||||
The anova test shows that the best model is Model 3, having the lowest p value of < 2.2e-16.
anova(glm1, glm2,glm3, test = "Chisq")
Analysis of Deviance Table
Model 1: background_checks ~ age
Model 2: background_checks ~ age + political_affiliation + gender
Model 3: background_checks ~ age + political_affiliation + more_restrictions *
gender
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 556 400.05
2 553 384.18 3 15.865 0.001208 **
3 551 248.71 2 135.470 < 2.2e-16 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Although not statistically significant, I am showing a visualization of the interaction between gender and supporting more restrictions on who can buy a gun. Compared to males that support more restrictions on who can buy a gun, females that support more restrictions are more likely to support more extensive background checks on gun buyers, but only by a little bit, which supports my interpretation of the interaction in model 3, which is what I found in my interpretation above.
However, males that do not support more restrictions on who can buy a gun are more likely to support more extensive background checks than females who also do not support more restrictions on who can buy a gun.
visreg::visreg(glm3,"more_restrictions",by = "gender",scale = 'response')
Something interesting to look at: when looking at more restrictions on who can buy a gun by age, senior citizens have the most support for background checks regardless of whether they support or do not support more restrictions. People under 18, young adults, and adults/middle aged people who support more restrictions on buying a gun are more likely to support background checks than people who do not support more gun restrictions, with those under age 18 being more likely to support background checks than young adults and adults/middle aged people who say don’t support more restrictions.
library(visreg)
visreg(glm3,"more_restrictions", by = "age",scale = 'response')
Looking at political affiliation by age in the 3rd model, senior citizens support background checks across all 3 political ideologies. Conservatives and liberals within the other age groups are slightly more likely to support background checks than libertarians.
library(visreg)
visreg(glm3,"political_affiliation", by = "age",scale = 'response')
Just to take a look at more_restrictions alone since it is statistically significant in model 3, I see that those who support more restrictions on who can buy a gun are more likely to support extensive background checks than those who do not support more restrictions, which supports my interpretation above.
visreg::visreg(glm3,"more_restrictions",scale = 'response')
Note that you are attempting to plot a 'main effect' in a model that contains an
interaction. This is potentially misleading; you may wish to consider using the 'by'
argument.
Conditions used in construction of plot
age: Young Adult
political_affiliation: Liberals
gender: Male