/
hw1p1-analytical-detective.Rmd
140 lines (112 loc) · 2.52 KB
/
hw1p1-analytical-detective.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
title: "An Analytical Detective"
author: "Terrel Shumway"
date: "04/28/2015"
output: html_document
---
This document presents answers for homework 1 part 1.
## Loading the Data
```{r}
baseurl = "https://courses.edx.org/c4x/MITx/15.071x_2/asset/"
getdata = function(local){
if(!file.exists(local)){
library(downloader)
remote = paste0(baseurl,local)
print(remote)
download(remote,local)
}
read.csv(local)
}
data = getdata("mvtWeek1.csv")
```
problem | answer
------------|--------
problem 1.1 | `r nrow(data)`
problem 1.2 | `r ncol(data)`
problem 1.3 | `r max(data[,"ID"])`
problem 1.4 | `r min(data[,"Beat"])`
problem 1.5 | `r sum(data[,"Arrest"])`
problem 1.6 | `r sum(data$Location=="ALLEY")`
## Understanding Dates in R
problem 2.1
```{r}
set.seed(42)
spl = sample.int(nrow(data),30)
data[spl,"Date"]
```
This is actually ambiguous because all of the dates are in the years 2001-2012, so the year *could* also be the first part, but `%y/%d/%m` doesn't happen in practice, so we will go with `%m/%d/%y`.
```{r}
datetimes = strptime(data$Date,"%m/%d/%y %H:%M")
dates = as.Date(datetimes)
dates[spl]
```
problem 2.2: `r strftime(median(dates),"%b %Y")`
```{r}
library(gdata)
data$Year = getYear(dates)
data$Month = months(dates)
data$Weekday = weekdays(dates)
data$Date = dates
```
problem 2.3:
```{r}
sort(table(data$Weekday))
```
problem 2.4:
```{r}
sort(table(data$Month))
```
problem 2.5:
```{r}
sort(table(data[data$Arrest,"Month"]))
```
## Visualizing Crime Trends
problem 3.1:
```{r}
hist(data$Date,breaks=100)
```
problem 3.2:
```{r}
boxplot(data$Date~data$Arrest)
```
TODO: study `boxplot`
problem 3.3,3.4,3.5:
```{r}
t = table(data$Year,data$Arrest)
t = cbind(t,ArrestRate = apply(t,1,function(x){x[2]/sum(x)}))
t
```
## Popular Locations
problem 4.1:
```{r}
u = table(data$LocationDescription)
un = setdiff(names(sort(u,decreasing=TRUE)[1:6]),"OTHER")
un
```
```{r}
top5 = data[data$LocationDescription %in% un,]
```
problem 4.2: `r nrow(top5)`
```{r}
top5$LocationDescription = factor(top5$LocationDescription)
```
```{r}
t5 = table(top5$LocationDescription,top5$Arrest)
t5 = cbind(t5,ArrestRate = apply(t5,1,function(x){x[2]/sum(x)}))
oo = order(t5[,3],decreasing=TRUE)
on = rownames(t5)
t5 = cbind(on[oo],t5[oo,3])
t5
highest_arrest =t5[1,1]
```
problem 4.3: `r highest_arrest`
problem 4.4:
```{r}
gs = data[data$LocationDescription==highest_arrest,]
tail(sort(table(gs$Weekday)),1)
```
problem 4.5:
```{r}
dw = data[data$LocationDescription=="DRIVEWAY - RESIDENTIAL",]
head(sort(table(dw$Weekday)),1)
```