Expt. 12 Forecasting 214

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Rayat Shikshan Sanstha’s

Yashavantrao Chavan Institute of Science, Satara (Autonomous)


Department of Statistics
Time Series Analysis
Forecasting
Practical No: 12 Date:
Name: Gore MayurPundlik
Shashikant RajaramBagdi Roll No:214
232

______________________________________________________________________________
Q1. Aim: To check whether the forecast errors are normally distributed with mean zero and
constant variance. Also plot the ACF and PACF plot.
Code:
volcanodust=scan("http://robjhyndman.com/tsdldata/annual/dvi.dat",skip=1)
volcanodust
volcanoseries=ts(volcanodust,start=c(1500))
volcanoseries
plot.ts(volcanoseries)
acf(volcanoseries,lag.max = 20,plot=FALSE)
pacf(volcanoseries,lag.max=20,plot = FALSE)
library(forecast)
model=auto.arima(volcanoseries)
model
auto.arima(volcanoseries,ic="aic",trace = TRUE)
myfore=forecast::forecast(model,level=c(95),h=31)
myfore
plot(myfore)
plot.ts(myfore$residuals)
hist(myfore$residuals)
Output:
Autocorrelation of series
0 1 2 3 4 5 6 7 8 9 10
1 0.666 0.374 0.162 0.046 0.017 - 0.016 0.021 0.006 0.01
0.007
11 12 13 14 15 16 17 18 19 20
0.004 0.024 0.075 0.082 0.064 0.039 0.005 0.028 0.108 0.182
Partial autocorrelation of series
1 2 3 4 5 6 7 8 9 10
0.666 -0.126 -0.064 -0.005 0.04 -0.039 0.058 -0.016 -0.025 0.028
11 12 13 14 15 16 17 18 19 20
-0.008 0.036 0.082 -0.025 -0.014 0.008 -0.025 0.073 0.131 0.063

Series: volcanoseries
ARIMA(1,0,2) with non-zero mean
Coefficients:
ar1 ma1 ma2 mean
0.4723 0.2694 0.1279 57.5178
s.e. 0.0936 0.0969 0.0752 8.4883
sigma^2 = 4897: log likelihood = -2661.84
AIC=5333.68 AICc=5333.81 BIC=5354.45
Fitting models using approximations to speed things up...
ARIMA(2,0,2) with non-zero mean : 5334.08
ARIMA(0,0,0) with non-zero mean : 5614.16
ARIMA(1,0,0) with non-zero mean : 5336.8
ARIMA(0,0,1) with non-zero mean : 5404.21
ARIMA(0,0,0) with zero mean : 5758.893
ARIMA(1,0,2) with non-zero mean : 5331.912
ARIMA(0,0,2) with non-zero mean : 5350.019
ARIMA(1,0,1) with non-zero mean : 5332.533
ARIMA(1,0,3) with non-zero mean : 5333.205
ARIMA(0,0,3) with non-zero mean : 5334.578
ARIMA(2,0,1) with non-zero mean : 5332.595
ARIMA(2,0,3) with non-zero mean : 5334.89
ARIMA(1,0,2) with zero mean : 5360.125
Now re-fitting the best model(s) without approximations...
ARIMA(1,0,2) with non-zero mean : 5333.682
Best model: ARIMA(1,0,2) with non-zero mean
Series: volcanoseries
ARIMA(1,0,2) with non-zero mean
Coefficients:
ar1 ma1 ma2 mean
0.4723 0.2694 0.1279 57.5178
s.e. 0.0936 0.0969 0.0752 8.4883
sigma^2 = 4897: log likelihood = -2661.84
AIC=5333.68 AICc=5333.81 BIC=5354.45
Point Forecast Lo 95 Hi 95
1970 22.67720 -114.4732 159.8276
1971 38.42748 -132.3340 209.1890
1972 48.50091 -134.4268 231.4286
1973 53.25886 -132.2742 238.7920
1974 55.50617 -130.6032 241.6155
1975 56.56763 -129.6701 242.8053
1976 57.06898 -129.1973 243.3353
1977 57.30579 -128.9669 243.5785
1978 57.41764 -128.8565 243.6917
1979 57.47046 -128.8040 243.7449
1980 57.49542 -128.7791 243.7699
1981 57.50720 -128.7673 243.7817
1982 57.51277 -128.7617 243.7873
1983 57.51540 -128.7591 243.7899
1984 57.51664 -128.7579 243.7912
1985 57.51723 -128.7573 243.7917
1986 57.51750 -128.7570 243.7920
1987 57.51764 -128.7569 243.7921
1988 57.51770 -128.7568 243.7922
1989 57.51773 -128.7568 243.7922
1990 57.51774 -128.7568 243.7923
1991 57.51775 -128.7568 243.7923
1992 57.51775 -128.7568 243.7923
1993 57.51775 -128.7568 243.7923
1994 57.51775 -128.7568 243.7923
1995 57.51775 -128.7568 243.7923
1996 57.51775 -128.7568 243.7923
1997 57.51775 -128.7568 243.7923
1998 57.51775 -128.7568 243.7923
1999 57.51775 -128.7568 243.7923
2000 57.51775 -128.7568 243.7923
Result:
a) We see from the correlogram that the autocorrelations for lags 1, 2 and 3 exceed the
significance bounds, and that the autocorrelations tail off to zero after lag 3. The
autocorrelations for lags 1, 2, 3 are positive, and decrease in magnitude with increasing lag
(lag 1: 0.666, lag 2: 0.374, lag 3: 0.162).
b) From the partial auto correlogram, we see that the partial autocorrelation at lag 1 is
positive and exceeds the significance bounds (0.666), while the partial autocorrelation at lag
2 is negative and also exceeds the significance bounds (-0.126). The partial autocorrelations
tail off to zero after lag 2.
c) The time plot of forecast errors shows that the forecast errors seem to have roughly
constant variance over time. However, the time series of forecast errors seems to have a
negative mean, rather than a zero mean. We can confirm this by calculating the mean forecast
error, which turns out to be about -0.22:
d) The histogram of forecast errors (above) shows that although the mean value of the
forecast errors is negative, the distribution of forecast errors is skewed to the right compared
to a normal curve. Therefore, it seems that we cannot comfortably conclude that the forecast
errors are normally distributed with mean zero and constant variance.

