Load packages and data
# install and load any package necessary
library(fpp3)
library(moments)
library(tidyverse)
library(tidyquant)
library(USgas)
tute1 <- readr::read_csv("C:\\Users\\PythonAcct\\Downloads\\tute1.csv")
tourism1 <- readxl::read_excel("C:\\Users\\PythonAcct\\Downloads\\tourism.xlsx")
gme <- readr::read_csv("C:\\Users\\PythonAcct\\Downloads\\GME (3).csv")
# install and load any package necessary
Questions
Exercise 1
tute2=tute1 %>%
mutate(Quarter=yearquarter(Quarter)) %>%
as_tsibble(index=Quarter)
ggplot(tute2, aes(Quarter, Sales, group=1,color='blue')) + geom_line()

ggplot(tute2, aes(Quarter, AdBudget, group=1,color='blue')) + geom_line()

ggplot(tute2, aes(Quarter, GDP, group=1,color='blue')) + geom_line()

tute3 <- tute2 %>%
pivot_longer(cols=c("Sales","AdBudget","GDP"),
names_to = "Measure",
values_to="Dollars")
ggplot(data=tute3) +
geom_line(mapping = aes(x = Quarter, y = Dollars, color=Measure)) +
facet_grid(~ Measure)

Exercise 2
totals <- us_total %>%
as_tsibble(index=year,key=state)
totals %>%
filter(state=="New Hampshire"|state=="Maine"|state=="Vermont"|state=="Massachusetts"|state=="Connecticut"|state=="Rhode Island") %>%
autoplot(y/1e3)+
labs(y="Natural Gas Consumption (Thousands)", x="Time")

Exercise 3
The output of true signifies that the constructed tsibble and given
tsibble are the same
Adelaide, Business is the highest average combination.
tourism2 <- tourism
tourism3 <- tourism1 %>%
mutate(Quarter=yearquarter(Quarter)) %>%
as_tsibble(index=Quarter,key=c(Region,State,Purpose))
all.equal(tourism3,tourism2)
## [1] TRUE
tourism4 <- tourism1 %>%
mutate(Quarter=yearquarter(Quarter)) %>%
as_tsibble(index=Quarter,key=c(Region,State,Purpose)) %>%
group_by(Region,Purpose) %>%
summarise(Avg=mean(Trips)) %>%
ungroup() %>%
mutate(Max=max(Avg))
tourism4
## # A tsibble: 24,320 x 5 [1Q]
## # Key: Region, Purpose [304]
## Region Purpose Quarter Avg Max
## <chr> <chr> <qtr> <dbl> <dbl>
## 1 Adelaide Business 1998 Q1 135. 985.
## 2 Adelaide Business 1998 Q2 110. 985.
## 3 Adelaide Business 1998 Q3 166. 985.
## 4 Adelaide Business 1998 Q4 127. 985.
## 5 Adelaide Business 1999 Q1 137. 985.
## 6 Adelaide Business 1999 Q2 200. 985.
## 7 Adelaide Business 1999 Q3 169. 985.
## 8 Adelaide Business 1999 Q4 134. 985.
## 9 Adelaide Business 2000 Q1 154. 985.
## 10 Adelaide Business 2000 Q2 169. 985.
## # … with 24,310 more rows
## # ℹ Use `print(n = ...)` to see more rows
tourism5 <- tourism1 %>%
mutate(Quarter=yearquarter(Quarter)) %>%
as_tsibble(index=Quarter,key=c(Region,State,Purpose)) %>%
group_by(State) %>%
summarise(Sum=sum(Trips)) %>%
ungroup()
tourism5
## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter Sum
## <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
## # ℹ Use `print(n = ...)` to see more rows
Exercise 4
aus_arrivals %>%
autoplot(Arrivals)

The only irregularity I see is the divergence after around 2001.
Japan also plummets after 1995.
aus_arrivals %>%
gg_season(Arrivals)

I don’t really see anything here.
aus_arrivals %>%
gg_subseries(Arrivals)

This graph makes Japan’s data look quite strange. It is the only one
that exhibits the stark concavity.
Exercise 5
set.seed(1888274)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries %>%
autoplot()
## Plot variable not specified, automatically selected `.vars = Turnover`

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

myseries %>%
gg_subseries
## Plot variable not specified, automatically selected `y = Turnover`

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

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

In the first plot, there is a large spike around 2008.
In the second plot, there is always a large spike at the end of the
year. The late 2000s are the highest.
The third plot proves the increased numbers for December.
The fourth plot provides a lot of insight.
The fifth plot exhibits a downward trend.
Exercise 6
Part 1
mystock <- gafa_stock %>%
filter(Symbol=="FB") %>%
pull(Close)
mean(mystock)
## [1] 120.4625
sd(mystock)
## [1] 41.32364
Part 2
len <- seq(1:nrow(gafa_stock %>% filter(Symbol=="FB")))
len2 <- seq(1:(nrow(gafa_stock %>% filter(Symbol=="FB"))-1))
first_dif = c()
for (i in len2){
first_dif[i]=mystock[i+1]-mystock[i]
}
mean(first_dif)
## [1] 0.06076372
sd(first_dif)
## [1] 2.414555
skewness(first_dif)
## [1] -3.973192
kurtosis(first_dif)
## [1] 71.02921
Part 3
fb_avg <- (sum(mystock))/nrow(gafa_stock %>% filter(Symbol=="FB"))
len <- seq(1:nrow(gafa_stock %>% filter(Symbol=="FB")))
total_var=0.0
for (i in len){
total_var=total_var+(((mystock[i]-fb_avg)^2)/length(len))
}
fb_sd <- sqrt(total_var)
total_skew=0.0
for (i in len){
total_skew=total_skew+(((mystock[i]-fb_avg)^3)/length(len))
}
fb_skew=total_skew/((sd(mystock))^3)
total_kurt=0.0
for (i in len){
total_kurt=total_kurt+(((mystock[i]-fb_avg)^4)/length(len))
}
fb_kurt=total_kurt/((fb_sd)^4)
fb_avg
## [1] 120.4625
fb_sd
## [1] 41.30721
fb_skew
## [1] 0.2276621
fb_kurt
## [1] 1.836712
Exercise 7
gme <- readr::read_csv("C:\\Users\\PythonAcct\\Downloads\\GME (3).csv")
## Rows: 169 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (6): Open, High, Low, Close, Adj Close, Volume
## date (1): Date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
g1 <- gme %>%
mutate(n=row_number()) %>%
mutate(Day=as.Date(Date)) %>%
as_tsibble(index=n) %>%
select("n","Day","Adj Close")
g2 <- g1 %>%
filter(month(Day)==6)
autoplot(g2)
## Plot variable not specified, automatically selected `.vars = Adj Close`

g22 <- g1 %>%
as_tibble()
g3 <- g22 %>%
group_by(month(Day)) %>%
summarise(avg=mean(`Adj Close`),var=var(`Adj Close`))
g3
## # A tibble: 9 × 3
## `month(Day)` avg var
## <dbl> <dbl> <dbl>
## 1 1 29.5 19.6
## 2 2 29.2 5.45
## 3 3 29.9 67.4
## 4 4 36.3 9.09
## 5 5 26.6 16.0
## 6 6 32.7 3.52
## 7 7 34.2 7.12
## 8 8 36.6 18.1
## 9 9 27.5 0.0364