# here are the steps i use to get started and prepare or clean the data
# I saved it to my quant methods folder and set my working directory

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
library(pastecs)
## 
## Attaching package: 'pastecs'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
k12 <- read_csv("Violence Project - K-12 SCHOOLS DATABASE 2026 - Event.csv")
## Rows: 398 Columns: 58
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (46): Full Date, Day of Week, Time of Day, During Class?, School Name, ...
## dbl  (10): ID #, Month, Day, Year, Latitude, Longitude, Number of
## Victims Ki...
## num   (1): Enrollment
## time  (1): Time Homicide
## Occurred
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
higher_ed <- read_csv("Violence Project - HIGHER ED DATABASE 2026 - Event.csv")
## Rows: 280 Columns: 60
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (48): Full Date, Day of Week, Time of Day, During Class?, School Name, ...
## dbl  (10): ID #, Month, Day, Year, Latitude, Longitude, Number of
## Victims Ki...
## num   (1): Enrollment
## time  (1): Time Homicide
## Occurred
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
k12 <- k12 %>%
  mutate(School_Type_Group = "K-12")

higher_ed <- higher_ed %>%
  mutate(School_Type_Group = "Higher Ed")

all_shootings <- bind_rows(k12, higher_ed)

all_shootings <- all_shootings %>%
  clean_names()

View(all_shootings)
head(all_shootings)
## # A tibble: 6 × 62
##   id_number full_date  month   day  year day_of_week time_of_day
##       <dbl> <chr>      <dbl> <dbl> <dbl> <chr>       <chr>      
## 1         1 01/19/2000     1    19  2000 Wednesday   Afternoon  
## 2         2 02/29/2000     2    29  2000 Tuesday     Morning    
## 3         3 03/10/2000     3    10  2000 Friday      Night      
## 4         4 05/05/2000     5     5  2000 Friday      Unknown    
## 5         5 05/10/2000     5    10  2000 Wednesday   Afternoon  
## 6         6 05/26/2000     5    26  2000 Friday      Afternoon  
## # ℹ 55 more variables: time_homicide_occurred <time>, during_class <chr>,
## #   school_name <chr>, address <chr>, city <chr>, state <chr>, zip_code <chr>,
## #   density <chr>, latitude <dbl>, longitude <dbl>,
## #   industry_type_of_business <chr>, school_level <chr>,
## #   number_of_employees_at_site <chr>, enrollment <dbl>,
## #   accessibility_to_public <chr>, gun_free_zone <chr>, security_systems <chr>,
## #   lockdown <chr>, how_perpetrator_accessed <chr>, …
summary(all_shootings$number_of_victims_killed)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   1.000   1.000   1.326   1.000  32.000
stat.desc(all_shootings$number_of_victims_killed)
##      nbr.val     nbr.null       nbr.na          min          max        range 
## 678.00000000   0.00000000   0.00000000   1.00000000  32.00000000  31.00000000 
##          sum       median         mean      SE.mean CI.mean.0.95          var 
## 899.00000000   1.00000000   1.32595870   0.07521833   0.14768926   3.83598689 
##      std.dev     coef.var 
##   1.95856756   1.47709545
# I want to view the accessibility category first to see how many events occurred in each level of restriction 

table(all_shootings$accessibility_to_public)
## 
##                     Publicly Accessible Restricted Space and Didn't Have Access 
##                                     489                                      28 
##         Restricted Space but Had Access                                 Unknown 
##                                     157                                       4
# next, I will see the average number of people killed in each different level of accessibility

aggregate(number_of_victims_killed ~ accessibility_to_public, data=all_shootings, FUN=mean)
##                   accessibility_to_public number_of_victims_killed
## 1                     Publicly Accessible                 1.167689
## 2 Restricted Space and Didn't Have Access                 3.357143
## 3         Restricted Space but Had Access                 1.464968
## 4                                 Unknown                 1.000000

