diff --git a/R/RMSSS.R b/R/RMSSS.R index 4f115d3cdb68433e53dcc3a903bbea6e1c1de02d..5967bd00ebcead4a20a80382b445187484058a28 100644 --- a/R/RMSSS.R +++ b/R/RMSSS.R @@ -13,11 +13,15 @@ #'The p-value is optionally provided by an one-sided Fisher test.\cr #' #'@param exp A named numeric array of experimental data which contains at least -#' two dimensions for memb_dim and time_dim. +#' two dimensions for memb_dim and time_dim. It can also be a vector with the +#' same length as 'obs', then the vector will automatically be 'time_dim' and +#' 'memb_dim' will be 1. #'@param obs A named numeric array of observational data which contains at least #' two dimensions for memb_dim and time_dim. The dimensions should be the same #' as paramter 'exp' except the length of 'memb_dim' dimension. The order of -#' dimension can be different. +#' dimension can be different. It can also be a vector with the same length as +#' 'exp', then the vector will automatically be 'time_dim' and 'memb_dim' will +#' be 1. #'@param memb_dim A character string indicating the name of member (nobs/nexp) #' dimension. The default value is 'member'. #'@param time_dim A character string indicating the name of dimension along @@ -62,9 +66,19 @@ RMSSS <- function(exp, obs, time_dim = 'sdate', memb_dim = 'member', 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 memb_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, memb_dim) + obs <- array(obs, dim = c(length(obs), 1)) + names(dim(obs)) <- c(time_dim, memb_dim) + } else { + stop(paste0("Parameter 'exp' and 'obs' must be array with as least two ", + "dimensions time_dim and memb_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 memb_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/RMSSS.Rd b/man/RMSSS.Rd index 3b9c1be48f6aee17c4ce51c11a01f8dca28f3559..0939a94730c08ec0e297064dcdccdd9f61d85297 100644 --- a/man/RMSSS.Rd +++ b/man/RMSSS.Rd @@ -9,12 +9,16 @@ RMSSS(exp, obs, time_dim = "sdate", memb_dim = "member", pval = TRUE, } \arguments{ \item{exp}{A named numeric array of experimental data which contains at least -two dimensions for memb_dim and time_dim.} +two dimensions for memb_dim and time_dim. It can also be a vector with the +same length as 'obs', then the vector will automatically be 'time_dim' and +'memb_dim' will be 1.} \item{obs}{A named numeric array of observational data which contains at least two dimensions for memb_dim and time_dim. The dimensions should be the same as paramter 'exp' except the length of 'memb_dim' dimension. The order of -dimension can be different.} +dimension can be different. It can also be a vector with the same length as +'exp', then the vector will automatically be 'time_dim' and 'memb_dim' will +be 1.} \item{time_dim}{A character string indicating the name of dimension along which the RMSSS are computed. The default value is 'sdate'.} diff --git a/tests/testthat/test-RMSSS.R b/tests/testthat/test-RMSSS.R index 9242d6dd8b1a94332f258c3f2df5f9352a56e565..7d5cc8d4223b2b0f1007e16d62a35ce561e91602 100644 --- a/tests/testthat/test-RMSSS.R +++ b/tests/testthat/test-RMSSS.R @@ -19,6 +19,12 @@ context("s2dv::RMSSS tests") set.seed(4) obs2 <- array(rnorm(60), dim = c(dat = 1, sdate = 10, member = 1, lat = 2, lon = 3)) + # case 3: vector + set.seed(5) + exp3 <- rnorm(10) + set.seed(6) + obs3 <- rnorm(10) + ############################################## test_that("1. Input checks", { @@ -33,11 +39,11 @@ test_that("1. Input checks", { ) expect_error( RMSSS(c(1:10), c(2:4)), - paste0("Parameter 'exp' and 'obs' must be at least two dimensions ", - "containing time_dim and memb_dim.") + paste0("Parameter 'exp' and 'obs' must be array with as least two dimensions ", + "time_dim and memb_dim, or vector of same length.") ) expect_error( - RMSSS(array(1:10, dim = c(2, 5)), array(1:4, dim = c(2, 2))), + RMSSS(array(1:10, dim = c(2, 5)), array(1:10, dim = c(2, 5))), "Parameter 'exp' and 'obs' must have dimension names." ) expect_error( @@ -81,7 +87,7 @@ test_that("1. Input checks", { }) ############################################## -test_that("1. Output checks: case 1", { +test_that("2. Output checks: case 1", { res1_1 <- RMSSS(exp1, obs1, time_dim = 'time', memb_dim = 'memb') expect_equal( @@ -118,7 +124,7 @@ test_that("1. Output checks: case 1", { ############################################## -test_that("2. Output checks: case 2", { +test_that("3. Output checks: case 2", { expect_equal( dim(RMSSS(exp2, obs2)$rmsss), @@ -139,4 +145,17 @@ test_that("2. Output checks: case 2", { ############################################## +test_that("4. Output checks: case 3", { + + expect_equal( + dim(RMSSS(exp3, obs3)$rmsss), + c(nexp = 1, nobs = 1) + ) + expect_equal( + as.vector(RMSSS(exp3, obs3)$rmsss), + -0.6289915, + tolerance = 0.00001 + ) + +})