-
Notifications
You must be signed in to change notification settings - Fork 10
/
Carls_Time_Series.Rmd
97 lines (70 loc) · 2.16 KB
/
Carls_Time_Series.Rmd
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
---
title: "Carl's Time Series Analysis"
author: "Carl"
date: "April 10, 2016"
output: html_document
---
```{r}
library('xts')
library('forecast')
library('rgl')
library('TSclust')
library('corrplot')
oil = read.csv("oil.csv")
colnames(oil) = c("date","oil")
oil$date = as.Date(oil$date, format = "%d/%m/%Y")
oil$date = as.POSIXct(oil$date)
oil$oil = as.numeric(as.character(oil$oil))
oil = xts(oil$oil,oil$date)
unemp = read.csv("unemp.csv")
unemp$AgeGroup == "15 years and over"
colnames(unemp)
unemp.f = unemp[unemp$AgeGroup == "15 years and over" ,]
unemp.f= unemp.f[unemp.f$Sex == "Both sexes",]
colnames(unemp)
provinces = colnames(unemp)[c(4:14)]
unemp = unemp.f[,c("When",provinces)]
colnames(unemp) = c("date",provinces)
class(unemp$date)
unemp$date = as.Date(unemp$date, format = "%m/%d/%Y")
unemp$date = as.POSIXct(unemp$date)
unemp[,provinces] = apply(unemp[,provinces],2,function(x) as.numeric(as.character(x)))
unemp = xts(unemp[,provinces],unemp$date)
colnames(unemp) = provinces
for(i in 1:length(provinces)){
if(i == 1){
data = merge.xts(to.monthly(oil)[,4],to.monthly(unemp[,provinces[i]])[,4])
}
if(i > 1){
data = merge.xts(data,to.monthly(unemp[,provinces[i]])[,4])
}
}
colnames(data) = c("oil", provinces)
data = na.omit(data)
plot(data[,"Alberta"])
cor(data[,"Alberta"],data[,"Ontario"])
M = cor(data[,provinces])
corrplot(M)
cor(data[130:193,"Alberta"],data[130:193,"Ontario"])
M = cor(data[,provinces])
corrplot(M)
tsdist = diss(t(data[,provinces]) , "ACF", p=0.05)
hc <- hclust(tsdist)
plot(hc)
fit4 = stl(data[,"Alberta"], s.window= "periodic")
plot(fit4$time.series[,"trend"], ylim=c(0,10), col = "red", main = "Comparison", xlab = c("time"), ylab = c("Unemployment"))
fit5 = stl(data[,"Ontario"], s.window= "periodic")
lines(fit5$time.series[,"trend"], col = "blue")
legend("bottomright",c("Ontario","Alberta"),lty=c(1,1),lwd=c(2.5,2.5),col=c("blue","red"))
library('vars')
model = VAR(data[,c("oil","Alberta")])
plot(model)
pred = predict(model, n.ahead =20)
plot(pred)
library('gam')
library('nlme')
library('mgcv')
gam.object = gam(Alberta ~ oil, data=data[,c("oil","Alberta")], na=na.gam.replace)
summary(gam.object)
plot(gam.object)
```