library(pacman)
p_load(tidyverse, DT, dygraphs, plotly, xts)
rm(list=ls())
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.5-18, (SVN revision 1082)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.1.1, released 2020/06/22
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
## Linking to sp version:1.4-4
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
library(geojsonio)
## 
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
library(tidyverse)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(zoo)
source('Chen_Xiaodan_Stat694_Project.R')
alam <- read_rds('alameda_crime_data.Rds')
alam <- alam %>% mutate(date = as.Date.POSIXct(time))
head(alam)
alam %>%
  select(CrimeCode) %>%
  group_by(CrimeCode) %>%
  count() %>%
  arrange(desc(n))
datatable(alam)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html
alam %>% select(City) %>%
  group_by(City) %>%
  count() %>%
  arrange(desc(n))
alame <- alam %>% 
         mutate(City=as.character(City)) %>%
         group_by(City) %>%
         summarize(n = n(), .groups = 'drop') %>%
         mutate(city_n = as.factor(ifelse(n > 800, City, 'OTHER'))) %>%
         left_join(alam, City = City)
## Joining, by = "City"
alame       
alame_plot1 <- alame %>% filter(!is.na(month)) %>%
                    group_by(month, city_n) %>%
                    summarise(n_crime=n()) %>% 
                    ggplot(aes(month, n_crime)) +
                    geom_line() + 
                    facet_wrap(~city_n)
## `summarise()` regrouping output by 'month' (override with `.groups` argument)
alame_plot1

alamed <- alame %>% filter(!is.na(month)) %>%
                    group_by(month) %>%
                    summarise(n_crime=n(), .groups = 'drop') %>%
                    mutate(city_n = 'TOTAL', city_n = as.factor(city_n))


alameda <- alame %>% filter(!is.na(month), city_n %in% c('CASTRO VALLEY', 'HAYWARD', 'SAN LEANDRO')) %>%
                    group_by(month, city_n) %>%
                    summarise(n_crime=n()) %>% 
                    rbind(alamed)
## `summarise()` regrouping output by 'month' (override with `.groups` argument)
alame_plot2 <- alameda %>%  ggplot(aes(month, n_crime, col = city_n)) +
                 geom_line() 
alame_plot2 

lockdown <- tibble(date = c(as.numeric(as.yearmon('2020-03-17')), 
                            as.numeric(as.yearmon('2020-05-22'))))
                           
alame_plot2 %+% filter(alameda, month > 'Jun 2018') +
  geom_vline(data = lockdown, aes(xintercept = date), linetype = 'dotted', col = 'blue4') +
  geom_text(aes(x =  as.numeric(as.yearmon('2020-04-22')), y = 2050), label = 'Lock down', col = 'blue4', size = 3)

dygraph

alamed <- alame %>% filter(!is.na(date)) %>%
                    group_by(date) %>%
                    summarise(n_crime=n(), .groups = 'drop') %>%
                    mutate(city_n = 'TOTAL', city_n = as.factor(city_n))

time_series <- xts(x= alamed$n_crime, order.by = alamed$date)
 
time_series %>%
  dygraph(main = 'Crime_Counts_Alameda') %>%
  dyRangeSelector(dateWindow = c('2019-01-01','2020-09-28'))
# alame_plot2 <- alame %>% filter(!is.na(date), city_n %in% c('CASTRO VALLEY', 'HAYWARD', 'SAN LEANDRO')) %>%
#                    group_by(date, city_n) %>%
#                    summarise(n_crime=n(), .groups = 'drop') %>% 
#                    rbind(alamed)

# alame_plot2 %>% select(date, city_n, n_crime) %>%
#  spread(key = city_n, value = n_crime) %>%
#  dyRangeSelector(dateWindow = c('2012-01-01','2020-09-28'))
  1. ShinyApp, echart
  2. heatmap
  3. hypothesis