AR Model Session2 Output: Install - Packages ("Forecast")
AR Model Session2 Output: Install - Packages ("Forecast")
> install.packages("forecast")
WARNING: Rtools is required to build R packages but is not currently
installed. Please download and install the appropriate version of Rtools
before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/maddu/OneDrive/Documents/R/win-
library/4.0’
(as ‘lib’ is unspecified)
trying URL
'https://cran.rstudio.com/bin/windows/contrib/4.0/forecast_8.13.zip'
Content type 'application/zip' length 2538207 bytes (2.4 MB)
downloaded 2.4 MB
Box-Ljung test
data: z_t
X-squared = 32.311, df = 12, p-value = 0.001239
Call:
ar(x = z_t, method = "mle")
Coefficients:
1
0.5548
Coefficients:
ar1 mean
0.5548 9.9661
s.e. 0.0915 0.2325
Box-Ljung test
data: res
X-squared = 8.1934, df = 12, p-value = 0.7698
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
> df@teststat
tau2 phi1
statistic -0.416982 40.33106
> df@cval
1pct 5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1 6.70 4.71 3.86
> z_t <- diff(z_t, 1)
> df= ur.df(z_t,type = "drift")
> df@teststat
tau2 phi1
statistic -6.994696 24.47025
> df@cval
1pct 5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1 6.70 4.71 3.86
> acf(z_t,plot = TRUE)
> pacf(z_t)
> ord= ar(z_t,method = "mle")
> ord
Call:
ar(x = z_t, method = "mle")
Coefficients:
mean
5.0585
s.e. 0.0974
Box-Ljung test
data: log_ret
X-squared = 13.579, df = 1, p-value = 0.0002287
> acf(log_ret)
> ord= ar(log_ret,method = "mle")
> ord
Call:
ar(x = log_ret, method = "mle")
Coefficients:
1 2 3 4 5 6 7 8
9 10 11 12
0.0308 -0.0447 0.0034 -0.0090 -0.0130 -0.0041 -0.0203 0.0123
-0.0115 0.0145 -0.0164 0.0284
Coefficients:
ar1 ar2 mean
0.0298 -0.0431 3e-04
s.e. 0.0078 0.0078 1e-04
Box-Ljung test
data: AutoArimaModel$residuals
X-squared = 0.00026939, df = 1, p-value = 0.9869
FTSF Session-4
> install.packages("forecast")
Error in install.packages : Updating loaded packages
> library("forecast")
> library("urca")
> z_t= c(rep(0,100))
> y_t = rnorm(100, 0,1)
> z_t[1] = 5+y_t[1]
>
> for(i in 2:100){
+ z_t[i]= 5+0.5*y_t[(i-1)]+ y_t[i]
+ }
> install.packages("forecast")
WARNING: Rtools is required to build R packages but is not currently
installed. Please download and install the appropriate version of Rtools
before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Warning in install.packages :
package ‘forecast’ is in use and will not be installed
> z_t= c(rep(0,100))
> y_t = rnorm(100, 0,1)
> z_t[1] = 5+y_t[1]
>
> for(i in 2:100){
+ z_t[i]= 5+0.5*y_t[(i-1)]+ y_t[i]
+ }
> ts.plot(z_t)
> z_t=arima.sim(list(order = c(0,0,1), ma=0.5), n = 100)+5
> z_t
Time Series:
Start = 1
End = 100
Frequency = 1
[1] 5.0242342 4.8453968 4.8584243 4.6033240 5.2688283 4.9035807
7.4560917 7.1434442 6.6476980 6.2253627 3.7865403
[12] 3.5646702 5.1747833 5.3783744 3.6393971 4.5083998 7.0351671
5.4861782 6.0007565 4.6842743 4.1434144 4.8573163
[23] 5.9800631 5.2176776 4.0164573 4.8164887 6.6463998 6.0340895
4.8407095 4.0113495 5.5214101 6.3407039 6.0681925
[34] 5.5117255 4.2886997 3.9148973 6.4789170 4.5936915 5.8728098
5.6468288 3.5105896 5.2583142 4.0316147 4.4844996
[45] 6.8634453 6.7061088 5.6659409 5.7434562 5.5568100 5.6543257
5.0586071 5.2699855 5.3627883 5.0589015 5.5112081
[56] 5.7995450 6.4279352 4.7815953 4.4643449 6.7636955 6.1252957
5.9109806 5.2541809 5.4207764 5.1482033 6.0165262
[67] 4.5758677 3.3746121 5.0181018 5.2068377 5.4112417 5.1176143
4.6874169 6.7451481 7.0376738 4.9990650 5.1852922
[78] 3.3433849 1.1951205 0.5995327 4.1751790 4.9307716 5.2869642
4.0007528 5.4996597 6.3468528 5.7718259 3.4220988
[89] 4.4099023 5.4486823 5.0441666 5.3574729 4.5157895 5.7378482
6.2336834 5.6399210 5.1568855 5.4415682 4.9350828
[100] 5.6317134
> df= ur.df(z_t,type = "drift")
> df
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
> df@teststat
tau2 phi1
statistic -6.629055 21.9753
> df@cval
1pct 5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1 6.70 4.71 3.86
> lju_box= Box.test(z_t,lag = 12,type = "Ljung-Box")
> lju_box
Box-Ljung test
data: z_t
X-squared = 35.711, df = 12, p-value = 0.0003606
> acf(z_t,plot = TRUE)
> ord= ar(z_t,method = "mle")
> ord
Call:
ar(x = z_t, method = "mle")
Coefficients:
1 2
0.5031 -0.2209
Coefficients:
ma1 mean
0.5154 5.1873
s.e. 0.0852 0.1457
Box-Ljung test
data: AutoArimaModel$residuals
X-squared = 8.6756, df = 12, p-value = 0.7304
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
> df@teststat
tau2 phi1
statistic -6.629055 21.9753
> df@cval
1pct 5pct 10pct
tau2 -3.51 -2.89 -2.58
phi1 6.70 4.71 3.86
> ts.plot(data)
> log_ret= c(rep(0,l))
>
> for(i in 2:l){
+ log_ret[i]= log(data[i])-log(data[(i-1)])
+ }
> log_ret= log_ret[-1]
> df= ur.df(log_ret,type = "drift")
> df@teststat
tau2 phi1
statistic -67.23783 2260.463
> df@cval
1pct 5pct 10pct
tau2 -3.43 -2.86 -2.57
phi1 6.43 4.59 3.78
> ts.plot(log_ret)
> kutosis(log_ret)
Error in kutosis(log_ret) : could not find function "kutosis"
> kurtosis(log_ret)
[1] 44.94525
> Box.test(log_ret,type = "Ljung-Box")
Box-Ljung test
data: log_ret
X-squared = 9.4741, df = 1, p-value = 0.002084
> acf(log_ret)
> pacf(log_ret)
> AutoArimaModel=auto.arima(log_ret)
> AutoArimaModel
Series: log_ret
ARIMA(0,0,2) with non-zero mean
Coefficients:
ma1 ma2 mean
-0.0362 -0.0578 3e-04
s.e. 0.0113 0.0115 1e-04
FTSF Session 5 R
> library("forecast")
> library("urca")
> library("TSA")
Registered S3 methods overwritten by 'TSA':
method from
fitted.Arima forecast
plot.Arima forecast
kurtosis, skewness
The following objects are masked from ‘package:stats’:
acf, arima
tar
Warning message:
package ‘TSA’ was built under R version 4.0.3
> e_t= rnorm(100,0,1)
> y_t= rnorm(100,0,1)
> timeseries= c(rep(0,100))
> timeseries[1]= 5+y_t[1]
> for(i in 2:100){
+ timeseries[i]= 5+ 0.5*timeseries[i-1]+ e_t[i]+ 0.6*e_t[i-1]
+ }
> ts.plot(timeseries)
> ur.df(timeseries,type = "drift")
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
Coefficients:
ar1 ma1 mean
0.6574 0.4983 9.6784
s.e. 0.0921 0.1068 0.4320
> library("forecast")
> library("urca")
> library("TSA")
> library("qrmdata")
> data= data("JPY_USD")
> data= JPY_USD
> l= length(data)
> #reading the data
> data= data$`JPY/USD`
> # making numeric
> data= as.numeric(data)
> l
[1] 5844
> ts.plot(data)
> a= ur.df(data,type = "drift")
> a= ur.df(data,type = "drift")
> a
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
> ts.plot(log_ret)
> Box.test(log_ret,type = "Ljung-Box")
Box-Ljung test
data: log_ret
X-squared = 88.708, df = 1, p-value < 2.2e-16
> acf(log_ret)
> pacf(log_ret)
> eacf(log_ret)
AR/MA
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 x x o x o o o o o o o o o x
1 x x o x o o o o o o o o o x
2 x x x o o o o o o o o o o x
3 x x x o o o o o o o o o o o
4 x x x x x o o o o o o o o o
5 x x x x x o o o o o o o o x
6 x x x x x o o o o o o o o o
7 x x x x x x o o o o o o o o
> AutoArimaModel=auto.arima(log_ret)
> AutoArimaModel
Series: log_ret
ARIMA(3,0,1) with zero mean
Coefficients:
ar1 ar2 ar3 ma1
0.6692 -0.0518 -0.0351 -0.5478
s.e. 0.2035 0.0297 0.0131 0.2035
sigma^2 estimated as 2.126e-05: log likelihood=23142
AIC=-46274 AICc=-46273.99 BIC=-46240.63
> Box.test(AutoArimaModel$residuals,type = "Ljung-Box")
Box-Ljung test
data: AutoArimaModel$residuals
X-squared = 0.0011124, df = 1, p-value = 0.9734
> library("forecast")
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Warning message:
package ‘forecast’ was built under R version 4.0.3
> library("urca")
Warning message:
package ‘urca’ was built under R version 4.0.3
> e_t= rnorm(100,0,1)
> y_t= rnorm(100,0,1)
> timeseries= c(rep(0,100))
> timeseries[1]= 5+y_t[1]
>
> for(i in 2:100){
+ timeseries[i]= 5+ 0.5*timeseries[i-1]+ e_t[i]+ 0.6*e_t[i-1]
+ }
>
> ts.plot(timeseries)
> ur.df(timeseries,type = "drift")
###############################################################
# Augmented Dickey-Fuller Test Unit Root / Cointegration Test #
###############################################################
> AutoArimaModel=auto.arima(train_series)
Error in as.ts(x) : object 'train_series' not found
> train_series=timeseries[1:90]
> test_series=timeseries[91:100]
> AutoArimaModel=auto.arima(train_series)
> AutoArimaModel
Series: train_series
ARIMA(1,0,1) with non-zero mean
Coefficients:
ar1 ma1 mean
0.5163 0.6327 9.6035
s.e. 0.1145 0.0896 0.3420
Coefficients:
ar1 ma1 mean
0.5056 0.6414 9.5476
s.e. 0.1088 0.0840 0.3135
Box-Ljung test
data: log_ret
X-squared = 88.708, df = 1, p-value < 2.2e-16
> acf(log_ret)
> pacf(log_ret)
> eacf(log_ret)
Error in eacf(log_ret) : could not find function "eacf"
> eacf(log_ret)
Error in eacf(log_ret) : could not find function "eacf"
> library("TSA")
acf, arima
tar
Warning message:
package ‘TSA’ was built under R version 4.0.3
> library("qrmdata")
> eacf(log_ret)
AR/MA
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 x x o x o o o o o o o o o x
1 x x o x o o o o o o o o o x
2 x x x o o o o o o o o o o x
3 x x x o o o o o o o o o o o
4 x x x x x o o o o o o o o o
5 x x x x x o o o o o o o o x
6 x x x x x o o o o o o o o o
7 x x x x x x o o o o o o o o
> arima_order= auto.arima(log_ret)
Error in auto.arima(log_ret) : could not find function "auto.arima"
> auto.arima(log_ret)
Error in auto.arima(log_ret) : could not find function "auto.arima"
> library("TSA")
> library("qrmdata")
> library("urca")
Warning message:
package ‘urca’ was built under R version 4.0.3
> auto.arima(log_ret)
Error in auto.arima(log_ret) : could not find function "auto.arima"
> arima_order= auto.arima(log_ret)
Error in auto.arima(log_ret) : could not find function "auto.arima"
> arima_order= auto.arima(log_ret)
Error in auto.arima(log_ret) : could not find function "auto.arima"
> library("forecast")
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Registered S3 methods overwritten by 'forecast':
method from
fitted.Arima TSA
plot.Arima TSA
Warning message:
package ‘forecast’ was built under R version 4.0.3
> auto.arima(log_ret)
Series: log_ret
ARIMA(3,0,1) with zero mean
Coefficients:
ar1 ar2 ar3 ma1
0.6692 -0.0518 -0.0351 -0.5478
s.e. 0.2035 0.0297 0.0131 0.2035
sigma^2 estimated as 2.126e-05: log likelihood=23142
AIC=-46274 AICc=-46273.99 BIC=-46240.63
> arima0(log_ret, order = (2,0,3))
Error: unexpected ',' in "arima0(log_ret, order = (2,"
> arima(log_ret, order = (2,0,3))
Error: unexpected ',' in "arima(log_ret, order = (2,"
> arima(log_ret, order= c(2,0,3))
Call:
arima(x = log_ret, order = c(2, 0, 3))
Coefficients:
ar1 ar2 ma1 ma2 ma3 intercept
0.267 -0.6406 -0.1449 0.6438 0.0665 0e+00
s.e. NaN 0.0163 NaN NaN NaN 1e-04
Box-Ljung test
data: AutoArimaModel$residuals
X-squared = 0.89449, df = 1, p-value = 0.3443
> l
[1] 5844
> predict= c(rep(0,10))
>
>
> for(q in 1:10){
+
+ start= q
+ end= 5833+q
+
+
+ train_data= log_ret[start:end]
+ arima_fit= arima(train_data,order = c(3,0,1)) #order should be
the same of auto arima
+ rolling_forecast= predict(arima_fit,1)
+ predict[q]= rolling_forecast$pred
+ }
> predict
[1] 2.924681e-05 9.369404e-05 3.718153e-04 2.267182e-04 -1.198793e-04
-1.415584e-04 -2.104004e-04 -7.275641e-05
[9] -1.329470e-04 9.385690e-05
> se_1= c(rep(0,10))
> for(i in 1:10){
+ se_1[i]= (test_series[i]- predict[i])^2
+ }
>
> mse_1= sum(se_1[1:10])/10
> mse_1
[1] 83.97259