-
Notifications
You must be signed in to change notification settings - Fork 59
How to contribute with new bias correction methods
Each method for bias correction in downscaleR
(e.g. "eqm", "scaling",etc.) is built as an independent simple function that operates over vectors, being biasCorrection
the wrapper function that deals with data dimensionality and subsetting of grid
objects. This code structure allows to easily add new methods for bias correction, by adding a single new function ("method function") with no need of modifying the existing code.
Users of downscaleR
are highly encouraged to contribute with new bias correction methods. In the following, requirements are detailed through and example.
###Building the function
The minimum data required are three vectors, these are objects named o
, p
and s
.
o
: A vector (e.g. station data) containing the observed climate data for the training period
p
: A vector containing the simulated climate by the model for the training period.
s
: A vector containing the simulated climate for the variable used in p
, but considering the test period.
This is the function for the delta
method:
delta <- function(o, p, s){
corrected <- o + (mean(s) - mean(p))
return(corrected)
}
Depending on the method, more arguments may be needed, for example, the scaling.type
argument for the scaling method, that is selected by the user when applying the biasCorrection
function and directly passed to the "method function":
scaling <- function(o, p, s, scaling.type){
if (scaling.type == "additive") {
s - mean(p) + mean(o)
} else if (scaling.type == "multiplicative") {
(s/mean(p)) * mean(o)
}
}
###Documentation
Functions must be documented with the format used by package roxygen2
by write specially-structured comments preceding each function definition. The following heading in function scaling
contains the minimum information required and could be use as a template:
#' @title Scaling method for bias correction
#' @description Implementation of Scaling method for bias correction
#' @param o A vector (e.g. station data) containing the observed climate data for the training period
#' @param p A vector containing the simulated climate by the model for the training period.
#' @param s A vector containing the simulated climate for the variable used in \code{x}, but considering the test period.
#' @param scaling.type Character indicating the type of the scaling method. Options are \code{"additive"} (default)
#' or \code{"multiplicative"} (see details). This argument is ignored if \code{"scaling"} is not selected as the bias correction method.
#' @author S. Herrera and M. Iturbide
scaling <- function(o, p, s, scaling.type){
if (scaling.type == "additive") {
s - mean(p) + mean(o)
} else if (scaling.type == "multiplicative") {
(s/mean(p)) * mean(o)
}
}
#end
If the new method requires importing a function from another package, add this information to the heading with @importFrom
in the following manner: @importFrom {package} {function 1} {function2} {function...}. For example:
#' @importFrom stats pgamma qgamma
For more examples go to http://kbroman.org/pkg_primer/pages/docs.html
downscaleR - Santander MetGroup (Univ. Cantabria - CSIC)