Skip to content

Commit

Permalink
Merge pull request #231 from mrc-ide/gh-230
Browse files Browse the repository at this point in the history
Allow just one entry in the particle filter data
  • Loading branch information
MJomaba committed Aug 10, 2023
2 parents df7d24f + 4f49092 commit 54c66d3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
linters: with_defaults(
indentation_linter = NULL,
object_length_linter = NULL,
object_usage_linter = NULL,
todo_comment_linter = NULL,
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
7 changes: 0 additions & 7 deletions R/particle_filter_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-particle-filter-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})


Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-particle-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 54c66d3

Please sign in to comment.