Applied Time Series Notebook
  • Master
  • Projects
    • Overview
    • Applied Time Series Projects

    • Project 1
    • Time Series: China Export Commodities

    • Project 2
    • Time Series Project 2: Consumer Credit

    • Project 3
    • Project title here
  • Ch 1
    • Overview
    • Chapter overview and Task
    • Time Series Homework: Chapter 1 Lesson 1
    • Time Series Homework: Chapter 1 Lesson 2
    • Time Series Homework: Chapter 1 Lesson 3
    • Time Series Homework: Chapter 1 Lesson 4
    • Time Series Homework: Chapter 1 Lesson 5
  • Ch 2
    • Overview
    • Autocorrelation Concepts
    • Time Series Homework: Chapter 2 Lesson 1
    • Time Series Homework: Chapter 2 Lesson 2
    • Time Series Homework: Chapter 2 Lesson 3
  • Ch 3
    • Overview
    • Chapter overview and Task
    • Time Series Homework: Chapter 3 Lesson 2
    • Time Series Homework: Chapter 3 Lesson 3
    • Time Series Homework: Chapter 3 Lesson 4
    • Time Series Homework: Chapter 3 Lesson 5

    • r code Models draft
    • Chapter 3 r code examples and practice

    • Lesson 1
    • White Noise and Random Walks - Part 1
    • Time Series Homework: Chapter 3 Lesson 1
  • Ch 4
    • Overview
    • Chapter overview and Task

    • r code Models draft
    • Chapter 4 r code examples and practice

    • Lesson 1
    • White Noise and Random Walks - Part 1
    • Ch 4.1 Code Notes

    • Lesson 2
    • White Noise and Random Walks - Part 2
    • Time Series Homework: Chapter 4 Lesson 2

    • Lesson 3
    • Autoregressive (AR) Models
    • Time Series Homework: Chapter 4 Lesson 3

    • Lesson 4
    • Fitted AR Models
    • Ch 4.4 Code Notes
  • Ch 5
    • Overview
    • Chapter overview and Task

    • Lesson 1
    • White Noise and Random Walks - Part 1

    • Lesson 1 Notes
    • Linear Models, GLS, and Seasonal Indicator Variables
  • Tools
    • Tools, Help & Ideas
    • Tools, Resources and Help Ideas
    • Markdown Visuals
    • Git and Terminal Commands
    • Steps for formatting Date and Creating Index
    • Functions & Formulas
    • test
  • Outcomes
  • def
  1. r code Models draft
  2. Chapter 3 r code examples and practice
  • Overview
    • Chapter overview and Task
    • Time Series Homework: Chapter 3 Lesson 2
    • Time Series Homework: Chapter 3 Lesson 3
    • Time Series Homework: Chapter 3 Lesson 4
    • Time Series Homework: Chapter 3 Lesson 5
  • r code Models draft
    • Chapter 3 r code examples and practice
  • Lesson 1
    • White Noise and Random Walks - Part 1
    • Time Series Homework: Chapter 3 Lesson 1
  1. r code Models draft
  2. Chapter 3 r code examples and practice

Chapter 3 r code examples and practice

This qmd is made to summarize chapter 3 and have models for the chapter to view.

  • I have a goal to make the code not be reliant on adjusting variable names and titles.

    • This will require having an initial r chunk that assigns variable names and dataset.

    • I will still have multiple df.

  • This will be a bit hard and the models will have to be pretty general because some data sets required different approaches

    • I think maybe I can also assign if its yearmonth, yearquater etc, but that will require so many if statements.

      • Idk how I’m going to approach this
# Assign your column names to variables
df0 <- rio::import("https://byuistats.github.io/timeseries/data/constructionequip_manu_orders_shipments.csv")


dates <- "date"
x_col <- "constructionequip_ord"
y_col <- "constructionequip_ship"



# Assign plot labels
x_label <- "Month"
y_label <- "New Orders & Value of Equip"
plot_title <- "Time Series of Construction Equip: New Orders & Equipment"
df <- df0 |>
  mutate(
    date = lubridate::mdy(.data[[dates]]),
    x = as.numeric(.data[[x_col]]), # Convert and rename to x
    y = as.numeric(.data[[y_col]])  # Convert and rename to y
  ) |>
  select(date, x, y) 
df1 <- df |> # this makes a new df so either df before or this one is use. 
  mutate(obs = row_number()) |> # makes new column with periods
  select(date, obs, x, y)

dfx <- df |> # lone df for variable x = ord
  mutate(obs = row_number()) |> 
  select(date, x)

dfy <- df |> # lone df for y = ship
  mutate(obs = row_number()) |> 
  select(date, y)

test

# this is code for hw 3-1

# this code is same as the dfx2 & dfy2 code
df1 <- df1 |>
  mutate(index = tsibble::yearmonth(date)) |> # 3.1
  as_tsibble(index = index) |>
  select(index, date, x, y)



autoplot(df1, .vars = x) +
  geom_line(data = df1, aes(x = index, y = y), color = "#E69F00") +
  labs(
    x = x_label,
    y = y_label,
    title = plot_title
  ) +
  theme(plot.title = element_text(hjust = 0.5))

Back to top

Eduardo Ramirez 2024©