Assignment 09

dygraphs1.R

# Plotting time series data using dygraph
# install.packages(c("quantmod", "tidyverse","dygraphs"))
# lapply(c("quantmod", "tidyverse","dygraphs"), require, character.only = TRUE)

library(dygraphs)
par(family="Palatino")
quantmod::getSymbols("TWTR", src="yahoo")
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Warning: TWTR contains missing values. Some functions will not work if objects
contain missing values in the middle of the series. Consider using na.omit(),
na.approx(), na.fill(), etc to remove or replace them.
[1] "TWTR"
class(TWTR)
[1] "xts" "zoo"
m = tail(TWTR, 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("~/quarto/eliannaevans.github.io/") # place dygraph.css into the same directory
dygraph(m, main = "Twitter Stock Prices (Candlestick Chart)")  |>  
  dyCandlestickGroup(c('Open', 'High', 'Low', 'Close')) |> 
  dyCandlestick()  |> 
  dyLegend(show = "always", hideOnMouseOut = T) |> 
  dyCSS("dygraph.css")

tsstudio1.R

# Plotting time series data using TSstudio
# install.packages(c("quantmod", "tidyverse","TSstudio"))
# lapply(c("quantmod", "tidyverse","TSstudio"), require, character.only = TRUE)

library(TSstudio)
quantmod::getSymbols("AAPL", src="yahoo")
[1] "AAPL"
ts_plot(AAPL$AAPL.Adjusted, 
        title = "Apple Stock prices",
        Ytitle = "")
class(AAPL) # What class is this object?
[1] "xts" "zoo"
# Some sample dataset from TSstudio
ts_seasonal(USgas, type = "box") # month-year matrix data
# What class is USgas?

# Sample charts
ts_heatmap(USgas)
ts_cor(USgas) # ACF and PACF
ts_lags(USgas, margin = .01)
usgas=data.frame(USgas)

Analyzing Time Series Data

quantmod::getSymbols("AAPL", src="yahoo")
[1] "AAPL"
quantmod::getSymbols("TWTR", src="yahoo")
Warning: TWTR contains missing values. Some functions will not work if objects
contain missing values in the middle of the series. Consider using na.omit(),
na.approx(), na.fill(), etc to remove or replace them.
[1] "TWTR"
summary(AAPL)
     Index              AAPL.Open         AAPL.High          AAPL.Low      
 Min.   :2007-01-03   Min.   :  2.835   Min.   :  2.929   Min.   :  2.793  
 1st Qu.:2011-01-05   1st Qu.: 11.635   1st Qu.: 11.732   1st Qu.: 11.540  
 Median :2015-01-13   Median : 24.860   Median : 25.142   Median : 24.657  
 Mean   :2015-01-12   Mean   : 42.936   Mean   : 43.421   Mean   : 42.456  
 3rd Qu.:2019-01-17   3rd Qu.: 48.664   3rd Qu.: 49.087   3rd Qu.: 48.169  
 Max.   :2023-01-24   Max.   :182.630   Max.   :182.940   Max.   :179.120  
   AAPL.Close       AAPL.Volume        AAPL.Adjusted    
 Min.   :  2.793   Min.   :3.520e+07   Min.   :  2.381  
 1st Qu.: 11.618   1st Qu.:1.111e+08   1st Qu.:  9.903  
 Median : 24.954   Median :2.260e+08   Median : 22.622  
 Mean   : 42.957   Mean   :3.749e+08   Mean   : 41.321  
 3rd Qu.: 48.545   3rd Qu.:5.082e+08   3rd Qu.: 47.099  
 Max.   :182.010   Max.   :3.373e+09   Max.   :180.960  
summary(TWTR)
     Index              TWTR.Open       TWTR.High        TWTR.Low    
 Min.   :2013-11-07   Min.   :13.95   Min.   :14.22   Min.   :13.72  
 1st Qu.:2016-02-29   1st Qu.:25.55   1st Qu.:26.21   1st Qu.:24.91  
 Median :2018-06-16   Median :35.42   Median :36.10   Median :34.82  
 Mean   :2018-06-16   Mean   :36.02   Mean   :36.70   Mean   :35.34  
 3rd Qu.:2020-10-04   3rd Qu.:44.20   3rd Qu.:45.02   3rd Qu.:43.33  
 Max.   :2023-01-24   Max.   :78.36   Max.   :80.75   Max.   :76.05  
                      NA's   :59      NA's   :59      NA's   :59     
   TWTR.Close     TWTR.Volume        TWTR.Adjusted  
 Min.   :14.01   Min.   :        0   Min.   :14.01  
 1st Qu.:25.41   1st Qu.: 12335302   1st Qu.:25.41  
 Median :35.49   Median : 16913051   Median :35.49  
 Mean   :36.00   Mean   : 21751861   Mean   :36.00  
 3rd Qu.:44.13   3rd Qu.: 24280822   3rd Qu.:44.13  
 Max.   :77.63   Max.   :269213085   Max.   :77.63  
 NA's   :59      NA's   :59          NA's   :59