-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_DataAnalysis_04.R
342 lines (224 loc) · 9 KB
/
03_DataAnalysis_04.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Description ---------------
#### Mal checken: https://www.datascience.com/blog/introduction-to-forecasting-with-arima-in-r-learn-data-science-tutorials #####
# In this script
# - we will forecast the parking density
# - of one parking meter
# - without features
# Setup ----------------------------------------------
# Load required packages
# library(data.table)
library(tidyverse)
# library(dplyr)
library(ggplot2)
library(forecast)
library(tseries)
library(lubridate)
# library(SpatioTemporal)
# library(plotrix)
# Clear workspace
rm(list=ls())
graphics.off()
# ...
# set.seed(100)
theme_set(theme_minimal())
# Load the previousely saved version of our parking data as well as new data (weather, events)
load("../Schramm, Cornelius - 02_Business_Analytics_Data/df_set_02_merged.RData")
# dates = fread("../Schramm, Cornelius - 02_Business_Analytics_Data/dates.csv")
# holidays = fread("../Schramm, Cornelius - 02_Business_Analytics_Data/holidays.csv")
# weather_01 = read_csv2("../02_Business_Analytics_Data/weather.csv") #(25th March to 22nd April)
# weather_02 = read_csv2("../02_Business_Analytics_Data/history_export_2019-04-08T16_02_14.csv", skip =11)
# events = fread("../02_Business_Analytics_Data/events.csv")
# Because of OneDrive we need to load from two different paths
load("../02_Business_Analytics_Data/df_set_02_merged.RData")
# dates = fread("../02_Business_Analytics_Data/dates.csv")
# holidays = fread("../02_Business_Analytics_Data/holidays.csv")
# weather_01 = read_csv2("../Schramm, Cornelius - 02_Business_Analytics_Data/weather.csv") #(25th March to 22nd April)
# weather_02 = read_csv2("../Schramm, Cornelius - 02_Business_Analytics_Data/history_export_2019-04-08T16_02_14.csv", skip =11)
# events = fread("../Schramm, Cornelius - 02_Business_Analytics_Data/events.csv")
# Prepare data -----
# From large data
p_large_slim = DF_merged[,c(1,6,7,24)]
####### Aggregated und mit absoluten Zahlen
rm(list=ls())
load("../Schramm, Cornelius - 02_Business_Analytics_Data/df_set_03_kmeanCluster.RData")
library(data.table)
tempDF2 = DF_KMclust
p_large_slim = tempDF2[, c(1:4)]
colnames(p_large_slim)[1] = "SourceElementKey"
colnames(p_large_slim)[2] = "date.x"
#######
# Choose cluster
parkingmeter = 3
# Filter one parking meter
parking_filtered = p_large_slim %>%
filter(SourceElementKey == parkingmeter)
# Merge date and time into one cell
parking_filtered$datetime = paste(parking_filtered$date.x, parking_filtered$hour)
parking_filtered = parking_filtered %>%
select(datetime, date.x, FreeSpots)
# Right format
parking_filtered$datetime = as.POSIXct(parking_filtered$datetime, format="%Y-%m-%d %H")
# Order by date and time
parking_filtered = parking_filtered[order(parking_filtered$datetime),]
# Oder ohne die datetime spalte zu kreieren
# parking_filtered = parking_filtered[order(parking_filtered$date, parking_filtered$time),]
# Reset index
rownames(parking_filtered) = NULL
# Analysis -----
# Now starting to copy from website mentioned above -----
# TS-Format and first plot ----
# Just for fun and viewing data
ggplot(parking_filtered, aes(as.numeric(datetime), FreeSpots)) +
geom_line() +
ylab("Free Parking Spaces") +
xlab("") +
geom_vline(xintercept=as.numeric(as.POSIXct("2019-03-28")), color="red") +
geom_vline(xintercept=as.numeric(as.POSIXct("2019-03-29")), color="red") +
geom_vline(xintercept=as.numeric(as.POSIXct("2019-03-30")), color="red") +
geom_vline(xintercept=as.numeric(as.POSIXct("2019-03-31")), color="red") +
geom_vline(xintercept=as.numeric(as.POSIXct("2019-04-01")), color="red") +
geom_vline(xintercept=as.numeric(as.POSIXct("2019-04-02")), color="red")
# xlim(1553750000,1554000000)
# ...
ts = ts(parking_filtered[, c('FreeSpots')])
parking_filtered$cleanX = tsclean(ts)
unique(parking_filtered$cleanX == parking_filtered$FreeSpots)
# No outliers found all the same data as before. Drop column and save shorter name
pf = parking_filtered[,c(1:3)]
# Training ----
# msts (2 seasonalities)
ts_kmc_2 = msts(parking_filtered$FreeSpots, seasonal.periods = c(12,12*6),
start = decimal_date(as.POSIXct("2019-03-25 08:00:00")),
ts.frequency = 12*6*52)
plot(ts_kmc_2)
# ts_kmc_2 = ts_kmc_2[, c(1,3)]
# tbats model smoothing
tbats = tbats(ts_kmc_2)
plot(tbats, main="Multiple Season Decomposition")
tbats.components(tbats)
# predicttions tbat
sp = predict(tbats,h=12*6)
plot(sp, main = "TBATS Forecast")
# testing tbat model on real data
##splitting and creating msts train and test
parking_filtered_train = parking_filtered[parking_filtered$datetime <= "2019-04-16",]
parking_filtered_test = parking_filtered[parking_filtered$datetime > "2019-04-16",]
ts_kmc_train = msts(parking_filtered_train$FreeSpots, seasonal.periods = c(12,12*6),
start = decimal_date(as.POSIXct("2019-03-25 08:00:00")),
ts.frequency = 12*6*52)
ts_kmc_test = msts(parking_filtered_test$FreeSpots, seasonal.periods = c(12,12*6),
start = decimal_date(as.POSIXct("2019-04-15 08:00:00")),
ts.frequency = 12*6*52)
## preds
tbats_2 = tbats(ts_kmc_train)
preds = predict(tbats_2, h=12*6)
plot(preds, main = "TBATS Forecast")
lines(ts_kmc_test)
#auto arima model
fit = auto.arima(ts_kmc_2, D=4)
tsdisplay(residuals(fit),
lag.max=66*4,
main='(1,1,1) Model Residuals')
#predictions auto arima
fcast <- forecast(fit, h=66)
plot(fcast)
#show decimal date as actual date
print(format(date_decimal(2019.31), "%d-%m-%Y %H"))
# Moving average ----
pf$ma_d = ma(pf$x, order=24)
pf$ma_w = ma(pf$x, order=168)
ggplot() +
geom_line(data = pf, aes(x = datetime, y = x, colour = "Counts")) +
geom_line(data = pf, aes(x = datetime, y = ma_d, colour = "Daily Moving Average")) +
geom_line(data = pf, aes(x = datetime, y = ma_w, colour = "Weekly Moving Average")) +
ylab("Percentage of Free Parking Spaces")
# wtf does this tell me
# Step 3: Decompose Your Data
ma = ts(na.omit(pf$ma_d), frequency=168)
decomp = stl(ma, s.window=24)
deseasonal_fPct <- seasadj(decomp)
plot(decomp)
# Step 4: Stationarity
adf.test(ma, alternative = "stationary")
# Step 5: Autocorrelations and Choosing Model Order
Acf(ma, main='')
Pacf(ma, main='')
# Skip rest of step 5
# ...
# ...
# ...
# Step 6: Fitting an ARIMA model
auto.arima(ma, seasonal=FALSE)
# Step 7: Evaluate and Iterate
fit = auto.arima(ma, seasonal=FALSE)
tsdisplay(residuals(fit),
lag.max=168,
main='(1,1,1) Model Residuals') # Was für ne Nummer muss da rein?
# Wenn man da jetzt was erkennen würde könnte man mit en Zahlen in der Klammer spielen
# Ich erkenne aber nichts
fcast <- forecast(fit, h=24)
plot(fcast)
# Mit Parametern spielen
pred_length = 24
fit2 = arima(ts(ma[-c((276-pred_length):276)]),
order=c(1,1,1))
tsdisplay(residuals(fit2),
lag.max=24,
main='(1,1,1) Model Residuals')
fcast2 <- forecast(fit2, h=pred_length)
plot(fcast2)
lines(ts(ma))
abline(v=seq(0,300,168), col="red") # Täglich
abline(v=seq(0,300,24), col="green") # Stündlich
##################################################
# With seasonality
pred_length = 1440
fit_w_seasonality = auto.arima(ts(deseasonal_fPct[-c((15300-pred_length):15300)]),
seasonal=TRUE)
seas_fcast <- forecast(fit_w_seasonality, h=pred_length)
plot(seas_fcast,
xlim=range(12000:15300))
lines(ts(deseasonal_fPct))
graphics.off()
# Plot parking density over one day
# Choose date
example_date1 = "2019-03-25"
data_plot = parking_filtered %>%
filter(parking_filtered$date.x == example_date1)
ggplot(data_plot) +
geom_line(aes(x=datetime, y=freePercent))
# Plot parking density over two days
# Choose dates
example_date2 = "2019-03-30"
data_plot = parking_filtered %>%
filter(date.x >= example_date1 & date.x <= example_date2)
ggplot(data_plot) +
geom_line(aes(x=datetime, y=freePercent))
# Separate into training and testing data
train_start = "2019-03-25"
train_end = "2019-03-30"
test_start = "2019-04-01"
test_end = "2019-04-01"
data_train = parking_filtered %>%
filter(date.x >= train_start & date.x <= train_end)
data_train = data_train[,-2]
data_test = parking_filtered %>%
filter(date.x >= test_start & date.x <= test_end)
data_test = data_test[,-2]
# Forecast model via "forecast" package
ts = ts(data = data_train, frequency = 600)
msts = msts(data_train, seasonal.periods=c(6, 600))
cycle(msts)
plot.ts(msts)
model = auto.arima(msts[,2], seasonal = T)
#Play with SpatioTemporal Package
obs = data.frame(paste(DF_final_small$date.x, DF_final_small$time, sep = " "), paste(DF_final_small$lon , DF_final_small$lat, sep = " "), DF_final_small$freePercent)
colnames(obs) = c("date", "ID", "obs")
createSTdata(obs)
model_st_1 = create.data.matrix(obs = DF_final_small$freePercent, date = DF_final_small$date.x , ID = c(DF_final_small$lon, DF_final_small$lat), subset = NULL)
createSTdata(DF_final_small)
# K Means
locations = data.frame(DF_final_small[!duplicated(DF_final_small[,c("SourceElementKey","lon","lat")]),][,c(1,2,3)])
KMean = kmeans(locations[,2:3], 10)
locations$cluster = KMean$cluster
ggplot(data = locations, )