Intro

The source for our data is kaggle.com and the data set was created on October 12, 2023. This data is not a sample as it is all the crime data from the year 2020 to the present (2023), and because of this, it does not have any sampling bias. The data from this set was collected from data.gov and was created in November 2020 and has been regularly updated since, and is maintained by LAPD OpenData. This was an observational study looking at all the crimes that occurred from 2020-2023 in Los Angeles. This data is of interest to us because we think that analyzing crime data could help people to better understand when/where/why crimes occur and what can be predicted based on the data. As for cleaning the data, we first cut the data set in half because it was too large, and making graphs, manipulating the data, etc was taking longer than we wanted. Next, we used dplyr::select() to keep the columns that we thought were relevant to the graphs we were going to make and the machine learning we planned to implement. After that, we used rename() to rename the columns to something more readable and intuitive and used mutate() to remove unnecessary white space from our ‘location’ column. Lastly, we used filter to keep the rows where the age was in the range we wanted (0 to 90), the latitude and longitude weren’t equal to zero, and the rows did not contain NA or no value.

Cleaning the data

Graphs

About

The following graph shows the number of crimes committed against people of varying ages. The ages range from 0-90 and the number of crimes committed ranges from 0-300,000. We created this graph to get a general understanding of who the most crimes were being committed against. We expected the bulk of the crimes to be committed against middle-aged people and we were correct in our prediction.

Interpretation

It can be seen that the age range with the highest number of crimes committed against it is the age range of 25-43, and after 43 is steadily declines. Up until the age of adulthood (18), you are also less likely to have a crime committed against you which makes sense, since most crimes do not happen to children. The graph also resembles a Chi^2 distribution curve, Which suggests that the data isn’t uniform and follows a specific pattern. This makes sense because 18-30-year-olds are more likely to participate in criminal activity.

About

This graph shows the number of crimes committed during each hour of the day. The number of crimes ranges from 0-7000. Like the previous graph, our initial predictions were correct. We thought that there would be a spike in crimes as it got closer to midnight and the evening hours. We made this graph to see if there were any trends or patterns in the times that crimes occurred.

Interpretation

As seen in the graph there is a spike in crime rates after 5pm, this makes sense because as it gets darker out it is harder for the criminal to be seen committing the crime so they are more likely to try to commit it when its dark. There are some times in the graph that show little to no crime but I don’t think that means that no crimes are committed. I would assume that the crimes committed during those times aren’t well documented and during our data cleaning, they were removed because of that. This could be due to fewer police on duty during that time and more criminals getting away with their crimes. It seems like the safest times are from the hours of 2am to 8am, with a spike of crime from 9am to 3pm, and then it is at its highest from 7pm to 12am.

About

The graph below shows the crime counts for victims of each descent in the dataset. The y-axis is the victim’s descent, while the x-axis is the crime count. We made this graph because we wanted to see the distribution of crimes across victims of all backgrounds.

Interpretation

In this graph, we observe the number of crimes committed against people of each of the descents listed. People of Hispanic descent have a significantly higher chance of having a crime committed against them, having 50000 more crimes than people of White descent which is in second place. It is also worth noting that Hispanic people make up around 40-50% of Los Angeles’ population. It is still surprising however due to the fact that people of White descent still have a significantly lower chance of having a crime committed against them. After the “Other” category, we observe a huge drop-off, most likely due to there not being a large population of people of those descents residing in Los Angeles County.

About

The graph below will show the crime counts for victims of each sex. We decided to not use “X” as the number of crimes committed against people of an unknown sex because the number was too small and could not be seen on the graph. The y-axis will be the crime count and the x-axis will be the number of months since 2020/the beginning of the dataset broken up by 6-month increments to see how the crime counts change as the years go on. We made this graph to see if there was a large gap between males and females in crime counts. If there is, further research could be done to determine the cause.

Interpretation

In this graph, we observe the number of crimes committed on male and female persons since 2020. The part that strikes me about this graph is during the time of the pandemic both male and female crimes drop significantly. We can also observe that around a year and a half later (15 months on this graph) the crimes started to pick up again which is when mandates started to become more lenient and people were leaving their houses more and more.

Although it makes sense in this graph that males are on the receiving end of most crimes, it strikes me that females around 2 years after 2020 start to experience more crimes than males, even spiking at around 34 months. I don’t think there is any particular causation of this, but it is interesting nonetheless. Finally, there is a significant drop off at 45 months, but that is most likely because crimes were not reported as much because of the new year.

About

The below graph will be a dumbbell plot for the average age and type of crime for each sex of the victims. The y axis will be the type of crime, which needed cleaning of the dataset to group types of crimes into a more general and well known term. This was because there was over 100 unique crimes in the dataset and some of them were as small as “Unauthorized Computer Access”. The x axis will be the average age of the victim. The points will represent the average age and the sex of the victim’s of the corresponding crime. The segment will represent the difference of the sex’s.

Interpretation

The graph above is a dumbbell plot comparing the average age for both sexes of victims for each type of crime. The line represents the difference between the two. Most of the average ages seem to be very similar for males and females, with sexual assault and assault crimes having the biggest difference between the two. It is not surprising to see a lower age for females in those two categories. Rather it is surprising to see such a high average age for males in sexual assault. The reason for this is unknown and requires further research to determine the cause. It is also surprising to see such a high average age in kidnapping. Typically one would assume most victims who get kidnapped are on the younger side.

