1. 식별
## ACF, PACF 그리기
par(oma=c(0,0,5,0))
par(mfrow = c(1,2))
acf(diff(log_df_ts),lag=36, main="ACF of diff(log(value))", ylab="", xlab="Lag")
pacf(diff(log_df_ts), lag=36, main="PACF of diff(log(value))", ylab="", xlab="Lag")
mtext("<Figure 1> ACF, PACF of Starbucks's Stock(2000.01~2017.08)",outer=TRUE,cex=2)
* 모두 시차에서 절단된 것으로 판단하고 ARIMA(0,1,0)
fit1 <- arima(log_df_ts, c(0,1,0))
* 계절성이 있는 모형으로 판단하고 계절 차분 후 다시 판단
## 계절차분 후 ACF, PACF 그리기
acf(diff(diff(log_df_ts),12), lag=36, main="ACF of diff(diff(log(value)),12)", ylab="", xlab="Lag")
pacf(diff(diff(log_df_ts),12), lag=36, main="PACF of diff(diff(log(value)),12)", ylab="", xlab="Lag")
mtext("<Figure 2> ACF, PACF of Starbucks's Stock(2000.01~2017.08)",outer=TRUE,cex=2)
* 자기상관함수는 꾸준히 감소하고, 편자기상관함수는 시차 6에서 절단된 것으로 판단하고 ARIMA(0,1,0)(0,1,0)12
fit2 <- arima(log_df_ts, c(0,1,0), seasonal=list(order=c(0,1,1), period = 12))
fit3 <- auto.arima(log_df_ts)
fit3 <- arima(log_df_ts, c(2,1,2), seasonal=list(order=c(2,0,1), period = 12), xreg=1:length(log_df_ts))
2. 검진
## 잔차도표, 잔차의 자기상관함수 그리기
par(oma=c(0,0,5,0))
par(mfrow = c(2,3))
plot(fit1$residuals, main="fit1$residuals", ylab="")
plot(fit2$residuals, main="fit2$residuals", ylab="")
plot(fit3$residuals, main="fit3$residuals", ylab="")
acf(fit1$residuals,lag=36)
acf(fit2$residuals,lag=36)
acf(fit3$residuals,lag=36)
mtext("<Figure 3> Residuals is independent?",outer=TRUE,cex=2)
* 잔차의 자기상관함수는 시차 6에서 조금 큰 것으로 판단됨
* Box Ljung 검정을 통해 세 모형 모두 잔차의 독립성 확인
## Box Ljung 검정
Box.test(fit1$residuals, type="Ljung-Box")
Box.test(fit2$residuals, type="Ljung-Box")
Box.test(fit3$residuals, type="Ljung-Box")
* 모형의 적합도는 AIC를 통해 값이 작은 모형을 선택
## 모형 적합도 확인
fit1
fit2
fit3
3. 예측
## 적합한 모형의 예측하기
pred1 <- predict(fit1, n.ahead = 60)
pred2 <- predict(fit2, n.ahead = 60)
pred3 <- predict(fit3, 60, newxreg=((length(log_df_ts)+1):(length(log_df_ts)+120)))
## 예측값 그래프 그리기
par(oma=c(0,0,5,0))
par(mfrow = c(1,3))
ts.plot(df_ts,2.718282^pred1$pred, log = "y", lty = c(1,3), main="Predict on fit1 model")
ts.plot(df_ts,2.718282^pred2$pred, log = "y", lty = c(1,3), main="Predict on fit2 model")
ts.plot(df_ts,2.718282^pred3$pred, lty=c(1,3), main="Predict on fit3 model")
mtext("<Figure 4> Plotting predicted models",outer=TRUE,cex=2)
* 모형을 선택하는 것이 아주 어려운거 같습니다. 아직, 내공이 부족합니다. 좀더 보완하겠습니다.
참고 홈페이지 : http://www.stat.pitt.edu/stoffer/tsa2/Rissues.htm
'시계열 분석' 카테고리의 다른 글
16. [R] 지수평활법 예제 (0) | 2017.09.26 |
---|---|
14. [R] 시계열분석 정상성 확인 예제 : 비정상적 분산, Box-Cox 변환 (2) | 2017.09.14 |
13. [R] 시계열분석 정상성 확인 예제 : 비정상적 분산, 로그변환 (0) | 2017.09.13 |
12. [R] 시계열분석 정상성 확인 예제 : 비정상적 평균 (13) | 2017.09.11 |
11. 개입모형 (0) | 2017.09.09 |