From dfd045e756aa2529f21526ad049472bcf1a36fa1 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 10 Aug 2023 12:46:05 +0100 Subject: [PATCH 1/2] Allow just one entry in the particle filter data Fixes #230 --- DESCRIPTION | 2 +- R/particle_filter_data.R | 7 ----- tests/testthat/test-particle-filter-data.R | 13 +++++---- tests/testthat/test-particle-filter.R | 32 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5c758864..404c10f6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: mcstate Title: Monte Carlo Methods for State Space Models -Version: 0.9.16 +Version: 0.9.17 Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), email = "rich.fitzjohn@gmail.com"), person("Marc", "Baguelin", role = "aut"), diff --git a/R/particle_filter_data.R b/R/particle_filter_data.R index 342455ba..d549631a 100644 --- a/R/particle_filter_data.R +++ b/R/particle_filter_data.R @@ -143,13 +143,6 @@ particle_filter_data <- function(data, time, rate, initial_time = NULL, } } - ## I am not entirely sure why we require two time windows and not - ## one - it's possible this is a hangover an earlier version where - ## the first line was the start time? - if (length(model_time_end) < 2) { - stop("Expected at least two time windows") - } - if (!is_continuous && any(model_time_end < 0)) { stop("All times must be non-negative") } diff --git a/tests/testthat/test-particle-filter-data.R b/tests/testthat/test-particle-filter-data.R index 69db7d19..65f14d6b 100644 --- a/tests/testthat/test-particle-filter-data.R +++ b/tests/testthat/test-particle-filter-data.R @@ -113,13 +113,14 @@ test_that("particle filter can offset initial data", { }) -test_that("require more than one observation", { +test_that("allow only one observation", { d <- data.frame(hour = 1:2, a = 2:3, b = 3:4) - expect_error( - particle_filter_data(d[1, ], "hour", 10, 0), - "Expected at least two time windows") - expect_silent( - particle_filter_data(d, "hour", 10, 0)) + df1 <- particle_filter_data(d[1, ], "hour", 10, 0) + df2 <- particle_filter_data(d[1:2, ], "hour", 10, 0) + + expect_equal(names(df1), names(df2)) + expect_equal(df1[, ], df2[1, ]) + expect_equal(nrow(df1), 1) }) diff --git a/tests/testthat/test-particle-filter.R b/tests/testthat/test-particle-filter.R index 2c7a82aa..7fd7dba3 100644 --- a/tests/testthat/test-particle-filter.R +++ b/tests/testthat/test-particle-filter.R @@ -1742,3 +1742,35 @@ test_that("filter works with irregular data", { expect_equal(ll2, ll1) }) + + +test_that("filter works with single data point", { + dat <- example_sir() + + set.seed(1) + d <- dat$data_raw[1:3, ] + d$incidence[1:2] <- NA + + df1 <- particle_filter_data(d, "day", 4, 0) + df2 <- particle_filter_data(d[-1, ], "day", 4, 0) + df3 <- particle_filter_data(d[-(1:2), ], "day", 4, 0) + + n_particles <- 42 + set.seed(1) + p1 <- particle_filter$new(df1, dat$model, n_particles, dat$compare, + index = dat$index, seed = 1L) + ll1 <- p1$run(list()) + + set.seed(1) + p2 <- particle_filter$new(df2, dat$model, n_particles, dat$compare, + index = dat$index, seed = 1L) + ll2 <- p2$run(list()) + + set.seed(1) + p3 <- particle_filter$new(df3, dat$model, n_particles, dat$compare, + index = dat$index, seed = 1L) + ll3 <- p3$run(list()) + + expect_equal(ll2, ll1) + expect_equal(ll3, ll1) +}) From 4f49092528e6b84f97bbbe0f5cb0b679d007d1af Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Thu, 10 Aug 2023 12:47:05 +0100 Subject: [PATCH 2/2] Disable lintr --- .lintr | 1 + 1 file changed, 1 insertion(+) diff --git a/.lintr b/.lintr index 5021b22c..06764092 100644 --- a/.lintr +++ b/.lintr @@ -1,4 +1,5 @@ linters: with_defaults( + indentation_linter = NULL, object_length_linter = NULL, object_usage_linter = NULL, todo_comment_linter = NULL,