Build a Degree Day Decision Support Tool in R

Part 1. Degree Days


May 3, 2024
https://ucanr-igis.github.io/degday-shiny-s24/

Start Recording



About Me…

IGIS Team

About You

Type of Organization

Email domain

Location

Familiarity with degree days

Familiarity with R

Workshop Goals

Primary focus

Programming techniques for working with weather data and degree day models in R


What we will cover

Degree Days

  • about degree days and degree days models
  • how to compute degree days in R
  • using degree day models to make predictions
  • visualizing predictions

Weather Data

  • what makes good weather data
  • weather data APIs for:
  • recent past
  • short term forecast
  • historical averages
  • seasonal projections
  • modeled climate futures
  • calling weather APIs
  • R packages with API wrappers
  • writing your own code to query APIs
  • wrangling weather data

Creating a Web App Decision Support Tool

  • Shiny web apps
  • reactive programming
  • deploying Shiny apps


What we will not cover

  • degree day models
  • weather data
  • weather models
  • ArcGIS services
  • gdal
  • best practices using modeled climate futures
  • transforming model predictions into digestible and actionable information
  • daisy-chaining models


Take Home Messages

Degree day models are widely used to predict the development / phenology of plants and insects

They are widely used in:

Implementing degree days requires:

R is well-equipped to wrangle data, compute degree days, and make predictions

APIs have simplified importing weather data, but its still tricky

Shiny greatly simplifies developing and publishing degree days models as lightweight web apps


Workshop Materials

Degree Days


dplyr


Top dplyr Functions

subset rows filter(), slice()
order rows arrange()
pick column(s) select(), pull()
add new columns mutate()


Piping syntax

Piping allows the result of one function to be passed as the first argument of the next function.

Pipe operators:

This allows a series of functions to be written like a sentence.

zoo(moo(boo(foo(99)),n=4))


degday


Overview

  • available on CRAN
  • contains functions for all the equations from Zalom et. al. (1983) and McMaster and Wilhelm (1997)
  • results have been validated with reference data from Zalom et. al. and the UC IPM website (see also Degree Days Validation Challenge)
  • the package does not include:
  • vertical and intermediate cutoff methods
  • degree hours
  • models for using degree days

https://ucanr-igis.github.io/degday/


Functions

dd_simp_avg()
dd_sng_sine()
dd_dbl_sine()
dd_sng_tri()
dd_dbl_tri()


Usage Notes

  • they presume these are daily min and max values (but do not check)
  • they return a vector of degree days of the same length
  1. Filter the weather data by date
  2. Use mutate() with one of the above functions to add a column for daily degree days
  3. Create an additional column for the accumulated degree days (e.g, using mutate() and cumsum())


Example:

Load some sample data:

library(degday)

espartoa_tbl <- system.file("extdata/espartoa-weather-2020.csv", package = "degday") |> 
  read.csv() |> 
  mutate(date = as.Date(date))

head(espartoa_tbl)
##     station       date tmin tmax
## 1 Esparto.A 2020-01-01   38   55
## 2 Esparto.A 2020-01-02   36   67
## 3 Esparto.A 2020-01-03   33   59
## 4 Esparto.A 2020-01-04   37   59
## 5 Esparto.A 2020-01-05   38   63
## 6 Esparto.A 2020-01-06   36   58


Compute daily degree days using the single sine method, and save it as a new column:

espartoa_tbl |> 
  mutate(degdays = dd_sng_sine(daily_min = tmin, daily_max = tmax, 
                                thresh_low = 55, thresh_up = 93.9)) |> 
  mutate(acc_degdays = cumsum(degdays)) |> 
  head()
##  - using single sine method
##     station       date tmin tmax degdays acc_degdays
## 1 Esparto.A 2020-01-01   38   55    0.00        0.00
## 2 Esparto.A 2020-01-02   36   67    3.31        3.31
## 3 Esparto.A 2020-01-03   33   59    0.68        3.99
## 4 Esparto.A 2020-01-04   37   59    0.74        4.73
## 5 Esparto.A 2020-01-05   38   63    1.99        6.72
## 6 Esparto.A 2020-01-06   36   58    0.48        7.20


Double-Sine and Double-Triangle Methods

The double-sine and double-triangle methods require adding tomorrow’s minimum temperature as an additional column:

espartoa_plus2mrw_tbl <- espartoa_tbl |> 
  mutate(tomorrow_tmin = lead(tmin, n = 1))

head(espartoa_plus2mrw_tbl)
##     station       date tmin tmax tomorrow_tmin
## 1 Esparto.A 2020-01-01   38   55            36
## 2 Esparto.A 2020-01-02   36   67            33
## 3 Esparto.A 2020-01-03   33   59            37
## 4 Esparto.A 2020-01-04   37   59            38
## 5 Esparto.A 2020-01-05   38   63            36
## 6 Esparto.A 2020-01-06   36   58            30


Compute daily degree days with the double-sine method:

espartoa_plus2mrw_tbl |> 
  mutate(dbl_tri = dd_dbl_tri(daily_min = tmin, daily_max = tmax, 
                              nextday_min = tomorrow_tmin,
                              thresh_low = 55, thresh_up = 93.9)) |> 
  head()
##  - using double triangle method
##     station       date tmin tmax tomorrow_tmin dbl_tri
## 1 Esparto.A 2020-01-01   38   55            36    0.00
## 2 Esparto.A 2020-01-02   36   67            33    2.22
## 3 Esparto.A 2020-01-03   33   59            37    0.34
## 4 Esparto.A 2020-01-04   37   59            38    0.37
## 5 Esparto.A 2020-01-05   38   63            36    1.23
## 6 Esparto.A 2020-01-06   36   58            30    0.18


ggplot

library(ggplot2)
library(palmerpenguins)

ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm, color = species)) +
  geom_point() +
  ggtitle("Bill Length vs Flipper Length for 3 Species of Penguins")


Anatomy of a ggplot expression


R Notebooks

R Notebooks are written in “R Markdown”, which combines text and R code.

Notebook #1: Compute Degree Days

Posit Cloud users

https://posit.cloud/content/8147278

Click ‘Save a permanent copy’

Done!


RStudio Desktop Power Users

1. Download RStudio project

Download a RStudio project with all the exercises and data:

Option 1

## Download and open on desktop
usethis::use_course("https://bit.ly/degday-weather-wrkshp-rproj-s24")

## Download to a specific location 
## usethis::use_course("https://bit.ly/degday-weather-wrkshp-rproj-s24", destdir = "c:/workshops")

Option 2

  1. download zip file: https://bit.ly/degday-weather-wrkshp-rproj-s24
  2. unzip
  3. open degday-shiny-s24.Rproj


2. Install required packages

/scripts/install_packages.R

End!