Introduction:

In this post we examine Crimes by Time of Day in both San Francisco and Seattle. We compare and contrast to see if certain crime types occur relatively more often in SF or Seattle. Or if the times that certain types of crimes occur differ between the two cities. After some examination we find that Theft/Fraud is much more common in Seattle than San Francisco during the 2014 Summer. Additionally, we see that night times and noon have more crimes than other times. See Appendix for Crime Classifications.

Code is provided for the figures so that you can reproduce and follow along if desired.

Note: Data was collected during the Summer of 2014. Seattle data from https://data.seattle.gov/Public-Safety/Seattle-Police-Department-Police-Report-Incident/7ais-f98f. San Francisco data from https://data.sfgov.org/Public-Safety/SFPD-Incidents-from-1-January-2003/tmnf-yvry.

Loading and Formatting Data

For the sake of reproducibility I am including the loading and formatting of the data

suppressMessages(library(dplyr))
suppressMessages(library(lubridate))
suppressMessages(library(ggplot2))
suppressMessages(library(RColorBrewer))

seattle.data<-read.csv("seattle_incidents_summer_2014.csv",stringsAsFactors = FALSE)
sf.data<-read.csv("sanfrancisco_incidents_summer_2014.csv",stringsAsFactors = FALSE)
sf.data$Category<-as.factor(sf.data$Category)
crime.type<-read.csv("crime.type.csv")

sf.crime<-suppressWarnings(select(sf.data,Category,Time)%>%
  mutate(City="San Francisco",Hour.of.Day=hour(hm(Time)))%>%
  left_join(crime.type,by=c("Category"="Crime"))%>%
  select(City,Type,Hour.of.Day))

seattle.crime<-suppressWarnings(select(seattle.data,Offense.Type,Occurred.Date.or.Date.Range.Start)%>%
  mutate(City="Seattle",Hour.of.Day=hour(mdy_hms(Occurred.Date.or.Date.Range.Start)))%>%
  left_join(crime.type,by=c("Offense.Type"="Crime"))%>%
  select(City,Type,Hour.of.Day))

crime.data<-rbind(sf.crime,seattle.crime)
crime.data$City<-as.factor(crime.data$City)
crime.data$Type<-as.factor(crime.data$Type)

Types of Crimes by City

crime.group.type<-group_by(crime.data,City,Type)%>%summarise(Count=n())
type.plot<-ggplot(crime.group.type,aes(x=Type,y=Count,fill=factor(City)))+
  geom_bar(stat="identity",position="dodge")+
  guides(fill=guide_legend(ncol=1,title="City"))+
  xlab("Crime Type")+ylab("Count of Crimes")+ggtitle("Type of Crime by City")+
  theme(legend.position="right",plot.title=element_text(size = 25, face="bold"), axis.text=element_text(size=10), axis.title=element_text(size=15,face="bold"))
print(type.plot)

From the above chart we see that Theft/Fraud is about twice as likely to occur in Seattle as in San Francisco. It seems that San Francisco has more issues with the Other cateogry which includes things like trespassing, warrents, and non-criminal offenses.

Times of Theft/Fraud for San Francisco

crime.group.sf<-filter(crime.data,City=="San Francisco")%>%group_by(Type,Hour.of.Day)%>%summarise(Count=n())

sf.plot<-ggplot(crime.group.sf, aes(x = Hour.of.Day, y = Count,fill=Type)) +
  geom_bar(stat='identity')+
  scale_fill_brewer(palette="Set1")+
  ggtitle("SF Crimes by Time of Day") +
  labs(x="Hour of Day",y="Count of Crimes")+
  theme(legend.position="right",plot.title=element_text(size = 25, face="bold"), axis.text=element_text(size=10), axis.title=element_text(size=15,face="bold"))
print(sf.plot)

For San Francisco, we see a spike in crime around noon at the lunchtime hour. There is another spike in crimes during the 5pm to 7pm hour. Perhaps criminals know people are stuck in traffic and trying to get home.

Times of Theft/Fraud for Seattle

