---
title: "Time Series Homework: Chapter 1 Lesson 2"
subtitle: "Please_put_your_name_here"
format: 
  html:
    embed-resources: true
    toc: true
editor: source
---

```{r}
#| message: false
#| echo: false

# Loading R packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, 
               tsibble, fable,
               feasts, tsibbledata,
               fable.prophet,
               patchwork,
               lubridate, 
               rio 
)
```

## Data

```{r}
# Weather data for Rexburg
rex_temp <- rio::import("https://byuistats.github.io/timeseries/data/rexburg_weather.csv")
```

## Questions

### Question 1: Context and Measurement (10 points)

The first part of any time series analysis is context. You cannot properly analyze data without knowing what the data is measuring. Without context, the most simple features of data can be obscure and inscrutable. This homework assignment will center around the series below.

Please research the time series. In the spaces below, give the data collection process, unit of analysis, and meaning of each observation for the series.

#### a) Rexburg, ID Daily High Temperatures

::: {.callout-note title="Answer" icon="false"}
<!-- Delete this and put your answer here. The three colons mark the end of your answer. -->
:::

### Question 2: Visualization (5 points)

Please plot the Rexburg Daily Temperature series choosing the range and frequency to illustrate the data in the most readable format. Use the appropriate axis labels, units, and captions.

::: {.callout-note title="Answer" icon="false"}
```{r}
# Please provide your code here

```
:::

### Question 3: Additive Decomposition - Manual Approach (25 points)

This exercise will guide you through all the steps to conduct an **additive decomposition** of the Rexburg Daily Temperature Series. The first step is to aggregate the daily series to a monthly frequency to ease on the calculation. The code below accomplished the task.

#### a) Please use AI to comment and explain the steps of the code below. Replace the code below with the fully explained code.

```{r}
# Weather data for Rexburg
monthly_tsibble <- rio::import("https://byuistats.github.io/timeseries/data/rexburg_weather.csv") |> 
  mutate(date2 = ymd(dates)) |>
  mutate(year_month = yearmonth(date2)) |>
  # as_tibble() |>
  group_by(year_month) |>
  summarize(average_daily_high_temp = mean(rexburg_airport_high)) |>
  ungroup() |> 
  as_tsibble(index = year_month)

view(monthly_tsibble) 
```

#### b) Please calculate the centered moving average of the Rexburg Monthly Temperature Series. Plot the series.

::: {.callout-note title="Answer" icon="false"}
```{r}
# Please provide your code here

```
:::

#### c) Please calculate the seasonally adjusted means series. Plot the series

::: {.callout-note title="Answer" icon="false"}
```{r}
# Please provide your code here

```
:::

#### d) Please calculate the random component. Please plot the series.

::: {.callout-note title="Answer" icon="false"}
```{r}
# Please provide your code here

```
:::

### Question 4: Additive Decomposition - R's Decompose (15)

#### a) Please use additive decomposition model described in the Time Series notebook to decompose the Rexburg Monthly Temperature Series. Plot the algorithm's output.

::: {.callout-note title="Answer" icon="false"}
```{r}
# Please provide your code here

```
:::

#### b) You can also decompose without aggregating the data series. The following code completes the additive decomposition with the original data set. Please use AI to comment and explain how to accomplish the task. Replace the code below with the fully explained code.

```{r}
# Weather data for Rexburg
daily_tsibble <- rio::import("https://byuistats.github.io/timeseries/data/rexburg_weather.csv") |>
  mutate(year_month_day = ymd(dates)) |>
  select(-imputed, -dates) |>
  # as_tibble() |>
  as_tsibble(index = year_month_day)

daily_tsibble %>% head

daily_decompose <- daily_tsibble  |>
  model(feasts::classical_decomposition(rexburg_airport_high ~ season(365.25),
                                        type = "add"))  |>
  components()


daily_decompose |> autoplot(.vars = seasonal)
```

### Question 5: Seasonally Adjusted Series - Analysis (20 points)

#### a) Justify why we use the additive decomposition model to seasonally adjust the Rexburg Daily Temperature series.

::: {.callout-note title="Answer" icon="false"}
<!-- Delete this and put your written answer here. The three colons mark the end of your answer. -->
:::

#### b) You calculated the random component of the series using three different procedures. Are the random component series the same? Are there patterns that are similar across all the random component series? Propose an explanation for the commonalities.

::: {.callout-note title="Answer" icon="false"}
<!-- Delete this and put your written answer here. The three colons mark the end of your answer. -->
:::

<!-- Try not to touch the table markdown below -->

## Rubric

