ROLL NO.
KSMSCSTAT004 DATE: 09/10/2024
TIME SERIES ANALYSIS
Q.3]
A) ARIMA
> library(forecast)
> library(readxl)
> daburstock2024_1_ <- read_excel("C:/Users/KC/Desktop/NEEL PRAC
EXAM/daburstock2024 (1).xlsx")
> View(daburstock2024_1_)
> TS = ts(data[,3],frequency = 12) ; TS
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 563.00 562.35 562.70 567.30 571.65 572.50 556.00 551.40 556.00 552.95 557.50 561.90
2 557.85 549.95 543.95 544.40 537.40 537.55 535.95 537.70 545.60 575.50 559.00 544.80
3 537.40 538.45 541.00 540.00 543.75 539.90 545.25 547.35 547.85 550.20 551.20 549.85
4 546.50 544.55 537.10 538.20 541.45 539.35 544.95 539.70 536.80 536.00 537.75 538.40
5 532.30 531.40 530.80 530.35 526.55 530.00 521.20 525.65 527.35 522.25 524.40 526.00
6 530.60 532.55 532.40 531.95 510.00 509.85 508.50 506.95 507.65 497.00 505.60 507.95
7 505.80 508.00 510.45 510.30 510.25 511.55 512.80
> plot(TS)
$figure
[1] 1.38511182 1.15234679 -0.40432461 0.39484709 -3.85640286 -3.47432558 -3.05029095 -2.792300
[9] 0.03128466 2.89831787 3.97887665 3.73685996
$type
[1] "additive"
attr(,"class")
[1] "decomposed.ts"
> plot(decompose(TS))
We can see that data is of additive type
Trend is present in the data
Seasonality is present in the data
H0: THE PROCESS IS STATIONARY V/S H1: THE PROCESS IS NOT STATIONARY
> library(tseries)
> adf.test(TS)
Augmented Dickey-Fuller Test
data: TS
Dickey-Fuller = -3.1381, Lag order = 4, p-value = 0.1103
alternative hypothesis: stationary
SINCE P-VALUE > 0.05 WE SAY THAT IT IS NOT STATIONARY
> first = diff(TS, differences =1)
> adf.test(first)
Augmented Dickey-Fuller Test
data: first
Dickey-Fuller = -5.2658, Lag order = 4, p-value = 0.01
alternative hypothesis: stationary
HERE P-VALUE < 0.05 WE CAN SAY THAT PROCESS IS STATIONARY
> acf(TS)
> pacf(TS)
> acf(first)
> pacf(first)
> model=auto.arima(first)
> model
Series: first
ARIMA(0,0,0) with zero mean
sigma^2 = 41.27: log likelihood = -255.76
AIC=513.52 AICc=513.57 BIC=515.88
> forecast(model)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Aug 7 0 -8.232738 8.232738 -12.59089 12.59089
Sep 7 0 -8.232738 8.232738 -12.59089 12.59089
Oct 7 0 -8.232738 8.232738 -12.59089 12.59089
Nov 7 0 -8.232738 8.232738 -12.59089 12.59089
Dec 7 0 -8.232738 8.232738 -12.59089 12.59089
Jan 8 0 -8.232738 8.232738 -12.59089 12.59089
Feb 8 0 -8.232738 8.232738 -12.59089 12.59089
Mar 8 0 -8.232738 8.232738 -12.59089 12.59089
Apr 8 0 -8.232738 8.232738 -12.59089 12.59089
May 8 0 -8.232738 8.232738 -12.59089 12.59089
Jun 8 0 -8.232738 8.232738 -12.59089 12.59089
Jul 8 0 -8.232738 8.232738 -12.59089 12.59089
Aug 8 0 -8.232738 8.232738 -12.59089 12.59089
Sep 8 0 -8.232738 8.232738 -12.59089 12.59089
Oct 8 0 -8.232738 8.232738 -12.59089 12.59089
Nov 8 0 -8.232738 8.232738 -12.59089 12.59089
Dec 8 0 -8.232738 8.232738 -12.59089 12.59089
Jan 9 0 -8.232738 8.232738 -12.59089 12.59089
Feb 9 0 -8.232738 8.232738 -12.59089 12.59089
Mar 9 0 -8.232738 8.232738 -12.59089 12.59089
Apr 9 0 -8.232738 8.232738 -12.59089 12.59089
May 9 0 -8.232738 8.232738 -12.59089 12.59089
Jun 9 0 -8.232738 8.232738 -12.59089 12.59089
Jul 9 0 -8.232738 8.232738 -12.59089 12.59089
> plot(forecast(model))
Q3]
B) ARMA
> library(datasets)
> datasets::AirPassengers
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432
> start(AirPassengers)
[1] 1949 1
> end(AirPassengers)
[1] 1960 12
> time(AirPassengers)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct
1949 1949.000 1949.083 1949.167 1949.250 1949.333 1949.417 1949.500 1949.583
1949.667 1949.750
1950 1950.000 1950.083 1950.167 1950.250 1950.333 1950.417 1950.500 1950.583
1950.667 1950.750
1951 1951.000 1951.083 1951.167 1951.250 1951.333 1951.417 1951.500 1951.583
1951.667 1951.750
1952 1952.000 1952.083 1952.167 1952.250 1952.333 1952.417 1952.500 1952.583
1952.667 1952.750
1953 1953.000 1953.083 1953.167 1953.250 1953.333 1953.417 1953.500 1953.583
1953.667 1953.750
1954 1954.000 1954.083 1954.167 1954.250 1954.333 1954.417 1954.500 1954.583
1954.667 1954.750
1955 1955.000 1955.083 1955.167 1955.250 1955.333 1955.417 1955.500 1955.583
1955.667 1955.750
1956 1956.000 1956.083 1956.167 1956.250 1956.333 1956.417 1956.500 1956.583
1956.667 1956.750
1957 1957.000 1957.083 1957.167 1957.250 1957.333 1957.417 1957.500 1957.583
1957.667 1957.750
1958 1958.000 1958.083 1958.167 1958.250 1958.333 1958.417 1958.500 1958.583
1958.667 1958.750
1959 1959.000 1959.083 1959.167 1959.250 1959.333 1959.417 1959.500 1959.583
1959.667 1959.750
1960 1960.000 1960.083 1960.167 1960.250 1960.333 1960.417 1960.500 1960.583
1960.667 1960.750
Nov Dec
1949 1949.833 1949.917
1950 1950.833 1950.917
1951 1951.833 1951.917
1952 1952.833 1952.917
1953 1953.833 1953.917
1954 1954.833 1954.917
1955 1955.833 1955.917
1956 1956.833 1956.917
1957 1957.833 1957.917
1958 1958.833 1958.917
1959 1959.833 1959.917
1960 1960.833 1960.917
> is.ts(AirPassengers)
[1] TRUE
> frequency(AirPassengers)
[1] 12
> plot.ts(AirPassengers)
> abline(reg = lm(AirPassengers~time(AirPassengers)))
> acf(AirPassengers)
> AR = arima(AirPassengers,order = c(1,0,0))
> AR
Call:
arima(x = AirPassengers, order = c(1, 0, 0))
Coefficients:
ar1 intercept
0.9646 278.4649
s.e. 0.0214 67.1141
sigma^2 estimated as 1119: log likelihood = -711.09, aic = 1428.18
> AR_fit= AirPassengers - residuals(AR)
> plot(AR_fit)
> MA = arima(AirPassengers,order = c(0,0,1)); MA
Call:
arima(x = AirPassengers, order = c(0, 0, 1))
Coefficients:
ma1 intercept
0.9642 280.6464
s.e. 0.0214 10.5788
sigma^2 estimated as 4205: log likelihood = -806.43, aic = 1618.86
> MA_fit = AirPassengers-residuals(MA)
> plot(MA_fit)
> cor(AR_fit,MA_fit)
[1] 0.954995
> AIC(AR)
[1] 1428.179
> AIC(MA)
[1] 1618.863
> BIC(AR)
[1] 1437.089
> BIC(MA)
[1] 1627.772
From AIC AND BIC WE CAN CONCLUDE THAT AR MODEL IS BEST AMONG THE
OTHERS AS IT HAS LOW VALUES OF AIC AND BIC.