Only 28 incident occurred in restricted locations where the perpetrator lacked autorized access, compared to 489 which took place in locations with public access. A few particularly deadly incident could be greatly impacting or skewing these averages.

# I will examine the distribution of fatalities

summary(all_shootings$number_of_victims_killed)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   1.000   1.000   1.326   1.000  32.000
hist(all_shootings$number_of_victims_killed)

# T-Test
# Is campus accessibility is related to how many people die during school homicides?
# I want to compare average fatalities between publicly accessible locations and restricted locations where the perpetrator did not have access.

access_comparison<-subset(all_shootings,accessibility_to_public %in% c("Publicly Accessible","Restricted Space and Didn't Have Access"))

t.test(number_of_victims_killed~accessibility_to_public,data=access_comparison)
## 
##  Welch Two Sample t-test
## 
## data:  number_of_victims_killed by accessibility_to_public
## t = -1.9615, df = 27.084, p-value = 0.06017
## alternative hypothesis: true difference in means between group Publicly Accessible and group Restricted Space and Didn't Have Access is not equal to 0
## 95 percent confidence interval:
##  -4.4793565  0.1004491
## sample estimates:
##                     mean in group Publicly Accessible 
##                                              1.167689 
## mean in group Restricted Space and Didn't Have Access 
##                                              3.357143

Results: The t-test returned a p-value of 0.06017, which is greater than 0.05. This means the difference in average fatalities between the two groups is not statistically significant at the 0.05 level. The T-test has limitations for my research because fatalities are not normally distributed and are counted in whole numbers. The incidents are not randomly sampled, and their independence has not been established.

# Shapiro Wilk test: I want to see if the number of fatalities is normally distributed in each accessibility group.

shapiro.test(access_comparison$number_of_victims_killed[access_comparison$accessibility_to_public=="Publicly Accessible"])
## 
##  Shapiro-Wilk normality test
## 
## data:  access_comparison$number_of_victims_killed[access_comparison$accessibility_to_public == "Publicly Accessible"]
## W = 0.15665, p-value < 2.2e-16
shapiro.test(access_comparison$number_of_victims_killed[access_comparison$accessibility_to_public=="Restricted Space and Didn't Have Access"])
## 
##  Shapiro-Wilk normality test
## 
## data:  access_comparison$number_of_victims_killed[access_comparison$accessibility_to_public == "Restricted Space and Didn't Have Access"]
## W = 0.45076, p-value = 3.682e-09

Results: Both tests have p-values below 0.05 which means fatalities are not normally distributed in either group. This violates the normality assumption of the t-test.

Note to self: In order to trust the results of a t-test, the assumption for the test to work is that you are giving the test data that has a bell shaped curve. So you run the T-test, and get the results. Then you run a Shapiro Wilk test to see if the data you fed it was bell curved. If the SW test says “the assumption of normality wasn’t met- meaning it wasn’t bell shaped like the test expects/assumes it to be, the the results of the T-test are unreliable.

# K S test
# This test is used to see if the data follows a bell shaped curve and should be used on data with more than 5000 observations. My data has less than 1000 so there are limitations

ks.test(all_shootings$number_of_victims_killed,"pnorm")
## Warning in ks.test.default(all_shootings$number_of_victims_killed, "pnorm"):
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  all_shootings$number_of_victims_killed
## D = 0.84134, p-value < 2.2e-16
## alternative hypothesis: two-sided

Results: I got a warning sign because much of my data involves one death, and this test is better for continuous numbers. The p-value is below 0.05, meaning fatalities do not follow the bell-shaped pattern tested.

Both Shapiro-Wilk and K-S suggest that the fatalities are not normally distributed

# Residual Plot
# I want to visually examine the residuals in my regression model to see whether there are any patterns or unequal variance.
model1<-lm(number_of_victims_killed~accessibility_to_public,data=all_shootings)
plot(model1,which=1)

Results: Most residuals are close to zero, but several are much larger. The residuals have unequal variance across accessibility groups which suggests the model violates the assumption of homoscedasticity.

