test_that("Log likelihood components work correctly", { # Create a simple model with known parameters modelParameters <- LinearAbxModel(nstates = 2) # Override with simpler values that shouldn't cause -inf modelParameters$Insitu$probs <- c(0.9, 0.0, 0.1) modelParameters$Insitu$priors <- c(0.9, 0.0, 0.1) modelParameters$Insitu$doit <- c(TRUE, FALSE, TRUE) # Create a minimal test with just a few events minimal_data <- data.frame( facility = c(1, 1, 1), unit = c(1, 1, 1), time = c(0.0, 0.1, 1.0), patient = c(1, 1, 1), type = c(0, 1, 3) # admission, pos test, discharge ) # Try to create a model model_result <- tryCatch({ newModelExport(modelParameters, verbose = FALSE) }, error = function(e) { list(error = e$message) }) if (!is.null(model_result$error)) { skip(paste("Model creation failed:", model_result$error)) } expect_true(!is.null(model_result)) }) test_that("InsituParams probabilities are valid", { # Test that insitu parameters don't produce -inf modelParameters <- LinearAbxModel(nstates = 2) # Check R-side values expect_true(all(modelParameters$Insitu$probs >= 0)) expect_true(all(modelParameters$Insitu$probs <= 1)) expect_equal(sum(modelParameters$Insitu$probs), 1.0, tolerance = 1e-10) # For 2-state model, latent should be 0 expect_equal(modelParameters$Insitu$probs[2], 0.0) }) test_that("Acquisition parameters don't produce NaN", { modelParameters <- LinearAbxModel(nstates = 2) acq <- modelParameters$InCol$acquisition # Check that init values are in valid ranges expect_true(acq$base$init > 0, info = "base should be positive") # Mass and freq are probabilities (logit-transformed) expect_true(acq$mass$init >= 0 && acq$mass$init <= 1, info = sprintf("mass init = %f should be in [0,1]", acq$mass$init)) expect_true(acq$freq$init >= 0 && acq$freq$init <= 1, info = sprintf("freq init = %f should be in [0,1]", acq$freq$init)) # Check priors are also valid expect_true(acq$mass$prior >= 0 && acq$mass$prior <= 1, info = sprintf("mass prior = %f should be in [0,1]", acq$mass$prior)) expect_true(acq$freq$prior >= 0 && acq$freq$prior <= 1, info = sprintf("freq prior = %f should be in [0,1]", acq$freq$prior)) }) test_that("Test with original model defaults", { # Try to replicate the original C++ defaults modelParameters <- LinearAbxModel(nstates = 2) # Set to values from original simulated.Model (all commented, so defaults used) # Original defaults from C++ code for 2-state: modelParameters$Insitu$probs <- c(0.9, 0.0, 0.1) modelParameters$Insitu$priors <- c(1, 1, 1) modelParameters$Insitu$doit <- c(TRUE, FALSE, TRUE) # Updated to match original C++ model file values: # - mass = 1.0 (to avoid logit issues at 0.5) # - freq = 1.0 (to avoid logit issues at 0.5) # - base = 0.001 (must be > 0) expect_equal(modelParameters$InCol$acquisition$mass$init, 1.0) expect_equal(modelParameters$InCol$acquisition$freq$init, 1.0) expect_equal(modelParameters$InCol$acquisition$base$init, 0.001) }) test_that("Simple MCMC with minimal iterations", { skip_if_not(interactive(), "Skipping interactive MCMC test") modelParameters <- LinearAbxModel(nstates = 2) # Use original-style parameters modelParameters$Insitu$probs <- c(0.9, 0.0, 0.1) # Try with just 1 burn-in and 1 simulation results <- tryCatch({ runMCMC( data = simulated.data, nburn = 0, # No burn-in nsims = 1, # Just 1 iteration outputparam = TRUE, outputfinal = FALSE, modelParameters = modelParameters, verbose = FALSE ) }, error = function(e) { list(error = e$message, trace = capture.output(traceback())) }) # Quiet: don't print errors; assertion below includes details on failure expect_true(is.null(results$error), info = paste("MCMC failed:", results$error, if (!is.null(results$trace)) paste0("\nTraceback:\n", paste(results$trace, collapse = "\n")) else "")) })