-
Notifications
You must be signed in to change notification settings - Fork 0
/
federalHillExploration.rmd
93 lines (74 loc) · 2.7 KB
/
federalHillExploration.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: "An Analysis of Crime in Federal Hill"
author: "Jason Hammett"
date: "February 15, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#Install function courtesy of CSCI 349
my.install <- function(pkg){
if (!(pkg %in% installed.packages()[,1])){
install.packages(pkg)
}
return (require(pkg, character.only=TRUE))
}
#Knitr
my.install("knitr")
#Decision Tree Libs
my.install("rpart")
my.install("rpart.plot")
#Plotting Libs
my.install("ggplot2")
my.install("ggmap")
my.install("leaflet")
#colorblind settings
# The palette with grey:
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# The palette with black:
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# To use for fills, add
#scale_fill_manual(values=cbPalette)
# To use for line and point colors, add
#scale_colour_manual(values=cbPalette)
```
## Federal Hill: A Neighborhood Level Analysis
To begin, I'll load the data cleaned in data_cleaning.rmd
```{r data import}
load("cleanedData.RData")
```
I'll grab the Federal Hill neighborhood from the data set
```{r federalHill}
federalHill <- victims[which(victims$Neighborhood == "Federal Hill"),]
```
I'm going to utilize ggmap to visualize these crimes.
```{r mapping federal hill}
#Grab a map centered on the federal hill neighborhood
federalHillMap <- get_map("federal hill, baltimore, maryland", zoom = 16)
#Plot all crimes
mapPoints <- ggmap(federalHillMap) + geom_point(aes(x = Longitude, y = Latitude), data = federalHill, alpha = .25)
#Titles
mapPoints <- mapPoints + ggtitle("Crime in Federal Hill") + ylab("Latitude") + xlab("Longitude")
#Display it
mapPoints
mapPoints2 <- ggmap(federalHillMap) + geom_point(aes(x = Longitude, y = Latitude, colour = Weapon), data = federalHill, alpha = .75) + scale_colour_manual(values=cbbPalette)
mapPoints2 <- mapPoints2 + ggtitle("Crime in Federal Hill by Weapon Used") + ylab("Latitude") + xlab("Longitude")
#Display
mapPoints2
```
Basic rpart decision tree on the Federal Hill neighborhood.
```{r training tree on federal hill}
#Form a 90-10 train test split
split <- sample(nrow(federalHill), nrow(federalHill) * 0.9)
#Divide the set into train and test
federalHillTrain <- federalHill[split,]
federalHillTest <- federalHill[-split,]
#Build the model
federalHillModel <- rpart(federalHillTrain, formula = CrimeCode ~ Description)
#Plot it
prp(federalHillModel)
#Make two more models
federalHillModel2 <- rpart(federalHillTrain, formula = Weapon ~ Description)
prp(federalHillModel2)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.