diff --git a/tests/testthat/test_post_correct.R b/tests/testthat/test_post_correct.R index 2ed64b01..0ee8e247 100644 --- a/tests/testthat/test_post_correct.R +++ b/tests/testthat/test_post_correct.R @@ -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", @@ -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) - }) diff --git a/tests/testthat/test_predict.R b/tests/testthat/test_predict.R index 54011a27..daf10d68 100644 --- a/tests/testthat/test_predict.R +++ b/tests/testthat/test_predict.R @@ -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)