Load packages and data

library(fpp3)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(tsibble)
library(moments)
library(tibbletime)
library(lubridate)

# install and load any package necessary

Questions

Exercise 1

tute1 <- readxl::read_excel("Downloads/tute1.xlsx", 
                    col_types = c("date", "numeric", "numeric", 
                                  "numeric"))
mytimeseries <- tute1 %>%
  mutate(Quarter = yearmonth(Quarter)) %>%
  as_tsibble(index = Quarter)

mytimeseries %>% 
  pivot_longer(-Quarter) %>%
  ggplot(aes(x = Quarter, y = value, colour = name)) + 
  geom_line()

mytimeseries %>% 
  pivot_longer(-Quarter) %>%
  ggplot(aes(x = Quarter, y = value, colour = name)) + 
  geom_line()+
  facet_grid(name ~ ., scales = "free_y")

Exercise 2

Exercise 3

tourismvs <- readxl::read_excel("//Users//hunterpatenaude//Downloads//tourism.xlsx")
tourismmm <- tourismvs %>%
  mutate(Quarter = yearquarter(Quarter)) %>%
  as_tsibble(key = c("Region", "State", "Purpose"),index = "Quarter")



tt<-tourismvs %>% group_by(Region,Purpose)%>%
  summarise (mean = mean(Trips)) %>% ungroup() %>% filter (mean == max(mean))
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
as.data.frame(tourismmm) %>% group_by(Region,Purpose) %>%
  summarise (mean = mean(Trips)) %>% ungroup() %>% filter (mean == max(mean))
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
## # A tibble: 1 × 3
##   Region Purpose   mean
##   <chr>  <chr>    <dbl>
## 1 Sydney Visiting  747.
tourismvs %>% group_by(State) %>% summarise (total = sum(Trips))
## # A tibble: 8 × 2
##   State                total
##   <chr>                <dbl>
## 1 ACT                 41007.
## 2 New South Wales    557367.
## 3 Northern Territory  28614.
## 4 Queensland         386643.
## 5 South Australia    118151.
## 6 Tasmania            54137.
## 7 Victoria           390463.
## 8 Western Australia  147820.

Exercise 4

ausarr <- aus_arrivals
autoplot(ausarr)
## Plot variable not specified, automatically selected `.vars = Arrivals`

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

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

The main observation that I noticed was that arrivals in the UK always fell during Q2 and Q3. During the 10 year span between 2000 Q1 and 2010 Q1 New Zealand had a huge spike of arrivals and Japan had a huge drop in arrivals.

Exercise 5

set.seed(6547)
ausser <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(ausser)
## Plot variable not specified, automatically selected `.vars = Turnover`

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

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

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

ausser %>% 
  ACF(Turnover) %>%
  autoplot()

Using this seed I can see a clear trend in turnovers. As time moves forward, there is a positive trend of turnover. Each yeah the amount of turnovers seem to increase. Seasonality seems to exists, because the amount of turnovers tends to spike around every December. I don’t see any evidence of the data being cyclic.

Exercise 6

library(tsibbledata)

fff <- as.data.frame(gafa_stock) %>% group_by(Symbol) %>%
  filter(Symbol == "FB") 

vec1 <- pull(fff,Close)
FBmean <- mean(vec1, na.rm = TRUE)
FBmedian <- median(vec1)
FBsd <- sd(vec1, na.rm = TRUE) 
FBkurt <- kurtosis(vec1, na.rm = TRUE)
FBskew <- skewness(vec1, na.rm = TRUE)

vecdiff <- diff(vec1)
FBmeandif <- mean(vecdiff, na.rm = TRUE)
FBsddif <- sd(vecdiff, na.rm = TRUE)
FBkurtdif <- kurtosis(vecdiff, na.rm = TRUE)
FBskewdif <- skewness(vecdiff, na.rm = TRUE)
 
FBmeanbh <- sum(vec1, na.rm = TRUE) / nrow(fff)
FBsdbh <- sqrt(sum((vec1-mean(vec1))^2/(length(vec1)-1)))
FBkurtbh <- ((sum((vec1-mean(vec1))^4))/length(vec1))/(sum((vec1-mean(vec1))^2)/length(vec1))^2
FBskewbh <- (3*(FBmeanbh-FBmedian))/FBsdbh
FBmean
## [1] 120.4625
FBmeanbh
## [1] 120.4625
FBmeandif
## [1] 0.06076372
FBsd
## [1] 41.32364
FBsdbh
## [1] 41.32364
FBsddif
## [1] 2.414555
FBkurtbh
## [1] 1.836712
FBkurtdif
## [1] 74.02921
FBskewbh
## [1] 0.2023634
FBskewbh
## [1] 0.2023634
FBskewdif
## [1] -3.973192

