—title: “HW #1” author: “Zachary Gooch” date: “09/07/22” output: html_document —

Load packages and data

library(fpp3)
library(dplyr)
library(ggplot2)
library(tidyr)
library(tsibble)
library(ggfortify)
library(tidyverse)
library(moments)
library(USgas)
library(readxl)


# install and load any package necessary

Questions

Exercise 1

tute1 <- readxl::read_excel("C:\\Users\\Zacha\\Downloads\\tute1.xlsx")
tute2 <- tute1 %>%
  mutate(Quarter=yearquarter(Quarter)) %>% as_tsibble(index=Quarter)
head(tute2)
## # A tsibble: 6 x 4 [1Q]
##   Quarter Sales AdBudget   GDP
##     <qtr> <dbl>    <dbl> <dbl>
## 1 1981 Q1 1020.     659.  252.
## 2 1981 Q2  889.     589   291.
## 3 1981 Q3  795      512.  291.
## 4 1981 Q4 1004.     614.  292.
## 5 1982 Q1 1058.     647.  279.
## 6 1982 Q2  944.     602   254
SalesP <- ggplot(tute2, aes(x=Quarter, y=Sales)) + geom_line()
AdBudgetP <- ggplot(tute2, aes(x=Quarter, y= AdBudget)) + geom_line()
GDPP <- ggplot(tute2, aes(x=quarter, y = GDP)) + geom_line()
BigPlot = tute2 %>% 
  pivot_longer(cols=c("Sales","AdBudget","GDP"),
                         names_to = "Measures",
                         values_to = "Money")
                        
ggplot(data = BigPlot)+ geom_point(mapping=aes(x=Quarter, y= Money )) + facet_grid(~Measures)

Exercise 2

USA <- us_total %>%
  as_tsibble(
    index= year,
    key= state)
Northeast <- USA %>% 
  filter(state == "Maine"| state == "Vermont"| state=="New Hampshire"| state == "Massachusetts"| state == "Connecticut"| state == "Rhode Island")  

ggplot(data = Northeast) + geom_line(mapping=aes(x=year, y=y, color = state))

Exercise 3

tourism1<- readxl::read_excel("C:\\Users\\Zacha\\Downloads\\tourism.xlsx")

Toursible <- tourism1 %>%
  mutate(Quarter = yearquarter (Quarter)) %>%
  as_tsibble(index=Quarter,
             key= c(Region, State, Purpose))

Toursible %>% group_by(Region, Purpose) %>% 
  summarise(mean=mean(Trips))%>%ungroup()%>%filter(mean==max(mean))
## # A tsibble: 1 x 4 [1Q]
## # Key:       Region, Purpose [1]
##   Region    Purpose  Quarter  mean
##   <chr>     <chr>      <qtr> <dbl>
## 1 Melbourne Visiting 2017 Q4  985.
ttyl <- Toursible %>% 
  group_by(State)%>%
  summarise(Trips =sum(Trips))%>%
  ungroup()
ttyl
## # A tsibble: 640 x 3 [1Q]
## # Key:       State [8]
##    State Quarter Trips
##    <chr>   <qtr> <dbl>
##  1 ACT   1998 Q1  551.
##  2 ACT   1998 Q2  416.
##  3 ACT   1998 Q3  436.
##  4 ACT   1998 Q4  450.
##  5 ACT   1999 Q1  379.
##  6 ACT   1999 Q2  558.
##  7 ACT   1999 Q3  449.
##  8 ACT   1999 Q4  595.
##  9 ACT   2000 Q1  600.
## 10 ACT   2000 Q2  557.
## # ... with 630 more rows
## # i Use `print(n = ...)` to see more rows

Exercise 4

head(aus_arrivals)
## # A tsibble: 6 x 3 [1Q]
## # Key:       Origin [1]
##   Quarter Origin Arrivals
##     <qtr> <chr>     <int>
## 1 1981 Q1 Japan     14763
## 2 1981 Q2 Japan      9321
## 3 1981 Q3 Japan     10166
## 4 1981 Q4 Japan     19509
## 5 1982 Q1 Japan     17117
## 6 1982 Q2 Japan     10617
autoplot(aus_arrivals)                  
## Plot variable not specified, automatically selected `.vars = Arrivals`

