library(tsibble)
##
## Attaching package: 'tsibble'
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
library(tsibbledata)
library(ggfortify)
## Loading required package: ggplot2
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(USgas)
library(readr)
library(tidyr)
library(readxl)
library(httr)
library(feasts)
## Loading required package: fabletools
library(stats)
a.) Use autoplot() to plot some of the series in these data sets.
gs = gafa_stock
pbs = PBS
ve = vic_elec
pe = pelt
autoplot(as.ts(as.ts(gs %>% select(Date,Adj_Close))))
autoplot(as.ts(pbs[,c('Month','Cost')]))
autoplot(as.ts(ve %>% select(Time,Demand)))
autoplot(as.ts(pe %>% select(Year,Hare)))
b.) What is the time interval of each series?
print(paste0('The frequency for the datasets are as follows: Gafa_Stock:','daily, ','PBS:','1 Month, ','VIC_ELEC: ', '30 minutes, ','PELT: ','1 Year'))
## [1] "The frequency for the datasets are as follows: Gafa_Stock:daily, PBS:1 Month, VIC_ELEC: 30 minutes, PELT: 1 Year"
gafa_stock %>% select(Symbol,Date,Close) %>% group_by(Symbol) %>% filter(Close == max(Close))
## # A tsibble: 4 x 3 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Close
## <chr> <date> <dbl>
## 1 AAPL 2018-10-03 232.
## 2 AMZN 2018-09-04 2040.
## 3 FB 2018-07-25 218.
## 4 GOOG 2018-07-26 1268.
tute1<- readr::read_csv('HW1tute1.csv')
## Rows: 100 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## dbl (3): Sales, AdBudget, GDP
## date (1): Quarter
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
mytimeseries <- tute1 %>%
mutate(Quarter = yearmonth(Quarter)) %>%
as_tsibble(index = Quarter)
mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")
No facet Grid
# No facet Grid
mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()
4.The USgas package contains data on the demand for natural gas in the US.
us_total_tsibble = as_tsibble(us_total,key=state,index=year)
head(us_total_tsibble)
## # A tsibble: 6 x 3 [1Y]
## # Key: state [1]
## year state y
## <int> <chr> <int>
## 1 1997 Alabama 324158
## 2 1998 Alabama 329134
## 3 1999 Alabama 337270
## 4 2000 Alabama 353614
## 5 2001 Alabama 332693
## 6 2002 Alabama 379343
new_hamp_data = us_total_tsibble %>% filter( state %in% c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut', 'Rhode Island'))
autoplot(as.ts(as.ts(new_hamp_data)))
5.a. Download tourism.xlsx from the book website and read it into R using readxl::read_excel().
tourism_dataset = readxl::read_excel('tourism.xlsx')
tourism_dataset = tourism_dataset %>% mutate(Quarter = yearquarter(Quarter)) %>% as_tsibble(index = Quarter,key=c(Region,State,Purpose))
all.equal(tourism,tourism_dataset)
## [1] TRUE
tourism_dataset %>% select(Region,Purpose,Trips) %>% group_by(Region,Purpose) %>% summarise(Average_Trips = mean(Trips)) %>% filter(Average_Trips == max(Average_Trips))
## # A tsibble: 76 x 4 [1Q]
## # Key: Region, Purpose [76]
## # Groups: Region [76]
## Region Purpose Quarter Average_Trips
## <chr> <chr> <qtr> <dbl>
## 1 Adelaide Visiting 2017 Q1 270.
## 2 Adelaide Hills Visiting 2002 Q4 81.1
## 3 Alice Springs Holiday 1998 Q3 76.5
## 4 Australia's Coral Coast Holiday 2014 Q3 198.
## 5 Australia's Golden Outback Business 2017 Q3 174.
## 6 Australia's North West Business 2016 Q3 297.
## 7 Australia's South West Holiday 2016 Q1 612.
## 8 Ballarat Visiting 2004 Q1 103.
## 9 Barkly Holiday 1998 Q3 37.9
## 10 Barossa Holiday 2006 Q1 51.0
## # ... with 66 more rows
tourism_dataset %>% group_by(State) %>% summarise (total = sum(Trips))
## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter total
## <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
set.seed(45678913)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(as.ts(myseries %>% select(Month,Turnover)))
feasts::gg_season(myseries %>% select(Month,Turnover))
## Plot variable not specified, automatically selected `y = Turnover`
feasts::gg_subseries(myseries %>% select(Month,Turnover))
## Plot variable not specified, automatically selected `y = Turnover`
feasts::gg_lag(myseries %>% select(Month,Turnover))
## Plot variable not specified, automatically selected `y = Turnover`
myseries %>% feasts::ACF(Turnover) %>% autoplot()