tests/testthat/testCRPS.R

library(atmoswing)
context("CRPS")

test_that("CRPS is correct for 1962-01-01", {
  obs <- 25.8
  precip <- c(35.3, 31.5, 14.9, 30.0, 3.0, 4.2, 15.1, 1.9, 1.2, 2.1, 0.6, 16.1, 
              15.0, 4.4, 23.6, 0.0, 0.0, 1.3, 0.0, 10.8, 2.2, 18.2, 2.1, 50.7, 
              3.9, 20.5, 0.0, 0.5, 30.1, 9.6, 7.0, 3.1, 25.1, 2.7, 17.3, 3.5, 
              12.5, 0.8, 6.6, 1.4, 0.0, 3.6, 0.0, 11.3, 20.5, 0.0, 15.0, 3.5, 
              0.0, 0.0)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.20294, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-02", {
  obs <- 14.4
  precip <- c(1.9, 3.5, 0.0, 0.0, 3.0, 3.6, 2.1, 20.5, 1.3, 3.1, 0.6, 0.0, 1.2, 
              0.0, 0.0, 2.7, 12.9, 0.0, 0.0, 2.1, 0.2, 0.0, 1.7, 1.9, 6.1, 0.2, 
              3.9, 0.0, 9.6, 22.2, 0.0, 9.3, 0.0, 0.0, 0.8, 11.7, 0.0, 0.0, 0.9, 
              24.8, 0.0, 0.9, 0.0, 0.0, 20.5, 14.6, 2.1, 0.8, 2.1, 0.0)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.21813, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-03", {
  obs <- 0
  precip <- c(0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 1.6, 0.0, 0.0, 0.0, 0.1, 1.6, 0.0, 
              1.3, 0.0, 0.0, 0.0, 0.0, 0.0, 2.7, 0.2, 3.6, 2.2, 0.0, 0.0, 3.9, 
              0.0, 0.0, 0.4, 0.0, 0.0, 1.3, 0.0, 0.0, 0.3, 0.0, 1.3, 1.2, 1.2, 
              0.0, 0.1, 1.7, 0.0, 0.0, 0.0, 11.7, 0.3, 0.0, 0.0, 0.8)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.01638, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-04", {
  obs <- 0
  precip <- c(0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.7, 0.0, 0.0, 
              0.0, 0.0, 0.2, 1.3, 0.0, 0.0, 0.0, 0.0, 0.0, 5.8, 0.7, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
              0.0, 0.5, 0.4, 0.0, 0.0, 1.6, 0.0, 0.0, 0.2, 2.7, 0.0)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.00463, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-05", {
  obs <- 0
  precip <- c(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.0, 0.0, 1.6, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 
              0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.00046, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-06", {
  obs <- 0
  precip <- c(0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 7.6, 0.6, 0.0, 0.0, 1.1, 
              0.0, 0.3, 0.0, 2.8, 0.0, 0.0, 0.0, 0.0, 2.8, 0.0, 1.7, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.1, 0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 
              0.4, 2.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 4.8, 0.0)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.00771, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-07", {
  obs <- 0
  precip <- c(0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 
              0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 
              0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.4, 0.0, 0.0, 0.0, 0.0, 
              1.1, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.00176, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-09", {
  obs <- 2.9
  precip <- c(28.6, 9.2, 0.7, 18.7, 9.5, 12.2, 10.6, 29.8, 0.0, 0.1, 12.0, 22.4, 
              4.0, 1.4, 3.6, 1.9, 22.2, 3.2, 5.6, 0.0, 10.1, 6.2, 10.3, 7.2, 
              0.0, 0.0, 2.3, 19.5, 0.0, 0.2, 8.9, 5.2, 6.3, 14.8, 3.4, 3.0, 0.0, 
              9.3, 9.8, 15.3, 0.0, 17.0, 8.1, 0.9, 7.7, 10.2, 0.0, 11.1, 0.2, 
              13.2)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.06690, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-10", {
  obs <- 9.9
  precip <- c(5.4, 26.7, 7.8, 0.5, 82.0, 8.6, 18.5, 3.8, 13.5, 49.0, 7.8, 3.6, 
              67.3, 15.8, 9.2, 0.2, 8.5, 7.5, 16.0, 2.3, 16.7, 0.0, 26.9, 13.2, 
              17.7, 32.0, 3.8, 0.0, 18.4, 18.2, 6.1, 10.2, 33.1, 15.4, 40.5, 
              22.4, 14.1, 0.3, 21.3, 3.2, 14.0, 0.1, 0.7, 29.6, 25.3, 9.8, 0.1, 
              0.0, 9.7, 3.9)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.05612, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-11", {
  obs <- 26.8
  precip <- c(25.5, 14.5, 23.6, 12.0, 24.6, 24.0, 13.9, 32.8, 34.4, 33.1, 38.0, 
              25.7, 8.5, 20.8, 34.7, 29.8, 10.5, 16.0, 19.1, 22.5, 11.9, 5.2, 
              22.4, 15.0, 13.7, 14.5, 14.3, 13.4, 9.7, 54.4, 10.3, 6.9, 0.7, 
              35.5, 46.4, 20.5, 16.3, 5.3, 9.4, 19.2, 19.1, 27.6, 34.7, 22.6, 
              9.5, 30.7, 13.8, 7.8, 15.7, 12.7)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.06618, 
               tolerance = .00003)
})

test_that("CRPS is correct for 1962-01-12", {
  obs <- 49.9
  precip <- c(33.6, 10.4, 25.0, 20.5, 21.0, 25.7, 11.6, 12.4, 15.7, 9.7, 30.7, 
              12.0, 24.5, 3.0, 9.3, 14.6, 21.3, 35.9, 25.0, 32.5, 19.5, 23.0, 
              19.2, 47.4, 15.2, 46.4, 40.1, 13.2, 13.3, 39.0, 21.3, 7.9, 11.9, 
              7.4, 18.2, 18.4, 10.5, 15.7, 20.6, 19.5, 14.5, 9.0, 7.5, 7.6, 
              14.4, 9.4, 49.0, 15.4, 9.7, 25.3)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.25168, 
               tolerance = .00004)
})

