Load the Packages

Let’s load up the packages for the problem first.

library(quantmod)
## Warning: package 'quantmod' was built under R version 3.5.1
## 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.
library(psych)
## Warning: package 'psych' was built under R version 3.5.1

Reading in a Stock

An easy way to read in a stock is by assigning start and end dates and then using getSymbols()>

start=as.Date("2010-01-01")
end=as.Date("2017-12-31")
getSymbols("CMCSA", src="yahoo", from=start, to=end)
## '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.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "CMCSA"

Look at the Data

We can run some basic descriptives and time series plots once we understand the structure.

head(CMCSA) #get the header data to see what we have
##            CMCSA.Open CMCSA.High CMCSA.Low CMCSA.Close CMCSA.Volume
## 2010-01-04      8.375      8.565     8.375       8.485     27146800
## 2010-01-05      8.465      8.490     8.275       8.370     35493000
## 2010-01-06      8.390      8.400     8.265       8.310     27741400
## 2010-01-07      8.355      8.530     8.275       8.485     30214800
## 2010-01-08      8.465      8.535     8.405       8.460     25454000
## 2010-01-11      8.435      8.500     8.365       8.405     38109200
##            CMCSA.Adjusted
## 2010-01-04       7.265583
## 2010-01-05       7.167109
## 2010-01-06       7.115733
## 2010-01-07       7.265583
## 2010-01-08       7.244176
## 2010-01-11       7.197077
str(CMCSA)  #get the structure of the data
## An 'xts' object on 2010-01-04/2017-12-29 containing:
##   Data: num [1:2013, 1:6] 8.38 8.46 8.39 8.36 8.46 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "CMCSA.Open" "CMCSA.High" "CMCSA.Low" "CMCSA.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2018-08-05 09:41:30"
describe(CMCSA) #describe all variables in the dataset
##                vars    n        mean          sd      median     trimmed
## CMCSA.Open        1 2013       23.08        9.69       24.86       22.88
## CMCSA.High        2 2013       23.29        9.76       25.11       23.08
## CMCSA.Low         3 2013       22.88        9.62       24.66       22.68
## CMCSA.Close       4 2013       23.09        9.69       24.89       22.89
## CMCSA.Volume      5 2013 28530727.97 15256971.37 25077800.00 26289943.95
## CMCSA.Adjusted    6 2013       21.65        9.80       22.95       21.34
##                        mad        min          max        range skew
## CMCSA.Open           11.71       7.58        42.13        34.56 0.02
## CMCSA.High           11.70       7.68        42.18        34.49 0.02
## CMCSA.Low            11.53       7.55        41.64        34.09 0.02
## CMCSA.Close          11.69       7.61        41.99        34.39 0.02
## CMCSA.Volume   10722756.24 5672200.00 166539600.00 160867400.00 2.51
## CMCSA.Adjusted       12.51       6.51        41.09        34.57 0.10
##                kurtosis        se
## CMCSA.Open        -1.21      0.22
## CMCSA.High        -1.21      0.22
## CMCSA.Low         -1.21      0.21
## CMCSA.Close       -1.21      0.22
## CMCSA.Volume      11.24 340052.87
## CMCSA.Adjusted    -1.18      0.22
#Let's plot a time series.
 plot.ts(CMCSA$CMCSA.Close, main="CMCSA Daily Stock Prices from 1 Jan 2010-31 Dec 2017", ylab="$ / Share")