-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeSeries.R
300 lines (218 loc) · 9.4 KB
/
TimeSeries.R
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#########################################################
# READ IN THE DATA AND SUBSET IT #
#########################################################
setwd("~/DataScience/capstone")
library(astsa) ##load package for acf2 and sarima functions
# line graph of total crime throughout the years with economics theme
library(zoo)
library(ggthemes)
library(ggplot2)
library(tidyverse)
library(dplyr)
#load in datafile
BPD <- read.csv("BPD_Part_1_Victim_Based_Crime_Data_April25_2020.csv")
### Creating date columns
BPD$CrimeDate <- as.character(BPD$CrimeDate) #sets CrimeDate column as character variable
BPD$CrimeDate <- as.Date(BPD$CrimeDate, "%m/%d/%Y") #creates CrimeDate column as date variable
df_date <- data.frame(date = BPD$CrimeDate,
year = as.numeric(format(BPD$CrimeDate, format = "%Y")),
month = as.numeric(format(BPD$CrimeDate, format = "%m")),
day = as.numeric(format(BPD$CrimeDate, format = "%d"))) #creates year, month, and day as separate columns
df_date <- select(df_date, year, month, day) #selecting certain columns
BPD_4 <- cbind(df_date, BPD) #binding date columns to original data frame
BPD_4$CrimeDate <- format(BPD_4$CrimeDate, "%m/%d/%Y") #reformatting CrimeDate Column to desired format
balt_clean <- BPD_4[BPD_4$year >= 2014,] #subsets dataframe to years 2014>
#adding season variable
yq <- as.yearqtr(as.yearmon(balt_clean$CrimeDate, "%m/%d/%Y") + 1/12)
balt_clean$Season <- factor(format(yq, "%q"),
levels=1:4,
labels=c("Winter","Spring", "Summer", "Fall"))
tot_c2 <- balt_clean %>% group_by(CrimeDate) %>% summarise(Count=n()) # obtaining count of crimes for each day
tot_c2<- merge(tot_c2, balt_clean[,c(4,20)], by=c("CrimeDate"))
tot_c2$CrimeDate <- as.Date(tot_c2$CrimeDate, format="%m/%d/%Y") # changing CrimeDate variable to date format
BPDD<- tot_c2[!duplicated(tot_c2)]
# Replace outliers with the value 200
BPDD$TotalCrime[BPDD$TotalCrime >= 200] <- 200
# Make into a Time Series Object
Crime2020 <-ts(BPDD$TotalCrime)
# Find the Number of Days
n <- length(Crime2020)
n
# Row number of last day of 2019
n2019 <- 1826
# Subset to pre-2019 only
DailyTotal <- Crime2020[1:n2019]
# Subset to only the last 100 days (excluding 2020)
Crime <- DailyTotal[(n2019-99):n2019]
length(Crime2020) #all crime daily totals (up to April 18th, 2020)
length(Crime) #subset to last 100 days of 2019
length(DailyTotal) #subset to 2015-2019
#########################################################
# TIME SERIES PLOTS #
#########################################################
# plot of all data
plot.ts(Crime2020, main="Time Series Plot (Jan. 1st 2015 - April 18th 2020)")
# too scrunched up - subset to smaller time frame
# plot of last 100 days of 2019
plot.ts(Crime, main="Time Series Plot (Sept. 22nd - Dec. 31st 2019)")
# ACF plot of data
acf(DailyTotal, main="ACF for Daily Crime", lag.max=100)
# 2 rows of charts
par(mfrow=c(2,1))
# Plot Various Differenced Data
plot.ts(diff(Crime), main="Time Series Plot for Differenced Data")
plot.ts(diff(Crime,7), main="Time Series Plot for Seasonal Differenced Data")
##log transform data
logCrime<-log(Crime)
# Plot Various Differenced Data
plot.ts(diff(logCrime), main="Time Series Plot for Differenced Log(Data)")
plot.ts(diff(logCrime,7), main="Time Series Plot for Seasonal Differenced Log(Data)")
plot.ts(diff(diff(Crime,365)), main="Time Series Plot for Seasonal Differenced Data")
plot.ts(diff(diff(DailyTotal,7)), main="Time Series Plot for Seasonal Differenced Data (all)")
# back to 1 plot in a row
par(mfrow=c(1,1))
#########################################################
# DETERMINE DIFFERENCING #
#########################################################
#Periodogram
mvspec(DailyTotal)
#Apply smoothing to the data
k<-kernel("daniell",c(7,7)) ##the value 7 means we smooth using 4 terms before, 7 terms after, plus current term, so L=15.
# daniell is specified when we apply equal weights to all 9 terms
soi.smooth<-mvspec(DailyTotal,k)
# plot of last 100 days of 2019
plot.ts(Crime, main="Time Series Plot (Sept. 22nd - Dec. 31st 2019)")
plot.ts(diff(diff(Crime,7)), main="Time Series Plot for Differenced Data")
#########################################################
# DETERMINE MODEL PARAMS #
#########################################################
# ACF AND PACF Plot - Seasonal and Nonseasonal Differencing
acf2(diff(diff(Crime,7)),35,
main="ACF/PACF with both regular and seasonal differencing")[-1:-35,]
#Calculate period (1/frequency)
1/0.15
# have d=1, D=1, s=7
# p, q, P, Q are 0 or 1
fit1<-sarima(DailyTotal,1,1,0,1,1,0,7) #diagnostics are OK
fit2<-sarima(DailyTotal,1,1,0,1,1,1,7) #diagnostics are off
fit3<-sarima(DailyTotal,0,1,1,1,1,0,7) #diagnostics are OK
fit4<-sarima(DailyTotal,0,1,1,1,1,1,7) #diagnostics are off
fit5<-sarima(DailyTotal,1,1,1,1,1,0,7) #diagnostics are OK
fit6<-sarima(DailyTotal,1,1,1,1,1,1,7) #diagnostics are off
fit7<-sarima(DailyTotal,1,1,1,0,1,1,7) #diagnostics are OK, plots are OK
# p =2, 3, 4
# P= 0
fit8<-sarima(DailyTotal,2,1,1,0,1,1,7) #diagnostics are off
fit9<-sarima(DailyTotal,3,1,1,0,1,1,7) #diagnostics are off
fit10<-sarima(DailyTotal,4,1,1,0,1,1,7) #diagnostics are off
# p=1 , P= 2, 3, 4, 5
fit11<-sarima(DailyTotal,1,1,1,2,1,1,7) #diagnostics are OK, plots are OK
fit12<-sarima(DailyTotal,1,1,1,3,1,1,7) #diagnostics are OK, plots are OK
# p=0, q=3
fit13<-sarima(DailyTotal,0,1,3,0,1,1,7) #diagnostics are ok, plots are OK
# so model 1 will be fit 7
fit7
# and model 2 will be fit 13
fit13
#########################################################
# FIT MODEL 1 #
#########################################################
#predict the next 12 observations
preds<-sarima.for(DailyTotal,31,1,1,1,0,1,1,7)
preds
#overlay actual values on the plot with predictions
lines(1827:(1827+30), Crime2020[1827:(1827+30)], type="b", col="blue")
title(main="Predictions for SARIMA(1,1,1)x(0,1,1)7 Model")
legend("bottomleft", c("Actual Value", "Prediction"),lty = 1, col = c("blue", "red"))
#calculate lower and upper bounds of prediction intervals
lower<-preds$pred - 1.96*preds$se
upper<-preds$pred + 1.96*preds$se
actual <- Crime2020[1827:(1827+30)]
predictions <- preds$pred
interval <- list(lower,upper)
#create a table with lower bound, actual value, and upper bound
comp<-cbind(actual, predictions, lower, upper)
df <- data.frame(comp)
df$Interval <- FALSE
for (i in (1:nrow(df))){
real <- df[i,"actual"]
lower <- df[i,"lower"]
upper <- df[i,"upper"]
if (real > lower){
if (real < upper){
df[i,"Interval"] <- TRUE
}
}
}
# Evaluate forecast with package
library(forecast)
accuracy(preds$pred, actual)
accuracy(fit7$preds, DailyTotal)s
# Manually evaluating forecasting - if you don't want to download the package
# Calculate error
error <- actual - predictions
# Function that returns Root Mean Squared Error
rmse <- function(error)
{
sqrt(mean(error^2))
}
# Calculate RMSE
rmse(error)
# Function that returns Mean Absolute Error
mae <- function(error)
{
mean(abs(error))
}
#Calculate MAE
mae(error)
#########################################################
# FIT MODEL 2 #
#########################################################
#predict the next 12 observations
preds2<-sarima.for(DailyTotal,31,0,1,3,0,1,1,7)
preds2
#overlay actual values on the plot with predictions
lines(1827:(1827+30), Crime2020[1827:(1827+30)], type="b", col="blue")
title(main="Predictions for SARIMA(0,1,3)x(0,1,1)7 Model")
legend("bottomleft", c("Actual Value", "Prediction"),lty = 1, col = c("blue", "red"))
#calculate lower and upper bounds of prediction intervals
lower<-preds2$pred - 1.96*preds$se
upper<-preds2$pred + 1.96*preds$se
actual <- Crime2020[1827:(1827+30)]
predictions <- preds2$pred
interval <- list(lower,upper)
#create a table with lower bound, actual value, and upper bound
comp2<-cbind(actual, predictions, lower, upper)
df <- data.frame(comp2)
df$Interval <- FALSE
for (i in (1:nrow(df))){
real <- df[i,"actual"]
lower <- df[i,"lower"]
upper <- df[i,"upper"]
if (real > lower){
if (real < upper){
df[i,"Interval"] <- TRUE
}
}
}
# Evaluate forecast with package
accuracy(preds2$pred, actual)
# Manually evaluating forecasting - if you don't want to download the package
# Calculate error
error <- actual - predictions
# Calculate RMSE
rmse(error)
#Calculate MAE
mae(error)
#########################################################
# COVID 19 #
#########################################################
remove <- which(tot_c2$Count >= 250) #finding which row to remove
tot_c_fin <- tot_c2[-remove,] #subsetting to everything but the removed row
ggplot(tot_c_fin, aes(x=CrimeDate, y=Count, color=Season)) +
geom_line(aes(group=1)) +
scale_x_date(date_labels="%b %Y", date_breaks="1 year") +
theme_economist() +
scale_color_economist() +
ggtitle("Total Crime Throughout the Years") +
theme(plot.title=element_text(hjust=0.5))