test_that("CRPS is correct for 1983-11-26", {
  obs <- 82.0
  precip <- c(49.0, 18.5, 5.4, 14.1, 24.5, 42.0, 26.9, 0.9, 2.3, 17.7, 15.4, 
              23.6, 32.5, 14.7, 11.9, 9.7, 0.0, 13.2, 0.5, 15.7, 3.2, 21.3, 
              33.6, 5.6, 26.7, 21.3, 8.6, 11.6, 0.2, 0.0, 31.6, 7.8, 36.5, 25.7, 
              50.2, 9.4, 18.2, 0.1, 13.8, 14.4, 16.8, 0.0, 2.8, 15.8, 3.9, 18.7, 
              15.2, 17.0, 11.0, 7.5)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.53505, 
               tolerance = .00008)
})

test_that("CRPS is correct for 1983-11-27", {
  obs <- 38.2
  precip <- c(27.9, 13.7, 20.8, 17.2, 17.9, 19.1, 20.5, 11.9, 34.7, 13.9, 13.4, 
              31.3, 38.0, 24.6, 14.3, 11.3, 24.0, 12.4, 15.3, 13.2, 12.2, 15.7, 
              5.1, 33.1, 29.8, 10.0, 26.3, 32.0, 9.7, 6.6, 54.4, 22.7, 6.3, 
              14.1, 16.8, 13.5, 18.4, 23.6, 28.2, 16.8, 46.4, 16.9, 19.5, 9.5, 
              18.2, 29.1, 8.6, 8.5, 31.3, 17.3)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.15578, 
               tolerance = .00008)
})

test_that("CRPS is correct for 1983-11-29", {
  obs <- 0
  precip <- c(0.0, 17.8, 4.3, 22.0, 0.1, 0.0, 0.3, 0.0, 6.9, 7.5, 18.0, 2.5, 
              7.4, 5.6, 0.4, 0.7, 3.0, 6.3, 4.8, 7.1, 5.8, 0.8, 5.0, 0.0, 1.6, 
              0.4, 2.3, 3.9, 0.0, 0.0, 0.0, 0.0, 1.3, 3.9, 0.0, 0.0, 0.0, 1.2, 
              8.6, 0.0, 0.7, 0.0, 0.4, 0.0, 2.4, 1.8, 0.1, 0.0, 0.3, 0.1)
  x <- sqrt(precip/70.82)
  x0 <- sqrt(obs/70.82)
  
  expect_equal(atmoswing::crps(x, x0, a=0.375, b=0.25), 0.06754, 
               tolerance = .00003)
})
atmoswing/R-toolbox documentation built on Nov. 8, 2019, 3:49 p.m.