From 5a0367572fbbf8dee3bbd1871ca23bf580b519d0 Mon Sep 17 00:00:00 2001 From: Eva Rifa Date: Thu, 12 Jan 2023 15:06:56 +0100 Subject: [PATCH] Correct line in the atomic function that was hard-coded for time_dim and add test --- R/RMS.R | 8 ++++---- tests/testthat/test-RMS.R | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/R/RMS.R b/R/RMS.R index 2c77d0a..645e34b 100644 --- a/R/RMS.R +++ b/R/RMS.R @@ -217,10 +217,9 @@ RMS <- function(exp, obs, time_dim = 'sdate', dat_dim = 'dataset', nobs <- as.numeric(dim(obs)[2]) } - nsdate <- as.numeric(dim(exp)[1]) - - dif <- array(dim = c(sdate = nsdate, nexp = nexp, nobs = nobs)) + dif <- array(dim = c(dim(exp)[1], nexp = nexp, nobs = nobs)) chi <- array(dim = c(nexp = nexp, nobs = nobs)) + if (conf) { conflow <- (1 - conf.lev) / 2 confhigh <- 1 - conflow @@ -232,6 +231,7 @@ RMS <- function(exp, obs, time_dim = 'sdate', dat_dim = 'dataset', for (i in 1:nobs) { dif[, , i] <- sapply(1:nexp, function(x) {exp[, x] - obs[, i]}) } + rms <- apply(dif^2, c(2, 3), mean, na.rm = TRUE)^0.5 #array(dim = c(_exp, nobs)) if (conf) { @@ -271,4 +271,4 @@ RMS <- function(exp, obs, time_dim = 'sdate', dat_dim = 'dataset', return(res) -} +} \ No newline at end of file diff --git a/tests/testthat/test-RMS.R b/tests/testthat/test-RMS.R index aac0672..bf059ef 100644 --- a/tests/testthat/test-RMS.R +++ b/tests/testthat/test-RMS.R @@ -24,6 +24,12 @@ context("s2dv::RMS tests") set.seed(2) obs3 <- array(rnorm(80), dim = c(sdate = 5, ftime = 2, lon = 1, lat = 4)) + # dat4 + set.seed(1) + exp4 <- array(rnorm(120), dim = c(sdates = 5, ftimes = 2, lon = 1, lat = 1)) + + set.seed(2) + obs4 <- array(rnorm(80), dim = c(sdates = 5, ftimes = 2, lon = 1, lat = 1)) ############################################## test_that("1. Input checks", { @@ -209,6 +215,10 @@ test_that("4. Output checks: dat3", { dim(RMS(exp3, obs3, dat_dim = NULL, conf = FALSE)$rms), c(ftime = 2, lon = 1, lat = 4) ) + expect_equal( + dim(RMS(exp4, obs4, time_dim = 'sdates', dat_dim = NULL, conf = TRUE)$rms), + c(ftimes = 2, lon = 1, lat = 1) + ) }) ############################################## \ No newline at end of file -- GitLab