2025-02-09
library(ggplot2)
library(readxl)
library(openxlsx)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
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
library(corrplot)
## corrplot 0.95 loaded
data <- read_excel("Houston.Crime.Stats.NIBRS2024.xlsx")
Introduction to Linear Regression and Crime Analysis
This presentation will provide a very brief overview of linear regression methods used to predict future values based on crime data.
What is Linear Regression?
Linear regression is a statistical method that models the relationship between two or more variables by fitting a linear equation to observed data.
For example, we can use linear regression to predict the future “Offense Count” based on different variables in the context of crime statistics.
Assumptions for Linear Regression
Before applying linear regression, it is important to check the following assumptions:
- Linearity: The relationship between the dependent and independent variables should be linear
- Independence of Errors: The errors must be independent of each other
- Homoscedasticity: The variance of the residuals should be constant across the independent variable.
- Normality of Errors: The residuals should have a normal distribution with few outliers.
Linear Regression Function
The formula for the line of best fit in linear regression is:
\[ y = \beta_0 + \beta_1 x \]
where: - \(\beta_0\) (intercept) is the value of \(y\) when \(x = 0\), - \(\beta_1\) (slope) represents the rate of change in \(y\) as \(x\) increases.
The least squares method is usually used to estimate the coefficients \(\beta_0\) and \(\beta_1\) in a way that minimizes the sum of squared errors between the predicted and observed values.
Correlation Coefficient
The correlation coefficient is a numerical measure of the strength and direction of the linear relationship between two variables. It is calculated using the formula:
\[ r = \frac{n \sum xy - (\sum x)(\sum y)}{\sqrt{[n \sum x^2 - (\sum x)^2][n \sum y^2 - (\sum y)^2]}} \]
- ** \(r\) ** is the correlation coefficient
- ** \(n\) ** is the number of data points
- ** \(x\) ** and ** \(y\) ** are the two variables.
Correlation Matrix
A correlation matrix is a great tool in data analysis, especially in the context of regression modeling. It provides a quick way to visually assess the relationship between multiple variables by calculating the correlation coefficients, which range from -1 to 1.
- A correlation of 1 indicates a perfect positive relationship between the variables.
- A correlation of -1 indicates a perfect negative relationship.
- A correlation of 0 means there is no linear relationship between the variables.
Houston Crime Correlation Matrix
Our data suggests that there are no pairs of variables that are highly linearly related in the data as currently organized.

Can we always use linear regression?
As we can see from our example, the linear regression method assumes a straight-line relationship between the variables. However, the actual pattern of the data might be more complxe and a non-linear model might be better suited. 
Can we always use linear regression?
The histogram would have also showed us that a simple linear regression may not be suitable for this measurement because it shows how the total offense count changes over the course of the day. 
Linear Regression
The previous example was not the best scenario so let’s try using a running total of offenses by month to see how the number of offenses accumulates over time.

Linear Regression With Running Offense Total by Date
Using the running total of offenses by date makes it possible to analyze the data in a linear way.
Residual standard error: 1555 on 10 degrees of freedom Multiple R-squared: 0.9996, Adjusted R-squared: 0.9996 F-statistic: 2.836e+04 on 1 and 10 DF, p-value: < 2.2e-16
##
## Call:
## lm(formula = CumulativeOffenses ~ MonthIndex, data = data_summary)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2917.3 -1113.7 510.7 1271.9 1623.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1036.0 956.8 -1.083 0.304
## MonthIndex 21895.8 130.0 168.418 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1555 on 10 degrees of freedom
## Multiple R-squared: 0.9996, Adjusted R-squared: 0.9996
## F-statistic: 2.836e+04 on 1 and 10 DF, p-value: < 2.2e-16
Plot for Linear Regression With Running Offense Total by Date
Citations and Sources
The data used in this analysis is sourced from the Houston Police Department’s Monthly Crime Data. The dataset includes crime statistics by street and police beat, and provides insights into the frequency and types of offenses across different times and locations.
Data Source Link: You can access the data directly from the Houston Police Department’s official website: -https://www.houstontx.gov/police/cs/Monthly_Crime_Data_by_Street_and_Police_Beat.html
Other Sources:
Code for Linear Regression Slide
data\(RMSOccurrenceDate <- as.Date(data\)RMSOccurrenceDate)
data_summary <- data %>% mutate(Month = format(RMSOccurrenceDate, “%Y-%m”)) %>%
group_by(Month) %>% arrange(Month) %>% summarise(TotalOffenses = sum(OffenseCount, na.rm = TRUE)) %>% mutate(CumulativeOffenses = cumsum(TotalOffenses)) %>%
filter(!is.na(Month))
ggplot(data_summary, aes(x = Month, y = CumulativeOffenses)) + geom_line(color = “blue”) + geom_point(color = “red”) +
labs(title = “Running Total of Crime Offenses by Month”, x = “Month”, y = “Cumulative Total Offenses”) + theme(axis.text.x = element_text(angle = 90, hjust = 1))