# Breusch-Pagan Test

# I will create a model to examine the relationship between campus accessibility and fatalities.

model1<-lm(number_of_victims_killed~accessibility_to_public,data=all_shootings)

# Then I will test whether the model's residuals have equal variance across accessibility groups.

bptest(model1)
## 
##  studentized Breusch-Pagan test
## 
## data:  model1
## BP = 15.825, df = 3, p-value = 0.001231

Results: The p-value of 0.001231 is less than 0.05, meaning the model is not homoscedastic, but instead heteroscedastic. This means the model’s predictions for the lethality of an event are sometimes wrong, and sometimes they are very wrong.

# Durbin-Watson Test
# Here, I am checking to see if the residuals in my regression model are correlated with one another.

durbinWatsonTest(model1)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.03522776      2.070163   0.114
##  Alternative hypothesis: rho != 0

Results: The p-value of 0.126 is greater than 0.05, meaning there is not significant evidence of autocorrelation among the residuals. The model does not appear to violate the assumption of no autocorrelation. Essentially, there isn’t a pattern between the model’s inaccurate prediction of fatalities during one event compared to the next.

# Log Transformation
# I want to see if transforming the number of fatalities using a log transformation will help address the assumption violations in my model.

model2<-lm(log(number_of_victims_killed)~accessibility_to_public,data=all_shootings)

bptest(model2)
## 
##  studentized Breusch-Pagan test
## 
## data:  model2
## BP = 29.983, df = 3, p-value = 1.392e-06

Results: The p-value is less than 0.05, meaning the model is still heteroscedastic. The log transformation did not resolve the violation of the homoscedasticity assumption.

# VIF Test
# Are two or more independent variables in a regression model are so closely related that it's difficult to separate their effects?

# I will run this test after I examine my next independent variable. 
# Rainbow Test
# I did not conduct a Rainbow test because my independent variable, campus accessibility, is categorical rather than continuous. Therefore, testing for a linear relationship is not applicable here.
aggregate(number_of_victims_killed ~ accessibility_to_public, data=all_shootings, FUN=mean)
##                   accessibility_to_public number_of_victims_killed
## 1                     Publicly Accessible                 1.167689
## 2 Restricted Space and Didn't Have Access                 3.357143
## 3         Restricted Space but Had Access                 1.464968
## 4                                 Unknown                 1.000000
#Poisson Regression
# I want to see whether campus accessibility is related to the number of fatalities using Poisson regression because fatalities are count data/ whole numbers so this may fit better. 

model5<-glm(number_of_victims_killed~accessibility_to_public,data=all_shootings,family=poisson)

summary(model5)
## 
## Call:
## glm(formula = number_of_victims_killed ~ accessibility_to_public, 
##     family = poisson, data = all_shootings)
## 
## Coefficients:
##                                                                Estimate
## (Intercept)                                                     0.15503
## accessibility_to_publicRestricted Space and Didn't Have Access  1.05606
## accessibility_to_publicRestricted Space but Had Access          0.22681
## accessibility_to_publicUnknown                                 -0.15503
##                                                                Std. Error
## (Intercept)                                                       0.04185
## accessibility_to_publicRestricted Space and Didn't Have Access    0.11131
## accessibility_to_publicRestricted Space but Had Access            0.07810
## accessibility_to_publicUnknown                                    0.50175
##                                                                z value Pr(>|z|)
## (Intercept)                                                      3.704 0.000212
## accessibility_to_publicRestricted Space and Didn't Have Access   9.488  < 2e-16
## accessibility_to_publicRestricted Space but Had Access           2.904 0.003682
## accessibility_to_publicUnknown                                  -0.309 0.757342
##                                                                   
## (Intercept)                                                    ***
## accessibility_to_publicRestricted Space and Didn't Have Access ***
## accessibility_to_publicRestricted Space but Had Access         ** 
## accessibility_to_publicUnknown                                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 572.81  on 677  degrees of freedom
## Residual deviance: 499.72  on 674  degrees of freedom
## AIC: 1935.3
## 
## Number of Fisher Scoring iterations: 6

