Time Series Analysis

This post demonstrates time series analysis for fish passage time at Willamette Falls from 2001 - 2010.

Yutian Fang true
2022-03-13
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(tidyverse)
library(here)
library(lubridate)
library(tsibble)
library(feasts)

Data Summary

# Read in data and replace NA with 0
passage <- read_csv(here('data/willamette_fish_passage.csv')) %>%
   mutate(across(everything(), ~ ifelse(is.na(.), 0, .)))

# Turn passage into time series data
passage_ts <- passage %>%
  mutate(Date = lubridate::mdy(Date)) %>%
  as_tsibble(key = NULL, index = Date) %>%
  janitor::clean_names() %>%
  select(date, coho, jack_coho, steelhead)

#Transform the format of passage_ts
passage_ts_clean <- passage_ts %>%
  pivot_longer(cols = c(coho, jack_coho, steelhead)) %>%
  rename(species = name)

Original time series

ggplot(data = passage_ts_clean, aes(x = date, y = value)) +
  geom_line(aes(color = species)) +
  scale_color_manual(values = c("red","blue","green")) +
  labs(x = 'Date',
       y = 'Counts by Species' ) +
  theme_classic()
This line figure shows the original time series for coho, jack coho and steelhead, demonstrated in red, blue and green lines. The x-axis represents date (on day), and y-axis represents counts of each species on that date.

Figure 1: This line figure shows the original time series for coho, jack coho and steelhead, demonstrated in red, blue and green lines. The x-axis represents date (on day), and y-axis represents counts of each species on that date.

Observed Trend

Seasonplots

#create monthly dataset
passage_ts_month <- passage_ts_clean %>%
  index_by(yr_mo = ~yearmonth(.)) %>%
  group_by(species) %>%
  summarize(monthly_species = sum(value))

#Make seasonal plot
gg_season(data = passage_ts_month, y = monthly_species) +
  labs(x = 'Month', y = 'Counts for Species') +
  theme_classic()
This figure is composed by three panels, with each panel represents the seasonal plot of each species. The x-axis represents month in each year, and y-axis represents the summarized counts of species in that month. The lines are colored based on survey year.

Figure 2: This figure is composed by three panels, with each panel represents the seasonal plot of each species. The x-axis represents month in each year, and y-axis represents the summarized counts of species in that month. The lines are colored based on survey year.

Observed Trend

Annual counts by species

# Create annual dataset
passage_ts_annual <- passage_ts_clean %>%
  index_by(yr = ~year(.)) %>%
  group_by(species) %>%
  summarize(annual_species = sum(value))

# Create line graph
ggplot(data = passage_ts_annual, aes(x = yr, y = annual_species)) +
  geom_line(aes(color = species)) +
  scale_x_continuous(breaks = c(2001:2010)) +
  scale_color_manual(values = c("red","blue","green")) +
  labs(x = 'Year',
       y = 'Counts by Species' ) +
  theme_classic()
This line figure demonstrates how the total annual counts of three fish species change with survey year. Red line represents coho, blue line represents jack coho, and green line represents steelhead. x-axis represents survey year, and y-axis represents total annual counts by species.

Figure 3: This line figure demonstrates how the total annual counts of three fish species change with survey year. Red line represents coho, blue line represents jack coho, and green line represents steelhead. x-axis represents survey year, and y-axis represents total annual counts by species.

Observed Trend