The other crimes make a lot more sense. Abuse and neglect are mostly crimes against children, so victims of both sexes are extremely likely to be a lot younger. Other crimes like trespassing, arson, vandalism, and robbery most likely occur to people who own businesses and have a lot of personal belongings. Those people are typically on the older side. In conclusion, this graph shows the relationship between the average ages of both sexes quite well. It reinforces things we predicted and some parts surprised us.

Machine Learning

Knn

We wanted to try to predict the sex of a victim based on two variables, we tried different combinations of variables but the variable with the most accurate results were the type of crime and the age of the victim. While they were our best predictors, the overall accuracy of the Knn was still low. We also tried both normalized and standardized data in our Knn.

# Finding the max 
norm_max <- max(knn_results$norm_acc)
stan_max <- max(knn_results$stan_acc)

norm_max_x <- mean(knn_results$k[knn_results$norm_acc == norm_max])
stan_max_x <- knn_results$k[knn_results$stan_acc == stan_max]

c("Maximum norm accuracy" = norm_max,
  "K value associated with the maximum of norm" = norm_max_x,
  "Maximum stan accuracy" = stan_max,
  "K value associated with the maximum of stan" = stan_max_x)
##                       Maximum norm accuracy 
##                                       0.538 
## K value associated with the maximum of norm 
##                                     146.000 
##                       Maximum stan accuracy 
##                                       0.536 
## K value associated with the maximum of stan 
##                                     152.000
RNGversion("4.1.0"); set.seed(2870)

# Making a new data frame with only the variables needed
crime_data |>
  
  dplyr::select(time_occurred, victim_age, victim_sex) |>
  
  mutate(time_occurred = as.numeric(time_occurred)) |>
  
  filter(victim_sex != "X") |>
  
  slice_sample(n = 500)->
  ml_crime


# Using the same data for the testing data frame
crime_for_knn <- 
  ml_crime


#  Performing knn
knn_crime <- 
  knn(
    train = crime_data_stan |> select(where(is.numeric)),
    test = crime_data_stan |> select(where(is.numeric)),
    cl = crime_for_knn$victim_sex,
    k = 142
  )


# Showing results
table(actual = crime_for_knn$victim_sex,
      predicted = knn_crime) |>
  
  confusionMatrix()
## Confusion Matrix and Statistics
## 
##       predicted
## actual   F   M
##      F 121 124
##      M  96 159
##                                          
##                Accuracy : 0.56           
##                  95% CI : (0.5152, 0.604)
##     No Information Rate : 0.566          
##     P-Value [Acc > NIR] : 0.62459        
##                                          
##                   Kappa : 0.1177         
##                                          
##  Mcnemar's Test P-Value : 0.06871        
##                                          
##             Sensitivity : 0.5576         
##             Specificity : 0.5618         
##          Pos Pred Value : 0.4939         
##          Neg Pred Value : 0.6235         
##              Prevalence : 0.4340         
##          Detection Rate : 0.2420         
##    Detection Prevalence : 0.4900         
##       Balanced Accuracy : 0.5597         
##                                          
##        'Positive' Class : F              
## 

Classification Trees

We tried to use a classification tree as well to predict the victims sex, but this time it was based on the time that the crime occurred and the victims age. We expect the full tree to be very large because of the large range in ages that are in our data set. We also expect to see that the time of the crime is a good predictor, along with the victim’s age, for the victim’s sex.

Regression

About

We believe that the linear model below will show some type of relationship between the victim’s age and the other categorical factors. We chose to predict the victim’s age as we found some interesting details in the above dumbbell plot. We will use 4 different models, one with just the type of crime, another with the type of crime and sex of the victim, one with both of the previous plus the time the crime occurred, and finally one with all three plus the descent of the victim.

## [1] 9.392944
## [1] 0.7538779

Interpretation

The graph above clearly indicates there is not a direct relationship between the victim’s age and type of crime, their sex, and the time the crime occurred. Using knn regression would not improve our accuracy as there is no clear relationship between the data points.

Conclusion

During our analysis, we found that middle-aged citizens are most likely to have a crime committed against them, the ages with the highest amount of crimes committed against them were ages 25-43. We also found that the most dangerous times of the day in terms of crime are from the hours of 7pm to midnight with the highest number of crimes being committed during these hours, the safest times are 2am to 8am. According to the data, Hispanic people are most likely to have a crime committed against them, Being follow by White and Black people respectively. During Covid the crime rates overall dropped significantly. They went back up over the years and eventually the crime rates against females rose above those of males.

Limitations and Recommendation

The first limitation we found was that not all of the crimes well documented, with many missing key information about the committed crime. The crimes with little documentation were removed from our data set during cleaning. We also found that at certain times crimes seemed to not be documented well either, as seen in the crimes by time graph. Future researches could include crimes from areas that are not only in Los Angeles. Researches could also look deeper at each of the graphs that we made and find the underlying ‘why’ to the trends that are seen in them.