forked from MariekeDirk/ML_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Time_Slice.R
More file actions
74 lines (51 loc) · 2.38 KB
/
Test_Time_Slice.R
File metadata and controls
74 lines (51 loc) · 2.38 KB
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
#####################################################################################
# #
# #
# Here I test the function create Time Slices #
# #
# #
#####################################################################################
# First empty environment and install relevant packages
rm(list=ls())
#install.packages("caret")
#install.packages("pls")
#install.packages("pbkrtest")
# call libraries and dataset
library(caret)
library(ggplot2)
library(pls)
data(economics)
# Make TimeSlices index
timeSlices <- createTimeSlices(1:nrow(economics), initialWindow = 36, horizon = 12, fixedWindow = TRUE)
# Inspect time series data
str(timeSlices,max.level = 1)
# Seperate train and test slices
trainSlices <- timeSlices[[1]]
testSlices <- timeSlices[[2]]
# Now train a pls model on the economics data with function (unemployment ~ personal
# consumption expenditures + population + personal savings rate)
# You train this model on the first slice only
plsFitTime <- train(unemploy ~ pce + pop + psavert,
data = economics[trainSlices[[1]],],
method = "pls",
preProc = c("center", "scale"))
# Predict based on the first test slice
pred <- predict(plsFitTime,economics[testSlices[[1]],])
# Plot target variable and predicted target variable for first test slice
true <- economics$unemploy[testSlices[[1]]]
plot(true, col = "red", ylab = "true (red) , pred (blue)", ylim = range(c(pred,true)))
points(pred, col = "blue")
# To do this for all the time slices:
# (CODE RUNS FOR VERY LONG TIME)
for(i in 1:length(trainSlices)){
plsFitTime <- train(unemploy ~ pce + pop + psavert,
data = economics[trainSlices[[i]],],
method = "pls",
preProc = c("center", "scale"))
pred <- predict(plsFitTime,economics[testSlices[[i]],])
true <- economics$unemploy[testSlices[[i]]]
plot(true, col = "red", ylab = "true (red) , pred (blue)",
main = i, ylim = range(c(pred,true)))
points(pred, col = "blue")
}
#