diff --git a/R/RMS.R b/R/RMS.R index c059c8f7ba36bb83d1915c171f6f3855b4c5d4b2..c2cb8bccd5405fae4bcb31aeb35119818c3cf187 100644 --- a/R/RMS.R +++ b/R/RMS.R @@ -11,9 +11,13 @@ #'The confidence interval is computed by the chi2 distribution.\cr #' #'@param exp A named numeric array of experimental data, with at least two -#' dimensions 'time_dim' and 'dat_dim'. +#' dimensions 'time_dim' and 'dat_dim'. It can also be a vector with the +#' same length as 'obs', then the vector will automatically be 'time_dim' and +#' 'dat_dim' will be 1. #'@param obs A named numeric array of observational data, same dimensions as -#' parameter 'exp' except along dat_dim. +#' parameter 'exp' except along dat_dim. It can also be a vector with the same +#' length as 'exp', then the vector will automatically be 'time_dim' and +#' 'dat_dim' will be 1. #'@param time_dim A character string indicating the name of dimension along #' which the correlations are computed. The default value is 'sdate'. #'@param dat_dim A character string indicating the name of member (nobs/nexp) @@ -73,9 +77,19 @@ RMS <- function(exp, obs, time_dim = 'sdate', dat_dim = 'dataset', if (!is.numeric(exp) | !is.numeric(obs)) { stop("Parameter 'exp' and 'obs' must be a numeric array.") } - if (is.null(dim(exp)) | is.null(dim(obs))) { - stop(paste0("Parameter 'exp' and 'obs' must be at least two dimensions ", - "containing time_dim and dat_dim.")) + if (is.null(dim(exp)) & is.null(dim(obs))) { #is vector + if (length(exp) == length(obs)) { + exp <- array(exp, dim = c(length(exp), 1)) + names(dim(exp)) <- c(time_dim, dat_dim) + obs <- array(obs, dim = c(length(obs), 1)) + names(dim(obs)) <- c(time_dim, dat_dim) + } else { + stop(paste0("Parameter 'exp' and 'obs' must be array with as least two ", + "dimensions time_dim and dat_dim, or vector of same length.")) + } + } else if (is.null(dim(exp)) | is.null(dim(obs))) { + stop(paste0("Parameter 'exp' and 'obs' must be array with as least two ", + "dimensions time_dim and dat_dim, or vector of same length.")) } if(any(is.null(names(dim(exp))))| any(nchar(names(dim(exp))) == 0) | any(is.null(names(dim(obs))))| any(nchar(names(dim(obs))) == 0)) { diff --git a/man/RMS.Rd b/man/RMS.Rd index 3ac1359ea069fd63b1765be06c91d31bebeb5f5e..7bd33b3ae69f711813806ff2dd266147791e8c7d 100644 --- a/man/RMS.Rd +++ b/man/RMS.Rd @@ -9,10 +9,14 @@ RMS(exp, obs, time_dim = "sdate", dat_dim = "dataset", comp_dim = NULL, } \arguments{ \item{exp}{A named numeric array of experimental data, with at least two -dimensions 'time_dim' and 'dat_dim'.} +dimensions 'time_dim' and 'dat_dim'. It can also be a vector with the +same length as 'obs', then the vector will automatically be 'time_dim' and +'dat_dim' will be 1.} \item{obs}{A named numeric array of observational data, same dimensions as -parameter 'exp' except along dat_dim.} +parameter 'exp' except along dat_dim. It can also be a vector with the same +length as 'exp', then the vector will automatically be 'time_dim' and +'dat_dim' will be 1.} \item{time_dim}{A character string indicating the name of dimension along which the correlations are computed. The default value is 'sdate'.} diff --git a/tests/testthat/test-RMS.R b/tests/testthat/test-RMS.R index 7b3e7b973cf09cf8ad5840945a434af75ffb71c7..35ea4e9da46c735dbec28ba59f9412f60cfffb08 100644 --- a/tests/testthat/test-RMS.R +++ b/tests/testthat/test-RMS.R @@ -11,6 +11,13 @@ context("s2dv::RMS tests") na <- floor(runif(10, min = 1, max = 80)) obs1[na] <- NA + # dat 2: vector + set.seed(5) + exp2 <- rnorm(10) + set.seed(6) + obs2 <- rnorm(10) + + ############################################## test_that("1. Input checks", { @@ -24,8 +31,7 @@ test_that("1. Input checks", { ) expect_error( RMS(c(1:10), c(2:4)), - paste0("Parameter 'exp' and 'obs' must be at least two dimensions ", - "containing time_dim and dat_dim.") + "Parameter 'exp' and 'obs' must be array with as least two dimensions time_dim and dat_dim, or vector of same length." ) expect_error( RMS(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))), @@ -147,4 +153,18 @@ test_that("2. Output checks: dat1", { }) ############################################## +test_that("3. Output checks: dat2", { + + expect_equal( + dim(RMS(exp2, obs2)$rms), + c(nexp = 1, nobs = 1) + ) + expect_equal( + as.vector(RMS(exp2, obs2)$rms), + 1.429815, + tolerance = 0.00001 + ) +}) + +##############################################