-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_seriers.Rmd
241 lines (187 loc) · 4.03 KB
/
time_seriers.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
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
---
title: "time seriers"
output: html_document
---
```{r,echo=FALSE}
library(TSA)
x=read.table(file="C:\\Users\\TSR\\Desktop\\共享資料\\4444.txt")
cc=x$V3
cc
###原型
plot.ts(cc,type="o")
acf(cc)
pacf(cc)
####
```
####從acf上看起來每4筆有季節效應,所以先處理季節效應
```{r}
###處理季節效應
cc1=diff(log(cc),lag=4)
plot.ts(cc1)
acf(cc1,lag.max=30)
pacf(cc1,lag.max=30)
eacf(cc1)
```
####acf看起來像是指數遞減,pacf有明顯的5根
####所以先從(5,0,0)x(0,1,0)開始嘗試
```{r}
mse=list()
###ar5
m1=arima(cc,order=c(5,0,0),seasonal=list(order=c(0,1,0),period=4))
m1
fitted(m1)
plot(cc,col="red",type="l")
lines(fitted(m1),col="blue")
p1=c(170,170)
p2=c(18,20)
t=c("red:原有數列","blue:fit數列")
text(p1,p2,labels=t,col = c("red","blue"))
plot(rstandard(m1))
abline(h=c(2,-2))
qqnorm(rstandard(m1))
qqline(rstandard(m1))
acf(rstandard(m1))
pacf(rstandard(m1))
plot(m1,n.ahead=20,type='b')
mse[1][1]=sum((unlist(m1[8]))^2)/length(cc)
```
####殘差都在容許的範圍之內
####但是ar3的係數可能為0,所以試試看(2,0,0)X(0,1,0)
```{r}
####
m1=arima(cc,order=c(2,0,0),seasonal=list(order=c(0,1,0),period=4))
m1
plot.ts(cc,col="red")
lines(fitted(m1),col="blue")
text(p1,p2,labels=t,col = c("red","blue"))
plot(rstandard(m1))
abline(h=c(2,-2))
qqnorm(rstandard(m1))
qqline(rstandard(m1))
acf(rstandard(m1))
pacf(rstandard(m1))
plot(m1,n.ahead=4,type='b')
mse[[1]][2]=sum((unlist(m1[8]))^2)/length(cc)
```
####從殘差上看起來,估計的蠻差的,故仍選擇(5,0,0)X(0,1,0)
#分段#
####由於此時間序列前半段有上升趨勢,後半段平穩,故考慮將前後分開討論
```{r}
p1=c(80,80)
p2=c(18,20)
cc=x$V3
cc1=cc[1:95]
cc2=cc[96:196]
plot.ts(cc1,type="o")
acf(cc1)
pacf(cc1)
```
####從acf看起來每隔4個時期有季節效應
####故先處理季節效應
```{r}
cc1=diff(cc1,lag=4)
plot.ts(cc1)
acf(cc1)
pacf(cc1)
```
####acf 看起來像指數遞減,但是pacf第一根突出所以猜測是ar1
####但是第四根也凸出,所以猜測在季節上有ar1
```{r}
cc=x$V3
cc1=cc[1:95]
cc2=cc[96:196]
##季節性
m1=arima(cc1,order=c(1,0,0),seasonal=list(order=c(1,1,0),period=4))
m1
plot.ts(cc1,col="red")
lines(fitted(m1),col="blue")
text(p1,p2,labels=t,col = c("red","blue"))
plot(rstandard(m1))
abline(h=c(2,-2))
qqnorm(rstandard(m1))
qqline(rstandard(m1))
acf(rstandard(m1))
pacf(rstandard(m1))
plot(m1,n.ahead=20,type='b')
mse[2][1]=sum((unlist(m1[8]))^2)/length(cc1)
```
####殘差大致都在可行範圍之內
####所以判定 (1,0,0)X(1,1,0) 為一個可行的模型
####下面在利用上面做出的(5,0,0)X(0,1,0)做預測
```{r}
cc=x$V3
cc1=cc[1:95]
cc2=cc[96:196]
##原模型
m1=arima(cc1,order=c(5,0,0),seasonal=list(order=c(0,1,0),period=4) )
m1
plot.ts(cc1,col="red")
lines(fitted(m1),col="blue")
text(p1,p2,labels=t,col = c("red","blue"))
plot(rstandard(m1))
abline(h=c(2,-2))
qqnorm(rstandard(m1))
qqline(rstandard(m1))
acf(rstandard(m1))
pacf(rstandard(m1))
plot(m1,n.ahead=20,type='b')
mse[[2]][2]=sum((unlist(m1[8]))^2)/length(cc1)
```
##後段##
```{r}
p1=c(90,90)
p2=c(26,27)
cc2=cc[96:196]
plot.ts(cc2,type="o")
acf(cc2)
pacf(cc2)
```
####看起來每4期有相關,先處理季節效應
```{r}
cc2=diff(cc2,lag=4)
plot.ts(cc2)
acf(cc2)
pacf(cc2)
```
####因acf還是很多呈現相關,判定為非平穩,故再diff一次
```{r}
cc2=diff(cc2)
plot.ts(cc2)
acf(cc2)
pacf(cc2)
```
差分後的acf pacf都在4有一根,所以季節性的模型為(1,1,1) s=4 ,acf pacf 的第一根也是突出,故用(1,1,1)配,整體為(1,1,1)x(1,1,1) s=4
```{r}
cc1=cc[1:95]
cc2=cc[96:196]
m1=arima(cc2,order=c(1,1,1),seasonal=list(order=c(1,1,1),period=4))
plot.ts(cc2,col="red")
lines(fitted(m1),col="blue")
text(p1,p2,labels=t,col = c("red","blue"))
plot(rstandard(m1))
abline(h=c(2,-2))
qqnorm(rstandard(m1))
qqline(rstandard(m1))
acf(rstandard(m1))
pacf(rstandard(m1))
plot(m1,n.ahead=20,type='b')
mse[3][1]=sum((unlist(m1[8]))^2)/length(cc2)
```
####再度利用原本(5,0,0)x(0,1,0) s=4做預測
```{r}
##原模型
m1=arima(cc2,order=c(5,0,0),seasonal=list(order=c(0,1,0),period=4))
m1
plot.ts(cc2,col="red")
lines(fitted(m1),col="blue")
text(p1,p2,labels=t,col = c("red","blue"))
plot(rstandard(m1))
abline(h=c(2,-2))
qqnorm(rstandard(m1))
qqline(rstandard(m1))
acf(rstandard(m1))
pacf(rstandard(m1))
plot(m1,n.ahead=20,type='b')
mse[[3]][3]=sum((unlist(m1[8]))^2)/length(cc2)
mse
```