gg_season(aus_arrivals)
## Plot variable not specified, automatically selected `y = Arrivals`

gg_subseries(aus_arrivals)  
## Plot variable not specified, automatically selected `y = Arrivals`

 #Japan after experiencing steady growth for some time began to plateau and decrease; no other country appears to decrease like Japan. New Zealand experienced a sharp increase around 2005. The US has a very steady consistant growth albeit minor.  

Exercise 5

set.seed(262)
myseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries
## # A tsibble: 441 x 5 [1M]
## # Key:       State, Industry [1]
##    State             Industry                           Serie~1    Month Turno~2
##    <chr>             <chr>                              <chr>      <mth>   <dbl>
##  1 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Apr     8  
##  2 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 May     8  
##  3 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Jun     7.3
##  4 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Jul     7.8
##  5 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Aug     7.6
##  6 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Sep     8.2
##  7 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Oct     9.3
##  8 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Nov     9.6
##  9 Western Australia Cafes, restaurants and catering s~ A33499~ 1982 Dec    13.4
## 10 Western Australia Cafes, restaurants and catering s~ A33499~ 1983 Jan     8  
## # ... with 431 more rows, and abbreviated variable names 1: `Series ID`,
## #   2: Turnover
## # i Use `print(n = ...)` to see more rows
autoplot(myseries) 
## Plot variable not specified, automatically selected `.vars = Turnover`

gg_season(myseries)
## Plot variable not specified, automatically selected `y = Turnover`

gg_subseries(myseries)
## Plot variable not specified, automatically selected `y = Turnover`

gg_lag(myseries)
## Plot variable not specified, automatically selected `y = Turnover`

myseries %>%
  ACF(Turnover) %>% autoplot()

#There's definitely a positive trend in the data it's always increasing in the different graph types. The seasonality is very apparent in the autoplot function. It peaks upwards and then goes back down quickly consistently through the graph. The graphs appear to follow a general business cycle with a consistant series of rises and falls.  

Exercise 6

gafa_stock
## # A tsibble: 5,032 x 8 [!]
## # Key:       Symbol [4]
##    Symbol Date        Open  High   Low Close Adj_Close    Volume
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>     <dbl>
##  1 AAPL   2014-01-02  79.4  79.6  78.9  79.0      67.0  58671200
##  2 AAPL   2014-01-03  79.0  79.1  77.2  77.3      65.5  98116900
##  3 AAPL   2014-01-06  76.8  78.1  76.2  77.7      65.9 103152700
##  4 AAPL   2014-01-07  77.8  78.0  76.8  77.1      65.4  79302300
##  5 AAPL   2014-01-08  77.0  77.9  77.0  77.6      65.8  64632400
##  6 AAPL   2014-01-09  78.1  78.1  76.5  76.6      65.0  69787200
##  7 AAPL   2014-01-10  77.1  77.3  75.9  76.1      64.5  76244000
##  8 AAPL   2014-01-13  75.7  77.5  75.7  76.5      64.9  94623200
##  9 AAPL   2014-01-14  76.9  78.1  76.8  78.1      66.1  83140400
## 10 AAPL   2014-01-15  79.1  80.0  78.8  79.6      67.5  97909700
## # ... with 5,022 more rows
## # i Use `print(n = ...)` to see more rows
autoplot(gafa_stock)
## Plot variable not specified, automatically selected `.vars = Open`

#calculate mean and standard deviation
fbstock <- gafa_stock %>%
  filter(Symbol=="FB")%>%
  select(Symbol,Close)
mean(fbstock$Close)
## [1] 120.4625
#120.4625
sd(fbstock$Close)
## [1] 41.32364
#41.32364
install.packages('moments')
## Warning: package 'moments' is in use and will not be installed
library("moments") 

