-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for
BayesSurvive()
(#11)
Since this case call all remaining functions to be translated, it should suffice for testing the rest of #11. Tests will still need to be readapted once R code is removed.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Load the example dataset | ||
dataset <- list( | ||
"X" = simData[[1]]$X, | ||
"t" = simData[[1]]$time, | ||
"di" = simData[[1]]$status | ||
) | ||
|
||
# Run a Bayesian Cox model | ||
|
||
## Initial value: null model without covariates | ||
initial <- list("gamma.ini" = rep(0, ncol(dataset$X))) | ||
|
||
# Prior parameters | ||
hyperparPooled = list( | ||
"c0" = 2, # prior of baseline hazard | ||
"tau" = 0.0375, # sd (spike) for coefficient prior | ||
"cb" = 20, # sd (slab) for coefficient prior | ||
"pi.ga" = 0.02, # prior variable selection probability for standard Cox models | ||
"a" = -4, # hyperparameter in MRF prior | ||
"b" = 0.1, # hyperparameter in MRF prior | ||
"G" = simData$G # hyperparameter in MRF prior | ||
) | ||
|
||
# Run a 'Pooled' Bayesian Cox model with graphical learning | ||
|
||
set.seed(715074) | ||
BayesSurvive_wrap <- function(use_cpp = FALSE) { | ||
# suppressWarnings( | ||
BayesSurvive( | ||
survObj = dataset, model.type = "Pooled", MRF.G = TRUE, verbose = TRUE, | ||
hyperpar = hyperparPooled, initial = initial, nIter = 100, burnin = 100, | ||
cpp = use_cpp | ||
) | ||
# ) | ||
} | ||
fit_R <- BayesSurvive_wrap(use_cpp = FALSE) | ||
fit_C <- BayesSurvive_wrap(use_cpp = TRUE) | ||
|
||
test_that("R and C++ objects are similar", { | ||
expect_equal(fit_R$call, fit_C$call) | ||
expect_equal(fit_R$input, fit_C$input) | ||
for (obj in names(fit_R$output)[2]) { | ||
expect_equal(fit_R$output[[obj]], fit_C$output[[obj]], tolerance = 1) | ||
} | ||
}) |