1. Use the help function to explore what the series gafa_stock, PBS, vic_elec and pelt represent.
  1. Use autoplot() to plot some of the series in these data sets.
gafa_stock %>%
  autoplot(Close) +
  labs(y="Day", title ="Closing Price")

PBS %>%
  select(Month, Concession, Scripts) %>%
  filter(Concession == "Concessional", ATC2 %in% c("A01", "A03")) %>%
  autoplot(Scripts)

autoplot(vic_elec)
## Plot variable not specified, automatically selected `.vars = Demand`

autoplot(pelt)
## Plot variable not specified, automatically selected `.vars = Hare`

b.What is the time interval of each series?

PBS; vic_elec; pelt;
## # A tsibble: 67,596 x 9 [1M]
## # Key:       Concession, Type, ATC1, ATC2 [336]
##       Month Concession  Type   ATC1  ATC1_desc   ATC2  ATC2_desc   Scripts  Cost
##       <mth> <chr>       <chr>  <chr> <chr>       <chr> <chr>         <dbl> <dbl>
##  1 1991 Jul Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   18228 67877
##  2 1991 Aug Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   15327 57011
##  3 1991 Sep Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   14775 55020
##  4 1991 Oct Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   15380 57222
##  5 1991 Nov Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   14371 52120
##  6 1991 Dec Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   15028 54299
##  7 1992 Jan Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   11040 39753
##  8 1992 Feb Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   15165 54405
##  9 1992 Mar Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   16898 61108
## 10 1992 Apr Concession~ Co-pa~ A     Alimentary~ A01   STOMATOLOG~   18141 65356
## # ... with 67,586 more rows
## # A tsibble: 52,608 x 5 [30m] <Australia/Melbourne>
##    Time                Demand Temperature Date       Holiday
##    <dttm>               <dbl>       <dbl> <date>     <lgl>  
##  1 2012-01-01 00:00:00  4383.        21.4 2012-01-01 TRUE   
##  2 2012-01-01 00:30:00  4263.        21.0 2012-01-01 TRUE   
##  3 2012-01-01 01:00:00  4049.        20.7 2012-01-01 TRUE   
##  4 2012-01-01 01:30:00  3878.        20.6 2012-01-01 TRUE   
##  5 2012-01-01 02:00:00  4036.        20.4 2012-01-01 TRUE   
##  6 2012-01-01 02:30:00  3866.        20.2 2012-01-01 TRUE   
##  7 2012-01-01 03:00:00  3694.        20.1 2012-01-01 TRUE   
##  8 2012-01-01 03:30:00  3562.        19.6 2012-01-01 TRUE   
##  9 2012-01-01 04:00:00  3433.        19.1 2012-01-01 TRUE   
## 10 2012-01-01 04:30:00  3359.        19.0 2012-01-01 TRUE   
## # ... with 52,598 more rows
## # A tsibble: 91 x 3 [1Y]
##     Year  Hare  Lynx
##    <dbl> <dbl> <dbl>
##  1  1845 19580 30090
##  2  1846 19600 45150
##  3  1847 19610 49150
##  4  1848 11990 39520
##  5  1849 28040 21230
##  6  1850 58000  8420
##  7  1851 74600  5560
##  8  1852 75090  5080
##  9  1853 88480 10170
## 10  1854 61280 19600
## # ... with 81 more rows
frequency(PBS); frequency(vic_elec); frequency(pelt)
## [1] 12
## [1] 48
## [1] 1

The time interval of gafa_stock is daily, PBS is monthly, vic_elec is half-hourly, and pelt is annualy.

  1. Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.
#head(gafa_stock)

gafa_stock %>%
  group_by(Symbol) %>%
  filter(Close == max(Close)) %>%
  select(Symbol, Date)
## # A tsibble: 4 x 2 [!]
## # Key:       Symbol [4]
## # Groups:    Symbol [4]
##   Symbol Date      
##   <chr>  <date>    
## 1 AAPL   2018-10-03
## 2 AMZN   2018-09-04
## 3 FB     2018-07-25
## 4 GOOG   2018-07-26
  1. Download the file tute1.csv here, open it in Excel (or some other spreadsheet application), and review its contents. You should find four columns of information. Columns B through D each contain a quarterly series, labelled Sales, AdBudget and GDP. Sales contains the quarterly sales for a small company over the period 1981-2005. AdBudget is the advertising budget and GDP is the gross domestic product. All series have been adjusted for inflation.
tute1 <- readr::read_csv("tute1.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.
View(tute1)
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")

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

Everything is on one plot/chart.

  1. The USgas package contains data on the demand for natural gas in the US.

a.Install the USgas package.

library(USgas)
## Warning: package 'USgas' was built under R version 4.1.2

b.Create a tsibble from us_total with year as the index and state as the key.

z<- us_total %>%
              as_tsibble(key = state,index = year)

c.Plot the annual natural gas consumption by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).

z <- us_total %>% 
               group_by(state) %>% 
               filter(state %in% c('Maine', 'Vermont', 'New 
                                   Hampshire', 'Massachusetts', 
                                   'Connecticut' ,'Rhode Island')) %>%
               ungroup()%>%
               as_tsibble(key = state,index = year)

autoplot(z,y)

  1. a.Download tourism.xlsx from the book website and read it into R using readxl::read_excel().
library(DT)
## Warning: package 'DT' was built under R version 4.1.2
tourism <- readxl::read_excel("tourism.xlsx")
datatable(tourism)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html

b.Create a tsibble which is identical to the tourism tsibble from the tsibble package.

t<- tourism %>%
              mutate(Quarter = yearquarter(Quarter)) %>%
              as_tsibble(key = c("Region", "State", "Purpose"),index = "Quarter")
datatable(t)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html

c.Find what combination of Region and Purpose had the maximum number of overnight trips on average.

max_stay <- tourism %>% 
              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.
datatable(max_stay)

d.Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.

total_trips <- tourism %>%
              group_by(Quarter, State) %>%
              summarise_at(vars(-Region,-Purpose), funs(sum(Trips, na.rm=TRUE)))
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## Please use a list of either functions or lambdas: 
## 
##   # Simple named list: 
##   list(mean = mean, median = median)
## 
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
## 
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
datatable(total_trips)

8.Monthly Australian retail data is provided in aus_retail. Select one of the time series as follows (but choose your own seed value):

set.seed(666)
myseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1))

Explore your chosen retail time series using the following functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() %>% autoplot()

autoplot(myseries,Turnover)

gg_season(myseries, Turnover)

gg_subseries(myseries, Turnover)

gg_lag(myseries, Turnover)

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

Can you spot any seasonality, cyclicity and trend? What do you learn about the series?

The autoplot function shows a clear seasonal or cyclic pattern in the time series, and a upward trend. In the lag graph, the data is difficult to analyze. There are some negative and positive relationships, but due to the high number of graphs and the fact that this is a monthly graph, it’s hard to tell much different.