From cb41c59ccdd21f04a52bab6b244dff545bf87da2 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 18 Dec 2019 16:25:18 +0100 Subject: [PATCH 1/3] Modify Eno() with Apply feature --- DESCRIPTION | 3 +- NAMESPACE | 2 + R/Eno.R | 198 +++++++++++++++++++++++++--------------------------- man/Eno.Rd | 70 ++++++++----------- 4 files changed, 129 insertions(+), 144 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0045265c..6780c83d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -50,7 +50,8 @@ Imports: ncdf4, parallel, plyr, - SpecsVerification (>= 0.5.0) + SpecsVerification (>= 0.5.0), + multiApply (>= 2.0.0) Suggests: easyVerification, testthat diff --git a/NAMESPACE b/NAMESPACE index 4e855789..8700555d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -90,6 +90,7 @@ import(graphics) import(mapproj) import(maps) import(methods) +import(multiApply) import(ncdf4) import(parallel) import(plyr) @@ -116,6 +117,7 @@ importFrom(stats,kmeans) importFrom(stats,lm) importFrom(stats,mad) importFrom(stats,median) +importFrom(stats,na.fail) importFrom(stats,na.omit) importFrom(stats,na.pass) importFrom(stats,pf) diff --git a/R/Eno.R b/R/Eno.R index 877710cb..63803fc3 100644 --- a/R/Eno.R +++ b/R/Eno.R @@ -1,116 +1,112 @@ -#'Computes Effective Sample Size With Classical Method +#'Compute Effective Sample Size With Classical Method #' -#'Computes the effective number of independent values along the posdim -#'dimension of a matrix.\cr -#'This effective number of independent observations can be used in -#'statistical/inference tests.\cr -#'Based on eno function from Caio Coelho from rclim.txt. +#'Compute the number of effective samples along one dimension of an array. This +#'effective number of independent observations can be used in statistical/inference +#'tests.\cr +#'The calculation is based on eno function from Caio Coelho from rclim.txt. #' -#'@param obs Matrix of any number of dimensions up to 10. -#'@param posdim Dimension along which to compute the effective sample size. +#'@param data A numeric array with named dimensions. +#'@param sdate_dim A function indicating the dimension along +#' which to compute the effective sample size. Default value is 'sdate'. +#'@param na.action A charater string indicating the action for missing values, +#' can be na.pass (missing values are allowed) or na.fail (no missing values +#' are allowed). See details in stats::acf(). Default value is 'na.pass'. +#'@param ncores An integer indicating the number of cores to use for parallel +#' computation. Default value is NULL. #' -#'@return Same dimensions as var but without the posdim dimension. +#'@return An array with the first dimension 'stats', which is the number of +#' effective samples, and the following dimensions same as parameter 'data' +#' except the sdate_dim dimension. #' #'@keywords datagen #'@author History:\cr -#'0.1 - 2011-05 (V. Guemas, \email{virginie.guemas at ic3.cat}) - Original code\cr -#'1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens at ic3.cat}) - Formatting to R CRAN +#'0.1 - 2011-05 (V. Guemas, \email{virginie.guemas@ic3.cat}) - Original code\cr +#'1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens@ic3.cat}) - Formatting to R CRAN +#'3.0 - 2019-12 (A. Ho, \email{an.ho@bsc.es}) - Adopt multiApply +#' #'@examples -#'# See examples on Load() to understand the first lines in this example -#' \dontrun{ -#'data_path <- system.file('sample_data', package = 's2dverification') -#'exp <- list( -#' name = 'experiment', -#' path = file.path(data_path, 'model/$EXP_NAME$/monthly_mean', -#' '$VAR_NAME$_3hourly/$VAR_NAME$_$START_DATES$.nc') -#' ) -#'obs <- list( -#' name = 'observation', -#' path = file.path(data_path, 'observation/$OBS_NAME$/monthly_mean', -#' '$VAR_NAME$/$VAR_NAME$_$YEAR$$MONTH$.nc') -#' ) -#'# Now we are ready to use Load(). -#'startDates <- c('19851101', '19901101', '19951101', '20001101', '20051101') -#'sampleData <- Load('tos', list(exp), list(obs), startDates, -#' leadtimemin = 1, leadtimemax = 4, output = 'lonlat', -#' latmin = 27, latmax = 48, lonmin = -12, lonmax = 40) -#' } -#' \dontshow{ -#'startDates <- c('19851101', '19901101', '19951101', '20001101', '20051101') -#'sampleData <- s2dverification:::.LoadSampleData('tos', c('experiment'), -#' c('observation'), startDates, -#' output = 'lonlat', -#' latmin = 27, latmax = 48, -#' lonmin = -12, lonmax = 40) -#' } -#'sampleData$mod <- Season(sampleData$mod, 4, 11, 1, 12) -#'eno <- Eno(sampleData$mod[1, 1, , 1, , ], 1) -#'PlotEquiMap(eno, sampleData$lon, sampleData$lat) +#'set.seed(1) +#'data <- array(rnorm(800), dim = c(dataset = 1, member = 2, sdate = 4, +#' ftime = 4, lat = 10, lon = 10)) +#'na <- floor(runif(40, min=1, max=800)) +#'data[na] <- NA +#'res <- Eno(data) #' -#'@importFrom stats acf na.pass +#'@importFrom stats acf na.pass na.fail +#'@import multiApply #'@export -Eno <- function(obs, posdim) { - dimsvar <- dim(obs) - if (is.null(dimsvar)) { - dimsvar <- length(obs) +Eno <- function(data, sdate_dim = 'sdate', na.action = na.pass, ncores = NULL) { + + # Check inputs + ## data + if (is.null(data)) { + stop("Parameter 'data' cannot be NULL.") + } + if (!is.numeric(data)) { + stop("Parameter 'data' must be a numeric array.") + } + if (is.null(dim(data))) { #is vector + dim(data) <- c(length(data)) + names(dim(data)) <- sdate_dim + } + if(any(is.null(names(dim(data))))| any(nchar(names(dim(data))) == 0)) { + stop("Parameter 'data' must have dimension names.") + } + + ## sdate_dim + if (!is.character(sdate_dim) | length(sdate_dim) > 1) { + stop("Parameter 'sdate_dim' must be a character string.") } - enlobs <- Enlarge(obs, 10) - outdim <- c(dimsvar, array(1, dim = (10 - length(dimsvar)))) - posaperm <- 1:10 - posaperm[posdim] <- 1 - posaperm[1] <- posdim - enlobs <- aperm(enlobs, posaperm) - dimsaperm <- outdim[posaperm] - # - # Loop on all dimensions to compute effective number of observations - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # - enleno <- array(dim = c(1, dimsaperm[2:10])) - for (j2 in 1:dimsaperm[2]) { - for (j3 in 1:dimsaperm[3]) { - for (j4 in 1:dimsaperm[4]) { - for (j5 in 1:dimsaperm[5]) { - for (j6 in 1:dimsaperm[6]) { - for (j7 in 1:dimsaperm[7]) { - for (j8 in 1:dimsaperm[8]) { - for (j9 in 1:dimsaperm[9]) { - for (j10 in 1:dimsaperm[10]) { - tmp <- enlobs[, j2, j3, j4, j5, j6, j7, j8, j9, j10] - if (length(sort(tmp)) > 1 ) { - n <- length(sort(tmp)) - a <- acf(tmp, lag.max = n - 1, plot = FALSE, - na.action = na.pass)$acf[2:n, 1, 1] - s <- 0 - for (k in 1:(n - 1)) { - s <- s + (((n - k) / n) * a[k]) - } - enleno[1, j2, j3, j4, j5, j6, j7, j8, j9, - j10] <- min(n / (1 + (2 * s)), n) - } - } - } - } - } - } - } - } + if (!sdate_dim %in% names(dim(data))) { + stop("Parameter 'sdate_dim' is not found in 'data' dimension.") + } + + ## na.action + if (as.character(substitute(na.action)) != c("na.pass") && + as.character(substitute(na.action)) != c("na.fail")) { + stop("Parameter 'na.action' must be either 'na.pass' or 'na.fail'.") + } + + if(as.character(substitute(na.action))== c("na.fail") && any(is.na(data))) { + stop(paste0("Calculation fails because NA is found in paratemter 'data', ", + "which is not accepted when ", + "parameter 'na.action' = 'na.fail'.")) + } + + ## ncores + if (!is.null(ncores)) { + if (!is.numeric(ncores)) { + stop("Parameter 'ncores' must be a positive integer.") + } else if (ncores <= 0 | ncores %% 1 != 0) { + stop("Parameter 'ncores' must be a positive integer.") } } - # - # Back to the original dimensions - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # - #dimsvar <- dimsvar[-posdim] - if (length(dimsvar) == 1) { - dimsvar <- 1 + + ############################### + # Calculate Eno + + eno <- Apply(data = list(data), + target_dims = sdate_dim, + output_dims = 'stats', + fun = .Eno, + na.action = na.action, + ncores = ncores)$output1 + + return(eno) +} + +.Eno <- function(x, na.action) { + n <- length(sort(x)) + if (n > 1) { + a <- acf(x, lag.max = n - 1, plot = FALSE, + na.action = na.action)$acf[2:n, 1, 1] + s <- 0 + for (k in 1:(n - 1)) { + s <- s + (((n - k) / n) * a[k]) + } + eno <- min(n / (1 + (2 * s)), n) } else { - dimsvar <- dimsvar[-posdim] + eno <- NA } - effnumobs <- array(dim = dimsvar) - effnumobs[] <- aperm(enleno, posaperm) - # - # Outputs - # ~~~~~~~~~ - # - effnumobs + return(as.array(eno)) } diff --git a/man/Eno.Rd b/man/Eno.Rd index ba4f2088..da049bf5 100644 --- a/man/Eno.Rd +++ b/man/Eno.Rd @@ -2,62 +2,48 @@ % Please edit documentation in R/Eno.R \name{Eno} \alias{Eno} -\title{Computes Effective Sample Size With Classical Method} +\title{Compute Effective Sample Size With Classical Method} \usage{ -Eno(obs, posdim) +Eno(data, sdate_dim = "sdate", na.action = na.pass, ncores = NULL) } \arguments{ -\item{obs}{Matrix of any number of dimensions up to 10.} +\item{data}{A numeric array with named dimensions.} -\item{posdim}{Dimension along which to compute the effective sample size.} +\item{sdate_dim}{A function indicating the dimension along +which to compute the effective sample size. Default value is 'sdate'.} + +\item{na.action}{A charater string indicating the action for missing values, +can be na.pass (missing values are allowed) or na.fail (no missing values +are allowed). See details in stats::acf(). Default value is 'na.pass'.} + +\item{ncores}{An integer indicating the number of cores to use for parallel +computation. Default value is NULL.} } \value{ -Same dimensions as var but without the posdim dimension. +An array with the first dimension 'stats', which is the number of + effective samples, and the following dimensions same as parameter 'data' + except the sdate_dim dimension. } \description{ -Computes the effective number of independent values along the posdim -dimension of a matrix.\cr -This effective number of independent observations can be used in -statistical/inference tests.\cr -Based on eno function from Caio Coelho from rclim.txt. +Compute the number of effective samples along one dimension of an array. This +effective number of independent observations can be used in statistical/inference +tests.\cr +The calculation is based on eno function from Caio Coelho from rclim.txt. } \examples{ -# See examples on Load() to understand the first lines in this example - \dontrun{ -data_path <- system.file('sample_data', package = 's2dverification') -exp <- list( - name = 'experiment', - path = file.path(data_path, 'model/$EXP_NAME$/monthly_mean', - '$VAR_NAME$_3hourly/$VAR_NAME$_$START_DATES$.nc') - ) -obs <- list( - name = 'observation', - path = file.path(data_path, 'observation/$OBS_NAME$/monthly_mean', - '$VAR_NAME$/$VAR_NAME$_$YEAR$$MONTH$.nc') - ) -# Now we are ready to use Load(). -startDates <- c('19851101', '19901101', '19951101', '20001101', '20051101') -sampleData <- Load('tos', list(exp), list(obs), startDates, - leadtimemin = 1, leadtimemax = 4, output = 'lonlat', - latmin = 27, latmax = 48, lonmin = -12, lonmax = 40) - } - \dontshow{ -startDates <- c('19851101', '19901101', '19951101', '20001101', '20051101') -sampleData <- s2dverification:::.LoadSampleData('tos', c('experiment'), - c('observation'), startDates, - output = 'lonlat', - latmin = 27, latmax = 48, - lonmin = -12, lonmax = 40) - } -sampleData$mod <- Season(sampleData$mod, 4, 11, 1, 12) -eno <- Eno(sampleData$mod[1, 1, , 1, , ], 1) -PlotEquiMap(eno, sampleData$lon, sampleData$lat) +set.seed(1) +data <- array(rnorm(800), dim = c(dataset = 1, member = 2, sdate = 4, + ftime = 4, lat = 10, lon = 10)) +na <- floor(runif(40, min=1, max=800)) +data[na] <- NA +res <- Eno(data) } \author{ History:\cr -0.1 - 2011-05 (V. Guemas, \email{virginie.guemas at ic3.cat}) - Original code\cr -1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens at ic3.cat}) - Formatting to R CRAN +0.1 - 2011-05 (V. Guemas, \email{virginie.guemas@ic3.cat}) - Original code\cr +1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens@ic3.cat}) - Formatting to R CRAN +3.0 - 2019-12 (A. Ho, \email{an.ho@bsc.es}) - Adopt multiApply } \keyword{datagen} -- GitLab From bbf9ee63302dc3ee6245b5d754f46abc4d55ec82 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 18 Dec 2019 17:13:22 +0100 Subject: [PATCH 2/3] Add test-Eno.R --- R/Eno.R | 11 ++--- tests/testthat/test-Eno.R | 99 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/test-Eno.R diff --git a/R/Eno.R b/R/Eno.R index 63803fc3..8ef177c4 100644 --- a/R/Eno.R +++ b/R/Eno.R @@ -28,7 +28,7 @@ #'set.seed(1) #'data <- array(rnorm(800), dim = c(dataset = 1, member = 2, sdate = 4, #' ftime = 4, lat = 10, lon = 10)) -#'na <- floor(runif(40, min=1, max=800)) +#'na <- floor(runif(40, min = 1, max = 800)) #'data[na] <- NA #'res <- Eno(data) #' @@ -52,7 +52,6 @@ Eno <- function(data, sdate_dim = 'sdate', na.action = na.pass, ncores = NULL) { if(any(is.null(names(dim(data))))| any(nchar(names(dim(data))) == 0)) { stop("Parameter 'data' must have dimension names.") } - ## sdate_dim if (!is.character(sdate_dim) | length(sdate_dim) > 1) { stop("Parameter 'sdate_dim' must be a character string.") @@ -60,24 +59,20 @@ Eno <- function(data, sdate_dim = 'sdate', na.action = na.pass, ncores = NULL) { if (!sdate_dim %in% names(dim(data))) { stop("Parameter 'sdate_dim' is not found in 'data' dimension.") } - ## na.action if (as.character(substitute(na.action)) != c("na.pass") && as.character(substitute(na.action)) != c("na.fail")) { stop("Parameter 'na.action' must be either 'na.pass' or 'na.fail'.") } - if(as.character(substitute(na.action))== c("na.fail") && any(is.na(data))) { stop(paste0("Calculation fails because NA is found in paratemter 'data', ", "which is not accepted when ", "parameter 'na.action' = 'na.fail'.")) } - ## ncores if (!is.null(ncores)) { - if (!is.numeric(ncores)) { - stop("Parameter 'ncores' must be a positive integer.") - } else if (ncores <= 0 | ncores %% 1 != 0) { + if (!is.numeric(ncores) | ncores %% 1 != 0 | ncores < 0 | + length(ncores) > 1) { stop("Parameter 'ncores' must be a positive integer.") } } diff --git a/tests/testthat/test-Eno.R b/tests/testthat/test-Eno.R new file mode 100644 index 00000000..e1a76565 --- /dev/null +++ b/tests/testthat/test-Eno.R @@ -0,0 +1,99 @@ +context("s2dverification::Eno tests") + +############################################## + set.seed(1) + dat1 <- array(rnorm(800), dim = c(dataset = 1, member = 2, sdate = 4, + ftime = 4, lat = 10, lon = 10)) + set.seed(1) + na <- floor(runif(40, min = 1, max = 800)) + dat1[na] <- NA + + + dat2 <- array(c(-5, -7, -10:10, 12, 11, 7, 16), + dim = c(date = 13, ftime = 2)) + +############################################## +test_that("1. Input checks", { + + expect_error( + Eno(c()), + "Parameter 'data' cannot be NULL." + ) + + expect_error( + Eno(data = 'a'), + "Parameter 'data' must be a numeric array." + ) + + expect_error( + Eno(data = array(1:10, dim = c(2,5))), + "Parameter 'data' must have dimension names." + ) + + expect_error( + Eno(data = 1:10, sdate_dim = 12), + "Parameter 'sdate_dim' must be a character string." + ) + + expect_error( + Eno(data = array(1:10, dim = c(a = 2, b = 5))), + "Parameter 'sdate_dim' is not found in 'data' dimension." + ) + + expect_error( + Eno(data = array(1:10, dim = c(a = 2, sdate = 5)), na.action = na.rm), + "Parameter 'na.action' must be either 'na.pass' or 'na.fail'." + ) + + expect_error( + Eno(data = c(NA,1:19), na.action = na.fail), + paste0("Calculation fails because NA is found in paratemter 'data', ", + "which is not accepted when ", + "parameter 'na.action' = 'na.fail'.") + ) + + expect_error( + Eno(data = array(1:10, dim = c(a = 2, sdate = 5)), ncores = 0.5), + "Parameter 'ncores' must be a positive integer." + ) + +}) + +############################################## +test_that("2. Output checks: dat1", { + res <- Eno(dat1) + + expect_equal( + dim(res), + dim(array(dim = c(stats = 1, dataset = 1, member = 2, ftime = 4, lat = 10, lon = 10))) + ) + + expect_equal( + length(res[which(is.na(res))]), + 1 + ) + + expect_equal( + length(res[which(res != 4)]), + 37 + ) + + expect_equal( + mean(res, na.rm = T), + 2.768103, + tolerance = 0.0001 + ) + +}) + +############################################## + +test_that("3. Output checks: dat2", { + + expect_equal( + Eno(dat2, sdate_dim = 'date'), + array(c(6.237689, 5.683186), dim = c(stats = 1, ftime = 2)), + tolerance = 0.0001 + ) + +}) -- GitLab From b0e8ca4835e987edfbcdd1da20375a43255dd1e6 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 29 Jan 2020 10:39:50 +0100 Subject: [PATCH 3/3] change sdate_dim to time_dim in Eno() --- R/Eno.R | 42 +++++++++++++++++++-------------------- man/Eno.Rd | 24 +++++++++++----------- tests/testthat/test-Eno.R | 19 ++++++------------ 3 files changed, 39 insertions(+), 46 deletions(-) diff --git a/R/Eno.R b/R/Eno.R index 8ef177c4..aac2197c 100644 --- a/R/Eno.R +++ b/R/Eno.R @@ -1,4 +1,4 @@ -#'Compute Effective Sample Size With Classical Method +#'Compute effective sample size with classical method #' #'Compute the number of effective samples along one dimension of an array. This #'effective number of independent observations can be used in statistical/inference @@ -6,23 +6,23 @@ #'The calculation is based on eno function from Caio Coelho from rclim.txt. #' #'@param data A numeric array with named dimensions. -#'@param sdate_dim A function indicating the dimension along -#' which to compute the effective sample size. Default value is 'sdate'. -#'@param na.action A charater string indicating the action for missing values, -#' can be na.pass (missing values are allowed) or na.fail (no missing values -#' are allowed). See details in stats::acf(). Default value is 'na.pass'. +#'@param time_dim A function indicating the dimension along which to compute +#' the effective sample size. The default value is 'sdate'. +#'@param na.action A function. It can be na.pass (missing values are allowed) +#' or na.fail (no missing values are allowed). See details in stats::acf(). +#' The default value is na.pass. #'@param ncores An integer indicating the number of cores to use for parallel -#' computation. Default value is NULL. +#' computation. The default value is NULL. #' #'@return An array with the first dimension 'stats', which is the number of -#' effective samples, and the following dimensions same as parameter 'data' -#' except the sdate_dim dimension. +#' effective samples, and the following dimensions are same as parameter 'data' +#' except the time_dim dimension, which is removed after the computation. #' #'@keywords datagen #'@author History:\cr #'0.1 - 2011-05 (V. Guemas, \email{virginie.guemas@ic3.cat}) - Original code\cr #'1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens@ic3.cat}) - Formatting to R CRAN -#'3.0 - 2019-12 (A. Ho, \email{an.ho@bsc.es}) - Adopt multiApply +#'3.0 - 2019-12 (A. Ho, \email{an.ho@bsc.es}) - Adopt multiApply feature #' #'@examples #'set.seed(1) @@ -35,7 +35,7 @@ #'@importFrom stats acf na.pass na.fail #'@import multiApply #'@export -Eno <- function(data, sdate_dim = 'sdate', na.action = na.pass, ncores = NULL) { +Eno <- function(data, time_dim = 'sdate', na.action = na.pass, ncores = NULL) { # Check inputs ## data @@ -47,27 +47,27 @@ Eno <- function(data, sdate_dim = 'sdate', na.action = na.pass, ncores = NULL) { } if (is.null(dim(data))) { #is vector dim(data) <- c(length(data)) - names(dim(data)) <- sdate_dim + names(dim(data)) <- time_dim } if(any(is.null(names(dim(data))))| any(nchar(names(dim(data))) == 0)) { stop("Parameter 'data' must have dimension names.") } - ## sdate_dim - if (!is.character(sdate_dim) | length(sdate_dim) > 1) { - stop("Parameter 'sdate_dim' must be a character string.") + ## time_dim + if (!is.character(time_dim) | length(time_dim) > 1) { + stop("Parameter 'time_dim' must be a character string.") } - if (!sdate_dim %in% names(dim(data))) { - stop("Parameter 'sdate_dim' is not found in 'data' dimension.") + if (!time_dim %in% names(dim(data))) { + stop("Parameter 'time_dim' is not found in 'data' dimension.") } ## na.action - if (as.character(substitute(na.action)) != c("na.pass") && + if (as.character(substitute(na.action)) != c("na.pass") & as.character(substitute(na.action)) != c("na.fail")) { - stop("Parameter 'na.action' must be either 'na.pass' or 'na.fail'.") + stop("Parameter 'na.action' must be a function either na.pass or na.fail.") } if(as.character(substitute(na.action))== c("na.fail") && any(is.na(data))) { stop(paste0("Calculation fails because NA is found in paratemter 'data', ", "which is not accepted when ", - "parameter 'na.action' = 'na.fail'.")) + "parameter 'na.action' = na.fail.")) } ## ncores if (!is.null(ncores)) { @@ -81,7 +81,7 @@ Eno <- function(data, sdate_dim = 'sdate', na.action = na.pass, ncores = NULL) { # Calculate Eno eno <- Apply(data = list(data), - target_dims = sdate_dim, + target_dims = time_dim, output_dims = 'stats', fun = .Eno, na.action = na.action, diff --git a/man/Eno.Rd b/man/Eno.Rd index da049bf5..02506785 100644 --- a/man/Eno.Rd +++ b/man/Eno.Rd @@ -2,27 +2,27 @@ % Please edit documentation in R/Eno.R \name{Eno} \alias{Eno} -\title{Compute Effective Sample Size With Classical Method} +\title{Compute effective sample size with classical method} \usage{ -Eno(data, sdate_dim = "sdate", na.action = na.pass, ncores = NULL) +Eno(data, time_dim = "sdate", na.action = na.pass, ncores = NULL) } \arguments{ \item{data}{A numeric array with named dimensions.} -\item{sdate_dim}{A function indicating the dimension along -which to compute the effective sample size. Default value is 'sdate'.} +\item{time_dim}{A function indicating the dimension along which to compute +the effective sample size. The default value is 'sdate'.} -\item{na.action}{A charater string indicating the action for missing values, -can be na.pass (missing values are allowed) or na.fail (no missing values -are allowed). See details in stats::acf(). Default value is 'na.pass'.} +\item{na.action}{A function. It can be na.pass (missing values are allowed) +or na.fail (no missing values are allowed). See details in stats::acf(). +The default value is na.pass.} \item{ncores}{An integer indicating the number of cores to use for parallel -computation. Default value is NULL.} +computation. The default value is NULL.} } \value{ An array with the first dimension 'stats', which is the number of - effective samples, and the following dimensions same as parameter 'data' - except the sdate_dim dimension. + effective samples, and the following dimensions are same as parameter 'data' + except the time_dim dimension, which is removed after the computation. } \description{ Compute the number of effective samples along one dimension of an array. This @@ -34,7 +34,7 @@ The calculation is based on eno function from Caio Coelho from rclim.txt. set.seed(1) data <- array(rnorm(800), dim = c(dataset = 1, member = 2, sdate = 4, ftime = 4, lat = 10, lon = 10)) -na <- floor(runif(40, min=1, max=800)) +na <- floor(runif(40, min = 1, max = 800)) data[na] <- NA res <- Eno(data) @@ -43,7 +43,7 @@ res <- Eno(data) History:\cr 0.1 - 2011-05 (V. Guemas, \email{virginie.guemas@ic3.cat}) - Original code\cr 1.0 - 2013-09 (N. Manubens, \email{nicolau.manubens@ic3.cat}) - Formatting to R CRAN -3.0 - 2019-12 (A. Ho, \email{an.ho@bsc.es}) - Adopt multiApply +3.0 - 2019-12 (A. Ho, \email{an.ho@bsc.es}) - Adopt multiApply feature } \keyword{datagen} diff --git a/tests/testthat/test-Eno.R b/tests/testthat/test-Eno.R index e1a76565..5c11dee9 100644 --- a/tests/testthat/test-Eno.R +++ b/tests/testthat/test-Eno.R @@ -19,39 +19,32 @@ test_that("1. Input checks", { Eno(c()), "Parameter 'data' cannot be NULL." ) - expect_error( Eno(data = 'a'), "Parameter 'data' must be a numeric array." ) - expect_error( Eno(data = array(1:10, dim = c(2,5))), "Parameter 'data' must have dimension names." ) - expect_error( - Eno(data = 1:10, sdate_dim = 12), - "Parameter 'sdate_dim' must be a character string." + Eno(data = 1:10, time_dim = 12), + "Parameter 'time_dim' must be a character string." ) - expect_error( Eno(data = array(1:10, dim = c(a = 2, b = 5))), - "Parameter 'sdate_dim' is not found in 'data' dimension." + "Parameter 'time_dim' is not found in 'data' dimension." ) - expect_error( Eno(data = array(1:10, dim = c(a = 2, sdate = 5)), na.action = na.rm), - "Parameter 'na.action' must be either 'na.pass' or 'na.fail'." + "Parameter 'na.action' must be a function either na.pass or na.fail." ) - expect_error( Eno(data = c(NA,1:19), na.action = na.fail), paste0("Calculation fails because NA is found in paratemter 'data', ", "which is not accepted when ", - "parameter 'na.action' = 'na.fail'.") + "parameter 'na.action' = na.fail.") ) - expect_error( Eno(data = array(1:10, dim = c(a = 2, sdate = 5)), ncores = 0.5), "Parameter 'ncores' must be a positive integer." @@ -91,7 +84,7 @@ test_that("2. Output checks: dat1", { test_that("3. Output checks: dat2", { expect_equal( - Eno(dat2, sdate_dim = 'date'), + Eno(dat2, time_dim = 'date'), array(c(6.237689, 5.683186), dim = c(stats = 1, ftime = 2)), tolerance = 0.0001 ) -- GitLab