-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_DataAnalysis_KMean_01.R
71 lines (47 loc) · 1.89 KB
/
03_DataAnalysis_KMean_01.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
# Description ---------------
# In this script
# - Predict KMeans Cluster as Time Series without Features, per cluster
# Setup ----------------------------------------------
library(data.table)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(forecast)
library(tseries)
rm(list=ls())
graphics.off()
load("../Schramm, Cornelius - 02_Business_Analytics_Data/df_set_04_Sort4Clust_Clust.RData")
#only 8-17 hour, main hours (all obs. have data)
DF_KMC = DF_KMclust[DF_KMclust$hour < 22, ]
#choose cluster
DF_KMC_1 = DF_KMC[DF_KMC$cluster == 3,]
#features raus
DF_KMC_1 = DF_KMC_1[,2:4]
#Time series object creation
#msts object creation
DF_KMC_1$datetime = paste(DF_KMC_1$date, DF_KMC_1$hour)
DF_KMC_1$datetime = as.POSIXct(DF_KMC_1$datetime, format="%Y-%m-%d %H")
ts_kmc = msts(DF_KMC_1$FreeSpots, seasonal.periods = c(12,12*6),
start = decimal_date(as.POSIXct("2019-03-25 08:00:00")),
ts.frequency = 12*6*52)
#tbats model smoothing
tbats = tbats(ts_kmc)
plot(tbats, main="Multiple Season Decomposition")
# 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(10,10*6),
start = decimal_date(as.POSIXct("2019-03-25 08:00:00")),
ts.frequency = 10*6*52)
ts_kmc_test = msts(parking_filtered_test$FreeSpots, seasonal.periods = c(10,10*6),
start = decimal_date(as.POSIXct("2019-04-15 08:00:00")),
ts.frequency = 10*6*52)
## preds
tbats_2 = tbats(ts_kmc_train)
preds = predict(tbats_2, h=10*6)
plot(preds, main = "TBATS Forecast")
lines(ts_kmc_test)