library("quantmod")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
da=read.table("q-jnj-earns-9211.txt",header = T)
head(da)
##   day mon year earns
## 1  30   1 1992  0.11
## 2  23   4 1992  0.18
## 3  21   7 1992  0.18
## 4  20  10 1992  0.17
## 5   1   2 1993  0.12
## 6  29   4 1993  0.20
jnj=da$earns
jnj1=ts(jnj,frequency=12,start=c(1992,1))
par(mfcol=c(2,1))
plot(jnj1,xlab="year",ylab="returns")
title(main="(a): Simple returns")
acf(jnj,lag=24) # command to obtain sample ACF of the data

ln.jnj=log(jnj+1)
Box.test(jnj,lag=12,type="Ljung")
## 
##  Box-Ljung test
## 
## data:  jnj
## X-squared = 584.59, df = 12, p-value < 2.2e-16
Box.test(ln.jnj,lag=12,type="Ljung")
## 
##  Box-Ljung test
## 
## data:  ln.jnj
## X-squared = 598.48, df = 12, p-value < 2.2e-16
jnj2=ts(jnj,frequency=12,start=c(1992,1))
par(mfcol=c(2,1))
plot(jnj1,xlab="year",ylab="returns")
title(main="(a): Simple returns")
acf(jnj1,lag=24)

gnp=diff(ln.jnj)
dim(da)
## [1] 78  4
tdx=c(1:78)/4+1992
par(mfcol=c(2,1))
plot(tdx,ln.jnj,xlab="year",ylab="gnp",type="l")
plot(tdx[2:78],gnp,type="l",xlab="year",ylab="growth")

acf(gnp,lag=12)
pacf(gnp,lag=12) # compute PACF

m1=arima(gnp,order=c(3,0,0))
m1
## 
## Call:
## arima(x = gnp, order = c(3, 0, 0))
## 
## Coefficients:
##           ar1      ar2      ar3  intercept
##       -0.9547  -0.9342  -0.9461     0.0087
## s.e.   0.0324   0.0363   0.0277     0.0006
## 
## sigma^2 estimated as 0.0003623:  log likelihood = 192.15,  aic = -374.3
tsdiag(m1,gof=12)  # model checking discussed later

p1=c(1,-m1$coef[1:3]) # set-up the polynomial
r1=polyroot(p1) # solve the polynomial equation
r1
## [1]  0.014824+1.019333i -1.017089+0.000000i  0.014824-1.019333i
Mod(r1)
## [1] 1.019441 1.017089 1.019441
k=2*pi/acos(0.014824/1.019441)
k
## [1] 4.037376
mm1=ar(gnp,method="mle")
mm1$order 
## [1] 4
names(mm1)
##  [1] "order"        "ar"           "var.pred"     "x.mean"      
##  [5] "aic"          "n.used"       "n.obs"        "order.max"   
##  [9] "partialacf"   "resid"        "method"       "series"      
## [13] "frequency"    "call"         "asy.var.coef"
print(mm1$aic,digits=3)
##       0       1       2       3       4       5       6       7       8 
## 251.685 232.820 226.296  51.073   0.000   1.821   3.812   5.118   0.102 
##       9      10      11      12 
##   1.430   1.319   2.384   4.215
aic=mm1$aic  
length(aic)
## [1] 13
plot(c(0:12),aic,type="h",xlab="order",ylab="aic")
lines(0:12,aic,lty=2)

#Forecasting
m1=arima(jnj,order=c(0,0,9))
m1
## 
## Call:
## arima(x = jnj, order = c(0, 0, 9))
## 
## Coefficients:
##          ma1     ma2     ma3     ma4     ma5    ma6     ma7     ma8
##       1.0058  1.2457  1.2014  2.1391  1.5133  1.319  1.0087  1.2378
## s.e.  0.1272  0.1732  0.2721  0.2876  0.2363  0.215  0.2427  0.2446
##          ma9  intercept
##       0.3804     0.6094
## s.e.  0.1330     0.0791
## 
## sigma^2 estimated as 0.003708:  log likelihood = 96.24,  aic = -170.47
m1=arima(jnj,order=c(0,0,9),fixed=c(NA,0,NA,0,0,0,0,0,NA,NA))
m1
## 
## Call:
## arima(x = jnj, order = c(0, 0, 9), fixed = c(NA, 0, NA, 0, 0, 0, 0, 0, NA, NA))
## 
## Coefficients:
##          ma1  ma2     ma3  ma4  ma5  ma6  ma7  ma8      ma9  intercept
##       0.1294    0  0.6503    0    0    0    0    0  -0.2064     0.6002
## s.e.  0.2891    0  0.3004    0    0    0    0    0   0.2011     0.0468
## 
## sigma^2 estimated as 0.06913:  log likelihood = -7.43,  aic = 24.87
sqrt(0.06913)
## [1] 0.2629258
Box.test(m1$residuals,lag=12,type="Ljung")
## 
##  Box-Ljung test
## 
## data:  m1$residuals
## X-squared = 394.68, df = 12, p-value < 2.2e-16
pv=1-pchisq(394.68,9)
pv
## [1] 0
m1=arima(jnj[1:78],order=c(0,0,9),fixed=c(NA,0,NA,0,0,0,0,0,NA,NA))
m1
## 
## Call:
## arima(x = jnj[1:78], order = c(0, 0, 9), fixed = c(NA, 0, NA, 0, 0, 0, 0, 0, 
##     NA, NA))
## 
## Coefficients:
##          ma1  ma2     ma3  ma4  ma5  ma6  ma7  ma8      ma9  intercept
##       0.1294    0  0.6503    0    0    0    0    0  -0.2064     0.6002
## s.e.  0.2891    0  0.3004    0    0    0    0    0   0.2011     0.0468
## 
## sigma^2 estimated as 0.06913:  log likelihood = -7.43,  aic = 24.87
predict(m1,10)
## $pred
## Time Series:
## Start = 79 
## End = 88 
## Frequency = 1 
##  [1] 0.9486664 0.6352115 0.8469624 0.5757401 0.4878375 0.5478441 0.4812723
##  [8] 0.5757913 0.4822479 0.6002087
## 
## $se
## Time Series:
## Start = 79 
## End = 88 
## Frequency = 1 
##  [1] 0.2629332 0.2651269 0.2651269 0.3154777 0.3154777 0.3154777 0.3154777
##  [8] 0.3154777 0.3154777 0.3201099