crime.group.seattle<-filter(crime.data,City=="Seattle")%>%group_by(Type,Hour.of.Day)%>%summarise(Count=n())
seattle.plot<-ggplot(crime.group.seattle,aes(x = Hour.of.Day, y = Count,fill=Type)) +
  geom_bar(stat='identity')+
  scale_fill_brewer(palette="Set1")+
  ggtitle("Seattle Crimes by Time of Day") +
  labs(x="Hour of Day",y="Count of Crimes")+
  theme(legend.position="right",plot.title=element_text(size = 25, face="bold"), axis.text=element_text(size=10), axis.title=element_text(size=15,face="bold"))
print(seattle.plot)

The time of crimes is a bit more evenly distributed throughout the night for Seattle compared to San Francisco. Theft is especially bad at noon and midnight but also quite troublesome from 6pm to midnight. The early morning hours are a downtime for criminals in Seattle.

Conclusions

San Francisco and Seattle have very different kinds of crime but show somewhat similar trends as to the time of day that trends occur. Namely, there is a peak of crimes around noon/lunchtime and also at evening/night between the hours of 6pm and midnight.

Appendix: Crime Classification

Crime types are broadly classified into 5 categories: Violent, Theft/Fraud, Damage, Self-Inflicted, and Other. Violent including things such as homicide, assult, arson, and any weapons charges. Theft/Fraud includes Robberies, Theft, Counterfeiting, and electronic fraud, etc. Damage includes property damage and vandalism. Self-Inflcited is Drug, DUI, Gambling, Prostitution, Pornography, Suicide, etc. Other is all other crimes and incudes Trespassing, fleeing arrest, and disorderly conduct to name few. See Apppendix for detailed mapping.

Here is the table showing the mapping from the Crimes to the categorizations (Types) I created.

