knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

neon4cast

R-CMD-check

neon4cast provides a collection of convenient helper utilities for anyone entering the EFI NEON Forecasting Challenge.

Installation

You can install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("eco4cast/neon4cast")

Examples

library(neon4cast)
library(tidyverse)
library(fable)
library(tsibble)

Download and read in the current target file for the Aquatics theme. For convenience, we read this in as a timeseries object, noting that the time is in the 'time' column, and timeseries are replicated over sites.

targets <- read_csv("https://data.ecoforecast.org/targets/terrestrial/terrestrial_daily-targets.csv.gz", guess_max = 1e5) %>% 
  as_tsibble(index=time, key=siteID)

Create a 35 day forecast for each variable, oxygen, and temperature. For illustrative purposes, we'll use the fable package because it is concise and well documented. We make separate forecasts for each of the two variables before reformatting them and combining them. Note the use of efi_format helper function from the neon4cast package, which merely replaces the special <S3:distribution> column used by fable with something we can write to text: either columns with a mean/sd (for normal distributions) or otherwise random draws from the distributions.

So that we can score our forecast right away instead of waiting for next month's data, we will filter out the most recent data available first.

# drop last 35 days and use explicit NAs for gaps in timeseries
blinded_aquatic <- aquatic %>% filter(time < max(time) - 35) %>% fill_gaps()

# A simple random walk forecast, see ?fable::RW
oxygen_fc <- blinded_aquatic %>%
  model(null = RW(oxygen)) %>%
  forecast(h = "35 days") %>%
  efi_format()

## also use random walk for temperature
temperature_fc <- blinded_aquatic  %>%
  model(null = RW(temperature)) %>%
  forecast(h = "35 days") %>%
  efi_format()

# combine into single table, drop the .model column
forecast <- inner_join(oxygen_fc, temperature_fc) %>% select(-.model)

## Write the forecast to a file following EFI naming conventions:
forecast_file <- glue::glue("{theme}-{date}-{team}.csv.gz",
                            theme = "aquatics", 
                            date=Sys.Date(),
                            team = "example_null")
write_csv(forecast, forecast_file)

Score forecast locally

Scores for valid forecasts should appear at https://shiny.ecoforecast.org the day after they are submitted. However, it is often more convenient to generate scores locally. Note that the "score" simply the crps_sample (for ensemble forecasts) or crps_norm (for summary statistic forecasts) score from the scoringRules R package, for each unique prediction (i.e. day/site/variable tuple).

Note that scores are only possible once the data becomes available in the corresponding targets file!

scores <- score(forecast, theme = "aquatics")

# The resulting data.frame scores each day for each site, but is also easy to summarize:
scores %>% 
  group_by(siteID, target) %>% 
  summarise(mean_score = mean(score, na.rm=TRUE))

Validate a forecast file

Validating a forecast file runs the same automated checks as the EFI server, verifying that the data is in the correct format for the appropriate challenge. Helpful errors or warnings will displayed on any invalid formats. Note that the validator accepts files in .csv (optionally compressed as .csv.gz) or netcdf.

forecast_output_validator(forecast_file)

Generate forecast metadata in EML

forecast_file <-"terrestrial_daily-2021-04-01-EFInulldaily.csv.gz"
download.file("https://data.ecoforecast.org/forecasts/terrestrial/terrestrial_daily-2021-04-01-EFInulldaily.csv.gz", forecast_file)
write_meta_template(forecast_file)
generate_metadata(forecast_file, 
                  gsub(".\\w+\\.?(gz)$", ".yml", forecast_file),
                  forecast_issue_time = Sys.Date(), 
                  forecast_iteration_id = 1)

Access EFI snapshots of NOAA forecasts at NEON sites

Many forecasts will want to make use of weather forecasts as potential drivers. EFI downscales NOAA GEFS 35-day forecast products at each NEON site and makes this data available. These helper functions provide convenient access for downloading and stacking the individual forecast files.

aq_sites <- unique(aquatic$siteID)
download_noaa(aq_sites)
noaa_fc <- stack_noaa()
noaa_fc

Submit a forecast

When you are ready to submit your forecast to EFI:

submit(forecast_file)

Ideally you should include the optional metadata = argument with your metadata file.

Other functions

Encountered a bug? Facing another challenge in participating in the challenge? Developed a cool approach you would like to share with the community? Open an issue or pull request here!

unlink(forecast_file)


eco4cast/neon4cast documentation built on May 31, 2024, 9:07 a.m.