Excercise 7

TSLA_stock <- readxl::read_excel("Downloads/TSLA stock.xlsx", 
                         col_types = c("date", "numeric", "numeric", 
                                       "numeric", "numeric", "numeric", 
                                       "numeric"))

tesla <- TSLA_stock %>% select(-(Open:Close)) %>%
  select(-(Volume))
teslats <-  tesla %>%
  mutate(Date = as_date(Date)) %>%
  as_tsibble(index = "Date", key = "Adj Close")
  
teslaline <- teslats %>% 
  filter(Date >= as.Date("2022-06-01") & Date <= as.Date("2022-06-30")) %>%
  ggplot(aes(x = Date, y = `Adj Close`)) +
  geom_line()

teslaline

teslaJAN <- teslats %>%
  filter(Date >= as.Date("2022-01-01") & Date <= as.Date("2022-01-31"))
teslaFEB <- teslats %>%
  filter(Date >= as.Date("2022-02-01") & Date <= as.Date("2022-02-28"))
teslaMAR <- teslats %>%
  filter(Date >= as.Date("2022-03-01") & Date <= as.Date("2022-03-31"))
teslaAPR <- teslats %>%
  filter(Date >= as.Date("2022-04-01") & Date <= as.Date("2022-04-30"))
teslaMAY <- teslats %>%
  filter(Date >= as.Date("2022-05-01") & Date <= as.Date("2022-05-31"))
teslaJUN <- teslats %>%
  filter(Date >= as.Date("2022-06-01") & Date <= as.Date("2022-06-30"))
teslaJUL <- teslats %>%
  filter(Date >= as.Date("2022-07-01") & Date <= as.Date("2022-07-31"))
teslaAUG <- teslats %>%
  filter(Date >= as.Date("2022-08-01") & Date <= as.Date("2022-08-31"))

TJAN_Vec <- pull(teslaJAN, "Adj Close") 
TJAN_M <- mean(TJAN_Vec)
TFEB_Vec <- pull(teslaFEB, "Adj Close")
TFEB_M <- mean(TFEB_Vec)
TMAR_Vec <- pull(teslaFEB, "Adj Close")
TMAR_M <- mean(TMAR_Vec)
TAPR_Vec <- pull(teslaAPR, "Adj Close")
TAPR_M <- mean(TAPR_Vec)
TMAY_Vec <- pull(teslaMAY, "Adj Close")
TMAY_M <- mean(TMAY_Vec)
TJUN_Vec <- pull(teslaJUN, "Adj Close")
TJUN_M <- mean(TJUN_Vec)
TJUL_Vec <- pull(teslaJUL, "Adj Close")
TJUL_M <- mean(TJUL_Vec)
TAUG_Vec <- pull(teslaAUG, "Adj Close")
TAUG_M <- mean(TAUG_Vec)

TJAN_var <- var(TJAN_Vec)
TFEB_var <- var(TFEB_Vec)
TMAR_var <- var(TMAR_Vec)
TAPR_var <- var(TAPR_Vec)
TMAY_var <- var(TMAY_Vec)
TJUN_var <- var(TJUN_Vec)
TJUL_var <- var(TJUL_Vec)
TAUG_var <- var(TAUG_Vec)
#JAN - SD and Mean
TJAN_var; TJAN_M
## [1] 1003.339
## [1] 336.7228
#FEB - SD and Mean
TFEB_var; TFEB_M
## [1] 270.8465
## [1] 292.9616
#MAR - SD and Mean
TMAR_var; TMAR_M
## [1] 270.8465
## [1] 292.9616
#APR - SD and Mean
TAPR_var; TAPR_M
## [1] 622.5522
## [1] 332.4625
#MAY - SD and Mean
TMAY_var; TMAY_M
## [1] 891.3576
## [1] 255.2233
#JUN - SD and Mean
TJUN_var; TJUN_M
## [1] 126.5048
## [1] 234.0259
#JUL - SD and Mean
TJUL_var; TJUL_M
## [1] 387.2411
## [1] 251.3947
#AUG - SD and Mean
TAUG_var; TAUG_M
## [1] 91.80111
## [1] 294.8699