prepareData.keras: Configuration of data for downscaling with a keras model

View source: R/prepareData.keras.R

prepareData.kerasR Documentation

Configuration of data for downscaling with a keras model

Description

Configuration of data for flexible downscaling keras experiment definition

Usage

prepareData.keras(
  x,
  y,
  global.vars = NULL,
  combined.only = TRUE,
  spatial.predictors = NULL,
  local.predictors = NULL,
  first.connection = c("dense", "conv"),
  last.connection = c("dense", "conv"),
  channels = c("first", "last"),
  time.frames = NULL
)

Arguments

x

A grid (usually a multigrid) of predictor fields

y

A grid (usually a stations grid, but not necessarily) of observations (predictands)

global.vars

An optional character vector with the short names of the variables of the input x multigrid to be retained as global predictors (use the getVarNames helper if not sure about variable names). This argument just produces a call to subsetGrid, but it is included here for better flexibility in downscaling experiments (predictor screening...). For instance, it allows to use some specific variables contained in x as local predictors and the remaining ones, specified in subset.vars, as either raw global predictors or to construct the combined PC.

combined.only

Optional, and only used if spatial.predictors parameters are passed. Should the combined PC be used as the only global predictor? Default to TRUE. Otherwise, the combined PC constructed with which.combine argument in prinComp is append to the PCs of the remaining variables within the grid.

spatial.predictors

Default to NULL, and not used. Otherwise, a named list of arguments in the form argument = value, with the arguments to be passed to prinComp to perform Principal Component Analysis of the predictors grid (x). See Details on principal component analysis of predictors.

local.predictors

Default to NULL, and not used. Otherwise, a named list of arguments in the form argument = value, with the following arguments:

  • vars: names of the variables in x to be used as local predictors

  • fun: Optional. Aggregation function for the selected local neighbours. The aggregation function is specified as a list, indicating the name of the aggregation function in first place (as character), and other optional arguments to be passed to the aggregation function. For instance, to compute the average skipping missing values: fun = list(FUN= "mean", na.rm = TRUE). Default to NULL, meaning that no aggregation is performed.

  • n: Integer. Number of nearest neighbours to use. If a single value is introduced, and there is more than one variable in vars, the same value is used for all variables. Otherwise, this should be a vector of the same length as vars to indicate a different number of nearest neighbours for different variables.

Note that grid 'y' has to be single-site, otherwise this will cause errors in the model training, since downscaleTrain.keras is designed to store only one model at a time due to Keras particularities. If your desire is to downscale to multiple-sites for independent models, please loop over this function for the different sites.

first.connection

A string. Possible values are c("dense","conv") depending on whether the first connection (i.e., input layer to first hidden layer) is dense or convolutional.

last.connection

A string. Same as first.connection but for the last connection (i.e., last hidden layer to output layer).

channels

A string. Possible values are c("first","last") and indicates the dimension of the channels (i.e., climate variables) in the array. If "first" then dimensions = c("channel","latitude","longitude") for regular grids or c("channel","loc") for irregular grids. If "last" then dimensions = c("latitude","longitude","channel") for regular grids or c("loc","channel") for irregular grids.

time.frames

The number of time frames to build the recurrent neural network. If e.g., time.frame = 2, then the value y(t) is a function of x(t) and x(t-1). The time frames stack in the input array prior to the input neurons or channels (in conv. layers). See layer_simple_rnn,layer_lstm or layer_conv_lstm_2d.

Details

Remove days containing NA in at least one predictand site.

Value

A named list with components y (the predictand), x.global (global predictors) and other attributes. For the case when spatial and local predictors are both computed, these are stacked together in the x.global object.

Author(s)

J. BaƱo-Medina

See Also

downscaleTrain.keras for training a downscaling deep model with keras downscalePredict.keras for predicting with a keras model prepareNewData.keras for predictor preparation with new (test) data downscaleR.keras Wiki

downscaleR Wiki for preparing predictors for downscaling and seasonal forecasting.

Examples


require(climate4R.datasets) 
# Loading data
require(transformeR)
require(downscaleR)
data("VALUE_Iberia_tas")
y <- VALUE_Iberia_tas
data("NCEP_Iberia_hus850", "NCEP_Iberia_psl", "NCEP_Iberia_ta850")
x <- makeMultiGrid(NCEP_Iberia_hus850, NCEP_Iberia_psl, NCEP_Iberia_ta850)
# We standardize the predictors using transformeR function scaleGrid
x <- scaleGrid(x,type = "standardize")
# Preparing the predictors
data <- prepareData.keras(x = x, y = y, 
                          first.connection = "conv",
                          last.connection = "dense",
                          channels = "last")
# We can visualize the outputield not imported f
str(data)

# We can call prepareData.keras to compute PCs over the predictor field
data <- prepareData.keras(x = x, y = y, 
                          spatial.predictors = list(v.exp = 0.95),   # the EOFs that explain the 95% of the total variance
                          first.connection = "dense",
                          last.connection = "dense",
                          channels = "last")


SantanderMetGroup/downscaleR.keras documentation built on July 7, 2023, 1:22 p.m.