
# com1d of january 2022,
# with a bit of analysis for the Johnson Johnson dataset
# of astsa

# module load R 

# install.packages("astsa")

# library(astsa)
# jj

# the book's figure 1.1: 
# plot(jj, type="o", ylab="Quarterly Earnings per Share")

# via nils jj_data etc.: 
# jjdata <- matrix(scan("jj_data",skip=4),byrow=T,ncol=5)
# dim is 21 x 5

# directly, this time, to avoid problems with not having the package:  
#       Qtr1      Qtr2      Qtr3      Qtr4

jjdata <- rbind(
c(1960,  0.710000,  0.630000,  0.850000,  0.440000),
c(1961,  0.610000,  0.690000,  0.920000,  0.550000),
c(1962,  0.720000,  0.770000,  0.920000,  0.600000),
c(1963,  0.830000,  0.800000,  1.000000,  0.770000),
c(1964,  0.920000,  1.000000,  1.240000,  1.000000),
c(1965,  1.160000,  1.300000,  1.450000,  1.250000),
c(1966,  1.260000,  1.380000,  1.860000,  1.560000),
c(1967,  1.530000,  1.590000,  1.830000,  1.860000),
c(1968,  1.530000,  2.070000,  2.340000,  2.250000),
c(1969,  2.160000,  2.430000,  2.700000,  2.250000),
c(1970,  2.790000,  3.420000,  3.690000,  3.600000),
c(1971,  3.600000,  4.320000,  4.320000,  4.050000),
c(1972,  4.860000,  5.040000,  5.040000,  4.410000),
c(1973,  5.580000,  5.850000,  6.570000,  5.310000),
c(1974,  6.030000,  6.390000,  6.930000,  5.850000),
c(1975,  6.930000,  7.740000,  7.830000,  6.120000),
c(1976,  7.740000,  8.910000,  8.280000,  6.840000),
c(1977,  9.540000, 10.260000,  9.540000,  8.729999),
c(1978, 11.880000, 12.060000, 12.150000,  8.910000),
c(1979, 14.040000, 12.960000, 14.850000,  9.990000),
c(1980, 16.200000, 14.670000, 16.020000, 11.610000))

year <- jjdata[ ,1]
yy1 <- jjdata[ ,2]
yy2 <- jjdata[ ,3]
yy3 <- jjdata[ ,4]
yy4 <- jjdata[ ,5]

yyall <-  c(t(jjdata[ ,2:5])) 
nnall <- length(yyall) # 84 

aux <- seq(1960 + 0, 1980 + 0.75, length=nnall)

par(bty="l")
par(cex.lab=1.4142) 

plot(aux,yyall,xlab="year",ylab="JJ score",type="o")

matplot(year,jjdata[ ,2:5],type="l")

xx <- aux - 1960
xx2 <- xx^2

summary( lm(yyall ~ xx) )
nils2 <- lm(yyall ~ xx + xx2)
summary( nils2 ) # very significant square term 

res2 <- nils2$res
plot(xx,res2,type="o") # increasing sigma

mm <- nrow(jjdata) # 21
yearly <- 0*(1:mm)
for (j in 1:mm)
{
yearly[j] <- mean(jjdata[j,c(2,3,4,5)])
}

zz <- 0:(mm-1)
zz2 <- zz^2

nils22 <- lm(yearly ~ zz + zz2)
summary(nils22)

#             Estimate Std. Error t value Pr(>|t|)    
#(Intercept)  1.005779   0.191868   5.242 5.51e-05 ***
#zz          -0.215062   0.044458  -4.837 0.000132 ***
#zz2          0.043497   0.002146  20.268 7.66e-14 ***

res22 <- nils22$res

plot(1960 + zz,yearly)

plot(1960 + zz,res22)

# nils22 <- lm(yearly, res22)

## ok, then, a regression model with increasing sigma too:
#  y is N( a + b x + c x^2, exp(gamma0 + gamma1 (x-xbar)) )

xbar <- mean(xx) 

logL33 <- function(para)
{
beta <- para[1:3]
gam <- para[4:5]
#
xi <- beta[1] + beta[2]*xx + beta[3]*xx^2
sigma <- exp(gam[1] + gam[2]*(xx-xbar))
#
well <- -log(sigma)-0.5*(yyall - xi)^2/sigma^2 - 0.5*log(2*pi)
sum(well)
}

start33 <- c(nils2$coef,1,0)

logL33(start33)

minuslogL33 <- function(para)
{-logL33(para)}

nils33 <- nlm(minuslogL33,start33,hessian=T)
ML33 <- nils33$estimate
Jhat33 <- nils33$hessian
se33 <- sqrt( diag(solve(Jhat33)) )
showme33 <- cbind(ML33,se33,ML33/se33)

print(round(showme33,4))

#       ML33   se33         
#[1,]  0.7603 0.0426  17.8569 beta0 
#[2,] -0.1121 0.0197  -5.6917 beta1
#[3,]  0.0353 0.0015  22.9026 beta2
#[4,] -0.8637 0.0772 -11.1848 gamma0
#[5,]  0.1510 0.0115  13.1485 gamma1

trend33 <- ML33[1] + ML33[2]*xx + ML33[3]*xx^2 

## then a little band around the trend: 

check <- 0*xx
Sigma00 <- solve(Jhat33)[1:3,1:3]

for (j in 1:length(xx))
{
xnow <- xx[j]
check[j] <- sqrt( t(c(1,xnow,xnow^2)) %*% Sigma00 %*% c(1,xnow,xnow^2) )
}

matplot(xx,check,type="l")

# making timeseries_fig11.pdf : 

par(bty="l")
par(cex.lab=1.4142) 

matplot(1960+xx,cbind(yyall,trend33),type="l",lwd=2.5,
   xlab="time",ylab="JJ data, and estimated trend")
 
matlines(1960+xx,trend33-qnorm(0.995)*check,type="l",lwd=2.5,col=4,lty=2)
matlines(1960+xx,trend33+qnorm(0.995)*check,type="l",lwd=2.5,col=4,lty=2)

res33 <- (yyall - trend33) / exp(ML33[4] + ML33[5]*(xx-xbar))
plot(density(res33)) 

plot(1960 + xx, res33) # looks good regarding normality and model 

acf(res33) # there's cyclic behaviour not captured by the 5-para model 
