library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.1.6
## ✔ 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.1
## ── 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(readxl)
library(pastecs)
##
## Attaching package: 'pastecs'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## The following object is masked from 'package:tidyr':
##
## extract
safedash_offenses<-read_excel("safedash_offenses.xlsx")
## New names:
## • `` -> `...1`
names(safedash_offenses)
## [1] "...1" "Report_Date" "NIBRS_Code_Name"
## [4] "NIBRS_Crime_Against" "Service_Area" "NIBRS_Group"
## [7] "Zip_Code" "DateTime"
library(dplyr)
crime_by_zip <- safedash_offenses %>%
filter(!is.na(Zip_Code), !is.na(NIBRS_Group)) %>%
group_by(Zip_Code, NIBRS_Group) %>%
summarise(Number_of_Offenses = n(), .groups = "drop")
crime_model <- lm(Number_of_Offenses ~ NIBRS_Group + Zip_Code,
data = crime_by_zip)
The linear regression model had an R-squared value of 0.617, meaning that approximately 61.7% of the variation in the number of reported offenses across ZIP-code and offense-group combinations was explained by the model. The adjusted R-squared was 0.5874. The overall model was statistically significant, with an F-statistic of 20.82 and a p-value below 0.001. This suggests that ZIP code and offense group, considered together, are associated with differences in reported offense counts. The model does not establish a cause-and-effect relationship.
plot(crime_model, which = 1)
I used plot(crime_model, which = 1) to create a Residuals vs. Fitted
plot to check the linearity assumption. The graph shows a curved pattern
in the residuals rather than a random scatter around the zero line. The
spread of the residuals also becomes larger at higher fitted values, and
there are several potential outliers. These patterns suggest that the
linearity assumption may be violated and that the variance of the
residuals may not be constant. Therefore, the linear model may not be
the best fit for these offense counts. The results should be interpreted
with caution.