Homework 2

Time Series Graphics

Getting started

R/Rstudio in Class

Each of your assignments will begin with the following setup steps.

  • Finding the instructions on our website: https://stats-tgeorge.github.io/STA363_AdvReg/

  • Going to our RStudio Server at http://turing.cornellcollege.edu:8787/

  • Open the respective file from the shared STA364_inst_files folder mentioned above. It will be named something like hw-02_LAST_NAME.qmd.

  • Then you need to save your copy. Click File -> Save as -> Navigate to the folder STA364_Projects (that we share) -> Change the “LAST_NAME part of the file name to your last name -> Save.

  • Update the top of the document, called the YAML, with your name.

Homework Instructions

Be sure to include the relevant R code and complete sentences answering each question (e.g., if I ask for the average, you can output the answer in R but also write a complete sentence with the answer). Be sure to save your files frequently!

Data for the homework will be in the STA364_inst_files -> data folder.

Exercises

Reading

Read fpp3 2.7-2.9.

Problems

Question 1

Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, and Demand from vic_elec.

  1. Use ? (or help()) to find out about the data in each series.
  2. What is the time interval of each series?
  3. Use autoplot() to produce a time plot of each series.
  4. For the last plot, modify the axis labels and title. See the labs() function.

Question 2 (fpp3)

  1. Create a data folder inside your STA364 project folder. Go to RStudio and navigate to your STA364Project folder in the files tab on the bottom right. Click New Folder and name the folder data.

  2. Now, you need to copy the data from the instructor data folder to a data folder in your folder. Go to the STA364_inst_files folder and into the data folder

  3. Check the box next to tute.csv, then More, then Copy to…, and save the file in your newly created data folder.

The first chunk below reads tute1.csv into R and then shows the data with the View() function. You should find four columns of information. Columns B through D each contain a quarterly series labeled 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.

You can read the data into R with the following script:

tute1 <- readr::read_csv("data/tute1.csv")
View(tute1)

Then converts the data to a time series object in R (tsibble).

mytimeseries <- tute1 |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(index = Quarter)

Then construct the time series plot for the Sales series.

mytimeseries |>
  autoplot(Sales)
  1. Use this code to generate a time series plot for the other two series in the data, AdBudget, and GDP.
  2. Compare and contrast the three series.

Question 3 (fpp4)

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

  1. Install the USgas package. (Use install.packages('package_name')). Then to use it you will need library(package_name).
  2. Create a tsibble from us_total with year as the index and state as the key.
  3. 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).

Question 4 (fpp6)

The aus_arrivals data set comprises quarterly international arrivals to Australia from Japan, New Zealand, the UK, and the US. It is available after you call in the fpp3 package.

  1. Use autoplot(), gg_season() and gg_subseries() to compare the differences between the arrivals from these four countries.

  2. Can you identify any unusual observations?

Submission

When you are finished with your homework, be sure to Render the final document. Once rendered, you can download your file by:

  • Finding the .html file in your File pane of RStudio (on the bottom right of the screen)
  • Click the check box next to the file
  • Click the blue gear above and then click “Export” to download
  • Submit your final HTML document to the respective assignment on Moodle