rm(list=ls())
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
library(TTR)
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(Hmisc)
## Warning: package 'Hmisc' was built under R version 4.3.2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
library(urca)
## Warning: package 'urca' was built under R version 4.3.3
library(forecast)
library(Metrics)
## Warning: package 'Metrics' was built under R version 4.3.2
##
## Attaching package: 'Metrics'
## The following object is masked from 'package:forecast':
##
## accuracy
library(car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
library(lmtest)
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(tsutils)
## Warning: package 'tsutils' was built under R version 4.3.2
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2
Revenue<- read_excel("~/Kim(1)/Kim NEU/HK6 NEU/Phân tích chuỗi tg và dự báo/Bài tập lớn/dữ liệu quý Time series.xlsx")
attach(Revenue)
save(Revenue, file = "dữ_liệu_quý_Time_series")
View(Revenue)
#PHẦN 1: DU BAO DOANH THU
##Tao bien xu the
ts (Revenue , start=c(2009,1), frequency = 4)
## Date DT TT
## 2009 Q1 1 343.427 NA
## 2009 Q2 2 361.554 NA
## 2009 Q3 3 316.209 NA
## 2009 Q4 4 451.208 NA
## 2010 Q1 5 523.961 52.568377
## 2010 Q2 6 609.576 68.598881
## 2010 Q3 7 448.162 41.729679
## 2010 Q4 8 457.013 1.286546
## 2011 Q1 9 810.906 54.764572
## 2011 Q2 10 600.672 -1.460687
## 2011 Q3 11 487.688 8.819579
## 2011 Q4 12 240.626 -47.348106
## 2012 Q1 13 321.257 -60.382954
## 2012 Q2 14 345.149 -42.539522
## 2012 Q3 15 249.646 -48.810305
## 2012 Q4 16 507.206 110.786033
## 2013 Q1 17 378.233 17.735333
## 2013 Q2 18 412.743 19.584006
## 2013 Q3 19 262.281 5.061167
## 2013 Q4 20 86.888 -82.869288
## 2014 Q1 21 658.953 74.218802
## 2014 Q2 22 672.887 63.028083
## 2014 Q3 23 218.219 -16.799539
## 2014 Q4 24 566.152 551.588252
## 2015 Q1 25 418.449 -36.497899
## 2015 Q2 26 432.960 -35.656358
## 2015 Q3 27 469.379 115.095386
## 2015 Q4 28 510.756 -9.784651
## 2016 Q1 29 389.450 -6.930116
## 2016 Q2 30 399.366 -7.759146
## 2016 Q3 31 459.211 -2.166266
## 2016 Q4 32 419.944 -17.779918
## 2017 Q1 33 168.655 -56.694056
## 2017 Q2 34 216.458 -45.799592
## 2017 Q3 35 349.310 -23.932571
## 2017 Q4 36 979.554 133.258244
## 2018 Q1 37 630.406 273.784353
## 2018 Q2 38 159.258 -26.425450
## 2018 Q3 39 206.411 -40.908935
## 2018 Q4 40 110.082 -88.762028
## 2019 Q1 41 262.872 -58.301158
## 2019 Q2 42 469.541 194.830401
## 2019 Q3 43 231.399 12.105944
## 2019 Q4 44 931.033 745.763158
## 2020 Q1 45 415.806 58.178125
## 2020 Q2 46 75.731 -83.871270
## 2020 Q3 47 109.785 -52.555975
## 2020 Q4 48 146.217 -84.295186
## 2021 Q1 49 227.444 -45.300453
## 2021 Q2 50 389.713 414.601682
## 2021 Q3 51 102.967 -6.210320
## 2021 Q4 52 493.201 237.307563
## 2022 Q1 53 100.000 -56.033133
## 2022 Q2 54 201.751 -48.230878
## 2022 Q3 55 166.629 61.827576
## 2022 Q4 56 145.015 -70.597180
## 2023 Q1 57 46.223 -53.777000
## 2023 Q2 58 641.475 217.953814
## 2023 Q3 59 44.466 -73.314369
## 2023 Q4 60 55.291 -61.872220
DT <- ts(DT, start=c(2009,1), frequency = 4)
# do thi
plot.ts(DT)
#tao bien xu the thoi gian
time <- seq_along(DT)
summary(time)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 15.75 30.50 30.50 45.25 60.00
#mô hình tuyến tính lin-lin
reg0<-lm(DT~time)
summary(reg0)
##
## Call:
## lm(formula = DT ~ time)
##
## Residuals:
## Min 1Q Median 3Q Max
## -326.39 -158.24 -42.14 100.81 639.67
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 505.030 52.133 9.687 9.94e-14 ***
## time -4.587 1.486 -3.086 0.00311 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 199.4 on 58 degrees of freedom
## Multiple R-squared: 0.1411, Adjusted R-squared: 0.1263
## F-statistic: 9.525 on 1 and 58 DF, p-value: 0.003105
rmse(DT,fitted(reg0))
## [1] 196.0406
mape(DT,fitted(reg0))
## [1] 0.7383309
#MAPE năm 2023
mape(DT[57:60],fitted(reg0)[57:60])
## [1] 3.080799
# vẽ đồ thị
Dataf0<-ts(fitted(reg0), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf0,col="red")
#dự báo( 4 quý)
fc0<- forecast(Dataf0,h=4)
print(fc0)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 225.1977 225.1977 225.1977 225.1977 225.1977
## 2024 Q2 220.6103 220.6103 220.6103 220.6103 220.6103
## 2024 Q3 216.0228 216.0228 216.0228 216.0228 216.0228
## 2024 Q4 211.4354 211.4354 211.4354 211.4354 211.4354
#MH1: chay mo hinh linear - log
reg1<-lm(DT~log(time))
summary(reg1)
##
## Call:
## lm(formula = DT ~ log(time))
##
## Residuals:
## Min 1Q Median 3Q Max
## -287.63 -164.68 -16.04 112.87 642.36
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 564.70 97.90 5.768 3.28e-07 ***
## log(time) -63.49 29.96 -2.119 0.0384 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 207.3 on 58 degrees of freedom
## Multiple R-squared: 0.07187, Adjusted R-squared: 0.05587
## F-statistic: 4.492 on 1 and 58 DF, p-value: 0.03835
#tinh rmse : sai số giữa DL thực và DL dự báo
rmse(DT,fitted(reg1))
## [1] 203.7831
mape(DT,fitted(reg1))
## [1] 0.8527425
mape(DT[57:60],fitted(reg1)[57:60])
## [1] 4.143864
#ve do thi
Dataf1<-ts(fitted(reg1), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf1,col="red")
#dự báo
fc1<- forecast(Dataf1,h=4)
print(fc1)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 304.4941 300.1743 308.8140 297.8875 311.1008
## 2024 Q2 304.2748 297.9656 310.5840 294.6257 313.9239
## 2024 Q3 304.0993 296.1587 312.0398 291.9553 316.2433
## 2024 Q4 303.9589 294.5753 313.3425 289.6079 318.3099
#MH2: mo hinh log - log
reg2<-lm(log(DT)~log(time))
summary(reg2)
##
## Call:
## lm(formula = log(DT) ~ log(time))
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.60390 -0.44076 0.05593 0.46813 1.34743
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.65299 0.32217 20.650 < 2e-16 ***
## log(time) -0.30763 0.09858 -3.121 0.00281 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6821 on 58 degrees of freedom
## Multiple R-squared: 0.1438, Adjusted R-squared: 0.129
## F-statistic: 9.739 on 1 and 58 DF, p-value: 0.002811
#tinh rmse
rmse(DT,exp(fitted(reg2)))
## [1] 223.0933
mape(DT,exp(fitted(reg2)))
## [1] 0.6636931
mape(DT[57:60],fitted(reg2)[57:60])
## [1] 0.9138986
#ve do thi
Dataf2<-ts(exp(fitted(reg2)), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf2,col="green")
#dự báo
fc2<- forecast(Dataf2,h=4)
print(fc2)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 219.9800 211.2014 228.7586 206.5543 233.4057
## 2024 Q2 219.9465 207.8264 232.0667 201.4103 238.4827
## 2024 Q3 219.9168 205.1932 234.6405 197.3989 242.4348
## 2024 Q4 219.8906 202.9574 236.8237 193.9936 245.7875
#MH2': mo hinh log - linear
reg22<-lm(log(DT)~(time))
summary(reg22)
##
## Call:
## lm(formula = log(DT) ~ (time))
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.44610 -0.45629 0.02772 0.38743 1.43953
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.339019 0.165579 38.284 < 2e-16 ***
## time -0.021415 0.004721 -4.536 2.93e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6333 on 58 degrees of freedom
## Multiple R-squared: 0.2619, Adjusted R-squared: 0.2491
## F-statistic: 20.58 on 1 and 58 DF, p-value: 2.929e-05
#tinh rmse
rmse(DT,exp(fitted(reg22)))
## [1] 209.2866
mape(DT,exp(fitted(reg22)))
## [1] 0.5619901
#MAPE 2023
mape(DT[57:60],fitted(reg22)[57:60])
## [1] 0.918942
#ve do thi
Dataf22<-ts(exp(fitted(reg22)), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf22,col="green")
#dự báo
fc22<- forecast(Dataf22,h=4)
print(fc22)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 153.3486 153.3486 153.3486 153.3486 153.3486
## 2024 Q2 150.0996 150.0996 150.0996 150.0996 150.0996
## 2024 Q3 146.9194 146.9194 146.9194 146.9194 146.9194
## 2024 Q4 143.8066 143.8066 143.8066 143.8066 143.8066
# Tao bien gia mua vu theo quy
s1 <- c(rep(c(1,0,0,0), 15))
s2 <- c(rep(c(0,1,0,0), 15))
s3 <- c(rep(c(0,0,1,0), 15))
s4 <- c(rep(c(0,0,0,1), 15))
# Hồi quy với biến giả mùa vụ
# Xu the thoi gian tuyen tinh + mua vu, dang cong
reg3 <- lm(DT ~ time + s2 + s3 + s4)
summary(reg3)
##
## Call:
## lm(formula = DT ~ time + s2 + s3 + s4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -374.85 -128.61 -36.62 102.52 591.23
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 512.798 66.457 7.716 2.53e-10 ***
## time -4.588 1.473 -3.115 0.00292 **
## s2 24.108 72.014 0.335 0.73908
## s3 -95.775 72.059 -1.329 0.18930
## s4 40.708 72.134 0.564 0.57482
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 197.2 on 55 degrees of freedom
## Multiple R-squared: 0.2035, Adjusted R-squared: 0.1456
## F-statistic: 3.513 on 4 and 55 DF, p-value: 0.01265
fitted(reg3)
## 1 2 3 4 5 6 7 8
## 508.2098 527.7293 403.2578 535.1527 489.8564 509.3759 384.9044 516.7994
## 9 10 11 12 13 14 15 16
## 471.5030 491.0225 366.5510 498.4460 453.1497 472.6691 348.1977 480.0926
## 17 18 19 20 21 22 23 24
## 434.7963 454.3157 329.8443 461.7392 416.4429 435.9624 311.4909 443.3858
## 25 26 27 28 29 30 31 32
## 398.0895 417.6090 293.1375 425.0324 379.7361 399.2556 274.7841 406.6791
## 33 34 35 36 37 38 39 40
## 361.3828 380.9022 256.4308 388.3257 343.0294 362.5488 238.0774 369.9723
## 41 42 43 44 45 46 47 48
## 324.6760 344.1955 219.7240 351.6189 306.3226 325.8421 201.3706 333.2655
## 49 50 51 52 53 54 55 56
## 287.9692 307.4887 183.0172 314.9122 269.6158 289.1353 164.6638 296.5588
## 57 58 59 60
## 251.2625 270.7819 146.3105 278.2054
rmse(DT,fitted(reg3))
## [1] 188.783
mape(DT,fitted(lm(reg3)))
## [1] 0.7055734
#ve do thi
Dataf3<-ts(fitted(reg3), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf3,col="green")
fc3<- forecast(Dataf3,h=4)
print(fc3)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 232.9091 232.9091 232.9091 232.9091 232.9091
## 2024 Q2 252.4285 252.4285 252.4285 252.4285 252.4285
## 2024 Q3 127.9571 127.9571 127.9571 127.9571 127.9571
## 2024 Q4 259.8520 259.8520 259.8520 259.8520 259.8520
# Xu the thoi gian tuyen tinh + mua vu, dang nhan
t1 <- time*s2
t2 <- time*s3
t3 <- time*s4
reg4 <- lm(DT ~ time + t1 + t2 + t3)
summary(reg4)
##
## Call:
## lm(formula = DT ~ time + t1 + t2 + t3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -359.93 -137.67 -25.38 94.50 579.94
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 505.811 51.308 9.858 9.28e-14 ***
## time -5.037 1.989 -2.533 0.0142 *
## t1 1.549 2.095 0.739 0.4630
## t2 -1.984 2.071 -0.958 0.3421
## t3 2.087 2.048 1.019 0.3125
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 196.1 on 55 degrees of freedom
## Multiple R-squared: 0.2119, Adjusted R-squared: 0.1546
## F-statistic: 3.698 on 4 and 55 DF, p-value: 0.009766
#tinh rmse
rmse(DT,fitted(reg4))
## [1] 187.778
mape(DT,fitted(reg4))
## [1] 0.6905081
#ve do thi
Dataf4<-ts(fitted(reg4), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf4,col="green")
fc4<- forecast(Dataf4,h=4)
print(fc4)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 196.54039 187.73375 205.34703 183.07179 210.00898
## 2024 Q2 281.31725 268.71190 293.92260 262.03903 300.59547
## 2024 Q3 69.38491 66.27588 72.49393 64.63007 74.13975
## 2024 Q4 306.66078 292.91983 320.40173 285.64581 327.67575
#MH5: xu the tuyen tinh thoi gian + vu mua dang hon hop
reg5<- lm(DT ~ time + time*s2 + time*s3 + time*s4)
summary(reg5)
##
## Call:
## lm(formula = DT ~ time + time * s2 + time * s3 + time * s4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -349.10 -116.85 -17.99 102.62 582.64
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 582.122 100.868 5.771 4.41e-07 ***
## time -6.979 2.988 -2.336 0.0234 *
## s2 -83.938 144.483 -0.581 0.5638
## s3 -132.664 146.356 -0.906 0.3689
## s4 -97.289 148.265 -0.656 0.5146
## time:s2 3.681 4.226 0.871 0.3877
## time:s3 1.344 4.226 0.318 0.7517
## time:s4 4.537 4.226 1.074 0.2880
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 200 on 52 degrees of freedom
## Multiple R-squared: 0.2253, Adjusted R-squared: 0.121
## F-statistic: 2.161 on 7 and 52 DF, p-value: 0.05315
#tinh rmse
rmse(DT,fitted(reg5))
## [1] 186.1783
mape(DT,fitted(reg5))
## [1] 0.6833954
#ve do thi
Dataf5<-ts(fitted(reg5), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf5,col="green")
fc5<- forecast(Dataf5,h=4)
print(fc5)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 163.59125 155.53299 171.6495 151.26721 175.9153
## 2024 Q2 286.18189 272.08501 300.2788 264.62257 307.7412
## 2024 Q3 96.27755 91.53507 101.0200 89.02455 103.5306
## 2024 Q4 317.55421 301.91198 333.1964 293.63148 341.4769
#MH6: xu the khong tuyen tinh thoi gian + vu mua dang cong
reg6<- lm(log(DT) ~ time +s2 +s3 +s4)
summary(reg6)
##
## Call:
## lm(formula = log(DT) ~ time + s2 + s3 + s4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.49953 -0.38325 0.05147 0.44658 1.38333
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.35367 0.21340 29.773 < 2e-16 ***
## time -0.02130 0.00473 -4.503 3.53e-05 ***
## s2 0.13885 0.23125 0.600 0.551
## s3 -0.24803 0.23139 -1.072 0.288
## s4 0.03646 0.23163 0.157 0.875
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6332 on 55 degrees of freedom
## Multiple R-squared: 0.3003, Adjusted R-squared: 0.2494
## F-statistic: 5.902 on 4 and 55 DF, p-value: 0.0005026
#tinh rmse
rmse(DT,fitted(reg6))
## [1] 416.9641
mape(DT,fitted(reg6))
## [1] 0.974662
#ve do thi
Dataf6<-ts(exp(fitted(reg6)), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf6,col="green")
fc6<- forecast(Dataf6,h=4)
print(fc6)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 156.6407 156.6371 156.6442 156.6353 156.6461
## 2024 Q2 176.0969 176.0925 176.1013 176.0901 176.1037
## 2024 Q3 117.0229 117.0194 117.0265 117.0175 117.0283
## 2024 Q4 152.1800 152.1743 152.1858 152.1713 152.1888
#MH7: xu the khong tuyen tinh thoi gian + vu mua dang nhan
t1 <- time*s2
t2 <- time*s3
t3 <- time*s4
reg7 <- lm(log(DT) ~ time + t1 + t2 + t3)
summary(reg7)
##
## Call:
## lm(formula = log(DT) ~ time + t1 + t2 + t3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.49455 -0.39590 0.07253 0.39957 1.33230
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.338483 0.163351 38.803 < 2e-16 ***
## time -0.022676 0.006331 -3.582 0.000724 ***
## t1 0.007415 0.006671 1.112 0.271132
## t2 -0.005975 0.006593 -0.906 0.368726
## t3 0.003710 0.006519 0.569 0.571622
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6244 on 55 degrees of freedom
## Multiple R-squared: 0.3195, Adjusted R-squared: 0.27
## F-statistic: 6.456 on 4 and 55 DF, p-value: 0.0002473
#tinh rmse
rmse(DT,fitted(reg7))
## [1] 416.9592
mape(DT,fitted(reg7))
## [1] 0.9747436
#ve do thi
Dataf7<-ts(exp(fitted(reg7)), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf7,col="green")
fc7<- forecast(Dataf7,h=4)
print(fc7)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 141.18139 137.46822 144.89456 135.50259 146.86020
## 2024 Q2 219.54573 213.73662 225.35485 210.66146 228.43000
## 2024 Q3 90.40106 87.65507 93.14704 86.20143 94.60068
## 2024 Q4 167.38533 162.55174 172.21892 159.99299 174.77767
#MH8:xu the khong tuyen tinh thoi gian + vu mua dang hon hop
reg8<- lm(log(DT) ~ time + time*s2 + time*s3 + time*s4)
summary(reg8)
##
## Call:
## lm(formula = log(DT) ~ time + time * s2 + time * s3 + time *
## s4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.44192 -0.38467 0.06489 0.37234 1.32572
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.5487465 0.3219656 20.340 < 2e-16 ***
## time -0.0280260 0.0095372 -2.939 0.00491 **
## s2 -0.3191340 0.4611836 -0.692 0.49202
## s3 -0.2311439 0.4671603 -0.495 0.62284
## s4 -0.3122325 0.4732537 -0.660 0.51232
## time:s2 0.0154905 0.0134876 1.148 0.25602
## time:s3 -0.0001106 0.0134876 -0.008 0.99349
## time:s4 0.0115274 0.0134876 0.855 0.39666
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6384 on 52 degrees of freedom
## Multiple R-squared: 0.3276, Adjusted R-squared: 0.2371
## F-statistic: 3.619 on 7 and 52 DF, p-value: 0.002995
#tinh rmse
rmse(DT,fitted(reg8))
## [1] 416.9561
mape(DT,fitted(reg8))
## [1] 0.97477
#ve do thi
Dataf8<-ts(exp(fitted(reg8)), start=c(2009,1), frequency = 4)
plot.ts(DT)
lines(Dataf8,col="green")
fc8<- forecast(Dataf8,h=4)
print(fc8)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 130.70307 124.93678 136.4694 121.88429 139.5219
## 2024 Q2 226.80216 216.79620 236.8081 211.49936 242.1050
## 2024 Q3 97.41364 93.11596 101.7113 90.84091 103.9864
## 2024 Q4 175.54606 167.80127 183.2908 163.70143 187.3907
#MH9: Holt winter dang cong
DT1 <- DT[1:60]
DT1 <- ts(DT, start=c(2009,1), frequency = 4 )
hw.DT1.a <- HoltWinters(DT1, seasonal = "a")
hw.DT1.a
## Holt-Winters exponential smoothing with trend and additive seasonal component.
##
## Call:
## HoltWinters(x = DT1, seasonal = "a")
##
## Smoothing parameters:
## alpha: 0.2146977
## beta : 0.1124811
## gamma: 0
##
## Coefficients:
## [,1]
## a 162.576801
## b -9.746193
## s1 15.886625
## s2 84.281875
## s3 -90.799000
## s4 -9.369500
plot.ts(DT1)
lines(fitted(hw.DT1.a)[,1],col = "red")
rmse(DT,fitted(hw.DT1.a))
## [1] 338.5038
mape(DT[57:60],fitted(hw.DT1.a)[57:60])
## [1] 6.337859
fc9<- forecast(hw.DT1.a,h=4)
print(fc9)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 168.71723 -122.73274 460.1672 -277.0170 614.4515
## 2024 Q2 227.36629 -72.28169 527.0143 -230.9057 685.6383
## 2024 Q3 42.53922 -266.75705 351.8355 -430.4886 515.5670
## 2024 Q4 114.22253 -206.19591 434.6410 -375.8152 604.2602
#Holt_Winter có mùa vụ dạng cộng (Tăng trưởng)
DT<- ts(DT, start = c(2009,1), frequency = 4)
reg9<-HoltWinters(DT, seasonal = "a")
reg9
## Holt-Winters exponential smoothing with trend and additive seasonal component.
##
## Call:
## HoltWinters(x = DT, seasonal = "a")
##
## Smoothing parameters:
## alpha: 0.2146977
## beta : 0.1124811
## gamma: 0
##
## Coefficients:
## [,1]
## a 162.576801
## b -9.746193
## s1 15.886625
## s2 84.281875
## s3 -90.799000
## s4 -9.369500
rmse(DT, fitted(reg9))
## [1] 338.5038
mape(DT, fitted(reg9))
## [1] 0.9335267
plot.ts(DT)
lines(fitted(reg9)[,1], col= "red")
#MH10: Holt winter dang nhan
hw.DT1.m <- HoltWinters(DT1, seasonal = "m")
hw.DT1.m
## Holt-Winters exponential smoothing with trend and multiplicative seasonal component.
##
## Call:
## HoltWinters(x = DT1, seasonal = "m")
##
## Smoothing parameters:
## alpha: 0.1963786
## beta : 0.1195461
## gamma: 0
##
## Coefficients:
## [,1]
## a 142.4952118
## b -11.6791741
## s1 1.0425133
## s2 1.1718240
## s3 0.7919176
## s4 0.9937450
plot.ts(DT)
lines(fitted(hw.DT1.m)[,1],col = "red")
rmse(DT1,fitted(hw.DT1.m))
## [1] 339.8194
mape(DT1,fitted(hw.DT1.m))
## [1] 0.921955
fc10<- forecast(hw.DT1.m,h=4)
print(fc10)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2024 Q1 136.37746 78.418952 194.3360 47.737569 225.0174
## 2024 Q2 139.60744 46.447355 232.7675 -2.868614 282.0835
## 2024 Q3 85.09764 -5.776524 175.9718 -53.882399 224.0777
## 2024 Q4 95.17942 -31.301724 221.6606 -98.256799 288.6156
#MH11:
#Trung binh truot
DT.ma3 <- ma(DT, order = 3, centre = TRUE)
reg11 <- lm(DT.ma3 ~ time)
summary(reg11)
##
## Call:
## lm(formula = DT.ma3 ~ time)
##
## Residuals:
## Min 1Q Median 3Q Max
## -185.427 -90.482 -2.961 58.712 307.230
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 509.0216 31.6535 16.081 < 2e-16 ***
## time -4.5323 0.9098 -4.982 6.4e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 116 on 56 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.3071, Adjusted R-squared: 0.2947
## F-statistic: 24.82 on 1 and 56 DF, p-value: 6.396e-06
rmse(DT,fitted(reg11))
## Warning in `-.default`(actual, predicted): longer object length is not a
## multiple of shorter object length
## [1] 209.7107
mape(DT,fitted(reg11))
## Warning in `-.default`(actual, predicted): longer object length is not a
## multiple of shorter object length
## [1] 0.9218373
Dataf11<-ts(exp(fitted(reg11)), start=c(2009,1), frequency = 4)
fc11<- forecast(Dataf11,h=6)
print(fc11)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2023 Q3 1.16909e+211 NaN NaN NaN NaN
## 2023 Q4 1.16909e+211 NaN NaN NaN NaN
## 2024 Q1 1.16909e+211 NaN NaN NaN NaN
## 2024 Q2 1.16909e+211 NaN NaN NaN NaN
## 2024 Q3 1.16909e+211 NaN NaN NaN NaN
## 2024 Q4 1.16909e+211 NaN NaN NaN NaN
plot.ts(DT)
# Trung binh truot dang khong tuyen tinh
reg12 <- lm(log(DT.ma3) ~ time)
summary(reg12)
##
## Call:
## lm(formula = log(DT.ma3) ~ time)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.88533 -0.24070 0.04931 0.22337 0.72766
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.287551 0.094715 66.384 < 2e-16 ***
## time -0.014819 0.002722 -5.444 1.2e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3471 on 56 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.346, Adjusted R-squared: 0.3344
## F-statistic: 29.63 on 1 and 56 DF, p-value: 1.198e-06
rmse(DT,fitted(reg12))
## Warning in `-.default`(actual, predicted): longer object length is not a
## multiple of shorter object length
## [1] 416.8833
mape(DT,fitted(reg12))
## Warning in `-.default`(actual, predicted): longer object length is not a
## multiple of shorter object length
## [1] 0.9730932
Dataf12<-ts(exp(fitted(reg12)), start=c(2009,1), frequency = 4)
fc12<- forecast(Dataf12,h=6)
print(fc12)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2023 Q3 221.0763 221.0221 221.1305 220.9934 221.1592
## 2023 Q4 217.8664 217.7596 217.9733 217.7030 218.0298
## 2024 Q1 214.7207 214.5531 214.8884 214.4643 214.9771
## 2024 Q2 211.6379 211.4023 211.8735 211.2776 211.9983
## 2024 Q3 208.6168 208.3069 208.9266 208.1429 209.0907
## 2024 Q4 205.6561 205.2665 206.0456 205.0602 206.2519
plot.ts(DT)
#PHẦN 2: DU BAO GIA
#BƯỚC 1: VE DO THI, NHAN XET
library(readxl)
Price<- read_excel("~/Kim(1)/Kim NEU/HK6 NEU/Phân tích chuỗi tg và dự báo/Bài tập lớn/daily data.xlsx")
attach(Price)
#Tao bien xu the
ts (Price , start=c(1,1), frequency = 1)
## Time Series:
## Start = 1
## End = 498
## Frequency = 1
## price log-return
## 1 13.006187 NA
## 2 12.864815 -1.0929071
## 3 12.723444 -1.1049836
## 4 12.723444 0.0000000
## 5 13.218244 3.8151766
## 6 13.076873 -1.0752792
## 7 12.511386 -4.4206093
## 8 12.476043 -0.2828856
## 9 12.476043 0.0000000
## 10 12.970844 3.8893791
## 11 12.228643 -5.8923073
## 12 12.157957 -0.5797118
## 13 12.157957 0.0000000
## 14 12.970844 6.4720191
## 15 12.334672 -5.0289926
## 16 12.652758 2.5461064
## 17 12.476043 -1.4064929
## 18 12.476043 0.0000000
## 19 13.041530 4.4328587
## 20 13.218244 1.3459153
## 21 13.041530 -1.3459153
## 22 13.041530 0.0000000
## 23 13.041530 0.0000000
## 24 13.041530 0.0000000
## 25 13.006187 -0.2713706
## 26 13.076873 0.5420067
## 27 13.359616 2.1391190
## 28 13.430301 0.5277057
## 29 13.430301 0.0000000
## 30 13.500987 0.5249356
## 31 13.430301 -0.5249356
## 32 13.359616 -0.5277057
## 33 13.076873 -2.1391190
## 34 12.723444 -2.7398974
## 35 12.758786 0.2773927
## 36 12.864815 0.8275909
## 37 12.935501 0.5479466
## 38 12.935501 0.0000000
## 39 12.794129 -1.0989122
## 40 12.935501 1.0989122
## 41 13.006187 0.5449605
## 42 12.723444 -2.1978907
## 43 12.652758 -0.5571045
## 44 12.476043 -1.4064929
## 45 12.794129 2.5176155
## 46 12.476043 -2.5176155
## 47 12.511386 0.2828856
## 48 12.723444 1.6807118
## 49 12.511386 -1.6807118
## 50 12.546729 0.2820876
## 51 12.970844 3.3244059
## 52 12.794129 -1.3717636
## 53 13.006187 1.6438726
## 54 12.935501 -0.5449605
## 55 12.794129 -1.0989122
## 56 12.900158 0.8253142
## 57 12.794129 -0.8253142
## 58 12.794129 0.0000000
## 59 12.794129 0.0000000
## 60 13.147558 2.7249642
## 61 13.041530 -0.8097210
## 62 13.006187 -0.2713706
## 63 13.041530 0.2713706
## 64 13.006187 -0.2713706
## 65 13.041530 0.2713706
## 66 12.970844 -0.5434796
## 67 12.970844 0.0000000
## 68 12.864815 -0.8207980
## 69 13.253587 2.9772158
## 70 13.359616 0.7968170
## 71 12.864815 -3.7740328
## 72 12.723444 -1.1049836
## 73 13.076873 2.7398974
## 74 12.652758 -3.2970019
## 75 12.652758 0.0000000
## 76 13.041530 3.0263658
## 77 12.794129 -1.9152432
## 78 13.006187 1.6438726
## 79 12.652758 -2.7549952
## 80 12.864815 1.6620881
## 81 13.041530 1.3642776
## 82 12.370015 -5.2863490
## 83 12.370015 0.0000000
## 84 12.370015 0.0000000
## 85 12.087271 -2.3122417
## 86 11.309728 -6.6489741
## 87 11.345070 0.3120127
## 88 11.945900 5.1604772
## 89 11.945900 0.0000000
## 90 11.945900 0.0000000
## 91 11.804528 -1.1904903
## 92 11.945900 1.1904903
## 93 11.592471 -3.0032287
## 94 11.875214 2.4097552
## 95 12.688101 6.6211229
## 96 12.370015 -2.5389234
## 97 12.299329 -0.5730675
## 98 12.228643 -0.5763705
## 99 12.228643 0.0000000
## 100 12.228643 0.0000000
## 101 12.511386 2.2858138
## 102 12.299329 -1.7094433
## 103 12.652758 2.8330507
## 104 12.688101 0.2789402
## 105 12.511386 -1.4025475
## 106 12.652758 1.1236073
## 107 12.688101 0.2789402
## 108 12.193300 -3.9777971
## 109 12.582072 3.1386314
## 110 12.511386 -0.5633818
## 111 12.334672 -1.4224991
## 112 11.945900 -3.2026027
## 113 11.945900 0.0000000
## 114 11.769185 -1.4903406
## 115 11.804528 0.2998503
## 116 11.981243 1.4859114
## 117 11.981243 0.0000000
## 118 11.839871 -1.1869576
## 119 11.804528 -0.2989539
## 120 11.804528 0.0000000
## 121 11.910557 0.8941937
## 122 11.733842 -1.4947961
## 123 11.733842 0.0000000
## 124 11.733842 0.0000000
## 125 11.875214 1.1976191
## 126 11.945900 0.5934736
## 127 11.981243 0.2954212
## 128 11.981243 0.0000000
## 129 11.663157 -2.6907453
## 130 11.663157 0.0000000
## 131 11.698499 0.3025721
## 132 11.910557 1.7964555
## 133 11.981243 0.5917177
## 134 12.016586 0.2945510
## 135 11.981243 -0.2945510
## 136 11.981243 0.0000000
## 137 11.663157 -2.6907453
## 138 11.839871 1.5037877
## 139 11.839871 0.0000000
## 140 11.839871 0.0000000
## 141 11.733842 -0.8995563
## 142 11.733842 0.0000000
## 143 11.804528 0.6006024
## 144 11.804528 0.0000000
## 145 12.016586 1.7804625
## 146 12.051928 0.2936860
## 147 11.804528 -2.0741484
## 148 11.981243 1.4859114
## 149 11.945900 -0.2954212
## 150 12.016586 0.5899722
## 151 11.945900 -0.5899722
## 152 11.945900 0.0000000
## 153 11.945900 0.0000000
## 154 11.945900 0.0000000
## 155 12.016586 0.5899722
## 156 12.016586 0.0000000
## 157 12.016586 0.0000000
## 158 12.016586 0.0000000
## 159 12.016586 0.0000000
## 160 12.794129 6.2698594
## 161 12.758786 -0.2766253
## 162 12.652758 -0.8344972
## 163 12.405357 -1.9746763
## 164 12.723444 2.5317808
## 165 12.794129 0.5540180
## 166 12.794129 0.0000000
## 167 12.440700 -2.8013036
## 168 12.476043 0.2836881
## 169 12.511386 0.2828856
## 170 12.511386 0.0000000
## 171 12.511386 0.0000000
## 172 12.440700 -0.5665738
## 173 12.440700 0.0000000
## 174 12.511386 0.5665738
## 175 12.617415 0.8438869
## 176 12.405357 -1.6949558
## 177 12.582072 1.4144507
## 178 12.582072 0.0000000
## 179 12.652758 0.5602256
## 180 12.582072 -0.5602256
## 181 12.582072 0.0000000
## 182 12.370015 -1.6997576
## 183 12.370015 0.0000000
## 184 12.370015 0.0000000
## 185 12.299329 -0.5730675
## 186 12.087271 -1.7391743
## 187 12.087271 0.0000000
## 188 11.274385 -6.9619634
## 189 12.054619 6.6914589
## 190 12.483748 3.4979719
## 191 12.093631 -3.1748698
## 192 12.093631 0.0000000
## 193 11.937584 -1.2987196
## 194 11.937584 0.0000000
## 195 11.898572 -0.3273325
## 196 11.820549 -0.6578971
## 197 11.391420 -3.6979003
## 198 11.898572 4.3557974
## 199 11.898572 0.0000000
## 200 11.391420 -4.3557974
## 201 11.235373 -1.3793322
## 202 11.040314 -1.7513582
## 203 11.235373 1.7513582
## 204 11.703514 4.0821995
## 205 11.157350 -4.7790664
## 206 11.469443 2.7587957
## 207 11.313396 -1.3698844
## 208 11.625490 2.7212564
## 209 11.235373 -3.4133006
## 210 11.079326 -1.3986242
## 211 11.664502 5.1469335
## 212 11.157350 -4.4451763
## 213 11.157350 0.0000000
## 214 11.079326 -0.7017573
## 215 10.923279 -1.4184635
## 216 10.299092 -5.8840500
## 217 10.689209 3.7179003
## 218 10.455139 -2.2141126
## 219 11.157350 6.5004830
## 220 11.196361 0.3490405
## 221 10.689209 -4.6354109
## 222 10.299092 -3.7179003
## 223 9.674904 -6.2520357
## 224 9.869963 1.9960743
## 225 10.299092 4.2559614
## 226 10.377115 0.7547206
## 227 10.299092 -0.7547206
## 228 10.455139 1.5037877
## 229 10.377115 -0.7490672
## 230 10.533162 1.4925650
## 231 10.533162 0.0000000
## 232 10.455139 -0.7434978
## 233 10.494150 0.3724399
## 234 10.455139 -0.3724399
## 235 10.260080 -1.8832948
## 236 10.650197 3.7317763
## 237 10.767232 1.0929071
## 238 10.494150 -2.5689486
## 239 10.494150 0.0000000
## 240 10.299092 -1.8762276
## 241 10.299092 0.0000000
## 242 10.299092 0.0000000
## 243 10.650197 3.3522692
## 244 10.533162 -1.1049836
## 245 10.533162 0.0000000
## 246 10.299092 -2.2472856
## 247 10.767232 4.4451763
## 248 10.767232 0.0000000
## 249 10.923279 1.4388737
## 250 11.157350 2.1202208
## 251 10.923279 -2.1202208
## 252 11.157350 2.1202208
## 253 11.157350 0.0000000
## 254 11.118338 -0.3502631
## 255 11.118338 0.0000000
## 256 11.118338 0.0000000
## 257 11.118338 0.0000000
## 258 11.118338 0.0000000
## 259 10.923279 -1.7699577
## 260 11.118338 1.7699577
## 261 11.001303 -1.0582109
## 262 10.845256 -1.4285957
## 263 10.767232 -0.7220248
## 264 10.845256 0.7220248
## 265 10.923279 0.7168489
## 266 10.611186 -2.8987537
## 267 10.728221 1.0969031
## 268 10.728221 0.0000000
## 269 10.728221 0.0000000
## 270 10.260080 -4.4617065
## 271 10.728221 4.4617065
## 272 10.611186 -1.0969031
## 273 10.611186 0.0000000
## 274 10.299092 -2.9852963
## 275 10.221068 -0.7604599
## 276 10.260080 0.3809528
## 277 10.533162 2.6267927
## 278 10.494150 -0.3710579
## 279 10.494150 0.0000000
## 280 10.455139 -0.3724399
## 281 10.299092 -1.5037877
## 282 10.299092 0.0000000
## 283 10.143045 -1.5267472
## 284 9.986998 -1.5504187
## 285 10.299092 3.0771659
## 286 10.104033 -1.9121041
## 287 10.143045 0.3853569
## 288 10.299092 1.5267472
## 289 10.026010 -2.6873018
## 290 10.065022 0.3883500
## 291 10.143045 0.7722046
## 292 10.143045 0.0000000
## 293 10.260080 1.1472401
## 294 10.065022 -1.9194447
## 295 10.143045 0.7722046
## 296 10.143045 0.0000000
## 297 10.104033 -0.3853569
## 298 10.143045 0.3853569
## 299 10.065022 -0.7722046
## 300 10.065022 0.0000000
## 301 10.143045 0.7722046
## 302 9.986998 -1.5504187
## 303 10.065022 0.7782140
## 304 10.065022 0.0000000
## 305 10.065022 0.0000000
## 306 10.026010 -0.3883500
## 307 10.065022 0.3883500
## 308 10.026010 -0.3883500
## 309 10.221068 1.9268419
## 310 10.221068 0.0000000
## 311 10.221068 0.0000000
## 312 10.221068 0.0000000
## 313 10.299092 0.7604599
## 314 10.143045 -1.5267472
## 315 10.065022 -0.7722046
## 316 10.143045 0.7722046
## 317 10.143045 0.0000000
## 318 10.065022 -0.7722046
## 319 9.986998 -0.7782140
## 320 10.065022 0.7782140
## 321 10.104033 0.3868477
## 322 10.026010 -0.7751977
## 323 10.026010 0.0000000
## 324 10.026010 0.0000000
## 325 9.986998 -0.3898640
## 326 10.026010 0.3898640
## 327 10.104033 0.7751977
## 328 10.143045 0.3853569
## 329 10.416127 2.6567027
## 330 10.221068 -1.8904155
## 331 10.221068 0.0000000
## 332 10.923279 6.6445099
## 333 10.689209 -2.1661497
## 334 10.767232 0.7272759
## 335 10.923279 1.4388737
## 336 11.235373 2.8170877
## 337 11.235373 0.0000000
## 338 11.196361 -0.3478264
## 339 11.079326 -1.0507978
## 340 11.196361 1.0507978
## 341 11.586478 3.4249923
## 342 11.781537 1.6694879
## 343 11.625490 -1.3333531
## 344 11.703514 0.6688988
## 345 11.937584 1.9802627
## 346 11.976595 0.3262646
## 347 11.859560 -0.9820046
## 348 12.171654 2.5975486
## 349 12.093631 -0.6430890
## 350 12.327701 1.9169916
## 351 12.366713 0.3159560
## 352 12.756830 3.1058397
## 353 12.483748 -2.1639175
## 354 12.756830 2.1639175
## 355 12.873865 0.9132484
## 356 12.834853 -0.3034904
## 357 12.834853 0.0000000
## 358 12.639795 -1.5314235
## 359 12.639795 0.0000000
## 360 12.405724 -1.8692133
## 361 12.210666 -1.5848192
## 362 12.639795 3.4540325
## 363 12.639795 0.0000000
## 364 12.327701 -2.5001302
## 365 12.093631 -1.9169916
## 366 12.327701 1.9169916
## 367 12.327701 0.0000000
## 368 12.249677 -0.6349228
## 369 12.249677 0.0000000
## 370 12.210666 -0.3189795
## 371 12.327701 0.9539023
## 372 12.327701 0.0000000
## 373 12.327701 0.0000000
## 374 12.327701 0.0000000
## 375 12.327701 0.0000000
## 376 12.327701 0.0000000
## 377 12.093631 -1.9169916
## 378 12.093631 0.0000000
## 379 12.483748 3.1748698
## 380 12.483748 0.0000000
## 381 12.483748 0.0000000
## 382 12.444736 -0.3129893
## 383 12.327701 -0.9448889
## 384 12.483748 1.2578782
## 385 12.483748 0.0000000
## 386 12.600783 0.9331327
## 387 12.405724 -1.5600940
## 388 12.444736 0.3139720
## 389 12.093631 -2.8618805
## 390 12.366713 2.2329476
## 391 12.366713 0.0000000
## 392 12.171654 -1.5898586
## 393 12.366713 1.5898586
## 394 12.093631 -2.2329476
## 395 12.249677 1.2820688
## 396 12.249677 0.0000000
## 397 12.210666 -0.3189795
## 398 12.132642 -0.6410278
## 399 12.093631 -0.3220615
## 400 12.249677 1.2820688
## 401 12.171654 -0.6389798
## 402 12.171654 0.0000000
## 403 12.327701 1.2739026
## 404 12.093631 -1.9169916
## 405 11.898572 -1.6260521
## 406 11.781537 -0.9884759
## 407 11.703514 -0.6644543
## 408 11.625490 -0.6688988
## 409 12.093631 3.9478811
## 410 12.171654 0.6430890
## 411 12.171654 0.0000000
## 412 12.327701 1.2739026
## 413 12.639795 2.5001302
## 414 12.639795 0.0000000
## 415 13.029912 3.0397477
## 416 13.029912 0.0000000
## 417 12.873865 -1.2048339
## 418 13.068923 1.5037877
## 419 13.029912 -0.2989539
## 420 12.990900 -0.2998503
## 421 12.639795 -2.7398974
## 422 12.386999 -2.0202707
## 423 12.429131 0.3395589
## 424 12.344866 -0.6802747
## 425 12.176335 -1.3745921
## 426 12.429131 2.0548668
## 427 12.639795 1.6807118
## 428 12.049938 -4.7790664
## 429 12.007805 -0.3502631
## 430 12.007805 0.0000000
## 431 11.797142 -1.7699577
## 432 11.670744 -1.0772097
## 433 11.670744 0.0000000
## 434 11.670744 0.0000000
## 435 11.375815 -2.5595547
## 436 11.249417 -1.1173301
## 437 11.544346 2.5879448
## 438 11.965672 3.5846132
## 439 11.965672 0.0000000
## 440 11.965672 0.0000000
## 441 11.965672 0.0000000
## 442 11.923540 -0.3527341
## 443 11.881407 -0.3539827
## 444 11.965672 0.7067167
## 445 11.965672 0.0000000
## 446 11.628611 -2.8573372
## 447 11.628611 0.0000000
## 448 11.628611 0.0000000
## 449 11.417948 -1.8282045
## 450 11.670744 2.1898685
## 451 11.375815 -2.5595547
## 452 11.375815 0.0000000
## 453 11.375815 0.0000000
## 454 11.291550 -0.7434978
## 455 11.291550 0.0000000
## 456 11.291550 0.0000000
## 457 11.291550 0.0000000
## 458 11.123019 -1.5037877
## 459 11.123019 0.0000000
## 460 11.123019 0.0000000
## 461 11.123019 0.0000000
## 462 11.123019 0.0000000
## 463 11.038754 -0.7604599
## 464 11.038754 0.0000000
## 465 10.996621 -0.3824096
## 466 11.123019 1.1428696
## 467 11.123019 0.0000000
## 468 11.249417 1.1299555
## 469 11.038754 -1.8904155
## 470 11.038754 0.0000000
## 471 10.954489 -0.7662873
## 472 11.123019 1.5267472
## 473 11.123019 0.0000000
## 474 11.291550 1.5037877
## 475 11.291550 0.0000000
## 476 11.291550 0.0000000
## 477 12.049938 6.5004830
## 478 12.892590 6.7593291
## 479 13.777376 6.6375069
## 480 14.704294 6.5111751
## 481 15.083488 2.5461064
## 482 14.030172 -7.2390496
## 483 13.061121 -7.1570193
## 484 13.650978 4.4171218
## 485 13.356050 -2.1841742
## 486 13.356050 0.0000000
## 487 13.356050 0.0000000
## 488 13.440315 0.6289329
## 489 13.861641 3.0866648
## 490 14.104676 1.7380947
## 491 14.017878 -0.6172859
## 492 13.887681 -0.9331327
## 493 13.714085 -1.2578782
## 494 13.714085 0.0000000
## 495 13.627287 -0.6349228
## 496 13.627287 0.0000000
## 497 13.800883 1.2658397
## 498 13.800883 0.0000000
Data1 <- ts(Price, start=c(1,1), frequency = 1)
# do thi, nhan xet
plot.ts(Data1)
plot.ts(diff(Data1))
View(Data1)
#BƯỚC 2: KIEM DINH ADF
#KIEM DINH CHUOI GIA
price<-Price$price
summary(ur.df(price, type = "trend"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.00415 -0.11306 -0.00790 0.08726 1.02288
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.696e-01 1.394e-01 1.934 0.0537 .
## z.lag.1 -2.357e-02 1.128e-02 -2.089 0.0373 *
## tt 4.517e-05 7.871e-05 0.574 0.5663
## z.diff.lag -7.063e-02 4.519e-02 -1.563 0.1187
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2452 on 492 degrees of freedom
## Multiple R-squared: 0.01761, Adjusted R-squared: 0.01162
## F-statistic: 2.94 on 3 and 492 DF, p-value: 0.0328
##
##
## Value of test-statistic is: -2.0886 1.822 2.7161
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.98 -3.42 -3.13
## phi2 6.15 4.71 4.05
## phi3 8.34 6.30 5.36
summary(ur.df(price, type = "drift"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression drift
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.98865 -0.11395 -0.00714 0.08575 1.03420
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.29689 0.13091 2.268 0.0238 *
## z.lag.1 -0.02492 0.01103 -2.260 0.0242 *
## z.diff.lag -0.06872 0.04504 -1.526 0.1277
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.245 on 493 degrees of freedom
## Multiple R-squared: 0.01695, Adjusted R-squared: 0.01297
## F-statistic: 4.251 on 2 and 493 DF, p-value: 0.01477
##
##
## Value of test-statistic is: -2.2605 2.5718
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau2 -3.44 -2.87 -2.57
## phi1 6.47 4.61 3.79
summary(ur.df(price, type = "none"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.05550 -0.09808 0.00007 0.08439 0.99970
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -6.593e-06 9.305e-04 -0.007 0.9943
## z.diff.lag -8.216e-02 4.483e-02 -1.833 0.0675 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.246 on 494 degrees of freedom
## Multiple R-squared: 0.006755, Adjusted R-squared: 0.002734
## F-statistic: 1.68 on 2 and 494 DF, p-value: 0.1874
##
##
## Value of test-statistic is: -0.0071
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.58 -1.95 -1.62
#KIEM DINH SAI PHAN CHUOI GIA
summary(ur.df(diff(price), type = "trend"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.05105 -0.10628 -0.00875 0.09196 1.07307
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.852e-02 2.217e-02 -0.835 0.4038
## z.lag.1 -1.195e+00 6.610e-02 -18.074 <2e-16 ***
## tt 8.434e-05 7.727e-05 1.092 0.2756
## z.diff.lag 1.013e-01 4.489e-02 2.256 0.0245 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2452 on 491 degrees of freedom
## Multiple R-squared: 0.5472, Adjusted R-squared: 0.5445
## F-statistic: 197.8 on 3 and 491 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -18.0737 108.8879 163.3303
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.98 -3.42 -3.13
## phi2 6.15 4.71 4.05
## phi3 8.34 6.30 5.36
summary(ur.df(diff(price), type = "drift"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression drift
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.02993 -0.10735 -0.00572 0.08158 1.08849
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.002473 0.011022 0.224 0.8226
## z.lag.1 -1.190118 0.065981 -18.037 <2e-16 ***
## z.diff.lag 0.099018 0.044851 2.208 0.0277 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2452 on 492 degrees of freedom
## Multiple R-squared: 0.5461, Adjusted R-squared: 0.5443
## F-statistic: 296 on 2 and 492 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -18.0373 162.6728
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau2 -3.44 -2.87 -2.57
## phi1 6.47 4.61 3.79
summary(ur.df(diff(price), type = "none"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.02740 -0.10489 -0.00324 0.08405 1.09084
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -1.18998 0.06591 -18.053 <2e-16 ***
## z.diff.lag 0.09896 0.04481 2.209 0.0277 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.245 on 493 degrees of freedom
## Multiple R-squared: 0.5461, Adjusted R-squared: 0.5443
## F-statistic: 296.6 on 2 and 493 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -18.0533
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.58 -1.95 -1.62
#BƯỚC 3: XAC DINH ARIMA
#Xac dinh bac AR, MA
acf(diff(price))
pacf(diff(price))
#ARIMA (2,1,0) or (2,1,2)
#BƯỚC 4: DANH GIA MO HINH HIEU CHINH TRUNG GIAN
reg.data.arima210 <- Arima(price, order = c(2,1,0))
summary(reg.data.arima210)
## Series: price
## ARIMA(2,1,0)
##
## Coefficients:
## ar1 ar2
## -0.0903 -0.0987
## s.e. 0.0446 0.0446
##
## sigma^2 = 0.05986: log likelihood = -4.52
## AIC=15.04 AICc=15.09 BIC=27.67
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001895163 0.2439311 0.1603869 -0.009474014 1.345363 1.007645
## ACF1
## Training set -0.004433067
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima210)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima210)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(2,1,0)
## Q* = 2.51, df = 8, p-value = 0.9613
##
## Model df: 2. Total lags used: 10
#BƯỚC 6: DU BAO
data.arima210 <- forecast(reg.data.arima210, h=10)
data.arima210
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 13.78375 13.47019 14.09730 13.30421 14.26329
## 500 13.78529 13.36140 14.20919 13.13700 14.43359
## 501 13.78685 13.29120 14.28249 13.02882 14.54488
## 502 13.78655 13.22582 14.34729 12.92898 14.64413
## 503 13.78643 13.16642 14.40643 12.83821 14.73464
## 504 13.78647 13.11267 14.46026 12.75599 14.81694
## 505 13.78648 13.06294 14.51001 12.67993 14.89302
## 506 13.78647 13.01637 14.55657 12.60871 14.96423
## 507 13.78647 12.97246 14.60048 12.54156 15.03139
## 508 13.78647 12.93081 14.64213 12.47785 15.09509
#Tương tự với arima(2,1,2)
reg.data.arima212 <- Arima(price, order = c(2,1,2))
summary(reg.data.arima212)
## Series: price
## ARIMA(2,1,2)
##
## Coefficients:
## ar1 ar2 ma1 ma2
## 0.2543 0.0396 -0.3504 -0.1116
## s.e. 0.7919 0.5043 0.7892 0.5716
##
## sigma^2 = 0.05994: log likelihood = -3.85
## AIC=17.69 AICc=17.81 BIC=38.73
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.002079541 0.2435973 0.16008 -0.009544242 1.342449 1.005716
## ACF1
## Training set -0.0001705748
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima212)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima212)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(2,1,2)
## Q* = 1.2083, df = 6, p-value = 0.9765
##
## Model df: 4. Total lags used: 10
#BƯỚC 6: DU BAO
data.arima212 <- forecast(reg.data.arima212, h=10)
data.arima212
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 13.78664 13.47288 14.10040 13.30678 14.26650
## 500 13.78195 13.35901 14.20489 13.13512 14.42878
## 501 13.78019 13.28717 14.27321 13.02619 14.53419
## 502 13.77956 13.22926 14.32986 12.93795 14.62117
## 503 13.77933 13.17857 14.38009 12.86054 14.69811
## 504 13.77924 13.13240 14.42609 12.78998 14.76851
## 505 13.77921 13.08951 14.46891 12.72441 14.83402
## 506 13.77920 13.04921 14.50919 12.66278 14.89563
## 507 13.77920 13.01104 14.54735 12.60441 14.95399
## 508 13.77920 12.97470 14.58370 12.54882 15.00957
#mape 10 giá dự báo
#Tương tự với arima(20,1,2)
reg.data.arima2012 <- Arima(price, order = c(20,1,2))
summary(reg.data.arima2012)
## Series: price
## ARIMA(20,1,2)
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8
## -0.7700 -1.1059 -0.2111 -0.1514 -0.0874 -0.0328 -0.0489 -0.0166
## s.e. 0.0491 0.0606 0.0752 0.0757 0.0759 0.0759 0.0760 0.0762
## ar9 ar10 ar11 ar12 ar13 ar14 ar15 ar16
## 0.0082 -0.0131 0.0706 0.0390 0.0597 0.0329 -0.0175 -0.0225
## s.e. 0.0762 0.0764 0.0765 0.0768 0.0770 0.0770 0.0772 0.0777
## ar17 ar18 ar19 ar20 ma1 ma2
## -0.0527 -0.0602 -0.0282 -0.0722 0.6872 0.9656
## s.e. 0.0784 0.0793 0.0618 0.0503 0.0209 0.0226
##
## sigma^2 = 0.05974: log likelihood = 5.42
## AIC=35.16 AICc=37.5 BIC=131.96
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.002216377 0.2387144 0.1634754 -0.007606769 1.37106 1.027048
## ACF1
## Training set 0.001184172
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima2012)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima2012)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(20,1,2)
## Q* = 1.4993, df = 3, p-value = 0.6824
##
## Model df: 22. Total lags used: 25
#BƯỚC 6: DU BAO
data.arima2012 <- forecast(reg.data.arima2012, h=10)
data.arima2012
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 13.84584 13.53259 14.15908 13.36677 14.32490
## 500 13.80356 13.37852 14.22860 13.15351 14.45360
## 501 13.84077 13.34076 14.34077 13.07608 14.60545
## 502 13.95348 13.39697 14.51000 13.10237 14.80460
## 503 13.91105 13.30359 14.51851 12.98203 14.84008
## 504 13.74643 13.09077 14.40210 12.74368 14.74919
## 505 13.87688 13.17699 14.57676 12.80650 14.94726
## 506 13.89573 13.15796 14.63351 12.76741 15.02406
## 507 13.72069 12.94414 14.49724 12.53306 14.90832
## 508 13.81627 12.99959 14.63295 12.56727 15.06528
save(df, file = 'TKT63_G12_NguyenThiKimAnh.rda')
load(file='TKT63_G12_NguyenThiKimAnh.rda')
mape(price,fitted(data.arima212))
## [1] 0.01342449
#PHẦN 3: DU BAO LOG-RETURN
#BƯỚC 1: VE DO THI, NHAN XET
library(readxl)
Price_log<- read_excel("~/Kim(1)/Kim NEU/HK6 NEU/Phân tích chuỗi tg và dự báo/Bài tập lớn/log-return.xlsx")
attach(Price_log)
## The following object is masked from Price:
##
## log-return
#Tao bien xu the
ts (Price_log , start=c(1,1), frequency = 1)
## Time Series:
## Start = 1
## End = 498
## Frequency = 1
## log-return
## [1,] NA
## [2,] -1.0929
## [3,] -1.1050
## [4,] 0.0000
## [5,] 3.8152
## [6,] -1.0753
## [7,] -4.4206
## [8,] -0.2829
## [9,] 0.0000
## [10,] 3.8894
## [11,] -5.8923
## [12,] -0.5797
## [13,] 0.0000
## [14,] 6.4720
## [15,] -5.0290
## [16,] 2.5461
## [17,] -1.4065
## [18,] 0.0000
## [19,] 4.4329
## [20,] 1.3459
## [21,] -1.3459
## [22,] 0.0000
## [23,] 0.0000
## [24,] 0.0000
## [25,] -0.2714
## [26,] 0.5420
## [27,] 2.1391
## [28,] 0.5277
## [29,] 0.0000
## [30,] 0.5249
## [31,] -0.5249
## [32,] -0.5277
## [33,] -2.1391
## [34,] -2.7399
## [35,] 0.2774
## [36,] 0.8276
## [37,] 0.5479
## [38,] 0.0000
## [39,] -1.0989
## [40,] 1.0989
## [41,] 0.5450
## [42,] -2.1979
## [43,] -0.5571
## [44,] -1.4065
## [45,] 2.5176
## [46,] -2.5176
## [47,] 0.2829
## [48,] 1.6807
## [49,] -1.6807
## [50,] 0.2821
## [51,] 3.3244
## [52,] -1.3718
## [53,] 1.6439
## [54,] -0.5450
## [55,] -1.0989
## [56,] 0.8253
## [57,] -0.8253
## [58,] 0.0000
## [59,] 0.0000
## [60,] 2.7250
## [61,] -0.8097
## [62,] -0.2714
## [63,] 0.2714
## [64,] -0.2714
## [65,] 0.2714
## [66,] -0.5435
## [67,] 0.0000
## [68,] -0.8208
## [69,] 2.9772
## [70,] 0.7968
## [71,] -3.7740
## [72,] -1.1050
## [73,] 2.7399
## [74,] -3.2970
## [75,] 0.0000
## [76,] 3.0264
## [77,] -1.9152
## [78,] 1.6439
## [79,] -2.7550
## [80,] 1.6621
## [81,] 1.3643
## [82,] -5.2863
## [83,] 0.0000
## [84,] 0.0000
## [85,] -2.3122
## [86,] -6.6490
## [87,] 0.3120
## [88,] 5.1605
## [89,] 0.0000
## [90,] 0.0000
## [91,] -1.1905
## [92,] 1.1905
## [93,] -3.0032
## [94,] 2.4098
## [95,] 6.6211
## [96,] -2.5389
## [97,] -0.5731
## [98,] -0.5764
## [99,] 0.0000
## [100,] 0.0000
## [101,] 2.2858
## [102,] -1.7094
## [103,] 2.8331
## [104,] 0.2789
## [105,] -1.4025
## [106,] 1.1236
## [107,] 0.2789
## [108,] -3.9778
## [109,] 3.1386
## [110,] -0.5634
## [111,] -1.4225
## [112,] -3.2026
## [113,] 0.0000
## [114,] -1.4903
## [115,] 0.2999
## [116,] 1.4859
## [117,] 0.0000
## [118,] -1.1870
## [119,] -0.2990
## [120,] 0.0000
## [121,] 0.8942
## [122,] -1.4948
## [123,] 0.0000
## [124,] 0.0000
## [125,] 1.1976
## [126,] 0.5935
## [127,] 0.2954
## [128,] 0.0000
## [129,] -2.6907
## [130,] 0.0000
## [131,] 0.3026
## [132,] 1.7965
## [133,] 0.5917
## [134,] 0.2946
## [135,] -0.2946
## [136,] 0.0000
## [137,] -2.6907
## [138,] 1.5038
## [139,] 0.0000
## [140,] 0.0000
## [141,] -0.8996
## [142,] 0.0000
## [143,] 0.6006
## [144,] 0.0000
## [145,] 1.7805
## [146,] 0.2937
## [147,] -2.0741
## [148,] 1.4859
## [149,] -0.2954
## [150,] 0.5900
## [151,] -0.5900
## [152,] 0.0000
## [153,] 0.0000
## [154,] 0.0000
## [155,] 0.5900
## [156,] 0.0000
## [157,] 0.0000
## [158,] 0.0000
## [159,] 0.0000
## [160,] 6.2699
## [161,] -0.2766
## [162,] -0.8345
## [163,] -1.9747
## [164,] 2.5318
## [165,] 0.5540
## [166,] 0.0000
## [167,] -2.8013
## [168,] 0.2837
## [169,] 0.2829
## [170,] 0.0000
## [171,] 0.0000
## [172,] -0.5666
## [173,] 0.0000
## [174,] 0.5666
## [175,] 0.8439
## [176,] -1.6950
## [177,] 1.4145
## [178,] 0.0000
## [179,] 0.5602
## [180,] -0.5602
## [181,] 0.0000
## [182,] -1.6998
## [183,] 0.0000
## [184,] 0.0000
## [185,] -0.5731
## [186,] -1.7392
## [187,] 0.0000
## [188,] -6.9620
## [189,] 6.6915
## [190,] 3.4980
## [191,] -3.1749
## [192,] 0.0000
## [193,] -1.2987
## [194,] 0.0000
## [195,] -0.3273
## [196,] -0.6579
## [197,] -3.6979
## [198,] 4.3558
## [199,] 0.0000
## [200,] -4.3558
## [201,] -1.3793
## [202,] -1.7514
## [203,] 1.7514
## [204,] 4.0822
## [205,] -4.7791
## [206,] 2.7588
## [207,] -1.3699
## [208,] 2.7213
## [209,] -3.4133
## [210,] -1.3986
## [211,] 5.1469
## [212,] -4.4452
## [213,] 0.0000
## [214,] -0.7018
## [215,] -1.4185
## [216,] -5.8841
## [217,] 3.7179
## [218,] -2.2141
## [219,] 6.5005
## [220,] 0.3490
## [221,] -4.6354
## [222,] -3.7179
## [223,] -6.2520
## [224,] 1.9961
## [225,] 4.2560
## [226,] 0.7547
## [227,] -0.7547
## [228,] 1.5038
## [229,] -0.7491
## [230,] 1.4926
## [231,] 0.0000
## [232,] -0.7435
## [233,] 0.3724
## [234,] -0.3724
## [235,] -1.8833
## [236,] 3.7318
## [237,] 1.0929
## [238,] -2.5689
## [239,] 0.0000
## [240,] -1.8762
## [241,] 0.0000
## [242,] 0.0000
## [243,] 3.3523
## [244,] -1.1050
## [245,] 0.0000
## [246,] -2.2473
## [247,] 4.4452
## [248,] 0.0000
## [249,] 1.4389
## [250,] 2.1202
## [251,] -2.1202
## [252,] 2.1202
## [253,] 0.0000
## [254,] -0.3503
## [255,] 0.0000
## [256,] 0.0000
## [257,] 0.0000
## [258,] 0.0000
## [259,] -1.7700
## [260,] 1.7700
## [261,] -1.0582
## [262,] -1.4286
## [263,] -0.7220
## [264,] 0.7220
## [265,] 0.7168
## [266,] -2.8988
## [267,] 1.0969
## [268,] 0.0000
## [269,] 0.0000
## [270,] -4.4617
## [271,] 4.4617
## [272,] -1.0969
## [273,] 0.0000
## [274,] -2.9853
## [275,] -0.7605
## [276,] 0.3810
## [277,] 2.6268
## [278,] -0.3711
## [279,] 0.0000
## [280,] -0.3724
## [281,] -1.5038
## [282,] 0.0000
## [283,] -1.5267
## [284,] -1.5504
## [285,] 3.0772
## [286,] -1.9121
## [287,] 0.3854
## [288,] 1.5267
## [289,] -2.6873
## [290,] 0.3884
## [291,] 0.7722
## [292,] 0.0000
## [293,] 1.1472
## [294,] -1.9194
## [295,] 0.7722
## [296,] 0.0000
## [297,] -0.3854
## [298,] 0.3854
## [299,] -0.7722
## [300,] 0.0000
## [301,] 0.7722
## [302,] -1.5504
## [303,] 0.7782
## [304,] 0.0000
## [305,] 0.0000
## [306,] -0.3884
## [307,] 0.3884
## [308,] -0.3884
## [309,] 1.9268
## [310,] 0.0000
## [311,] 0.0000
## [312,] 0.0000
## [313,] 0.7605
## [314,] -1.5267
## [315,] -0.7722
## [316,] 0.7722
## [317,] 0.0000
## [318,] -0.7722
## [319,] -0.7782
## [320,] 0.7782
## [321,] 0.3868
## [322,] -0.7752
## [323,] 0.0000
## [324,] 0.0000
## [325,] -0.3899
## [326,] 0.3899
## [327,] 0.7752
## [328,] 0.3854
## [329,] 2.6567
## [330,] -1.8904
## [331,] 0.0000
## [332,] 6.6445
## [333,] -2.1661
## [334,] 0.7273
## [335,] 1.4389
## [336,] 2.8171
## [337,] 0.0000
## [338,] -0.3478
## [339,] -1.0508
## [340,] 1.0508
## [341,] 3.4250
## [342,] 1.6695
## [343,] -1.3334
## [344,] 0.6689
## [345,] 1.9803
## [346,] 0.3263
## [347,] -0.9820
## [348,] 2.5975
## [349,] -0.6431
## [350,] 1.9170
## [351,] 0.3160
## [352,] 3.1058
## [353,] -2.1639
## [354,] 2.1639
## [355,] 0.9132
## [356,] -0.3035
## [357,] 0.0000
## [358,] -1.5314
## [359,] 0.0000
## [360,] -1.8692
## [361,] -1.5848
## [362,] 3.4540
## [363,] 0.0000
## [364,] -2.5001
## [365,] -1.9170
## [366,] 1.9170
## [367,] 0.0000
## [368,] -0.6349
## [369,] 0.0000
## [370,] -0.3190
## [371,] 0.9539
## [372,] 0.0000
## [373,] 0.0000
## [374,] 0.0000
## [375,] 0.0000
## [376,] 0.0000
## [377,] -1.9170
## [378,] 0.0000
## [379,] 3.1749
## [380,] 0.0000
## [381,] 0.0000
## [382,] -0.3130
## [383,] -0.9449
## [384,] 1.2579
## [385,] 0.0000
## [386,] 0.9331
## [387,] -1.5601
## [388,] 0.3140
## [389,] -2.8619
## [390,] 2.2329
## [391,] 0.0000
## [392,] -1.5899
## [393,] 1.5899
## [394,] -2.2329
## [395,] 1.2821
## [396,] 0.0000
## [397,] -0.3190
## [398,] -0.6410
## [399,] -0.3221
## [400,] 1.2821
## [401,] -0.6390
## [402,] 0.0000
## [403,] 1.2739
## [404,] -1.9170
## [405,] -1.6261
## [406,] -0.9885
## [407,] -0.6645
## [408,] -0.6689
## [409,] 3.9479
## [410,] 0.6431
## [411,] 0.0000
## [412,] 1.2739
## [413,] 2.5001
## [414,] 0.0000
## [415,] 3.0397
## [416,] 0.0000
## [417,] -1.2048
## [418,] 1.5038
## [419,] -0.2990
## [420,] -0.2999
## [421,] -2.7399
## [422,] -2.0203
## [423,] 0.3396
## [424,] -0.6803
## [425,] -1.3746
## [426,] 2.0549
## [427,] 1.6807
## [428,] -4.7791
## [429,] -0.3503
## [430,] 0.0000
## [431,] -1.7700
## [432,] -1.0772
## [433,] 0.0000
## [434,] 0.0000
## [435,] -2.5596
## [436,] -1.1173
## [437,] 2.5879
## [438,] 3.5846
## [439,] 0.0000
## [440,] 0.0000
## [441,] 0.0000
## [442,] -0.3527
## [443,] -0.3540
## [444,] 0.7067
## [445,] 0.0000
## [446,] -2.8573
## [447,] 0.0000
## [448,] 0.0000
## [449,] -1.8282
## [450,] 2.1899
## [451,] -2.5596
## [452,] 0.0000
## [453,] 0.0000
## [454,] -0.7435
## [455,] 0.0000
## [456,] 0.0000
## [457,] 0.0000
## [458,] -1.5038
## [459,] 0.0000
## [460,] 0.0000
## [461,] 0.0000
## [462,] 0.0000
## [463,] -0.7605
## [464,] 0.0000
## [465,] -0.3824
## [466,] 1.1429
## [467,] 0.0000
## [468,] 1.1300
## [469,] -1.8904
## [470,] 0.0000
## [471,] -0.7663
## [472,] 1.5267
## [473,] 0.0000
## [474,] 1.5038
## [475,] 0.0000
## [476,] 0.0000
## [477,] 6.5005
## [478,] 6.7593
## [479,] 6.6375
## [480,] 6.5112
## [481,] 2.5461
## [482,] -7.2390
## [483,] -7.1570
## [484,] 4.4171
## [485,] -2.1842
## [486,] 0.0000
## [487,] 0.0000
## [488,] 0.6289
## [489,] 3.0867
## [490,] 1.7381
## [491,] -0.6173
## [492,] -0.9331
## [493,] -1.2579
## [494,] 0.0000
## [495,] -0.6349
## [496,] 0.0000
## [497,] 1.2658
## [498,] 0.0000
Data2 <- ts(Price_log, start=c(1,1), frequency = 1)
# do thi, nhan xet
plot.ts(Data2)
#BƯỚC 2: KIEM DINH ADF
#KIEM DINH CHUOI log-return
lnprice<-na.omit(Data2)
summary(ur.df(lnprice, type = "trend"))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.9326 -0.9105 -0.0635 0.7570 7.8741
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.1555506 0.1840263 -0.845 0.3984
## z.lag.1 -1.2131086 0.0667693 -18.169 <2e-16 ***
## tt 0.0007009 0.0006415 1.093 0.2750
## z.diff.lag 0.0969578 0.0449073 2.159 0.0313 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.035 on 491 degrees of freedom
## Multiple R-squared: 0.5573, Adjusted R-squared: 0.5546
## F-statistic: 206 on 3 and 491 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -18.1687 110.0347 165.0507
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.98 -3.42 -3.13
## phi2 6.15 4.71 4.05
## phi3 8.34 6.30 5.36
#BƯỚC 3: XAC DINH ARIMA
#Xac dinh bac AR, MA
acf(lnprice)
pacf(lnprice)
#BƯỚC 4: DANH GIA MO HINH HIEU CHINH TRUNG GIAN
reg.data.arima100 <- Arima(lnprice, order = c(1,0,0),include.mean = FALSE)
summary(reg.data.arima100)
## Series: lnprice
## ARIMA(1,0,0) with zero mean
##
## Coefficients:
## ar1
## -0.1032
## s.e. 0.0446
##
## sigma^2 = 4.154: log likelihood = -1058.59
## AIC=2121.17 AICc=2121.2 BIC=2129.59
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.01317741 2.036034 1.347143 NaN Inf 0.6230161 -0.00995489
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima100)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima100)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,0,0) with zero mean
## Q* = 6.9338, df = 9, p-value = 0.644
##
## Model df: 1. Total lags used: 10
#BƯỚC 6: DU BAO
data.arima100 <- forecast(reg.data.arima100, h=10)
data.arima100
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 0 -2.611912 2.611912 -3.994575 3.994575
## 500 0 -2.625790 2.625790 -4.015800 4.015800
## 501 0 -2.625938 2.625938 -4.016025 4.016025
## 502 0 -2.625939 2.625939 -4.016028 4.016028
## 503 0 -2.625939 2.625939 -4.016028 4.016028
## 504 0 -2.625939 2.625939 -4.016028 4.016028
## 505 0 -2.625939 2.625939 -4.016028 4.016028
## 506 0 -2.625939 2.625939 -4.016028 4.016028
## 507 0 -2.625939 2.625939 -4.016028 4.016028
## 508 0 -2.625939 2.625939 -4.016028 4.016028
mape(lnprice,fitted(data.arima100))
## [1] NaN
#BƯỚC 4: DANH GIA MO HINH HIEU CHINH TRUNG GIAN
reg.data.arima200 <- Arima(lnprice, order = c(2,0,0),include.mean = FALSE)
summary(reg.data.arima200)
## Series: lnprice
## ARIMA(2,0,0) with zero mean
##
## Coefficients:
## ar1 ar2
## -0.1131 -0.0944
## s.e. 0.0446 0.0446
##
## sigma^2 = 4.125: log likelihood = -1056.36
## AIC=2118.72 AICc=2118.77 BIC=2131.34
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.01422289 2.026891 1.349564 NaN Inf 0.6241356 -0.004647101
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima200)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima200)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(2,0,0) with zero mean
## Q* = 2.7023, df = 8, p-value = 0.9516
##
## Model df: 2. Total lags used: 10
#BƯỚC 6: DU BAO
data.arima200 <- forecast(reg.data.arima200, h=10)
data.arima200
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 -1.194662e-01 -2.722274 2.483342 -4.100118 3.861185
## 500 1.350693e-02 -2.605884 2.632898 -3.992506 4.019519
## 501 9.748115e-03 -2.618239 2.637735 -4.009411 4.028907
## 502 -2.376913e-03 -2.630874 2.626120 -4.022316 4.017562
## 503 -6.512915e-04 -2.629186 2.627884 -4.020649 4.019346
## 504 2.979685e-04 -2.628245 2.628841 -4.019712 4.020308
## 505 2.778035e-05 -2.628516 2.628571 -4.019982 4.020038
## 506 -3.126313e-05 -2.628575 2.628512 -4.020041 4.019979
## 507 9.127236e-07 -2.628542 2.628544 -4.020009 4.020011
## 508 2.847421e-06 -2.628541 2.628546 -4.020007 4.020013
mape(lnprice,fitted(data.arima200))
## [1] NaN
reg.data.arima101 <- Arima(lnprice, order = c(1,0,1),include.mean = FALSE)
summary(reg.data.arima101)
## Series: lnprice
## ARIMA(1,0,1) with zero mean
##
## Coefficients:
## ar1 ma1
## 0.4803 -0.6090
## s.e. 0.1792 0.1608
##
## sigma^2 = 4.118: log likelihood = -1055.95
## AIC=2117.9 AICc=2117.95 BIC=2130.53
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.01564191 2.025213 1.346244 NaN Inf 0.6226006 0.009957104
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima101)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima101)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,0,1) with zero mean
## Q* = 1.82, df = 8, p-value = 0.986
##
## Model df: 2. Total lags used: 10
#BƯỚC 6: DU BAO
data.arima101 <- forecast(reg.data.arima101, h=30)
data.arima101
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 -6.755926e-02 -2.668212 2.533093 -4.044914 3.909795
## 500 -3.244740e-02 -2.654571 2.589676 -4.042639 3.977744
## 501 -1.558385e-02 -2.642635 2.611467 -4.033311 4.002144
## 502 -7.484620e-03 -2.635671 2.620702 -4.026949 4.011979
## 503 -3.594717e-03 -2.632043 2.624853 -4.023459 4.016270
## 504 -1.726472e-03 -2.630235 2.626782 -4.021683 4.018230
## 505 -8.291911e-04 -2.629352 2.627693 -4.020807 4.019149
## 506 -3.982443e-04 -2.628924 2.628127 -4.020381 4.019585
## 507 -1.912690e-04 -2.628718 2.628335 -4.020175 4.019793
## 508 -9.186276e-05 -2.628618 2.628435 -4.020076 4.019892
## 509 -4.411989e-05 -2.628571 2.628482 -4.020029 4.019940
## 510 -2.118992e-05 -2.628548 2.628505 -4.020006 4.019963
## 511 -1.017711e-05 -2.628537 2.628516 -4.019995 4.019974
## 512 -4.887866e-06 -2.628531 2.628522 -4.019989 4.019980
## 513 -2.347547e-06 -2.628529 2.628524 -4.019987 4.019982
## 514 -1.127481e-06 -2.628528 2.628525 -4.019986 4.019983
## 515 -5.415071e-07 -2.628527 2.628526 -4.019985 4.019984
## 516 -2.600753e-07 -2.628527 2.628526 -4.019985 4.019984
## 517 -1.249091e-07 -2.628527 2.628526 -4.019985 4.019984
## 518 -5.999140e-08 -2.628527 2.628526 -4.019984 4.019984
## 519 -2.881270e-08 -2.628527 2.628526 -4.019984 4.019984
## 520 -1.383818e-08 -2.628527 2.628527 -4.019984 4.019984
## 521 -6.646207e-09 -2.628527 2.628527 -4.019984 4.019984
## 522 -3.192044e-09 -2.628527 2.628527 -4.019984 4.019984
## 523 -1.533076e-09 -2.628527 2.628527 -4.019984 4.019984
## 524 -7.363067e-10 -2.628527 2.628527 -4.019984 4.019984
## 525 -3.536337e-10 -2.628527 2.628527 -4.019984 4.019984
## 526 -1.698434e-10 -2.628527 2.628527 -4.019984 4.019984
## 527 -8.157247e-11 -2.628527 2.628527 -4.019984 4.019984
## 528 -3.917767e-11 -2.628527 2.628527 -4.019984 4.019984
mape(lnprice,fitted(data.arima101))
## [1] Inf
reg.data.arima201 <- Arima(lnprice, order = c(2,0,1),include.mean = FALSE)
summary(reg.data.arima201)
## Series: lnprice
## ARIMA(2,0,1) with zero mean
##
## Coefficients:
## ar1 ar2 ma1
## 0.3418 -0.0486 -0.4603
## s.e. 0.3024 0.0652 0.3010
##
## sigma^2 = 4.122: log likelihood = -1055.69
## AIC=2119.37 AICc=2119.45 BIC=2136.21
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.01534225 2.02413 1.347363 NaN Inf 0.6231177 -0.0003631749
#BƯỚC 5: KIEM DINH TINH DUNG
#Tinh dung
autoplot(reg.data.arima201)
#Danh gia ut có ttq va nhieu trang khong
checkresiduals(reg.data.arima201)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(2,0,1) with zero mean
## Q* = 1.3738, df = 7, p-value = 0.9864
##
## Model df: 3. Total lags used: 10
#BƯỚC 6: DU BAO
data.arima201 <- forecast(reg.data.arima201, h=10)
data.arima201
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 499 -1.097859e-01 -2.711677 2.492105 -4.089035 3.869463
## 500 -3.752486e-02 -2.657623 2.582573 -4.044618 3.969569
## 501 -7.485540e-03 -2.637831 2.622860 -4.030252 4.015281
## 502 -7.331862e-04 -2.631864 2.630398 -4.024700 4.023234
## 503 1.135262e-04 -2.631039 2.631266 -4.023887 4.024114
## 504 7.446868e-05 -2.631078 2.631227 -4.023926 4.024075
## 505 1.993100e-05 -2.631133 2.631172 -4.023981 4.024020
## 506 3.189940e-06 -2.631149 2.631156 -4.023997 4.024004
## 507 1.207912e-07 -2.631152 2.631153 -4.024000 4.024001
## 508 -1.138862e-07 -2.631153 2.631152 -4.024001 4.024000
mape(lnprice,fitted(data.arima201))
## [1] Inf
2118.72