Introduction

This R is the assignment of week 7 discussion for the course of Predictive Analytics. The task is : Pick a time series of interest to you. For the time series data you selected, use neural nets to build forecasts. Interpret.

knitr::opts_chunk$set(echo = TRUE)

library(forecast)
## Warning: package 'forecast' was built under R version 4.0.5
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
library(sna)
## Warning: package 'sna' was built under R version 4.0.5
## Loading required package: statnet.common
## Warning: package 'statnet.common' was built under R version 4.0.5
## 
## Attaching package: 'statnet.common'
## The following objects are masked from 'package:base':
## 
##     attr, order
## Loading required package: network
## Warning: package 'network' was built under R version 4.0.5
## 
## 'network' 1.17.1 (2021-06-12), part of the Statnet Project
## * 'news(package="network")' for changes since last version
## * 'citation("network")' for citation information
## * 'https://statnet.org' for help, support, and other information
## sna: Tools for Social Network Analysis
## Version 2.6 created on 2020-10-5.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##  For citation information, type citation("sna").
##  Type help(package="sna") to get started.
library(lessR)
## Warning: package 'lessR' was built under R version 4.0.5
## 
## lessR 4.1.6  feedback: gerbing@pdx.edu  web: lessRstats.com/new
## ---------------------------------------------------------------
## > d <- Read("")   Read text, Excel, SPSS, SAS, or R data file
##   d is default data frame, data= in analysis routines optional
## 
## Learn about reading, writing, and manipulating data, graphics,
## testing means and proportions, regression, factor analysis,
## customization, and descriptive statistics from pivot tables.
##   Enter:  browseVignettes("lessR")
## 
## View changes in this or recent versions of lessR.
##   Enter: help(package=lessR)  Click: Package NEWS
##   Enter: interact()  for access to interactive graphics
##   New function: reshape_long() to move data from wide to long
library(feasts)
## Loading required package: fabletools
## 
## Attaching package: 'fabletools'
## The following object is masked from 'package:lessR':
## 
##     model
## The following object is masked from 'package:sna':
## 
##     components
## The following objects are masked from 'package:forecast':
## 
##     accuracy, forecast
library(seasonal)
library(readxl)
library(tsibble)
## 
## Attaching package: 'tsibble'
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x dplyr::recode() masks lessR::recode()
## x tibble::view()  masks seasonal::view()
library(fpp2)  
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v fma       2.4     v expsmooth 2.3
## -- Conflicts ------------------------------------------------- fpp2_conflicts --
## x fabletools::forecast() masks forecast::forecast()
library(readr)     # for reading data
library(dplyr)     # for manipulating data
library(lubridate) # for date and time manipulation
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:tsibble':
## 
##     interval
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(ggplot2)
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:tsibble':
## 
##     index
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR

Descriptive

The purpose of this section is to preprocess and describe the dataset. The dataset used was the daily stock data from APPPLE

data <- getSymbols("AAPL", auto.assign=FALSE, from="2014-01-01", to="2019-12-31")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
summary(data)
##      Index              AAPL.Open       AAPL.High        AAPL.Low    
##  Min.   :2014-01-02   Min.   :17.68   Min.   :17.91   Min.   :17.63  
##  1st Qu.:2015-07-02   1st Qu.:26.97   1st Qu.:27.22   1st Qu.:26.70  
##  Median :2016-12-29   Median :32.30   Median :32.62   Median :32.08  
##  Mean   :2016-12-30   Mean   :35.98   Mean   :36.30   Mean   :35.67  
##  3rd Qu.:2018-06-29   3rd Qu.:43.81   3rd Qu.:44.30   3rd Qu.:43.62  
##  Max.   :2019-12-30   Max.   :72.78   Max.   :73.49   Max.   :72.03  
##    AAPL.Close     AAPL.Volume         AAPL.Adjusted  
##  Min.   :17.85   Min.   :  45448000   Min.   :15.81  
##  1st Qu.:26.98   1st Qu.: 101360000   1st Qu.:24.78  
##  Median :32.34   Median : 136272800   Median :29.48  
##  Mean   :36.00   Mean   : 161832638   Mean   :34.06  
##  3rd Qu.:43.95   3rd Qu.: 197085600   3rd Qu.:42.34  
##  Max.   :72.88   Max.   :1065523200   Max.   :71.72
autoplot(data$AAPL.Adjusted) + ggtitle("Apple Adjusted Closed")

AAPLE<-data.frame(data$AAPL.Adjusted)
AAPLE_TS <-ts(AAPLE)
AAPLE_TS %>% autoplot()

library(forecast)

AAPLE_TS_2 <- AAPLE_TS %>% as_tsibble()

fit <- nnetar(AAPLE_TS, lambda=0.2)

fit
## Series: AAPLE_TS 
## Model:  NNAR(1,1) 
## Call:   nnetar(y = AAPLE_TS, lambda = 0.2)
## 
## Average of 20 networks, each of which is
## a 1-1-1 network with 4 weights
## options were - linear output units 
## 
## sigma^2 estimated as 0.0009737
AAPLE_TS_2 <- AAPLE_TS %>% as_tsibble()

fit2 <- nnetar(AAPLE_TS, lambda=0.2,size=5)

fit2
## Series: AAPLE_TS 
## Model:  NNAR(1,5) 
## Call:   nnetar(y = AAPLE_TS, size = 5, lambda = 0.2)
## 
## Average of 20 networks, each of which is
## a 1-5-1 network with 16 weights
## options were - linear output units 
## 
## sigma^2 estimated as 0.0009511
fcast <- forecast(fit, PI=TRUE, h=30)
autoplot(fcast)

fcast <- forecast(fit2, PI=TRUE, h=30)
autoplot(fcast)