## Installing package into 'C:/Users/linwe/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## Installing package into 'C:/Users/linwe/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## 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
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

Loading Data and Data Exploration

I am going to use the Adjusted Stock Price of SPY from past year 2011 - 2022. And after loading the data, I need to convert them into time series.

fro = '2021-11-30'
SPY = getSymbols('SPY',auto.assign = F,from=fro)
str(SPY)
## An 'xts' object on 2021-11-30/2022-12-02 containing:
##   Data: num [1:255, 1:6] 462 462 451 459 456 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "SPY.Open" "SPY.High" "SPY.Low" "SPY.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2022-12-03 13:38:49"
head(SPY)
##            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 2021-11-30   462.00   464.03  455.30    455.56  148559600     448.7692
## 2021-12-01   461.64   464.67  450.29    450.50  131939200     443.7846
## 2021-12-02   450.73   459.07  450.31    457.40  127637800     450.5818
## 2021-12-03   459.17   460.30  448.92    453.42  137331600     446.6611
## 2021-12-06   456.13   460.79  453.56    458.79   98977500     451.9511
## 2021-12-07   464.41   468.88  458.65    468.28   95484700     461.2996
summary(SPY)
##      Index               SPY.Open        SPY.High        SPY.Low     
##  Min.   :2021-11-30   Min.   :349.2   Min.   :359.8   Min.   :348.1  
##  1st Qu.:2022-03-02   1st Qu.:389.8   1st Qu.:393.6   1st Qu.:385.5  
##  Median :2022-06-02   Median :412.1   Median :415.7   Median :409.5  
##  Mean   :2022-06-02   Mean   :415.6   Mean   :419.1   Mean   :411.7  
##  3rd Qu.:2022-09-01   3rd Qu.:444.0   3rd Qu.:447.1   3rd Qu.:439.2  
##  Max.   :2022-12-02   Max.   :479.2   Max.   :480.0   Max.   :476.1  
##    SPY.Close       SPY.Volume         SPY.Adjusted  
##  Min.   :356.6   Min.   : 30545400   Min.   :356.6  
##  1st Qu.:389.7   1st Qu.: 72533000   1st Qu.:388.5  
##  Median :412.4   Median : 87633800   Median :410.3  
##  Mean   :415.5   Mean   : 95239522   Mean   :412.7  
##  3rd Qu.:444.1   3rd Qu.:113465250   3rd Qu.:440.4  
##  Max.   :477.7   Max.   :251783900   Max.   :472.2
# Combine ad
s <- cbind(SPY$SPY.Adjusted)
head(s)
##            SPY.Adjusted
## 2021-11-30     448.7692
## 2021-12-01     443.7846
## 2021-12-02     450.5818
## 2021-12-03     446.6611
## 2021-12-06     451.9511
## 2021-12-07     461.2996
tss <- ts_ts(s)

Data Plots and ARIMA Model

We can see the general stock prices of last year. After fitting the Arima model to forecast the data, we get to see a typical wide range of the forcast results.

fit_ts <- StructTS(tss, "level")
fit_ts
## 
## Call:
## StructTS(x = tss, type = "level")
## 
## Variances:
##   level  epsilon  
##  23.953    5.681
plot(tss, type = "o", col = "grey")
lines(fitted(fit_ts), lty = "dashed", lwd = 2)
lines(tsSmooth(fit_ts), lty = "dotted", lwd = 2, col = "blue")

# Fit an arima model
fit2 <- arima(tss, c(1, 1, 0))
predict(fit2, 12)
## $pred
## Time Series:
## Start = 2022.9192728119 
## End = 2022.94938978898 
## Frequency = 365.2425 
##  [1] 406.9786 406.9686 406.9701 406.9699 406.9699 406.9699 406.9699 406.9699
##  [9] 406.9699 406.9699 406.9699 406.9699
## 
## $se
## Time Series:
## Start = 2022.9192728119 
## End = 2022.94938978898 
## Frequency = 365.2425 
##  [1]  5.847199  7.689171  9.236757 10.551113 11.720128 12.782521 13.763172
##  [8] 14.678448 15.539910 16.356062 17.133381 17.876932
autoplot(forecast(fit2, 12))

Data Plots and Kalman Filter

mod <- fit2$model
for (y in 1:3) {
  pr <- KalmanForecast(4, mod, TRUE)
  print(list(pred = pr$pred + fit2$coef["intercept"],
             se = sqrt(pr$var * fit2$sigma2)))
  mod <- attr(pr, "mod")
}
## $pred
## [1] NA NA NA NA
## 
## $se
## [1]  5.847199  7.689171  9.236757 10.551113
## 
## $pred
## [1] NA NA NA NA
## 
## $se
## [1] 11.72013 12.78252 13.76317 14.67845
## 
## $pred
## [1] NA NA NA NA
## 
## $se
## [1] 15.53991 16.35606 17.13338 17.87693
fit <- KalmanLike(tss, mod, nit = 0L, update = FALSE)
out <- KalmanSmooth(tss, mod, nit = 0L)
Kalman_est <- out$smooth[,2]
plot(Kalman_est)
lines(Kalman_est, col = "yellow")

auto.arima(tss)
## Series: tss 
## ARIMA(1,1,1) with drift 
## 
## Coefficients:
##          ar1      ma1    drift
##       0.2076  -0.3615  -0.1130
## s.e.  0.3205   0.2937   0.2468
## 
## sigma^2 = 34.79:  log likelihood = -833.77
## AIC=1675.54   AICc=1675.65   BIC=1691.16
autoplot(tss)

Conclustion

I would anticipate that SPY will have a trend of dropping dowm after 2022 and be cautious on the SPY investmnet.