Results: The average number of fatalities per incident was 1.17 at publicly accessible locations, compared to 3.36 in restricted spaces where the perpetrator did not have authorized access. The average number of fatalities where the perpetrator had authorized access averaged 1.46.

Takeaway: incidents in restricted spaces where the perpetrator did not have authorized access had the highest average number of fatalities.

# This doesn't mean that the deadliest shootings happened in restricted/no authorized access space. I want to see where the deadliest shootings happened. The literature did not give me a number, so I used 5

table(all_shootings$accessibility_to_public[all_shootings$number_of_victims_killed>=5])
## 
##                     Publicly Accessible Restricted Space and Didn't Have Access 
##                                       3                                       5 
##         Restricted Space but Had Access 
##                                       3
# Bar Graph Restricted Spaces Incidents
# I want to see the number of fatalities in each incident that occurred in a restricted space without authorized access.

restricted<-subset(all_shootings,accessibility_to_public=="Restricted Space and Didn't Have Access")

barplot(restricted$number_of_victims_killed,main="Fatalities in Restricted Spaces Without Authorized Access",xlab="Incident",ylab="Number of Fatalities")

# I want to compare the number of fatalities across all campus accessibility groups, including incidents with unusually high fatalities.

par(mar=c(5,12,4,2))

boxplot(number_of_victims_killed~accessibility_to_public,data=all_shootings,names=c("Public","Restricted: No Access","Restricted: Had Access","Unknown"),horizontal=TRUE,las=1,main="Fatalities by Campus Accessibility",xlab="Number of Fatalities")

Results: each catageory has extreme events, but I still know that areas that were restricted had the highest average. Let’s see about the other areas.

# I want to see how many incidents in each accessibility category involved five or more fatalities.I want to calculate the percentage of incidents with five or more fatalities within each accessibility category. 

table(all_shootings$accessibility_to_public,all_shootings$number_of_victims_killed>=5)
##                                          
##                                           FALSE TRUE
##   Publicly Accessible                       486    3
##   Restricted Space and Didn't Have Access    23    5
##   Restricted Space but Had Access           154    3
##   Unknown                                     4    0
prop.table(table(all_shootings$accessibility_to_public,all_shootings$number_of_victims_killed>=5),1)*100
##                                          
##                                                 FALSE        TRUE
##   Publicly Accessible                      99.3865031   0.6134969
##   Restricted Space and Didn't Have Access  82.1428571  17.8571429
##   Restricted Space but Had Access          98.0891720   1.9108280
##   Unknown                                 100.0000000   0.0000000

Results: Restricted spaces where perpetrators did not have authorized access had the highest average number of fatalities per incident at 3.36 and the highest percentage of incidents involving five or more fatalities at 17.86%. This compares to 1.91% in restricted spaces where perpetrators had authorized access and 0.61% in publicly accessible locations.

Research Question 2 Is the presence of armed personnel on scene associated with the number of fatalities in school homicide incidents? Hypothesis Alternate Hypothesis

# I want to see how armed personnel is categorized in my dataset.

table(all_shootings$armed_person_on_scene, useNA="ifany")
## 
## No evidence of            Yes 
##            628             50
# let's look at the overall average in each category. 

aggregate(number_of_victims_killed~armed_person_on_scene,data=all_shootings,FUN=mean)
##   armed_person_on_scene number_of_victims_killed
## 1        No evidence of                 1.265924
## 2                   Yes                 2.080000

Results: incidents with documented armed personnel averaged 2.08 fatalities, compared to 1.27 fatalities when there was no evidence of armed personnel.

