Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.
Use ? (or help()) to find out about the data in each series.
What is the time interval of each series?
Use autoplot() to produce a time plot of each series.
For the last plot, modify the axis labels and title.
According to the help page for aus_production, the time interval for Bricks from aus_production is quarterly. Looking at the tsibble above, it goes from “1956 Q1” to “2010 Q2”.
autoplot(aus_production, Bricks)
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
At first, the time interval was a little confusing to figure out since there are some missing dates. But after cross referencing with a calendar of 2014, it looks like the time interval for Close from gafa_stock is every day that the stock market was open from 2014 to 2018.
The time interval for Demand from vic_elec is half-hourly starting from 2012-01-01 00:00:00 to 2014-12-31 23:30:00. I was able to use the tail command to see the last values in order to see what the last date time was.
Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.
gafa_stock |>filter(Close ==max(Close))
# A tsibble: 1 x 8 [!]
# Key: Symbol [1]
Symbol Date Open High Low Close Adj_Close Volume
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AMZN 2018-09-04 2026. 2050. 2013 2040. 2040. 5721100
# A tsibble: 4 x 8 [!]
# Key: Symbol [4]
# Groups: Symbol [4]
Symbol Date Open High Low Close Adj_Close Volume
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AAPL 2018-10-03 230. 233. 230. 232. 230. 28654800
2 AMZN 2018-09-04 2026. 2050. 2013 2040. 2040. 5721100
3 FB 2018-07-25 216. 219. 214. 218. 218. 58954200
4 GOOG 2018-07-26 1251 1270. 1249. 1268. 1268. 2405600
In order to do this, I first tried the filter command on its own but it only returned AMZN since that had a highest closing price. So in order to get the peak closing price for each of the four stocks from gafa_stock, I used group_by(Symbol) to get the peak for each of the different stocks from gafa_stock. For AAPL, it was 2018-10-03 with a closing value of 232.07. For AMZN, it was 2018-09-04 with a closing value of 2039.51. For FB, it was 2018-07-25 with a closing value of 217.50. For GOOG, it was 2018-07-26 with a closing value of 1268.33.
Exercise 2.3
Download the file tute1.csv from the book website, 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.
# a. You can read the data into R with the following script:tute1 <- readr::read_csv("tute1.csv")
Rows: 100 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): Sales, AdBudget, GDP
date (1): Quarter
ℹ 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.
View(tute1)
# b. Convert the data to time seriesmytimeseries <- tute1 |>mutate(Quarter =yearquarter(Quarter)) |>as_tsibble(index = Quarter)
# c. Construct time series plots of each of the three seriesmytimeseries |>pivot_longer(-Quarter) |>ggplot(aes(x = Quarter, y = value, colour = name)) +geom_line() +facet_grid(name ~ ., scales ="free_y")
# d. Check what happens when you don’t include facet_grid().mytimeseries |>pivot_longer(-Quarter) |>ggplot(aes(x = Quarter, y = value, colour = name)) +geom_line()
It looks like without the facet_grid, the data is plotted all on the same plot and the values become very hard to read since the scale is very big, especially the GDP’s green plot. I can’t really tell what the values are besides just the overall shape.
Exercise 2.4
The USgas package contains data on the demand for natural gas in the US.
# a. Install the USgas package.library(USgas)
# b. Create a tsibble from us_total with year as the index and state as the key.us_total_tsibble <- us_total |>as_tsibble(index = year, key = state)
# 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).new_england_gas <- us_total_tsibble |>filter(state %in%c("Maine", "Vermont", "New Hampshire", "Massachusetts","Connecticut", "Rhode Island") )
autoplot(new_england_gas, y) +labs(y ="Total Natural Gas Consumption (million cubic feet)",title ="Annual Total Natural Gas Consumption")
Exercise 2.5
# a. Download tourism.xlsx from the book website and read it into R using readxl::read_excel().tourism_xlsx <- readxl::read_excel("tourism.xlsx")View(tourism_xlsx)
# b. Create a tsibble which is identical to the tourism tsibble from the tsibble package.tsibble::tourism
# A tsibble: 24,320 x 5 [1Q]
# Key: Region, State, Purpose [304]
Quarter Region State Purpose Trips
<qtr> <chr> <chr> <chr> <dbl>
1 1998 Q1 Adelaide South Australia Business 135.
2 1998 Q2 Adelaide South Australia Business 110.
3 1998 Q3 Adelaide South Australia Business 166.
4 1998 Q4 Adelaide South Australia Business 127.
5 1999 Q1 Adelaide South Australia Business 137.
6 1999 Q2 Adelaide South Australia Business 200.
7 1999 Q3 Adelaide South Australia Business 169.
8 1999 Q4 Adelaide South Australia Business 134.
9 2000 Q1 Adelaide South Australia Business 154.
10 2000 Q2 Adelaide South Australia Business 169.
# ℹ 24,310 more rows
# A tsibble: 24,320 x 5 [1Q]
# Key: Region, State, Purpose [304]
Quarter Region State Purpose Trips
<qtr> <chr> <chr> <chr> <dbl>
1 1998 Q1 Adelaide South Australia Business 135.
2 1998 Q2 Adelaide South Australia Business 110.
3 1998 Q3 Adelaide South Australia Business 166.
4 1998 Q4 Adelaide South Australia Business 127.
5 1999 Q1 Adelaide South Australia Business 137.
6 1999 Q2 Adelaide South Australia Business 200.
7 1999 Q3 Adelaide South Australia Business 169.
8 1999 Q4 Adelaide South Australia Business 134.
9 2000 Q1 Adelaide South Australia Business 154.
10 2000 Q2 Adelaide South Australia Business 169.
# ℹ 24,310 more rows
# c. Find what combination of Region and Purpose had the maximum number of overnight trips on average.tourism_tsibble_2 |>group_by(Region, Purpose) |>filter(Trips ==max(Trips)) |>arrange(desc(Trips))
# A tsibble: 304 x 5 [1Q]
# Key: Region, State, Purpose [304]
# Groups: Region, Purpose [304]
Quarter Region State Purpose Trips
<qtr> <chr> <chr> <chr> <dbl>
1 2017 Q4 Melbourne Victoria Visiting 985.
2 2001 Q4 Sydney New South Wales Business 948.
3 2016 Q4 Sydney New South Wales Visiting 921.
4 1998 Q1 South Coast New South Wales Holiday 915.
5 2016 Q1 North Coast NSW New South Wales Holiday 906.
6 1998 Q1 Sydney New South Wales Holiday 828.
7 2017 Q4 Melbourne Victoria Holiday 806.
8 2016 Q4 Brisbane Queensland Visiting 796.
9 2002 Q1 Gold Coast Queensland Holiday 711.
10 2017 Q3 Melbourne Victoria Business 704.
# ℹ 294 more rows
The combination of Region and Purpose with the maximum number of overnight trips on average was “Melbourne” and “Visiting” with 985.278401 overnight trips on average.
# d. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.tourism_tsibble_3 <- tourism_xlsx |>mutate(Quarter =yearquarter(Quarter)) |>as_tsibble(index = Quarter, key =c(Region, State, Purpose))
Use the following graphics functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() and explore features from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and Barrels from us_gasoline.
Can you spot any seasonality, cyclicity and trend?
What do you learn about the series?
What can you say about the seasonal patterns?
Can you identify any unusual years?
Total private employment has a trend that goes upward. It looks to be seasonal with many rises and falls every year. There also seems to be cycles too where the dips are larger after a certain number of years before rebounding and trending upwards again. There are some unusual years like 2010 and the early 2000s due to the great recession and the dot com bubble, respectively.
Looking at the data, it seems that employment is slightly higher in the summer months (likely due to breaks from school) and lower during the start of the year.
The correlogram shows a strong positive correlation.
Bricks from aus_production
autoplot(aus_production, Bricks)
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
This time series looks like it is seasonal due to the regular rising and falling. There doesn’t seem to be any overall trend. It looks like it is cyclical though, with the sharp drops at 1975 and early 1980s. Besides 1975 and like 1983 being unusually low, 1996 also looks like it goes down a lot too.
gg_season(aus_production, Bricks)
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
Looking at this plot, it seems the demand for bricks is lower in Q1 and rises in Q2 and Q4 before falling again in Q4. This suggests that summer months are more popular to be building with bricks. The demand for bricks in 1975 to 1985 was also greater than the demand in the time periods before and after.
gg_subseries(aus_production, Bricks)
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).
This plot also reinforces that summer time has more demand for bricks compared to winter time.
This plot also shows that Q1 is noticeably lower than everything else, especially when compared to Q3.
aus_production |>ACF(Bricks) |>autoplot()
This shows a positive correlation at 0 Lag that gradually drops to around 0.5.
Hare from pelt
autoplot(pelt, Hare)
This time series doesn’t look like it has any trends, it just goes up and down. It does look cyclical though since every 10 years or so, it looks to go up and down. It’s not seasonal since its annual data so no data for different months or seasons.
gg_subseries(pelt, Hare)
It only has 1 panel since it’s yearly data, not much else to comment on this.
gg_lag(pelt, Hare, geom ="point")
Looking at this lagplot, the first panel for lag 1 looks like there’s a positive correlation but as the lag increases, the correlation gets weaker and then by lag 9, it looks like it’s becoming a positive correlation again. This suggests that hare population routinely is good when the previous year was good until it just starts falling cyclically every 10 years or so before it goes up again.
pelt |>ACF(Hare) |>autoplot()
The ACF shows that there are cycles which are positive and negative correlation. Around lag 5 is when it’s the most negative before turning it all the way around at the highest around lag 10.
This plot looks like it’s seasonal since there looks to be regular up and downs. It looks like it trends upwards over time. It doesn’t look cyclical or have any unusual years.
This plot shows a peak at 12 lag and then again near the 24 lag. This suggests that the plot is seasonal every year. This is true since in Febuary it always falls only to rebound right back up.
Barrels from us_gasoline
autoplot(us_gasoline, Barrels)
This plot has an upward trend. The plot also appears to be seasonal since it regularly has dips and rises. It also appears to be cyclical since there is an unusually bigger dip after 2009 which is due to the great recession.
gg_season(us_gasoline, Barrels)
The plots looks like the summer months experience a raise probably due to the fact that school is out and people are travelling.
gg_subseries(us_gasoline, Barrels)
This plot is pretty hard to read due to everything being squished together. But it looks like as the weeks go on, it gradually raises until during the middle of the year, where the volume is the highest due to summer travel. Then it slowly tapers down again.
gg_lag(us_gasoline, Barrels, geom ="point")
The plot shows a strong positive correlation in each of the panels.
us_gasoline |>ACF(Barrels) |>autoplot()
The plot shows a pretty strong correlation throughout the plot, only dropping minimally around 30 weeks.