https://github.com/eco4cast/neon4cast
A helper R package for the neon4cast challenge
https://github.com/eco4cast/neon4cast
Last synced: 4 months ago
JSON representation
A helper R package for the neon4cast challenge
- Host: GitHub
- URL: https://github.com/eco4cast/neon4cast
- Owner: eco4cast
- License: other
- Created: 2021-03-15T22:09:46.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-13T13:16:42.000Z (7 months ago)
- Last Synced: 2024-09-14T03:58:45.843Z (7 months ago)
- Language: R
- Size: 1.29 MB
- Stars: 7
- Watchers: 6
- Forks: 7
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - eco4cast/neon4cast - A helper R package for the neon4cast challenge (R)
README
---
output:
github_document:
df_print: tibble
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# neon4cast
[](https://github.com/eco4cast/neon4cast/actions)
`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](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("eco4cast/neon4cast")
```## Examples
```{r message=FALSE}
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.```{r, message=FALSE}
target <- read_csv("https://data.ecoforecast.org/neon4cast-targets/aquatics/aquatics-targets.csv.gz")aquatic <- target %>%
pivot_wider(names_from = "variable", values_from = "observation") %>%
as_tsibble(index = datetime, key = site_id)
```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 `` 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.
```{r, message = FALSE}
# drop last 35 days and use explicit NAs for gaps in timeseries
blinded_aquatic <- aquatic %>%
filter(datetime < max(datetime) - 35) %>%
fill_gaps()# A simple random walk forecast, see ?fable::RW
oxygen_fc <- blinded_aquatic %>%
model(benchmark_rw = RW(oxygen)) %>%
forecast(h = "35 days") %>%
efi_format()## also use random walk for temperature
temperature_fc <- blinded_aquatic %>%
model(benchmark_rw = RW(temperature)) %>%
forecast(h = "35 days") %>%
efi_format_ensemble()# stack into single table
forecast <- bind_rows(oxygen_fc, temperature_fc)## 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 = "benchmark_rw")
write_csv(forecast, forecast_file)```
### Score forecast locally
Scores for valid forecasts should appear at 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!
```{r message=FALSE}
scores <- score(forecast, target)
# The resulting data.frame scores each day for each site, but is also easy to summarize:
scores %>%
group_by(variable) %>%
summarise(mean_crps = mean(crps, na.rm=TRUE),
mean_logs = mean(logs, 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.```{r}
forecast_output_validator(forecast_file)
```### 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.
```{r}
aq_sites <- unique(aquatic$site_id)noaa <- noaa_stage1()
ref_date <- Sys.Date()-1
# Average temperature forecast across ensembles by site_id, next 240 hours
noaa_temp <- noaa |>
filter(site_id %in% aq_sites,
reference_datetime == ref_date,
variable == "TMP",
horizon < 240) |>
group_by(site_id, reference_datetime, datetime, variable) |>
summarise(prediction = mean(prediction, na.rm=TRUE)) |>
collect()noaa_temp
```Additional NOAA products are available. `stage2` product includes downscaling to hourly increments with fluxes corrected for solar geometries.
The stage3 product is assembled from all the 0-hour horizons, which may be more useful for calibrating models than reliance on direct measurements of meterological data, as the latter may differ systematically from the long-term forecast at any given site.### Submit a forecast
When you are ready to submit your forecast to EFI:
```{r eval=FALSE}
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](https://github.com/eco4cast/neon4cast/issues) or [pull request](https://github.com/eco4cast/neon4cast/pulls) here!
```{r include=FALSE}
unlink(forecast_file)
```