diff --git a/DESCRIPTION b/DESCRIPTION index c2852fd..495b913 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,8 @@ Imports: Suggests: knitr, rmarkdown, - testthat (>= 3.0) + testthat (>= 3.0), + RobinCar VignetteBuilder: knitr Config/testthat/edition: 3 diff --git a/tests/testthat/test-validate-robin_glm.R b/tests/testthat/test-validate-robin_glm.R new file mode 100644 index 0000000..9d0d7a0 --- /dev/null +++ b/tests/testthat/test-validate-robin_glm.R @@ -0,0 +1,55 @@ +library(RobinCar) + +# Compare RobinCar2 to RobinCar results +# ------------------------------------- + +test_that("simple randomization, difference contrast", { + # Function to compare RobinCar to RobinCar2 outputs + compare <- function(r1, r2) { + # Estimates and variance from RobinCar + estimates1 <- unname(r1$contrast$result$estimate) + variances1 <- unname(r1$contrast$result$se**2) + + # Estimates and variance for the first two + # contrast vector elements from RobinCar2 + estimates2 <- attributes(r2)$effects[-3] + variances2 <- attributes(r2)$variance[-3] + + testthat::expect_equal(estimates1, estimates2) + } + + for (family in list(gaussian, binomial)) { + for (model in c( + "~ treatment", + "~ treatment * covar", + "~ treatment * (covar + s1)" + )) { + # Set formula based on parameters + yname <- ifelse(identical(family, binomial), "y_b", "y") + form <- as.formula(paste0(yname, model)) + + # Use RobinCar2 + robincar2 <- robin_glm( + form, + data = dummy_data, + treatment = "treatment", + vcov = vcovANHECOVA, + family = family + ) + + # Use RobinCar + robincar1 <- robincar_glm( + df = dummy_data, + treat_col = "treatment", + response_col = yname, + formula = form, + car_scheme = "simple", + g_family = family, + contrast_h = "diff" + ) + + # Compare the results + compare(robincar1, robincar2) + } + } +})