Q2. Aim: To forecast weekly demand.


Code:
data=read.csv("C:\\Users\\LENOVO\\Downloads\\Train_timeseries.csv")
head(data)
data1=ts(data,start=c(1))
head(data1)
plot.ts(data1,main="Timeplot")
library(forecast)
mymodel=auto.arima(data)
mymodel
auto.arima(data,ic="aic",trace = TRUE)
myforecast=forecast::forecast(mymodel,level=c(95),h=10)
myforecast
plot(myforecast)
Solution:
Series: data
ARIMA(2,1,1)

Coefficients:
ar1 ar2 ma1
-0.2817 0.0529 0.3107
s.e. 0.1548 0.0081 0.1550

sigma^2 = 1679: log likelihood = -93846.77


AIC=187701.5 AICc=187701.5 BIC=187732.8

Fitting models using approximations to speed things up...

ARIMA(2,1,2) with drift : Inf


ARIMA(0,1,0) with drift : 187744.8
ARIMA(1,1,0) with drift : 187732
ARIMA(0,1,1) with drift : 187732.3
ARIMA(0,1,0) : 187742.8
ARIMA(2,1,0) with drift : 187700.8
ARIMA(3,1,0) with drift : 187702.3
ARIMA(2,1,1) with drift : 187697.6
ARIMA(1,1,1) with drift : 187719.7
ARIMA(3,1,1) with drift : Inf
ARIMA(1,1,2) with drift : Inf
ARIMA(3,1,2) with drift : Inf
ARIMA(2,1,1) : 187696.1
ARIMA(1,1,1) : 187717.7
ARIMA(2,1,0) : 187698.8
ARIMA(3,1,1) : Inf
ARIMA(2,1,2) : 187697.1
ARIMA(1,1,0) : 187730
ARIMA(1,1,2) : Inf
ARIMA(3,1,0) : 187700.4
ARIMA(3,1,2) : Inf

Now re-fitting the best model(s) without approximations...

ARIMA(2,1,1) : 187701.5

Best model: ARIMA(2,1,1)

Series: data
ARIMA(2,1,1)

Coefficients:
ar1 ar2 ma1
-0.2817 0.0529 0.3107
s.e. 0.1548 0.0081 0.1550

sigma^2 = 1679: log likelihood = -93846.77


