---
title: "Model Components (Notes)"
subtitle: "Stat 452"
author: "Your Name"
format:
  html:
    toc: true
    toc-depth: 2
    embed-resources: true
---




# Small Group Activity {.unnumbered .smaller}

**Download a template Qmd file to start from [here](https://bcheggeseth.github.io/452_fall_2025/student_notes/L05-model-components-notes.qmd).**


## Data Context {.unnumbered .smaller}

The data come from Mauna Loa Observatory (MLO) in Hawaii. According to the website, "the undisturbed air, remote location, and minimal influences of vegetation and human activity at MLO are ideal for monitoring constituents in the atmosphere that can cause climate change."

You'll analyze the CO2 concentration measurements available on the MLO’s website. CO2 measurements have been collected since 1958 until present day and they are presented as monthly averages. Click the website below to see the data in the text file. The first 50 lines include information about the data and provide variable definitions. Read the data into R with the code below. I have provided you some code to add Variable Names to a data frame.

```{r eval=TRUE, include = TRUE}
co2 <- read.table('ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_mm_mlo.txt',skip=50,header=FALSE)
names(co2) <- c('Year','Month','Date','CO2','CO2Trend','Days','sdDays','seCO2')
co2[co2 < 0] <- NA
head(co2)
```

## Exercises {.unnumbered .smaller}

1. Using `ggplot()`, make a plot of the CO2 concentration (`CO2`) as a function of the `Date`. Comment on what you observe in the data context. 


```{r}
require(ggplot2)
require(dplyr)


```

2.  Using `ggplot()`, make a plot of the CO2 concentration (`CO2`) as a function of the `Month`, grouped by `Year`.  Comment on what you observe in the data context. 



```{r}


```

### Estimating Trend {.unnumbered .smaller}

3.  Estimate the trend of the data using a moving average filter. Create a vector of weights (save it as `fltr`) for points before and after the time that you are estimating the trend, such that you are adequately averaging over one years of data. 

```{r}
#| eval: true
# Save data as a ts (time series) object to keep track of time and the values.
co2_ts_data <- ts(co2$CO2, start = c(1958,11), frequency = 12) 
```

```{r}
fltr <- ??  #vector of weights for moving average filter

# Adjust data frame by adding MA trend as variable  
co2 <- co2 %>%
  mutate( co2Trend_MA = as.numeric(stats::filter(co2_ts_data, filter = fltr, method = 'convo', sides = 2)))

co2  %>% 
  ggplot(aes(x = Date, y = CO2)) +
  geom_line() +
  geom_line(aes(y = co2Trend_MA), color = 'red') +
  theme_classic()
```

4. Estimate the trend of the data using local regression, `loess()`. Adjust `span=` so as to only capture the trend and not the seasonality. Compare the estimated trend with that from the moving average. 

```{r}
# Adjust data frame by adding Loess trend as variable  
co2 <- co2 %>%
  mutate(co2Trend_loess = predict(loess(CO2 ~ Date, data = co2, span = ??)))

co2  %>% 
  ggplot(aes(x = Date, y = CO2)) +
  geom_line() +
  geom_line(aes(y = co2Trend_MA), color = 'red') +
  geom_line(aes(y = co2Trend_loess), color = 'blue') +
  theme_classic()
```

5. What other ways might we estimate this trend? Think about other classes you've taken (Stat 155, Stat 253). Try implementing those approaches to estimating the trend, save the predicted values as `co2Trend_1` and `co2Trend_2`, and compare them to the other two estimated trends by plotting it on top.

```{r}
# Add new variables, co2Trend_1 and co2Trend_2 to the co2 data set


```

6. Choose a method (of the four above) to estimate trend that you find works best. Demonstrate its utility on this data set by visualizing the residuals (also known as the detrended data). Make sure the plot includes a horizontal line at 0 and a smooth estimated mean of the residuals across time.

```{r}
co2 <- co2 %>%
  mutate(Detrend = CO2 - ??)


```

### Estimating Seasonality {.unnumbered .smaller}

7. Visualize the seasonality by first removing the estimated trend (the trend you prefer) and plotting those residuals (`Detrend`) as a function of `Month`, grouped by `Year`. 

```{r}

```

8. Estimate the seasonality using indicators for `Month` to estimate the monthly average deviations due to seasonality using linear regression. Plot the estimated average monthly deviations (estimate of the seasonality). Comment on the plot. Does this make sense based on your understanding of the planet and CO2 in the atmosphere?  

```{r}
lm.season <- lm(Detrend ~ ??, data = co2)
  
# Add new variable, Season, as the predictions from lm.season to the co2 data set
co2 <- co2 %>%
  mutate(??)  

# Plot estimated average monthly deviations
  
```


9. Visualize the leftover residuals after removing the estimated seasonality (`Errors`). Do the residuals look like independent random noise?

```{r}
co2 <- co2 %>%
  mutate(Errors = Detrend - Season)

co2 %>%
  ggplot(aes(x = Date, y = Errors)) + 
  geom_point() +
  geom_line() +
  geom_hline(yintercept = 0) + 
  theme_classic()
```


10. Do you think it is safe to assume the resulting random process is stationary? 




11. Assuming stationarity, estimate the correlation function. Comment and what you notice. What does it mean in the context of the problem? Is it independent random noise?

```{r}
acf(na.omit(co2$Errors))
```


We will consider models for these error processes, assuming they are stationary. 


12. CHALLENGE: Let's say you want to predict the CO2 concentrations in the near future. How could you use what we've done so far to do that prediction? How might you adjust the processes we've used to make forecasts in the future? Don't implement, just brainstorm.





### Concept: Differencing {.unnumbered .smaller}

*If you have time during class, continue working. Otherwise, we'll come back to this concept in another class activity.*

Instead of estimating the trend and seasonality, we could directly remove the trend and seasonality with differencing. 

A first-order difference is $y_t - y_{t-1}$. A first-order seasonal difference with lag 12 is $y_t - y_{t-12}$. 

#### First-order Differences {.unnumbered .smaller}

1. If you take a first-order difference between observations 12 months apart (lag 12), you get this. Describe what is left over.

```{r}
#| eval: true
plot(diff(co2_ts_data,lag = 12))
```

2. If you take a first-order difference between observations 1 month apart (lag 1), you get this. Describe what is left over.

```{r}
#| eval: true
plot(diff(co2_ts_data,lag = 1))
```

3. If you take the 1 month differences and then take 12 month differences, we get this. Describe what is left over.

 $$(y_t - y_{t-1}) - (y_{t-12} - y_{t-13})$$

```{r}
#| eval: true
plot(diff(diff(co2_ts_data,lag = 1),lag = 12))
```

4. Try a seasonal lag of a different value other than 12, what happens? Describe what is left over.

```{r}

plot(diff(diff(co2_ts_data),lag = ?))
```




5. Imagine process with a linear trend line ($y_t = t$). What will a first-order difference look like? Prove it to yourself algebraically (and then look at the graph).

 $$(y_t - y_{t-1})$$

```{r}
times = 1:10
y = times

plot(times, y)
plot(diff(y))
```

6. Imagine a process with no trend but a seasonal component where $y_t = y_{t-k}$. What would a first-order difference of seasonal lag $k$ do? Prove it to yourself algebraically (and then look at the graph).
 
  $$(y_t - y_{t-k})$$

```{r}
times = 1:100
y = rep(c(1:5,5:1),10)

plot(times,y,type='l')
plot(diff(y, lag=10))
```

#### Second-order Differences {.unnumbered .smaller}

7. Imagine a random process with quadratic trend ($y_t = t^2$). What will a first-order difference look like? Prove it to yourself algebraically (and then look at the graph).

```{r}
times = 1:10
y = times^2

plot(times,y)
plot(diff(y))
```

8. For the process with quadratic trend, what will a second-order difference (difference the difference) look like? Prove it to yourself algebraically (and then look at the graph).
 
  $$(y_t - y_{t-1}) - (y_{t-1} - y_{t-2}) = y_t - 2y_{t-1} + y_{t-2}$$

```{r}
times = 1:10
y = times^2

plot(times,y)
plot(diff(y, differences=2))
```


9. Imagine a random walk where $y_t = y_{t-1} + \epsilon_t$. What would a first-order difference of lag 1 do?
 
  $$(y_t - y_{t-1})$$

```{r}
e = rnorm(100)
y = cumsum(e)

plot(y,type='l')
plot(diff(y, lag=1),type='l')
acf(diff(y, lag=1))
```
## Done!

- Render your notes.
- Check the solutions in the course website (Solution drop downs).
- If you finish all that during class, start your homework!


