From e51dbddefb0b64106b10737f532250019b621ead Mon Sep 17 00:00:00 2001
From: samuelsmock <smock.samuel@gmail.com>
Date: Thu, 8 Feb 2024 08:14:11 -0600
Subject: [PATCH] Reasonable results test now checks every b coeff.

---
 tests/manual-tests/SE_Drive.R |  11 +++++++++
 tests/testthat/Rplots.pdf     | Bin 234011 -> 234011 bytes
 tests/testthat/test-sim_all.R |  42 ++++++++++++++++++++++++++++++++--
 3 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/tests/manual-tests/SE_Drive.R b/tests/manual-tests/SE_Drive.R
index e5d644d..322d933 100644
--- a/tests/manual-tests/SE_Drive.R
+++ b/tests/manual-tests/SE_Drive.R
@@ -52,3 +52,14 @@ destype="ngene"
 
 sedrive <- sim_all(nosim = nosim, resps=resps, destype = destype,
                    designpath = designpath, u=ul, bcoeff = bcoeff)
+
+## nested results are hard coded, if the design changes this must aswell
+coeffNestedOutput <- sedrive$olddesign$coefs
+
+variable_names <- names(coeffNestedOutput)
+
+# Filter variable names that start with "est_"
+est_variables <- grep("^est_", variable_names, value = TRUE)
+
+print('hello')
+print(class(coeffNestedOutput))
diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf
index 8809a1169cd18196e4da209f19e222171cef5a32..e9033a3ec9ebb6184785100f40f6b08f70370f59 100644
GIT binary patch
delta 40
qcmbQegm3l|z6loW76uju<_6{y?L}eC#^}~)#@1-2t<lT}CISHU=nORg

delta 40
qcmbQegm3l|z6loW=7we_W=0kh?L}eC#^}~)#@1-2t<lT}CISHViVQsf

diff --git a/tests/testthat/test-sim_all.R b/tests/testthat/test-sim_all.R
index e4e4a9d..381e35d 100644
--- a/tests/testthat/test-sim_all.R
+++ b/tests/testthat/test-sim_all.R
@@ -152,7 +152,45 @@ test_that("Simulation results are reasonable", {
 
   result1 <- sim_all(nosim = nosim, resps = resps, destype = destype, designpath = designpath, u = ul, bcoeff = bcoeff)
 
-  expect_gt(result1$est_bsq, -1)
-  expect_lt(result1$est_bsq, 1)
+  ## The function below is intended to find a dataframe called $coef in the output
+  ## regardless of the output data structure which can vary
+  find_dataframe <- function(list_object, dataframe_name) {
+    # Check if the current object is a list
+    if (is.list(list_object)) {
+      # Check if the dataframe_name exists in the names of the current list object
+      if (dataframe_name %in% names(list_object)) {
+        # Check if the object corresponding to dataframe_name is a data frame
+        if (is.data.frame(list_object[[dataframe_name]])) {
+          return(list_object[[dataframe_name]])  # Return the data frame if found
+        }
+      }
+
+      # Recursively search through each element of the current list object
+      for (element in list_object) {
+        result <- find_dataframe(element, dataframe_name)
+        if (!is.null(result)) {
+          return(result)  # Return the data frame if found in any nested list
+        }
+      }
+    }
+
+    return(NULL)  # Return NULL if dataframe_name not found
+  }
+
+  # Now access the coef data frame to compare to the input
+  coeffNestedOutput <- find_dataframe(result1, "coefs")
+
+  for (variable in names(bcoeff)){
+    ### Compare singular input value (hypothesis) with the average value of all iterations. ###
+    ### This could be made more rigorous by testing each iteration or by changing the       ###
+    ### tolerance around the input value considered valid.
+    input_value <- bcoeff[[variable]]
+    mean_output_value <- mean(coeffNestedOutput[[paste0("est_", variable)]]) ## access the mean value of each iteration
+
+    ##change this depending on how rigorous you want to be
+    expect_gt(mean_output_value, input_value - 1)
+    expect_lt(mean_output_value, input_value + 1)
+  }
+
 
 })
-- 
GitLab