AIC=187701.5 AICc=187701.5 BIC=187732.8
Point Forecast Lo 95 Hi 95
18289 526.9180 446.6032 607.2328
18290 526.4782 411.2381 641.7182
18291 526.2272 382.2921 670.1623
18292 526.2746 358.9376 693.6117
18293 526.2480 338.2013 714.2946
18294 526.2580 319.6375 732.8784
18295 526.2537 302.5693 749.9382
18296 526.2555 286.7299 765.7810
18297 526.2548 271.8687 780.6408
18298 526.2551 257.8315 794.6786
Result:
a) From the Time plot we observe that the series is not stationary, therefore by the time series
of first differences appears to be stationary in mean and variance, and so an ARIMA(p,1,q)
model is probably appropriate
b) It is likely that our ARIMA (0,1,1) model for the time series of weekly Demand is the best
model that we could make.

Q3. Aim: To forecast the traffic on Jet Rail for the next 7 months.
Code:
data=read.csv("C:\\Users\\LENOVO\\Downloads\\Train_SU63ISt.csv")
head(data)
plot.ts(data)
mymode=auto.arima(data)
mymode
auto.arima(data,ic="aic",trace=TRUE)
forecast1=forecast::forecast(mymode,level=c(95),h=24)
forecast1
plot(forecast1)
Solution:

Series: data
ARIMA(2,1,1)

Coefficients:
ar1 ar2 ma1
-0.2817 0.0529 0.3107
s.e. 0.1548 0.0081 0.1550

sigma^2 = 1679: log likelihood = -93846.77


AIC=187701.5 AICc=187701.5 BIC=187732.8

Fitting models using approximations to speed things up...

ARIMA(2,1,2) with drift : Inf


ARIMA(0,1,0) with drift : 187744.8
ARIMA(1,1,0) with drift : 187732
ARIMA(0,1,1) with drift : 187732.3
ARIMA(0,1,0) : 187742.8
ARIMA(2,1,0) with drift : 187700.8
ARIMA(3,1,0) with drift : 187702.3
ARIMA(2,1,1) with drift : 187697.6
ARIMA(1,1,1) with drift : 187719.7
ARIMA(3,1,1) with drift : Inf
ARIMA(1,1,2) with drift : Inf
ARIMA(3,1,2) with drift : Inf
ARIMA(2,1,1) : 187696.1
ARIMA(1,1,1) : 187717.7
ARIMA(2,1,0) : 187698.8
ARIMA(3,1,1) : Inf
ARIMA(2,1,2) : 187697.1
ARIMA(1,1,0) : 187730
ARIMA(1,1,2) : Inf
ARIMA(3,1,0) : 187700.4
ARIMA(3,1,2) : Inf

Now re-fitting the best model(s) without approximations...

ARIMA(2,1,1) : 187701.5

Best model: ARIMA(2,1,1)

Series: data
ARIMA(2,1,1)

Coefficients:
ar1 ar2 ma1
-0.2817 0.0529 0.3107
s.e. 0.1548 0.0081 0.1550

sigma^2 = 1679: log likelihood = -93846.77


AIC=187701.5 AICc=187701.5 BIC=187732.8
Point Forecast Lo 95 Hi 95
18289 526.9180 446.6032 607.2328
18290 526.4782 411.2381 641.7182
18291 526.2272 382.2921 670.1623
18292 526.2746 358.9376 693.6117
18293 526.2480 338.2013 714.2946
18294 526.2580 319.6375 732.8784
18295 526.2537 302.5693 749.9382
18296 526.2555 286.7299 765.7810
18297 526.2548 271.8687 780.6408
18298 526.2551 257.8315 794.6786
18299 526.2549 244.4917 808.0181
18300 526.2550 231.7560 820.7540
18301 526.2550 219.5485 832.9614
18302 526.2550 207.8087 844.7013
18303 526.2550 196.4865 856.0234
18304 526.2550 185.5404 866.9696
18305 526.2550 174.9351 877.5748
18306 526.2550 164.6408 887.8692
18307 526.2550 154.6315 897.8785
18308 526.2550 144.8848 907.6251
18309 526.2550 135.3811 917.1289
18310 526.2550 126.1030 926.4069
18311 526.2550 117.0352 935.4747
18312 526.2550 108.1641 944.3458
Result:
a) From the Time plot we observe that the series is not stationary, therefore by the The time
series of first differences appears to be stationary in mean and variance, and so an
ARIMA(p.1,q) model is probably appropriate
b) It is likely that our ARIMA(2,1,1) model for the time series of Demand is the best model
that we could make

You might also like