How many rows of data (observations) are in this dataset?
Sys.setlocale("LC_ALL","C")
[1] "C"
nrow(mvt)
[1] 191641
How many variables are in this dataset?
ncol(mvt)
[1] 11
Using the “max” function, what is the maximum value of the variable “ID”?
max(mvt$ID)
[1] 9181151
What is the minimum value of the variable “Beat”?
min(mvt$Beat)
[1] 111
How many observations have value TRUE in the Arrest variable (this is the number of crimes for which an arrest was made)?
summary(mvt$Arrest)
Mode FALSE TRUE
logical 176105 15536
How many observations have a LocationDescription value of ALLEY?
table(mvt$LocationDescription)
ABANDONED BUILDING
4
AIRPORT BUILDING NON-TERMINAL - NON-SECURE AREA
4
AIRPORT BUILDING NON-TERMINAL - SECURE AREA
1
AIRPORT EXTERIOR - NON-SECURE AREA
24
AIRPORT EXTERIOR - SECURE AREA
1
AIRPORT PARKING LOT
11
AIRPORT TERMINAL UPPER LEVEL - NON-SECURE AREA
5
AIRPORT VENDING ESTABLISHMENT
10
AIRPORT/AIRCRAFT
363
ALLEY
2308
ANIMAL HOSPITAL
1
APARTMENT
184
APPLIANCE STORE
1
ATHLETIC CLUB
9
BANK
7
BAR OR TAVERN
17
BARBERSHOP
4
BOWLING ALLEY
3
BRIDGE
2
CAR WASH
44
CHA APARTMENT
5
CHA PARKING LOT/GROUNDS
405
CHURCH/SYNAGOGUE/PLACE OF WORSHIP
56
CLEANING STORE
3
COLLEGE/UNIVERSITY GROUNDS
47
COLLEGE/UNIVERSITY RESIDENCE HALL
2
COMMERCIAL / BUSINESS OFFICE
126
CONSTRUCTION SITE
35
CONVENIENCE STORE
7
CTA GARAGE / OTHER PROPERTY
148
CTA TRAIN
1
CURRENCY EXCHANGE
2
DAY CARE CENTER
5
DEPARTMENT STORE
22
DRIVEWAY - RESIDENTIAL
1675
DRUG STORE
8
FACTORY/MANUFACTURING BUILDING
16
FIRE STATION
5
FOREST PRESERVE
6
GAS STATION
2111
GOVERNMENT BUILDING/PROPERTY
48
GROCERY FOOD STORE
80
HIGHWAY/EXPRESSWAY
22
HOSPITAL BUILDING/GROUNDS
101
HOTEL/MOTEL
124
JAIL / LOCK-UP FACILITY
1
LAKEFRONT/WATERFRONT/RIVERBANK
4
LIBRARY
4
MEDICAL/DENTAL OFFICE
3
MOVIE HOUSE/THEATER
18
NEWSSTAND
1
NURSING HOME/RETIREMENT HOME
21
OTHER
4573
OTHER COMMERCIAL TRANSPORTATION
8
OTHER RAILROAD PROP / TRAIN DEPOT
28
PARK PROPERTY
255
PARKING LOT/GARAGE(NON.RESID.)
14852
POLICE FACILITY/VEH PARKING LOT
266
RESIDENCE
1302
RESIDENCE-GARAGE
1176
RESIDENCE PORCH/HALLWAY
18
RESIDENTIAL YARD (FRONT/BACK)
1536
RESTAURANT
49
SAVINGS AND LOAN
4
SCHOOL, PRIVATE, BUILDING
14
SCHOOL, PRIVATE, GROUNDS
23
SCHOOL, PUBLIC, BUILDING
114
SCHOOL, PUBLIC, GROUNDS
206
SIDEWALK
462
SMALL RETAIL STORE
33
SPORTS ARENA/STADIUM
166
STREET
156564
TAVERN/LIQUOR STORE
14
TAXICAB
21
VACANT LOT/LAND
985
VEHICLE-COMMERCIAL
23
VEHICLE NON-COMMERCIAL
817
WAREHOUSE
17
In many datasets, like this one, you have a date field. Unfortunately, R does not automatically recognize entries that look like dates. We need to use a function in R to extract the date and time. Take a look at the first entry of Date (remember to use square brackets when looking at a certain entry of a variable).
In what format are the entries in the variable Date?
(mvt$Date)[1]
[1] 12/31/12 23:15
131680 Levels: 1/1/01 0:01 1/1/01 0:05 1/1/01 0:30 1/1/01 1:17 ... 9/9/12 9:50
Now, let’s convert these characters into a Date object in R. In your R console, type
DateConvert = as.Date(strptime(mvt$Date, "%m/%d/%y %H:%M"))
This converts the variable “Date” into a Date object in R. Take a look at the variable DateConvert using the summary function.
What is the month and year of the median date in our dataset? Enter your answer as “Month Year”, without the quotes. (Ex: if the answer was 2008-03-28, you would give the answer “March 2008”, without the quotes.)
DateConvert = as.Date(strptime(mvt$Date, "%m/%d/%y %H:%M"))
summary(DateConvert)
Min. 1st Qu. Median Mean 3rd Qu.
"2001-01-01" "2003-07-10" "2006-05-21" "2006-08-23" "2009-10-24"
Max.
"2012-12-31"
Now, let’s extract the month and the day of the week, and add these variables to our data frame mvt. We can do this with two simple functions. Type the following commands in R:
mvt$Month = months(DateConvert)
mvt$Weekday = weekdays(DateConvert)
This creates two new variables in our data frame, Month and Weekday, and sets them equal to the month and weekday values that we can extract from the Date object. Lastly, replace the old Date variable with DateConvert by typing:
mvt$Date = DateConvert
Using the table command, answer the following questions.
In which month did the fewest motor vehicle thefts occur?
mvt$Month = months(DateConvert)
mvt$Weekday = weekdays(DateConvert)
mvt$Date = DateConvert
table(mvt$Month)
April August December February January July June
15280 16572 16426 13511 16047 16801 16002
March May November October September
15758 16035 16063 17086 16060
On which weekday did the most motor vehicle thefts occur?
table(mvt$Weekday)
Friday Monday Saturday Sunday Thursday Tuesday Wednesday
29284 27397 27118 26316 27319 26791 27416
Each observation in the dataset represents a motor vehicle theft, and the Arrest variable indicates whether an arrest was later made for this theft. Which month has the largest number of motor vehicle thefts for which an arrest was made?
table(mvt$Month, mvt$Arrest)
FALSE TRUE
April 14028 1252
August 15243 1329
December 15029 1397
February 12273 1238
January 14612 1435
July 15477 1324
June 14772 1230
March 14460 1298
May 14848 1187
November 14807 1256
October 15744 1342
September 14812 1248
Now, let’s make some plots to help us better understand how crime has changed over time in Chicago. Throughout this problem, and in general, you can save your plot to a file. For more information, this website very clearly explains the process.
First, let’s make a histogram of the variable Date. We’ll add an extra argument, to specify the number of bars we want in our histogram. In your R console, type
hist(mvt$Date, breaks=100)
hist(mvt$Date, breaks=100)
Looking at the histogram, answer the following questions.
In general, does it look like crime increases or decreases from 2002 - 2012?
In general, does it look like crime increases or decreases from 2005 - 2008?
In general, does it look like crime increases or decreases from 2009 - 2011?
Now, let’s see how arrests have changed over time. Create a boxplot of the variable “Date”, sorted by the variable “Arrest” (if you are not familiar with boxplots and would like to learn more, check out this tutorial). In a boxplot, the bold horizontal line is the median value of the data, the box shows the range of values between the first quartile and third quartile, and the whiskers (the dotted lines extending outside the box) show the minimum and maximum values, excluding any outliers (which are plotted as circles). Outliers are defined by first computing the difference between the first and third quartile values, or the height of the box. This number is called the Inter-Quartile Range (IQR). Any point that is greater than the third quartile plus the IQR or less than the first quartile minus the IQR is considered an outlier.
Does it look like there were more crimes for which arrests were made in the first half of the time period or the second half of the time period? (Note that the time period is from 2001 to 2012, so the middle of the time period is the beginning of 2007.)
boxplot(mvt$Date~mvt$Arrest)
Let’s investigate this further. Use the table function for the next few questions.
For what proportion of motor vehicle thefts in 2001 was an arrest made?
Note: in this question and many others in the course, we are asking for an answer as a proportion. Therefore, your answer should take a value between 0 and 1.
table(mvt$Year,mvt$Arrest)
FALSE TRUE
2001 18517 2152
2002 16638 2115
2003 14859 1798
2004 15169 1693
2005 14956 1528
2006 14796 1302
2007 13068 1212
2008 13425 1020
2009 11327 840
2010 14796 701
2011 15012 625
2012 13542 550
2152/(2152+18517)
[1] 0.1041173
For what proportion of motor vehicle thefts in 2007 was an arrest made?
1212/(1212+13068)
[1] 0.08487395
For what proportion of motor vehicle thefts in 2012 was an arrest made?
550/(550+13542)
[1] 0.03902924
Since there may still be open investigations for recent crimes, this could explain the trend we are seeing in the data. There could also be other factors at play, and this trend should be investigated further. However, since we don’t know when the arrests were actually made, our detective work in this area has reached a dead end.
Analyzing this data could be useful to the Chicago Police Department when deciding where to allocate resources. If they want to increase the number of arrests that are made for motor vehicle thefts, where should they focus their efforts?
We want to find the top five locations where motor vehicle thefts occur. If you create a table of the LocationDescription variable, it is unfortunately very hard to read since there are 78 different locations in the data set. By using the sort function, we can view this same table, but sorted by the number of observations in each category. In your R console, type:
sort(table(mvt$LocationDescription))
Which locations are the top five locations for motor vehicle thefts, excluding the “Other” category? You should select 5 of the following options.
sort(table(mvt$LocationDescription))
AIRPORT BUILDING NON-TERMINAL - SECURE AREA
1
AIRPORT EXTERIOR - SECURE AREA
1
ANIMAL HOSPITAL
1
APPLIANCE STORE
1
CTA TRAIN
1
JAIL / LOCK-UP FACILITY
1
NEWSSTAND
1
BRIDGE
2
COLLEGE/UNIVERSITY RESIDENCE HALL
2
CURRENCY EXCHANGE
2
BOWLING ALLEY
3
CLEANING STORE
3
MEDICAL/DENTAL OFFICE
3
ABANDONED BUILDING
4
AIRPORT BUILDING NON-TERMINAL - NON-SECURE AREA
4
BARBERSHOP
4
LAKEFRONT/WATERFRONT/RIVERBANK
4
LIBRARY
4
SAVINGS AND LOAN
4
AIRPORT TERMINAL UPPER LEVEL - NON-SECURE AREA
5
CHA APARTMENT
5
DAY CARE CENTER
5
FIRE STATION
5
FOREST PRESERVE
6
BANK
7
CONVENIENCE STORE
7
DRUG STORE
8
OTHER COMMERCIAL TRANSPORTATION
8
ATHLETIC CLUB
9
AIRPORT VENDING ESTABLISHMENT
10
AIRPORT PARKING LOT
11
SCHOOL, PRIVATE, BUILDING
14
TAVERN/LIQUOR STORE
14
FACTORY/MANUFACTURING BUILDING
16
BAR OR TAVERN
17
WAREHOUSE
17
MOVIE HOUSE/THEATER
18
RESIDENCE PORCH/HALLWAY
18
NURSING HOME/RETIREMENT HOME
21
TAXICAB
21
DEPARTMENT STORE
22
HIGHWAY/EXPRESSWAY
22
SCHOOL, PRIVATE, GROUNDS
23
VEHICLE-COMMERCIAL
23
AIRPORT EXTERIOR - NON-SECURE AREA
24
OTHER RAILROAD PROP / TRAIN DEPOT
28
SMALL RETAIL STORE
33
CONSTRUCTION SITE
35
CAR WASH
44
COLLEGE/UNIVERSITY GROUNDS
47
GOVERNMENT BUILDING/PROPERTY
48
RESTAURANT
49
CHURCH/SYNAGOGUE/PLACE OF WORSHIP
56
GROCERY FOOD STORE
80
HOSPITAL BUILDING/GROUNDS
101
SCHOOL, PUBLIC, BUILDING
114
HOTEL/MOTEL
124
COMMERCIAL / BUSINESS OFFICE
126
CTA GARAGE / OTHER PROPERTY
148
SPORTS ARENA/STADIUM
166
APARTMENT
184
SCHOOL, PUBLIC, GROUNDS
206
PARK PROPERTY
255
POLICE FACILITY/VEH PARKING LOT
266
AIRPORT/AIRCRAFT
363
CHA PARKING LOT/GROUNDS
405
SIDEWALK
462
VEHICLE NON-COMMERCIAL
817
VACANT LOT/LAND
985
RESIDENCE-GARAGE
1176
RESIDENCE
1302
RESIDENTIAL YARD (FRONT/BACK)
1536
DRIVEWAY - RESIDENTIAL
1675
GAS STATION
2111
ALLEY
2308
OTHER
4573
PARKING LOT/GARAGE(NON.RESID.)
14852
STREET
156564
Create a subset of your data, only taking observations for which the theft happened in one of these five locations, and call this new data set “Top5”. To do this, you can use the | symbol. In lecture, we used the & symbol to use two criteria to make a subset of the data. To only take observations that have a certain value in one variable or the other, the | character can be used in place of the & symbol. This is also called a logical “or” operation.
Alternately, you could create five different subsets, and then merge them together into one data frame using rbind.
How many observations are in Top5?
Top5 = subset(mvt,LocationDescription=="STREET"|LocationDescription=="PARKING LOT/GARAGE(NON.RESID.)"|LocationDescription=="ALLEY"|LocationDescription=="GAS STATION"|LocationDescription=="DRIVEWAY - RESIDENTIAL")
str(Top5)
'data.frame': 177510 obs. of 13 variables:
$ ID : int 8951354 8951141 8952223 8951608 8950793 8950760 8951611 8951802 8950706 8951585 ...
$ Date : Date, format: "2012-12-31" "2012-12-31" ...
$ LocationDescription: Factor w/ 78 levels "ABANDONED BUILDING",..: 72 72 72 72 72 72 72 72 72 72 ...
$ Arrest : logi FALSE FALSE FALSE FALSE TRUE FALSE ...
$ Domestic : logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$ Beat : int 623 1213 724 211 2521 423 231 1021 1215 1011 ...
$ District : int 6 12 7 2 25 4 2 10 12 10 ...
$ CommunityArea : int 69 24 67 35 19 48 40 29 24 29 ...
$ Year : int 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 ...
$ Latitude : num 41.8 41.9 41.8 41.8 41.9 ...
$ Longitude : num -87.6 -87.7 -87.7 -87.6 -87.8 ...
$ Month : chr "December" "December" "December" "December" ...
$ Weekday : chr "Monday" "Monday" "Monday" "Monday" ...
R will remember the other categories of the LocationDescription variable from the original dataset, so running table(Top5$LocationDescription) will have a lot of unnecessary output. To make our tables a bit nicer to read, we can refresh this factor variable. In your R console, type:
Top5$LocationDescription = factor(Top5$LocationDescription)
If you run the str or table function on Top5 now, you should see that LocationDescription now only has 5 values, as we expect.
Use the Top5 data frame to answer the remaining questions.
One of the locations has a much higher arrest rate than the other locations. Which is it? Please enter the text in exactly the same way as how it looks in the answer options for Problem 4.1.
Top5$LocationDescription = factor(Top5$LocationDescription)
table(Top5$LocationDescription,Top5$Arrest)
FALSE TRUE
ALLEY 2059 249
DRIVEWAY - RESIDENTIAL 1543 132
GAS STATION 1672 439
PARKING LOT/GARAGE(NON.RESID.) 13249 1603
STREET 144969 11595
249/(249+2059)
[1] 0.1078856
132/(132+1543)
[1] 0.07880597
439/(439+1672)
[1] 0.2079583
1603/(1603+13249)
[1] 0.1079316
11595/(11595+144969)
[1] 0.07405917
On which day of the week do the most motor vehicle thefts at gas stations happen?(Monday~Sunday)
table(Top5$Weekday,Top5$LocationDescription)
ALLEY DRIVEWAY - RESIDENTIAL GAS STATION
Friday 385 257 332
Monday 320 255 280
Saturday 341 202 338
Sunday 307 221 336
Thursday 315 263 282
Tuesday 323 243 270
Wednesday 317 234 273
PARKING LOT/GARAGE(NON.RESID.) STREET
Friday 2331 23773
Monday 2128 22305
Saturday 2199 22175
Sunday 1936 21756
Thursday 2082 22296
Tuesday 2073 21888
Wednesday 2103 22371
On which day of the week do the fewest motor vehicle thefts in residential driveways happen?(Monday~Sunday)
table(Top5$Weekday,Top5$LocationDescription)
ALLEY DRIVEWAY - RESIDENTIAL GAS STATION
Friday 385 257 332
Monday 320 255 280
Saturday 341 202 338
Sunday 307 221 336
Thursday 315 263 282
Tuesday 323 243 270
Wednesday 317 234 273
PARKING LOT/GARAGE(NON.RESID.) STREET
Friday 2331 23773
Monday 2128 22305
Saturday 2199 22175
Sunday 1936 21756
Thursday 2082 22296
Tuesday 2073 21888
Wednesday 2103 22371