+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Criteria**                                      | **Mastery (10)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 1: Context and Measurement**           | The student thoroughly researches the data collection process, unit of analysis, and meaning of each observation for both the requested time series. Clear and comprehensive explanations are provided.                                                                                                                                                                                                                                                                                                       | The student does not adequately research or provide information on the data collection process, unit of analysis, and meaning of each observation for the specified series.                                                                                                                                                                   |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (5)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 2: Visualization**                     | Chooses a reasonable manual range for the Rexburg Daily Temperature series, providing a readable plot that captures the essential data trends. Creates a plot with accurate and clear axis labels, appropriate units, and a caption that enhances the understanding of the Rexburg Daily Temperature series.                                                                                                                                                                                                  | Attempts manual range selection, but with significant issues impacting the readability of the plot. The chosen range may obscure important data trends, demonstrating a limited understanding of graphical representation.Fails to include, axis labels, units, or captions, leaving the visual representation and interpretation incomplete. |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (5)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 3a: Monthly Aggregation**              | The code has been updated with comments and clear explanations of what each command and function does. The student shows they understand the intuition behind the procedure.                                                                                                                                                                                                                                                                                                                                  | The code has not been updated or the comments and explanation do not provide enough evidence to prove the student understand the code.                                                                                                                                                                                                        |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (5)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 3b: Centered Moving Average**          | Correctly calculates the centered moving average. Clearly presents the results with well-labeled axes, titles, and a properly formatted plot. \| Incorrectly calculates the centered moving average or omits it entirely. The plot is either missing or poorly presented, lacking clear labels, titles, or proper formatting, making it difficult to interpret the results. \|                                                                                                                                |                                                                                                                                                                                                                                                                                                                                               |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (10)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 3c: Seasonally Adjusted Means Series** | Correctly calculates the seasonally adjusted means series using an appropriate method. Produces a clear, accurate plot of the seasonally adjusted time series with well-labeled axes and titles. \| Incorrect calculation or missing/incorrect plot. Plot lacks essential elements like labels, titles, or fails to represent the seasonally adjusted series. \|                                                                                                                                              |                                                                                                                                                                                                                                                                                                                                               |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (5)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 3d: Random Component Series**          | Correctly calculates the random component of the series by removing the trend and seasonal components. Produces a clear, accurate plot with well-labeled axes, titles, and proper formatting.                                                                                                                                                                                                                                                                                                                 | Incorrectly calculates the random component, omits steps (e.g., does not remove trend/seasonality), or the plot is unclear or missing essential elements like labels and titles.                                                                                                                                                              |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (10)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 4a: Decompose Monthly**                | Correctly applies the additive decomposition model to the Rexburg Monthly Temperature Series. Clearly presents the trend, seasonal, and random components in well-labeled plots, with appropriate titles, axes labels, and formatting.                                                                                                                                                                                                                                                                        | Fails to correctly apply the additive decomposition model, resulting in incorrect or incomplete separation of the trend, seasonal, and random components. The plot is either missing or poorly presented, lacking proper labels, titles, or clear distinction between components.\                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (5)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 4b: Decompose Daily**                  | The code has been updated with comments and clear explanations of what each command and function does. The student shows they understand the intuition behind the procedure.                                                                                                                                                                                                                                                                                                                                  | The code has not been updated or the comments and explanation do not provide enough evidence to prove the student understand the code.                                                                                                                                                                                                        |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (10)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 5a: Modeling Justification**           | Clearly differentiates between the multiplicative and additive model assumptions, and shows how the series best matches the additive model's assumptions.                                                                                                                                                                                                                                                                                                                                                     | It's not clear that the student understands the difference between the additive and multiplicative model or their assumptions.                                                                                                                                                                                                                |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
|                                                   | **Mastery (10)**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | **Incomplete (0)**                                                                                                                                                                                                                                                                                                                            |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Question 5b: Random Component Analysis**        | Compares the random components derived from three different procedures, accurately stating whether they are the same or different. Identifies and explains any observed similarities or differences. Proposes a logical, well-reasoned explanation for why similar patterns might exist in the random components, using relevant time series concepts (e.g., common noise factors, model assumptions). \| Fails to correctly compare the random components, or provides an unclear or inaccurate comparison.\ |                                                                                                                                                                                                                                                                                                                                               |         |
|                                                   | \| Fails to provide a clear or accurate explanation for the commonalities, or provides irrelevant reasoning. \|                                                                                                                                                                                                                                                                                                                                                                                               |                                                                                                                                                                                                                                                                                                                                               |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
| **Total Points**                                  | **75**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |                                                                                                                                                                                                                                                                                                                                               |         |
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+
