Question 2.1
Use the help function to explore what the series gafa_stock , PBS , vic_elec and pelt represent.
- Use autoplot() to plot some of the series in these data sets.
- Whatisthetimeintervalofeachseries?
help(gafa_stock)
help(PBS)
help(vic_elec)
help(pelt)A:
autoplot(gafa_stock)## Plot variable not specified, automatically selected `.vars = Open`
autoplot(gafa_stock,Volume)autoplot(gafa_stock,Adj_Close)#autoplot(PBS,Cost,legend = NULL)
autoplot(vic_elec)## Plot variable not specified, automatically selected `.vars = Demand`
autoplot(pelt,Hare)autoplot(pelt,Lynx)B:
Time Interval of each series:
gafa_range <- c('Range: ',as.character(range(gafa_stock$Date)))
pbs_range <- c('Range: ',as.character(range(PBS$Month)))
vic_range <- c('Range: ',as.character(range(vic_elec$Time)))
pelt_range <- c('Range: ',as.character(range(pelt$Year)))
gafa_range## [1] "Range: " "2014-01-02" "2018-12-31"
range(gafa_stock$Date)## [1] "2014-01-02" "2018-12-31"
ranges <- data.frame(rbind(gafa_range,pbs_range,vic_range,pelt_range))
colnames(ranges) <- c('Range','Minimum','Maximum')
kbl(ranges, longtable = T, booktabs = T, caption = "Ranges of Each Series") %>%
kable_styling(latex_options = c("repeat_header"))| Range | Minimum | Maximum | |
|---|---|---|---|
| gafa_range | Range: | 2014-01-02 | 2018-12-31 |
| pbs_range | Range: | 1991 Jul | 2008 Jun |
| vic_range | Range: | 2012-01-01 00:00:00 | 2014-12-31 23:30:00 |
| pelt_range | Range: | 1845 | 1935 |
Question 2.2
Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock .
peaks <- gafa_stock %>%
group_by(Symbol) %>%
filter(Close == max(Close))
peaksQuestion 2.3
tute1 <- read.csv('https://otexts.com/fpp3/extrafiles/tute1.csv')
tute_time_series <- tute1 %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(index=Quarter)
tute_time_series %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name))+
geom_line()+
facet_grid(name ~ ., scales = "free_y")tute_time_series %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name))+
geom_line() #facet_grid(name ~ ., scales = "free_y")Facet Grid makes for a much more easy viewing experience, as all of the three plots are separate. This would be most useful in instances where the plots overlap one another. Without Facet Grid enabled for these plots, we see the immense gap between each of the three series.
Question 2.4
library(USgas)
us_total <- USgas::us_total
us_total <- as_tsibble(us_total,index = year,key = state)
ne_states <- us_total %>%
filter(state == 'Maine' | state == 'Vermont' | state == 'New Hampshire' | state == 'Massachusetts' | state == 'Rhode Island' | state == 'Connecticut')
ne_states %>%
ggplot(aes(x = year, y = y, colour = state))+
geom_line()Question 2.5
tourism <- readxl::read_excel('tourism.xlsx')
tourism <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(index = Quarter,key = c(Region,State,Purpose))
#tsibble::tourism
head(tourism)avg_region_purpose <- aggregate(tourism$Trips, list(Region = tourism$Region, Purpose = tourism$Purpose),mean)
colnames(avg_region_purpose) <- c("Region","Purpose","Trips")
max_region_purpose <- avg_region_purpose %>%
filter(Trips == max(Trips))
kbl(max_region_purpose, longtable = T, booktabs = T, caption = "Maximum Average Overnight Trips") %>%
kable_styling(latex_options = c("repeat_header"))| Region | Purpose | Trips |
|---|---|---|
| Sydney | Visiting | 747.27 |
tourism_state <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(index = Quarter,key = c(Region,State,Purpose))
tourism_state <- aggregate(tourism_state$Trips, list(State = tourism_state$State),sum)
tourism_stateQuestion 2.8
set.seed(34)
aus_retail_series <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(aus_retail_series)## Plot variable not specified, automatically selected `.vars = Turnover`
gg_season(aus_retail_series)## Plot variable not specified, automatically selected `y = Turnover`
gg_subseries(aus_retail_series)## Plot variable not specified, automatically selected `y = Turnover`
gg_lag(aus_retail_series)## Plot variable not specified, automatically selected `y = Turnover`
aus_retail_series %>%
ACF(Turnover) %>%
autoplot()Year over year, the highest month for turnover is consistently December. The holiday season is the biggest driver of turnover according to the plots.