Restricted cubic spline transformation and aggregation of climate data
Source:R/staggregate.R
staggregate_spline.Rd
The function staggregate_spline()
aggregates climate data to the daily
level, performs a restricted cubic spline transformation on these daily
values, and aggregates the transformed values to the polygon level and
desired temporal scale.
Usage
staggregate_spline(
data,
overlay_weights,
daily_agg,
time_agg = "month",
start_date = NA,
time_interval = "1 hour",
knot_locs
)
Arguments
- data
The raster brick with the data to be transformed and aggregated
- overlay_weights
A table of weights which can be generated using the function
overlay_weights()
- daily_agg
How to aggregate hourly values to daily values prior to transformation. Options are
'sum'
,'average'
, or'none'
('none'
will transform values without first aggregating to the daily level)- time_agg
the temporal scale to aggregate data to. Options are
'hour'
,'day'
,'month'
, or'year'
('hour'
cannot be selected unlessdaily_agg = 'none'
)- start_date
the date (and time, if applicable) of the first layer in the raster. To be input in a format compatible with lubridate::as_datetime(), e.g.
"1991-10-29"
or"1991-10-29 00:00:00"
. The default isNA
since the rasters usually already contain temporal information in the layer names and they do not need to be manually supplied.- time_interval
the time interval between layers in the raster to be aggregated. To be input in a format compatible with seq(), e.g.
'1 day'
or'3 months'
. The default is'1 hour'
and this argument is required if daily_agg is not'none'
or if thestart_date
argument is notNA
.- knot_locs
where to place the knots
Examples
spline_output <- staggregate_spline(
data = temp_nj_jun_2024_era5 - 273.15, # Climate data to transform and
# aggregate
overlay_weights = overlay_weights_nj, # Output from overlay_weights()
daily_agg = "average", # Average hourly values to produce daily values
# before transformation
time_agg = "month", # Sum the transformed daily values across months
start_date = "2024-06-01 00:00:00", # The start date of the supplied data,
# only required if the layer name format
# is not compatible with stagg
time_interval = "1 hour", # The temporal interval of the supplied data,
# required if daily_agg is not "none" or if the
# start_date argument is not NA
knot_locs = c(0, 7.5, 12.5, 20) # Where to place knots
)
#> Rewriting the data's temporal metadata (layer names) to reflect a dataset starting on the supplied start date and with a temporal interval of 1 hour
#> Averaging over 24 layers per day to get daily values
#> Executing spline transformation
#> Aggregating by polygon and month
head(spline_output)
#> year month poly_id value term_1 term_2
#> <num> <num> <char> <num> <num> <num>
#> 1: 2024 6 011 726.5687 301176.6 61231.64
#> 2: 2024 6 033 733.6797 306509.9 62564.98
#> 3: 2024 6 015 728.9664 302974.9 61681.26
#> 4: 2024 6 009 686.7632 271322.6 53768.18
#> 5: 2024 6 007 730.9276 304445.8 62048.98
#> 6: 2024 6 041 683.5539 268988.1 53195.44