# Here is a boxplot showing these averages 
boxplot(number_of_victims_killed~armed_person_on_scene,data=all_shootings,horizontal=TRUE,main="Fatalities by Armed Personnel on Scene",xlab="Number of Fatalities",ylab="Armed Personnel on Scene")

Results: Visually, it looks like both groups had a median of one fatality, and both also included incidents with substantially higher numbers of deaths. The highest number of fatalities was 21 under incidents with armed personnel on scene and 32 under incidents with no evidence of armed personnel.

# T-Test
# I want to see if the average number of fatalities is significantly different between incidents with and without documented armed personnel.

t.test(number_of_victims_killed~armed_person_on_scene,data=all_shootings)
## 
##  Welch Two Sample t-test
## 
## data:  number_of_victims_killed by armed_person_on_scene
## t = -1.5025, df = 50.613, p-value = 0.1392
## alternative hypothesis: true difference in means between group No evidence of and group Yes is not equal to 0
## 95 percent confidence interval:
##  -1.9020276  0.2738748
## sample estimates:
## mean in group No evidence of            mean in group Yes 
##                     1.265924                     2.080000

Result: The p-value of 0.1392 is greater than 0.05, meaning there is no statistically significant difference in average fatalities between incidents with documented armed personnel on scene and incidents without documented evidence of armed personnel.

# Shapiro-Wilk Test
# I am checking to see if fatalities are normally distributed in each armed personnel category.

shapiro.test(all_shootings$number_of_victims_killed[all_shootings$armed_person_on_scene=="No evidence of"])
## 
##  Shapiro-Wilk normality test
## 
## data:  all_shootings$number_of_victims_killed[all_shootings$armed_person_on_scene == "No evidence of"]
## W = 0.12803, p-value < 2.2e-16
shapiro.test(all_shootings$number_of_victims_killed[all_shootings$armed_person_on_scene=="Yes"])
## 
##  Shapiro-Wilk normality test
## 
## data:  all_shootings$number_of_victims_killed[all_shootings$armed_person_on_scene == "Yes"]
## W = 0.3218, p-value = 7.672e-14

Results: Both groups have p-values below 0.05 which means that the fatalities are not normally distributed in either group. This violates the normality assumption of the t-test.

Note to self: In order to trust the results of a t-test, the assumption for the test to work is that you are giving the test data that has a bell shaped curve. So you run the T-test, and get the results. Then you run a Shapiro Wilk test to see if the data you fed it was bell curved. If the SW test says “the assumption of normality wasn’t met- meaning it wasn’t bell shaped like the test expects/assumes it to be, the the results of the T-test are unreliable.

#KS Test
# The KS test tests for normal distribution, but it's different than the SW test because the K-S test can compare data with different kind if distributions, not just a bell curve.
# A limitationL My fatalities are whole numbers, and the K-S test assumes continuous data. Also, my dataset has 678 incidents, fewer than the 5,000 observations your professor recommends.

ks.test(all_shootings$number_of_victims_killed,"pnorm")
## Warning in ks.test.default(all_shootings$number_of_victims_killed, "pnorm"):
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  all_shootings$number_of_victims_killed
## D = 0.84134, p-value < 2.2e-16
## alternative hypothesis: two-sided

Results: My data have hundreds of repeated values with creates excessive ties and caused this error message. The p-value is less than 0.05, meaning the distribution of fatalities is significantly different from the standard normal distribution.

# Breusch-Pagan Test
# I want to see if the residuals in my regression model have an equal variance between the two armed personnel groups.

armed_model<-lm(number_of_victims_killed~armed_person_on_scene,data=all_shootings)

bptest(armed_model)
## 
##  studentized Breusch-Pagan test
## 
## data:  armed_model
## BP = 2.7192, df = 1, p-value = 0.09915

Results: The p-value of 0.09915 is greater than 0.05, meaning there is no significant evidence of heteroscedasticity. The model does not appear to violate the assumption of homoscedasticity. No evidence of armed personnel has an average of 1.27 fatalities. Armed personnel on scene has an average of 2.08 fatalities. This p-value shows we didn’t find strong enough evidence that the spread was different.

