Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Data/
# or .Rmd files
*.pdf
*.tex

1 change: 1 addition & 0 deletions Data/Crime/Crime.CPG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file added Data/Crime/Crime.dbf
Binary file not shown.
Binary file added Data/Crime/Crime.docx
Binary file not shown.
1 change: 1 addition & 0 deletions Data/Crime/Crime.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet",GEOGCS["GCS_North_American_1983_HARN",DATUM["D_North_American_1983_HARN",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1640416.666666667],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-120.8333333333333],PARAMETER["Standard_Parallel_1",47.5],PARAMETER["Standard_Parallel_2",48.73333333333333],PARAMETER["Latitude_Of_Origin",47.0],UNIT["Foot_US",0.3048006096012192]],VERTCS["NAVD_1988",VDATUM["North_American_Vertical_Datum_1988"],PARAMETER["Vertical_Shift",0.0],PARAMETER["Direction",1.0],UNIT["Foot_US",0.3048006096012192]]
Binary file added Data/Crime/Crime.sbn
Binary file not shown.
Binary file added Data/Crime/Crime.sbx
Binary file not shown.
Binary file not shown.
Binary file added Data/Crime/Crime.shx
Binary file not shown.
1 change: 1 addition & 0 deletions Data/Neighborhood/Neighborhood.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UTF-8
Binary file added Data/Neighborhood/Neighborhood.dbf
Binary file not shown.
Binary file added Data/Neighborhood/Neighborhood.docx
Binary file not shown.
1 change: 1 addition & 0 deletions Data/Neighborhood/Neighborhood.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["NAD_1983_HARN_StatePlane_Washington_North_FIPS_4601_Feet",GEOGCS["GCS_North_American_1983_HARN",DATUM["D_North_American_1983_HARN",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",1640416.666666667],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-120.8333333333333],PARAMETER["Standard_Parallel_1",47.5],PARAMETER["Standard_Parallel_2",48.73333333333333],PARAMETER["Latitude_Of_Origin",47.0],UNIT["Foot_US",0.3048006096012192]],VERTCS["NAVD_1988",VDATUM["North_American_Vertical_Datum_1988"],PARAMETER["Vertical_Shift",0.0],PARAMETER["Direction",1.0],UNIT["Foot_US",0.3048006096012192]]
Binary file added Data/Neighborhood/Neighborhood.sbn
Binary file not shown.
Binary file added Data/Neighborhood/Neighborhood.sbx
Binary file not shown.
Binary file added Data/Neighborhood/Neighborhood.shp
Binary file not shown.
Binary file added Data/Neighborhood/Neighborhood.shx
Binary file not shown.
1 change: 1 addition & 0 deletions Data/spokanepolicestations.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Row,Lat,Lng1,47.655343,-117.4205012,47.665633,-117.4289983,47.658928,-117.4295134,47.665286,-117.4289985,47.676037,-117.4224756,47.700997,-117.4027347,47.713703,-117.4051378,47.705618,-117.3651409,47.656268,-117.37904510,47.667252,-117.43998511,47.628279,-117.369646
Expand Down
42 changes: 42 additions & 0 deletions Station_Clustering/crime_location_by_year.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: "Spokane Crime Location by Year"
output: html_document
---

```{r}
library(maps)
library(ggmap)
library(lubridate)
library(dplyr)

# Produce a scatterplot with crime colored by offense type for each year
# ======================================================================
Crime_byNeighborhood <- read.csv('Data/Spokane_CrimeData_Neighborhood.csv') # Read in the data
# This will be updated
# when ezknitr is
# included

#police <- read.csv('../Data/spokanepolicestations.csv') # Read in police station locations
police <- data.frame(lat=c(47.665534),lng=c(-117.429014))

crime <- tbl_df(data.frame(Crime_byNeighborhood)) %>% # Convert to local dataframe for easy printing
mutate(BEGINDATE = as.Date(BEGINDATE,format="%Y/%m/%d"), # Date conversion
ENDDATE = as.Date(ENDDATE,format="%Y/%m/%d"),
year = year(BEGINDATE)) # Extract year

lm.lng <- lm(Lng~coords.x1,data=crime %>% # Linear models to convert x/y to lat/long (change)
filter(!is.na(Lat),!is.na(Lng)))
lm.lat <- lm(Lat~coords.x2,data=crime %>%
filter(!is.na(Lat),!is.na(Lng)))

crime <- crime %>% mutate(Lat=predict(lm.lat,newdata=crime), # Use predict to update lat/long
Lng=predict(lm.lng,newdata=crime))


ggplot(county_map[county_map$subregion=="spokane",],aes(x=long, y=lat)) + geom_polygon(colour="grey",fill="grey") + geom_point(data=crime %>% filter(year>2007) %>% sample_n(20000),aes(Lng,Lat,colour=OFFGEN)) + geom_point(data=police,aes(lng,lat),colour="black",size=2) + facet_wrap(~year)

ggplot() + geom_point(data=crime %>% filter(year>2007) %>% sample_n(20000),aes(longitude,latitude,colour=OFFGEN)) + geom_point(data=police,aes(lng,lat),colour="black",size=2) + facet_wrap(~year)


ggplot() +geom_point(data=crime %>% filter(year>2007) %>% sample_n(200),aes(coords.x1,coords.x2,colour=OFFGEN)) + facet_wrap(~year)
```
Binary file added annualplot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added annualplot9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assaultoveryears.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 16 additions & 16 deletions basecode.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#this is an R script to import Spokane City crime data, merge it with Neighborhoods, and export it as a flat file
rm(list=ls())
library(rgdal)
library(maptools)
#reading in Crime shapefile
Crime=readOGR(dsn="Crime",layer="Crime")
#reading in Neighborhood shapefile
Neighborhood=readOGR(dsn="Neighborhood",layer="Neighborhood")
#Extracting Neighborhood designation for each point by location
Crime_byNeighborhood=over(Crime,Neighborhood)
#appending rest of data to each point
Crime_byNeighborhood=spCbind(Crime,Crime_byNeighborhood)
#saving resulting dataset as csv
write.csv(file="Spokane_CrimeData_Neighborhood.csv",Crime_byNeighborhood)
#this is an R script to import Spokane City crime data, merge it with Neighborhoods, and export it as a flat file
rm(list=ls())
library(rgdal)
library(maptools)

#reading in Crime shapefile
Crime=readOGR(dsn="Crime",layer="Crime")
#reading in Neighborhood shapefile
Neighborhood=readOGR(dsn="Neighborhood",layer="Neighborhood")

#Extracting Neighborhood designation for each point by location
Crime_byNeighborhood=over(Crime,Neighborhood)
#appending rest of data to each point
Crime_byNeighborhood=spCbind(Crime,Crime_byNeighborhood)
#saving resulting dataset as csv
write.csv(file="/Data/Spokane_CrimeData_Neighborhood.csv",Crime_byNeighborhood)
Binary file added figure/unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figure/unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figure/unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 130 additions & 0 deletions spokanecrimedata.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
title: "Spokane Crime - Exploring Data"
author: "Patil"
date: "September 26, 2016"
output: html_document

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Reading data

```{r}
# Arrive at Crime_byNeighborhood using earlier code that was originated by Krisztian
# crimedfonly=data.frame(Crime_byNeighborhood)
# save(crimedfonly,file="Data/Crime_byNeighborhood.Rda")

load("Data/Crime_byNeighborhood.Rda")
str(crimedfonly)
```

# Calling on a few packages

```{r}
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(lubridate))
library(leaflet)
library(ggmap)

```

# Total offenses

```{r}
crimedfonly %>% group_by(OFFGEN)%>%summarise(Counts=length(OFFGEN))%>% ggplot(.,aes(reorder(OFFGEN,Counts),Counts))+geom_bar(stat="identity")+geom_text(aes(x=OFFGEN,y=Counts+2000,label=Counts))+coord_flip()+theme_bw()+labs(y="Count",x="")

```

# Number of times offenses occured in different neighborhoods

```{r}

crimedfonly %>% group_by(Name)%>%summarise(Counts=length(Name))%>%filter(Name!="")%>% ggplot(.,aes(reorder(Name,Counts),Counts))+geom_bar(stat="identity")+geom_text(aes(x=Name,y=Counts+1000,label=Counts))+coord_flip()+theme_bw()+labs(y="Count",x="")
```

## Offenses in different neighborhoods

```{r fig.width=10, fig.height=6}
# A shiny app will do well here

crimedfonly %>% group_by(Name,OFFGEN)%>%summarise(Counts=length(OFFGEN))%>%filter(Name!="")%>% ggplot(.,aes(OFFGEN,Counts))+geom_bar(stat="identity")+facet_wrap(~Name,scales="free")

```

## Messing with Dates

```{r}
# Adds 9 columns to the dataset

crimedfonly=crimedfonly[,1:11]

# Beginning dates parsed

crimedfonly$beginyear=year(ymd(crimedfonly$BEGINDATE))
crimedfonly$beginmonth=month(ymd(crimedfonly$BEGINDATE),label=TRUE)# label parameter inserts name of month instead of number
crimedfonly$begindate=day(ymd(crimedfonly$BEGINDATE))
crimedfonly$beginday=wday(ymd(crimedfonly$BEGINDATE)) # label parameter inserts day of week instead of number of the day in a 7-day week

# Ending dates parsed

crimedfonly$endyear=year(ymd(crimedfonly$ENDDATE))
crimedfonly$endmonth=month(ymd(crimedfonly$ENDDATE),label=TRUE)
crimedfonly$enddate=day(ymd(crimedfonly$ENDDATE))
crimedfonly$endday=wday(ymd(crimedfonly$ENDDATE),label=TRUE)

# duration, in days, between starting and ending days
crimedfonly$durationdays= (as.duration(ymd(crimedfonly$ENDDATE)-ymd(crimedfonly$BEGINDATE)))/ddays(1)

head(crimedfonly)

```

```{r eval=FALSE}
crimedfonly=crimedfonly[!is.na(crimedfonly$Lat),]
crimedfonly=crimedfonly[!is.na(crimedfonly$Lng),]
crime2008=crimedfonly[crimedfonly$beginyear==2008,]

location=c(-117.402209,47.665330)
map=get_map(location,source="osm", color="bw",zoom=11)

for (i in 1:10){


crimedfsub=crimedfonly[crimedfonly$OFFGEN==levels(crimedfonly$OFFGEN)[i],]
ggmap(map)+geom_point(data=crimedfsub,aes(Lng,Lat),color="red")+facet_wrap(~beginyear)+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_blank(),axis.title = element_blank(),
axis.ticks = element_blank())+ggtitle(paste(levels(crimedfonly$OFFGEN)[i],"Over the Years"))

ggsave(file=paste("annualplot",i,".png",sep=""),scale=4)
}

```





```{r eval=FALSE}
pal <- colorFactor(rainbow(10), domain = levels(crime2008$OFFGEN))
crimedfonly=crimedfonly[!is.na(crimedfonly$Lat),]
crimedfonly=crimedfonly[!is.na(crimedfonly$Lng),]
crime2008=crimedfonly[crimedfonly$beginyear==2008,]
levels(crime2008$OFFGEN)
leaflet(data=crime2008)%>%addTiles()%>%addCircleMarkers(color =~pal(OFFGEN),popup=~LOCATION)
```



# Few low hanging fruits to pick next



* plots of trends for years, months, weeks, days, offense types
* mapping stuff by different variables


Loading