EPPS 6356 Assignment 9

EPPS 6356 Assignment 9: Time series data

  1. Collect your own time series data and import into R (e.g. you can use quantmod to collect stock data)

    Figure 1: Time Series Plot of Clover Health Insurance Stocks.

    Figure 2: Candlestick Time Series Plot of Clover Health Insurance Stocks.

  2. Examine class of time series object and variables:

    i. Trend

    Trend refers to the general movement of the data over time, such as in the example time series plots. The trend of the CLOV data had a fairly stable neutral trend until July 2021, which has since been on a general downwards trend.

    ii. Stationarity

    Stationarity is demonstrated by a flat looking series, without trend, constant variance over time, a constant autocorrelation structure over time and no periodic fluctuations (seasonality). On the other hand, nonstationarity is the status of a time series whose statistical properties are changing through time. The CLOV data generally is nonstationary, i.e., the CLOV data does have a general negative trend.

    iii. pdq

    A nonseasonal ARIMA model is classified as an “ARIMA(p,d,q)” model, where: p is the number of autoregressive terms, d is the number of nonseasonal differences needed for stationarity, and q is the number of lagged forecast errors in the prediction equation. The pdq of the CLOV data generally, at least since July 2021, could be generally predicted due to general negative trend and nonstationarity.

# Plotting time series data using TSstudio

lapply(c("quantmod", "tidyverse","TSstudio"), require, character.only = TRUE)
Loading required package: 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
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Loading required package: tidyverse
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   0.3.4 
✔ tibble  3.1.6      ✔ dplyr   1.0.10
✔ tidyr   1.2.0      ✔ stringr 1.4.0 
✔ readr   2.1.1      ✔ forcats 0.5.1 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::first()  masks xts::first()
✖ dplyr::lag()    masks stats::lag()
✖ dplyr::last()   masks xts::last()
Loading required package: TSstudio
[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE
library(TSstudio)
quantmod::getSymbols("CLOV", src="yahoo")
[1] "CLOV"
ts_plot(CLOV$CLOV.Adjusted, 
        title = "Clover Health Insurance Stock prices",
        Ytitle = "")
class(CLOV)
[1] "xts" "zoo"
# Plotting time series data using dygraph
lapply(c("quantmod", "tidyverse","dygraphs"), require, character.only = TRUE)
Loading required package: dygraphs
[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE
library(dygraphs)
par(family="Palatino")
quantmod::getSymbols("CLOV", src="yahoo")
[1] "CLOV"
class(CLOV)
[1] "xts" "zoo"
m = tail(CLOV, n=30)
m =m[,1:(ncol(m)-2)] # drop last two columns 
names(m)<-c('Open', 'High', 'Low', 'Close') # rename columns for plotting
path <- getwd()
setwd("~/Documents/Fall 2022/EPPS 6356/samantha-manuel.github.io") # place dygraph.css into the same directory
dygraph(m, main = "Clover Health Insurance Stock Prices (Candlestick Chart)")  |>  
  dyCandlestickGroup(c('Open', 'High', 'Low', 'Close')) |> 
  dyCandlestick()  |> 
  dyLegend(show = "always", hideOnMouseOut = T) |> 
  dyCSS("dygraph.css")