fbvector<- pull(fbstock,Close)
firstdiff<- diff(fbvector)
head(firstdiff)
## [1] -0.149998  2.640000  0.719997  0.310002 -1.009999  0.719998
FBMean<- mean(firstdiff)
#0.06076372
FBMedian <- median(firstdiff)
#.1
FBsd <- sd(firstdiff)
#2.414555
FBskew <- skewness(firstdiff)
#-3.973192
FBkurt <- kurtosis(firstdiff)
#74.02921

#By Hand Stuff
FBMeanbh <- sum(fbvector, na.rm = TRUE) / length(fbvector)
FBsdbh <- sqrt(sum((fbvector-mean(fbvector))^2/(length(fbvector)-1)))
FBkurbh <- ((sum((fbvector-mean(fbvector))^4))/length(fbvector))/(sum((fbvector-mean(fbvector))^2)/length(fbvector))^2
FBskewbh <- (3*(FBMeanbh-FBMedian))/FBsdbh

Exercise #7

PELE <- readxl::read_excel("C:\\Users\\Zacha\\Downloads\\PTON.xlsx")
Pele <- PELE %>% select(-(Open:Close)) %>%
  select(-(Volume))
Peloton <- PELE %>%
  mutate(Date= as_date(Date)) %>%
  as_tsibble(index="Date", key= "Adj Close")

Pelaline <- Peloton %>%
  filter(Date >= as.Date("2022-06-01") & Date <= as.Date("2022-06-30"))%>%
           ggplot(aes(x=Date, y= `Adj Close`)) + geom_line()
Pelaline

#Mean and Variance per month

PelaJan <- Peloton %>% 
  filter(Date >= as.Date("2022-01-01") & Date <= as.Date("2022-01-31"))
PelaFeb <- Peloton %>% 
  filter(Date >= as.Date("2022-02-01") & Date <= as.Date("2022-02-28"))
PelaMarch <- Peloton %>% 
  filter(Date >= as.Date("2022-03-01") & Date <= as.Date("2022-03-31"))
PelaApril <- Peloton %>% 
  filter(Date >= as.Date("2022-04-01") & Date <= as.Date("2022-04-30"))
PelaMay <- Peloton %>% 
  filter(Date >= as.Date("2022-05-01") & Date <= as.Date("2022-05-31"))
PelaJune <- Peloton %>% 
  filter(Date >= as.Date("2022-06-01") & Date <= as.Date("2022-06-30"))
PelaJuly <- Peloton %>% 
  filter(Date >= as.Date("2022-07-01") & Date <= as.Date("2022-07-31"))
PelaAugust <- Peloton %>% 
  filter(Date >= as.Date("2022-08-01") & Date <= as.Date("2022-08-31"))
PelaSeptember <- Peloton %>%
  filter(Date >= as.Date("2022-09-01") & Date <= as.Date("2022-09-02"))

JanVec <- pull(PelaJan, "Adj Close")
JanMean <- mean(JanVec)
FebVec <- pull(PelaFeb, "Adj Close")
FebMea  <- mean(FebVec)
MarchVec <- pull(PelaMarch, "Adj Close")
MarchMean <- mean(MarchVec)
AprilVec <- pull(PelaApril, "Adj Close")
AprilMean <- mean(AprilVec)
MayVec <- pull(PelaMay, "Adj Close")
MayMean <- mean(MayVec)
JuneVec <- pull(PelaJune, "Adj Close")
JuneMean <- mean(JuneVec)
JulyVec <- pull(PelaJuly, "Adj Close")
JulyMean <- mean(JulyVec)
AugustVec <- pull(PelaAugust, "Adj Close")
AugustMean <- mean(AugustVec)
SeptemberVec <- pull(PelaSeptember, "Adj Close")
SeptemberMean <- mean(SeptemberVec)

JanVar <- var(JanVec)
FebVar <- var(FebVec)
MarchVar <- var(MarchVec)
AprilVar <- var(AprilVec)
MayVar <- var(MayVec)
JuneVar <- var(JuneVec)
JulyVar <- var(JulyVec)
AugustVar <- var(AugustVec)
SeptemberVar <-var(SeptemberVec)