Note to self: The regression model created in this Breusch-Pagan test is using the averages to estimate fatalities. But some incidents have more deaths than the model estimated, and others have fewer. Those differences are called residuals.The Breusch-Pagan test is asking whether or not those residuals spread out about the same amount in both groups? The p-value was 0.09915, which means we didn’t find strong enough evidence that the spread was different.

# Durbin-Watson Test
# I want to see whether the residuals in my regression model are correlated with one another.

durbinWatsonTest(armed_model)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.02764715       2.05463    0.15
##  Alternative hypothesis: rho != 0

Results: Is there a a pattern between the model’s inaccurate prediction of fatalities during one event compared to the next? The p-value of 0.144 is greater than 0.05, meaning there is no significant evidence of autocorrelation between consecutive residuals. The D-W statistic of 2.05463 is also close to 2, which supports this finding.

Essentially, this means we did not find a significant pattern between the model’s inaccurate estimates of fatalities from one incident to the next when looking at events in order.

# Combined Regression Model
# I want to examine whether campus accessibility and armed personnel on scene are associated with the number of fatalities when both variables are included in the same model.

combined_model<-lm(number_of_victims_killed~accessibility_to_public+armed_person_on_scene,data=all_shootings)

summary(combined_model)
## 
## Call:
## lm(formula = number_of_victims_killed ~ accessibility_to_public + 
##     armed_person_on_scene, data = all_shootings)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.0313 -0.3976 -0.1167 -0.1167 30.6024 
## 
## Coefficients:
##                                                                Estimate
## (Intercept)                                                     1.11674
## accessibility_to_publicRestricted Space and Didn't Have Access  2.15951
## accessibility_to_publicRestricted Space but Had Access          0.28090
## accessibility_to_publicUnknown                                 -0.11674
## armed_person_on_sceneYes                                        0.75504
##                                                                Std. Error
## (Intercept)                                                       0.08815
## accessibility_to_publicRestricted Space and Didn't Have Access    0.37012
## accessibility_to_publicRestricted Space but Had Access            0.17475
## accessibility_to_publicUnknown                                    0.95600
## armed_person_on_sceneYes                                          0.28009
##                                                                t value Pr(>|t|)
## (Intercept)                                                     12.669  < 2e-16
## accessibility_to_publicRestricted Space and Didn't Have Access   5.835 8.38e-09
## accessibility_to_publicRestricted Space but Had Access           1.607   0.1084
## accessibility_to_publicUnknown                                  -0.122   0.9028
## armed_person_on_sceneYes                                         2.696   0.0072
##                                                                   
## (Intercept)                                                    ***
## accessibility_to_publicRestricted Space and Didn't Have Access ***
## accessibility_to_publicRestricted Space but Had Access            
## accessibility_to_publicUnknown                                    
## armed_person_on_sceneYes                                       ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.904 on 673 degrees of freedom
## Multiple R-squared:  0.06067,    Adjusted R-squared:  0.05509 
## F-statistic: 10.87 on 4 and 673 DF,  p-value: 1.526e-08

Results: The multiple regression model examined independent variables campus accessibility and armed personnel on scene together to see how they relate to the number of fatalities. Restricted spaces where perpetrators did not have authorized access had approximately 2.16 more fatalities per incident than publicly accessible spaces (p < 0.001), even when accounting for armed personnel. Incidents with armed personnel on scene also had approximately 0.76 more fatalities than incidents without documented evidence of armed personnel (p = 0.0072), even when accounting for campus accessibility. Both relationships were statistically significant according to the model. However, the R-squared value of 0.06067 shows that these two variables explain only about 6.1% of the differences in fatalities between incidents. This last result suggests that other factors may also play an important role.

#VIF Test

# I want to see whether campus accessibility and armed personnel on scene are too closely related to one another.

