-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
70 lines (48 loc) · 1.9 KB
/
script.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
#-0.31 0.41 0.51 -0.20 0.61 0.20 0.61 -0.30 -0.10 -0.20 -0.51 0.41
#-0.51 0.61 -0.20 0.10 -0.10 0.30 0.00 0.20 -0.30 0.00 -0.10 0.81
#-0.60 0.40 0.50 0.10 -0.10 0.00 0.20 0.10 -0.10 0.10 0.10 0.60
#-0.20 0.60 0.59 0.00 0.00 0.10 0.20 0.10 0.20 0.00 0.20 0.19
#-0.10 0.68 0.58 -0.19 0.00 -0.19 0.39 0.38 0.10 0.00 0.10 0.29
#-0.48 0.57 0.48 -0.47 0.38 0.09 0.47 0.00 0.00 -0.19 0.19 0.38
#-0.56 0.47 0.28 -0.19 -0.09 0.28 0.28 0.00 0.00 -0.28 0.00 0.00
#-1.03 0.85 0.47 0.00 0.09 -0.09 0.19 0.00 -0.19 0.00 0.09 -0.09
#-0.84 0.38 0.75 -0.37 0.28 0.09 0.28 0.00 0.09 0.19 0.09 0.74
#-0.64 0.65 0.18 0.00 -0.18 0.18 0.37 0.09 0.09 0.00
data = scan()
germaninfl = ts(data,start=2008,frequency = 12)
plot(germaninfl,ylab='Inflation rate',xlab='Year',main='Monthly Inflation rate of Germany')
# Old Decomposition method
plot(decompose(germaninfl))
# New decomposition method
plot(stl(germaninfl,s.window = 7))
# Decomposition forecasting
plot(stlf(germaninfl,method='ets'))
autoplot(stlf(germaninfl,method='ets'))
# Seasonal ARIMA
auto.arima(germaninfl,stepwise = T,
approximation = F,trace = T)
germaninflarima = auto.arima(germaninfl,stepwise = T,
approximation = F,trace = T)
# Forecast
forec = forecast(germaninflarima)
autoplot(forec)
#Exponential Smoothing
ets(germaninfl)
germaninflets = ets(germaninfl)
plot(forecast(germaninflets,h=60))
# Time Series Cross Validation
forecastets = function(x,h){
forecast(ets(x),h=h)
}
forecastarima = function(x,h){
forecast(auto.arima(x),
stepwise=T,
approximation=F,
h=h)
}
etserror = tsCV(germaninfl,forecastets,h=1)
arimaerror = tsCV(germaninfl,forecastarima,h=1)
etsmse = mean(etserror^2,na.rm=TRUE)
arimamse = mean(arimaerror^2,na.rm=TRUE)
names = c('Exponential Smoothing','ARIMA')
barplot(c(etsmse,arimamse),names.arg = names,xlab='Models',ylab='MSE',main='Model Accuracy for different models')