print(crime.type)
##                              Crime           Type
## 1                            ARSON        Violent
## 2                     NON-CRIMINAL          Other
## 3                    LARCENY/THEFT    Theft/Fraud
## 4                    DRUG/NARCOTIC Self-Inflicted
## 5      DRIVING UNDER THE INFLUENCE Self-Inflicted
## 6                   OTHER OFFENSES          Other
## 7                         TRESPASS          Other
## 8                    VEHICLE THEFT    Theft/Fraud
## 9                          ASSAULT        Violent
## 10                           FRAUD    Theft/Fraud
## 11                  SUSPICIOUS OCC          Other
## 12                 SECONDARY CODES          Other
## 13                     WEAPON LAWS        Violent
## 14                  MISSING PERSON          Other
## 15                        WARRANTS          Other
## 16                         ROBBERY    Theft/Fraud
## 17                     DRUNKENNESS Self-Inflicted
## 18                    PROSTITUTION Self-Inflicted
## 19                     LIQUOR LAWS Self-Inflicted
## 20                      KIDNAPPING        Violent
## 21                 FAMILY OFFENSES          Other
## 22                       LOITERING          Other
## 23              DISORDERLY CONDUCT          Other
## 24          FORGERY/COUNTERFEITING    Theft/Fraud
## 25                    EMBEZZLEMENT    Theft/Fraud
## 26                        BURGLARY    Theft/Fraud
## 27                         SUICIDE Self-Inflicted
## 28                       VANDALISM         Damage
## 29                 STOLEN PROPERTY    Theft/Fraud
## 30                         RUNAWAY          Other
## 31                        GAMBLING Self-Inflicted
## 32                       EXTORTION    Theft/Fraud
## 33         PORNOGRAPHY/OBSCENE MAT Self-Inflicted
## 34                         BRIBERY    Theft/Fraud
## 35              BURGLARY-FORCE-RES    Theft/Fraud
## 36            FRAUD-IDENTITY THEFT    Theft/Fraud
## 37                      THEFT-MAIL    Theft/Fraud
## 38                     COUNTERFEIT    Theft/Fraud
## 39                       THEFT-OTH    Theft/Fraud
## 40                  THEFT-BUILDING    Theft/Fraud
## 41               FRAUD-CREDIT CARD    Theft/Fraud
## 42                     FRAUD-CHECK    Theft/Fraud
## 43                        EMBEZZLE    Theft/Fraud
## 44         BURGLARY-NOFORCE-NONRES    Theft/Fraud
## 45                     FRAUD-OTHER    Theft/Fraud
## 46                  THEFT-CARPROWL    Theft/Fraud
## 47                      HARASSMENT          Other
## 48                   THEFT-AUTOACC    Theft/Fraud
## 49            BURGLARY-NOFORCE-RES    Theft/Fraud
## 50  PROPERTY DAMAGE-NON RESIDENTIA         Damage
## 51             THEFT-LICENSE PLATE    Theft/Fraud
## 52                   PROPERTY LOST          Other
## 53                   FORGERY-CHECK    Theft/Fraud
## 54                  VEH-THEFT-AUTO    Theft/Fraud
## 55     BURGLARY-SECURE PARKING-RES    Theft/Fraud
## 56                  THEFT-PKPOCKET    Theft/Fraud
## 57                   THREATS-OTHER    Theft/Fraud
## 58  PROPERTY LOST - POLICE EQUIPME          Other
## 59                   THEFT-BICYCLE    Theft/Fraud
## 60       VEH-RCVD-FOR OTHER AGENCY          Other
## 61              VEH-THEFT-MTRCYCLE    Theft/Fraud
## 62                NARC-SELL-HEROIN Self-Inflicted
## 63  DISPUTE-CIVIL PROPERTY (NON AU          Other
## 64               VEH-THEFT-TRAILER    Theft/Fraud
## 65                ASSLT-AGG-WEAPON        Violent
## 66                  PROPERTY FOUND          Other
## 67                    THREATS-KILL        Violent
## 68                     DISPUTE-OTH          Other
## 69             WARRARR-MISDEMEANOR          Other
## 70             ASSLT-AGG-BODYFORCE        Violent
## 71     PROPERTY DAMAGE-RESIDENTIAL         Damage
## 72                 VEH-THEFT-TRUCK    Theft/Fraud
## 73      PROPERTY DAMAGE - GRAFFITI         Damage
## 74                THEFT-AUTO PARTS    Theft/Fraud
## 75                    ASSLT-NONAGG        Violent
## 76           FRAUD-WIRE-ELECTRONIC    Theft/Fraud
## 77                         TRAFFIC          Other
## 78           BURGLARY-FORCE-NONRES    Theft/Fraud
## 79     PROPERTY STOLEN-TRAFFICKING    Theft/Fraud
## 80                  THEFT-SHOPLIFT    Theft/Fraud
## 81                 DISTURBANCE-OTH          Other
## 82                VIOL-COURT ORDER          Other
## 83                 ILLEGAL DUMPING          Other
## 84     PROSTITUTION-ASSIST-PROMOTE Self-Inflicted
## 85        ROBBERY-STREET-BODYFORCE    Theft/Fraud
## 86                      THEFT-BOAT    Theft/Fraud
## 87                        TRESPASS          Other
## 88         PROPERTY STOLEN-POSSESS    Theft/Fraud
## 89                  THREATS-WEAPON        Violent
## 90      ROBBERY-BUSINESS-BODYFORCE    Theft/Fraud
## 91            LIQUOR LAW VIOLATION Self-Inflicted
## 92                   BIAS INCIDENT          Other
## 93               THEFT OF SERVICES    Theft/Fraud
## 94               DISTURBANCE-NOISE          Other
## 95            ASSLT-AGG-POLICE-GUN        Violent
## 96            HOMICIDE-JUST-WEAPON        Violent
## 97     PROP RECOVERED-OTHER AGENCY          Other
## 98                        OBSTRUCT          Other
## 99             ROBBERY-BANK-WEAPON    Theft/Fraud
## 100               NARC-FOUND-OTHER Self-Inflicted
## 101  DISPUTE-CIVIL PROPERTY (AUTO)          Other
## 102                       DRIVE-BY        Violent
## 103        ROBBERY-BUSINESS-WEAPON    Theft/Fraud
## 104               RECKLESS BURNING         Damage
## 105            ASSLT-NONAGG-POLICE        Violent
## 106            INJURY - ACCIDENTAL          Other
## 107      NARC-POSSESS-HALLUCINOGEN Self-Inflicted
## 108              NARC-POSSESS-METH Self-Inflicted
## 109                 WARRARR-FELONY          Other
## 110             ROBBERY-STREET-GUN    Theft/Fraud
## 111              NARC-FOUND-MARIJU Self-Inflicted
## 112    ROBBERY-RESIDENCE-BODYFORCE    Theft/Fraud
## 113           NARC-POSSESS-COCAINE Self-Inflicted
## 114                 INJURY - OTHER          Other
## 115 BURGLARY-SECURE PARKING-NONRES    Theft/Fraud
## 116    NARC-EQUIPMENT/PARAPHENALIA Self-Inflicted
## 117              WEAPON-POSSESSION        Violent
## 118       NARC-POSSESS-AMPHETAMINE Self-Inflicted
## 119                 THEFT-PRSNATCH    Theft/Fraud
## 120            NARC-POSSESS-HEROIN Self-Inflicted
## 121                   FALSE REPORT          Other
## 122          ROBBERY-STREET-WEAPON    Theft/Fraud
## 123               WARRANT-FUGITIVE          Other
## 124          ELUDING-FELONY FLIGHT          Other
## 125                  ASSLT-AGG-GUN        Violent
## 126               WEAPON-DISCHARGE        Violent
## 127              NARC-SELL-COCAINE Self-Inflicted
## 128                     ANIMAL-OTH          Other
## 129         NARC-FOUND-AMPHETAMINE Self-Inflicted
## 130                 FRAUD-COMPUTER    Theft/Fraud
## 131              NARC-FOUND-HEROIN Self-Inflicted
## 132                    ANIMAL-BITE          Other
## 133                 ANIMAL-CRUELTY          Other
## 134                   ENDANGERMENT          Other
## 135   PORNOGRAPHY-OBSCENE MATERIAL Self-Inflicted
## 136             NARC-POSSESS-OTHER Self-Inflicted
## 137                 NARC-SELL-METH Self-Inflicted
## 138                   THEFT-COINOP    Theft/Fraud
## 139                     DUI-LIQUOR Self-Inflicted
## 140           ROBBERY-BUSINESS-GUN    Theft/Fraud
## 141           NARC-FOUND-SYNTHETIC Self-Inflicted
## 142            NARC-POSSESS-MARIJU Self-Inflicted
## 143                    FORGERY-OTH    Theft/Fraud
## 144             NARC-FOUND-COCAINE Self-Inflicted
## 145                   PROSTITUTION Self-Inflicted
## 146        ASSLT-AGG-POLICE-WEAPON        Violent
## 147           MALICIOUS HARASSMENT          Other
## 148                      DUI-DRUGS Self-Inflicted
## 149            WEAPON-UNLAWFUL USE        Violent
## 150       ROBBERY-RESIDENCE-WEAPON    Theft/Fraud
## 151                   FIREWORK-USE          Other
## 152       PROSTITUTION PATRONIZING Self-Inflicted
## 153         PROSTITUTION LOITERING Self-Inflicted
## 154              HOMICIDE-JUST-GUN        Violent
## 155               ROBBERY-BANK-GUN    Theft/Fraud
## 156      NARC-FORGERY-PRESCRIPTION Self-Inflicted
## 157       [INC - CASE DC USE ONLY]          Other
## 158               WEAPON-CONCEALED        Violent
## 159          NARC-SELL-AMPHETAMINE    Theft/Fraud
## 160          ROBBERY-RESIDENCE-GUN    Theft/Fraud
## 161 URINATING/DEFECATING-IN PUBLIC          Other
## 162               VEH-THEFT-OTHVEH    Theft/Fraud
## 163         ROBBERY-BANK-BODYFORCE    Theft/Fraud
## 164    NARC-DRUG TRAFFIC LOITERING Self-Inflicted
## 165               NARC-FOUND-OPIUM Self-Inflicted
## 166     ASSLT-AGG-POLICE-BODYFORCE        Violent
## 167       NARC-POSSESS-PILL/TABLET Self-Inflicted
## 168           PROPERTY STOLEN-SELL    Theft/Fraud
## 169               FIREWORK-POSSESS          Other
## 170 WEAPON-SURRENDER-EXCLUDING FIR        Violent
## 171             DISORDERLY CONDUCT          Other
## 172                NARC-FOUND-METH Self-Inflicted
## 173        NARC-FRAUD-PRESCRIPTION Self-Inflicted
## 174             NARC-SMUGGLE-OTHER Self-Inflicted
## 175                         ESCAPE          Other
## 176            NARC-SELL-SYNTHETIC Self-Inflicted
## 177            FORGERY-CREDIT CARD    Theft/Fraud
## 178               NARC-SELL-MARIJU Self-Inflicted
## 179      HOMICIDE-PREMEDITATED-GUN        Violent
## 180            NARC-PRODUCE-MARIJU Self-Inflicted
## 181                 WEAPON-SELLING        Violent