Skip to content

Commit

Permalink
switch nlg models in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helske committed Sep 19, 2021
1 parent 04be11e commit a85504e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
44 changes: 30 additions & 14 deletions tests/testthat/test_post_correct.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,39 @@ test_that("Test post correction for AR1 model", {
test_that("Test post correction for non-linear model", {
skip_on_cran()
set.seed(1)
n <- 10
x <- y <- numeric(n)
y[1] <- rnorm(1, exp(x[1]), 0.1)
for(i in 1:(n-1)) {
x[i+1] <- rnorm(1, sin(x[i]), 0.1)
y[i+1] <- rnorm(1, exp(x[i+1]), 0.1)
}
y[2:3] <- NA
pntrs <- cpp_example_model("nlg_sin_exp")

expect_error(model <- ssm_nlg(y = y, a1 = pntrs$a1, P1 = pntrs$P1,
p1 <- 50 # population size at t = 1
K <- 500 # carrying capacity
H <- 1 # standard deviation of obs noise
R_1 <- 0.05 # standard deviation of the noise on logit-growth
R_2 <- 1 # standard deviation of the noise in population level
#sample time
dT <- .1

#observation times
t <- seq(0.1, 10, dT)
n <- length(t)
r <- plogis(cumsum(c(-1.5, rnorm(n - 1, sd = R_1))))
p <- numeric(n)
p[1] <- p1
for(i in 2:n)
p[i] <- rnorm(1, K * p[i-1] * exp(r[i-1] * dT) / (K + p[i-1] * (exp(r[i-1] * dT) - 1)), R_2)
# observations
y <- p + rnorm(n, 0, H)
y[2:15] <- NA
pntrs <- cpp_example_model("nlg_growth")

initial_theta <- c(log_H = 0, log_R1 = log(0.05), log_R2 = 0)

# dT, K, a1 and the prior variances of first and second state (logit r and and p)
known_params <- c(dT = dT, K = K, a11 = -1, a12 = 50, P11 = 1, P12 = 100)

expect_error(model <- ssm_nlg(y = y, a1=pntrs$a1, P1 = pntrs$P1,
Z = pntrs$Z_fn, H = pntrs$H_fn, T = pntrs$T_fn, R = pntrs$R_fn,
Z_gn = pntrs$Z_gn, T_gn = pntrs$T_gn,
theta = c(log_H = log(0.1), log_R = log(0.1)),
log_prior_pdf = pntrs$log_prior_pdf,
n_states = 1, n_etas = 1, state_names = "state"), NA)
theta = initial_theta, log_prior_pdf = pntrs$log_prior_pdf,
known_params = known_params, known_tv_params = matrix(1),
n_states = 2, n_etas = 2, state_names = c("logit_r", "p")), NA)


expect_error(out_approx <- run_mcmc(model, mcmc_type = "approx",
Expand All @@ -80,5 +97,4 @@ test_that("Test post correction for non-linear model", {
expect_lt(sum(out_is2$Vt), Inf)
expect_lt(max(out_is2$weights), Inf)
expect_gt(max(out_is2$weights), 0)

})
6 changes: 3 additions & 3 deletions tests/testthat/test_predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ test_that("Predictions for nlg_ssm work", {
x <- y <- numeric(n)
y[1] <- rnorm(1, exp(x[1]), 0.1)
for(i in 1:(n-1)) {
x[i+1] <- rnorm(1, sin(x[i]), 0.1)
x[i+1] <- rnorm(1, 0.9 * x[i], 0.1)
y[i+1] <- rnorm(1, exp(x[i+1]), 0.1)
}

pntrs <- cpp_example_model("nlg_sin_exp")
pntrs <- cpp_example_model("nlg_ar_exp")

expect_error(model <- ssm_nlg(y = y, a1 = pntrs$a1, P1 = pntrs$P1,
Z = pntrs$Z_fn, H = pntrs$H_fn, T = pntrs$T_fn, R = pntrs$R_fn,
Z_gn = pntrs$Z_gn, T_gn = pntrs$T_gn,
theta = c(log_H = log(0.1), log_R = log(0.1)),
theta = c(mu = 0, rho = 0.9, log_R = log(0.1), log_H = log(0.1)),
log_prior_pdf = pntrs$log_prior_pdf,
n_states = 1, n_etas = 1, state_names = "state"), NA)

Expand Down

0 comments on commit a85504e

Please sign in to comment.