detrendGrid: Linear detrending

View source: R/detrendGrid.R

detrendGridR Documentation

Linear detrending

Description

Perform a linear detrending along the time dimension of a grid

Usage

detrendGrid(
  grid,
  grid2 = NULL,
  parallel = FALSE,
  max.ncores = 16,
  ncores = NULL
)

Arguments

grid

Input grid (possibly multimember)

grid2

Optional grid. If provided, the output is the detrended grid2 using the regression coefficients calculated using grid. Default to NULL.

parallel

Logical. Should parallel execution be used?

max.ncores

Integer. Upper bound for user-defined number of cores.

ncores

Integer number of cores used in parallel computation. Self-selected number of cores is used when ncpus = NULL (the default), or when maxcores exceeds the default ncores value.

Details

Performs a simple linear detrending by fitting a linear model and retaining the residuals. An attribute indicating the linear detrending is added to the Variable component of the output grid.

In the presence of missing data in the time series, it operates by filtering them prior to linear model fitting. The missing data positions are then restored back to the output detrended series.

The function uses the fastLm implementation from package RcppEigen, significantly speeding-up the linear model fitting.

Value

A detrended grid.

Parallel Processing

Parallel processing is enabled using the parallel package. Parallelization is undertaken by a FORK-type parallel socket cluster formed by ncores. If ncores is not specified (default), ncores will be one less than the autodetected number of cores. The maximum number of cores used for parallel processing can be set with the max.ncores argument, although this will be reset to the auto-detected number of cores minus 1 if this number is exceeded. Note that not all code, but just some critical loops within the function are parallelized.

In practice, parallelization does not always result in smaller execution times, due to the parallel overhead. However, parallel computing may potentially provide a significant speedup for the particular case of large multimember datasets or large grids.

Parallel computing is currently not available for Windows machines.

Author(s)

J Bedia, J Fernandez, M.D. Frias

Examples

 
require(climate4R.datasets) 
data("NCEP_Iberia_ta850")
monthly <- aggregateGrid(NCEP_Iberia_ta850, aggr.m = list(FUN = "mean"))
plot(monthly$Data[,4,2], ty = 'l')
abline(reg = lm(monthly$Data[,4,2] ~ I(1:length(monthly$Data[,4,2]))), lty = 2)
det <- detrendGrid(monthly, parallel = FALSE)
# Detrended series in red
lines(det$Data[,4,2], col = "red")
abline(reg = lm(det$Data[,4,2] ~ I(1:length(det$Data[,4,2]))), col = "red", lty = 2)
legend("topright", c("Raw", "Detrended"), lty = 1, col = c(1,2))
## example of forecast detrend
# Suppose 1991-2001 is the reference period. We use it to detrend data from year 2002
grid <- subsetGrid(monthly, years = 1991:2001)
grid2 <- subsetGrid(monthly, years = 2002)
det2 <- detrendGrid(grid, grid2, parallel = FALSE)
# det2 is the grid2 (year 2002) corrected according to the linear trend computed for 1991-2001


SantanderMetGroup/transformeR documentation built on Oct. 28, 2023, 5:26 a.m.