Skip to content

Commit

Permalink
Merge pull request #7 from certara/data_validation
Browse files Browse the repository at this point in the history
Data validation function
  • Loading branch information
certara-jcraig authored Dec 11, 2023
2 parents 0f42e86 + d721a7e commit 462fbfc
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
33 changes: 32 additions & 1 deletion R/cov_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#'
MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed = 123) {

# Check that covariates supplied by user exist in the data
errors <- data_validation(tab, list_pop_param, cov_continuous, cov_factors)
if (length(errors) > 0) {
stop(paste0(errors, sep = "\n"), call. = FALSE)
}

stopifnot(is.numeric(seed))
set.seed(seed)

Expand Down Expand Up @@ -247,7 +253,25 @@ MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed =
)
}


data_validation <- function(tab, list_pop_param, cov_continuous, cov_factors) {
errors <- c()

for (i in 1:3) {
vectors <- list(list_pop_param, cov_continuous, cov_factors)
vector_names <- c("list_pop_param", "cov_continuous", "cov_factors")

missing_values <- setdiff(vectors[[i]], colnames(tab))

if (length(missing_values) > 0) {
error_message <-
paste( "The following values from", vector_names[[i]], "are missing in the dataset:", toString(missing_values))

errors <- c(errors, error_message)
}
}
return(errors)

}



Expand Down Expand Up @@ -282,6 +306,13 @@ MLCovSearch <- function(tab, list_pop_param, cov_continuous, cov_factors, seed =
#' }
#' @export
generate_residualsplots <- function(tab, list_pop_param, cov_continuous, cov_factors, result_ML, result_5folds, i, seed = 123 ) {

# Check that covariates supplied by user exist in the data
errors <- data_validation(tab, list_pop_param, cov_continuous, cov_factors)
if (length(errors) > 0) {
stop(paste0(errors, sep = "\n"), call. = FALSE)
}

stopifnot(is.numeric(seed))
set.seed(seed)

Expand Down
6 changes: 6 additions & 0 deletions R/generate_residualsplots2.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ generate_residualsplots2 <- function(data, result, i, seed = NULL) {
result_ML <- result$result_ML
result_5folds <- result$result_5folds

# Check that covariates supplied by user exist in the data
errors <- data_validation(data, list_pop_param, cov_continuous, cov_factors)
if (length(errors) > 0) {
stop(paste0(errors, sep = "\n"), call. = FALSE)
}

# Selection of columns required
data <- data %>%
dplyr::select(ID, dplyr::all_of(list_pop_param), dplyr::all_of(cov_continuous), dplyr::all_of(cov_factors))
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-generate_residualsplots.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ testthat::test_that("test-generate_residualsplots returns the same plots each ti
vdiffr::expect_doppelganger("SEX Residuals", plot_V1$SEX)
})

testthat::test_that("Error messages will be generated when values supplied to the function are absent in the data.frame", {

testthat::expect_error(generate_residualsplots(tab = data,
list_pop_param = c("clearance"),
cov_continuous = c("weight"),
cov_factors = c("dIaB"),
result_ML = result$result_ML, #selected covariates for parameter of interest after the voting
result_5folds = result$result_5folds, #selected covariates for parameter of interest for each folds
i=c('CL')))
})

6 changes: 6 additions & 0 deletions tests/testthat/test-generateresidualsplots2.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ testthat::test_that("generate_residualsplots does not return plots for Cl", {
testthat::test_that("generate_residualsplots does not return plots for V1", {
testthat::expect_null(plot_V1)
})

testthat::test_that("Error messages will be generated when values supplied to the function are absent in the data.frame", {

#Since residualsplots2 function pulls these values from the results object, it should always run correctly.
testthat::expect_no_error(generate_residualsplots2(data, result, i = c('V1')))
})
8 changes: 8 additions & 0 deletions tests/testthat/test-mlcovsearch.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ result <- suppressWarnings(MLCovSearch(data, #NONMEM output
"HGB","HCT","PLT"),
cov_factors = c("SEX","RACE","DIAB","ALQ","WACT","SMQ")))

testthat::test_that("Error messages will be generated when values supplied to the MLCovSearch are absent in the data.frame", {

testthat::expect_error(MLCovSearch(data, #NONMEM output
list_pop_param = c("clearance"),
cov_continuous = c("weight"),
cov_factors = c("dIaB")))
})

testthat::test_that("MLCovSearch result_ML is a data.frame with only one covariate selection", {

testthat::expect_true(is.data.frame(result$result_ML))
Expand Down

0 comments on commit 462fbfc

Please sign in to comment.