vif(combined_model)
##                            GVIF Df GVIF^(1/(2*Df))
## accessibility_to_public 1.00237  3        1.000395
## armed_person_on_scene   1.00237  1        1.001184

Results: Both independent variables have GVIF values of approximately 1, which means that multicollinearity is not a problem here.

# Residual Plot
# Now I have to check the assumptions:

plot(combined_model,which=1)

Results: Most incidents have residuals close to zero but particularly deadly incidents have very large positive residuals. The model is struggling to account for extreme incidents. These incidents are especially important to my research because I am interested in understanding the factors associated with the deadliest school shootings.

# Combined Poisson Regression
# I want to examine campus accessibility and armed personnel together using Poisson regression because fatalities are count data.

combined_poisson<-glm(number_of_victims_killed~accessibility_to_public+armed_person_on_scene,data=all_shootings,family=poisson)

summary(combined_poisson)
## 
## Call:
## glm(formula = number_of_victims_killed ~ accessibility_to_public + 
##     armed_person_on_scene, family = poisson, data = all_shootings)
## 
## Coefficients:
##                                                                Estimate
## (Intercept)                                                     0.11706
## accessibility_to_publicRestricted Space and Didn't Have Access  1.03441
## accessibility_to_publicRestricted Space but Had Access          0.21491
## accessibility_to_publicUnknown                                 -0.11706
## armed_person_on_sceneYes                                        0.45324
##                                                                Std. Error
## (Intercept)                                                       0.04319
## accessibility_to_publicRestricted Space and Didn't Have Access    0.11146
## accessibility_to_publicRestricted Space but Had Access            0.07816
## accessibility_to_publicUnknown                                    0.50186
## armed_person_on_sceneYes                                          0.10450
##                                                                z value Pr(>|z|)
## (Intercept)                                                      2.710  0.00672
## accessibility_to_publicRestricted Space and Didn't Have Access   9.281  < 2e-16
## accessibility_to_publicRestricted Space but Had Access           2.749  0.00597
## accessibility_to_publicUnknown                                  -0.233  0.81557
## armed_person_on_sceneYes                                         4.337 1.44e-05
##                                                                   
## (Intercept)                                                    ** 
## accessibility_to_publicRestricted Space and Didn't Have Access ***
## accessibility_to_publicRestricted Space but Had Access         ** 
## accessibility_to_publicUnknown                                    
## armed_person_on_sceneYes                                       ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 572.81  on 677  degrees of freedom
## Residual deviance: 482.95  on 673  degrees of freedom
## AIC: 1920.5
## 
## Number of Fisher Scoring iterations: 5

Results: I used Poisson regression to examine campus accessibility and armed personnel on scene together because fatalities are count data. Restricted spaces where perpetrators did not have authorized access had a positive coefficient of 1.03441. Restricted spaces where perpetrators had authorized access also had a positive coefficient of 0.21491. Armed personnel on scene had a positive coefficient of 0.45324. All three relationships were statistically significant according to the Poisson model, suggesting that these factors are associated with higher fatality counts.

# Overdispersion Check
# I have to test the assumptions of this model to see if I can rely on the results of the abov test. Did the number of fatalities vary more than my Poisson regression model expects.

sum(residuals(combined_poisson,type="pearson")^2)/df.residual(combined_poisson)
## [1] 1.947885

Results: I checked for overdispersion in my combined Poisson regression model and got a value of 1.947885. Since this is greater than 1, it suggests that the fatalities vary more than the model expects. This means the model may underestimate the variation in the data, and its p-values may be less reliable.

Log and Square root transformations: Important note: On a practice sheet, I attempted both log and square-root transformations to address violations of the regression assumptions. However, neither transformation resolved the problems with normality and unequal variance since most of my data is one fatality per event. The reason I used Poisson regression is because my dependent variable, the number of fatalities, is count data. Although the Poisson model showed evidence of overdispersion, I kept the original fatality counts because their size is part of my focus and because the number which most skews the data is one, not the outliers.