Skip to content

Commit b13d931

Browse files
author
Ridwan Olaniran
committed
2 parents fa6bf1c + 52b8377 commit b13d931

14 files changed

Lines changed: 186 additions & 59 deletions

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Imports:
4848
utils,
4949
glmnet,
5050
ranger,
51-
pec
51+
pec,
52+
cli
5253
Suggests:
5354
testthat (>= 3.0.0),
5455
covr,

NAMESPACE

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
S3method(plot,pmsims)
44
S3method(print,pmsims)
55
S3method(summary,pmsims)
6-
export(calculate_bisection)
7-
export(calculate_mlpwr_bs)
8-
export(generate_binary_data)
9-
export(objective_function)
10-
export(parse_inputs)
11-
export(predict_custom)
126
export(simulate_binary)
137
export(simulate_continuous)
14-
export(simulate_custom)
158
export(simulate_survival)
16-
export(survival_tuning)
179
importFrom(survival,Surv)
1810
importFrom(utils,setTxtProgressBar)
1911
importFrom(utils,txtProgressBar)

R/data_generators.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ generate_continuous_data <- function(
6161
#'
6262
#' @return A data frame with one outcome column and n_signal_parameters + noise_parameters predictor columns
6363
#' @keywords internal
64-
#' @export
64+
6565
generate_binary_data <- function(
6666
n,
6767
mu_lp,
@@ -145,6 +145,12 @@ update_arguments <- function(fn, opts) {
145145

146146
generate_predictors <- function(n, parameters, type, predictor_prop) {
147147
if (type == "binary") {
148+
if (is.null(predictor_prop)) {
149+
stop("predictor_prop must be provided when predictor type is binary")
150+
}
151+
if (predictor_prop < 0 || predictor_prop > 1) {
152+
stop("predictor_prop must be between 0 and 1")
153+
}
148154
X <- stats::rbinom(n * parameters, 1, predictor_prop)
149155
} else if (type == "continuous") {
150156
X <- stats::rnorm(n * parameters)

R/engines.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ calculate_mlpwr <- function(
539539
#' @return A list containing the simulation `results`, performance `summaries`,
540540
#' optional tracking `history`, and the `track_bisection` records.
541541
#' @keywords internal
542-
#' @export
542+
543543
calculate_bisection <- function(
544544
data_function = data_function,
545545
model_function = model_function,
@@ -680,7 +680,7 @@ calculate_bisection <- function(
680680
#'
681681
#' @return List containing the combined bisection and mlpwr results (`results`, `summaries`, `min_n`, `perf_n`, and `mlpwr_ds`).
682682
#' @keywords internal
683-
#' @export
683+
684684
calculate_mlpwr_bs <- function(
685685
test_n,
686686
n_reps_total,

R/input_validation.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ validate_metric_constraints <- function(metric,
3131
}
3232
}
3333

34+
35+
36+
3437
validate_outcome_prevalence <- function(outcome_prevalence) {
3538
if (is.null(outcome_prevalence)) {
3639
cli::cli_abort("`outcome_prevalence` must be specified.")
@@ -44,3 +47,53 @@ validate_outcome_prevalence <- function(outcome_prevalence) {
4447

4548
invisible(TRUE)
4649
}
50+
51+
#' check_pmsims_args - a custom version of the base R match.arg function with improved error message
52+
#'
53+
#'@inherit base::match.arg
54+
#'
55+
check_pmsims_args <- function(arg, choices, several.ok = FALSE) {
56+
if (missing(choices)) {
57+
formal.args <- formals(sys.function(sysP <- sys.parent()))
58+
choices <- eval(
59+
formal.args[[as.character(substitute(arg))]],
60+
envir = sys.frame(sysP)
61+
)
62+
}
63+
arg_name <- as.character(substitute(arg))
64+
65+
if (is.null(arg)) {
66+
return(choices[1L])
67+
} else if (!is.character(arg)) {
68+
stop(paste0(arg_name, " must be NULL or a character vector"))
69+
}
70+
if (!several.ok) {
71+
if (identical(arg, choices)) {
72+
return(arg[1L])
73+
}
74+
if (length(arg) > 1L) {
75+
stop(paste0(arg_name, " must be of length 1"))
76+
}
77+
} else if (length(arg) == 0L) {
78+
stop(paste0(arg_name, " must be of length >= 1"))
79+
}
80+
i <- pmatch(arg, choices, nomatch = 0L, duplicates.ok = TRUE)
81+
if (all(i == 0L)) {
82+
stop(
83+
sprintf(
84+
ngettext(
85+
length(chs <- unique(choices[nzchar(choices)])),
86+
sprintf("'%s' should be %%s", arg_name),
87+
sprintf("'%s' should be one of %%s", arg_name)
88+
),
89+
paste(dQuote(chs), collapse = ", ")
90+
),
91+
domain = NA
92+
)
93+
}
94+
i <- i[i > 0L]
95+
if (!several.ok && length(i) > 1) {
96+
stop("there is more than one match in 'check_pmsims_args'")
97+
}
98+
choices[i]
99+
}

R/metric_generators.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ default_metric_generator <- function(metric, data_function) {
6565
}
6666

6767
#' @keywords internal
68-
#' @export
68+
6969
predict_custom <- function(x, y, fit, model, type = "response") {
7070
if (model == "glm") {
7171
stats::predict(fit, newdata = x, type = type)

R/pmsims_objective_function.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#'
1717
#' @return A single numeric: the objective value at \eqn{n}.
1818
#' @keywords internal
19-
#' @export
2019
#'
2120
#' @examples
2221
#' # \dontrun{

R/simulate_custom.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#'
2121
#' @return An object of class `"pmsims"` containing the estimated minimum sample size and simulation diagnostics.
2222
#' @keywords internal
23-
#' @export
23+
2424
simulate_custom <- function(
2525
data_function = NULL,
2626
model_function = NULL,
@@ -195,7 +195,7 @@ simulate_custom <- function(
195195
#' the supplied inputs.
196196
#'
197197
#' @keywords internal
198-
#' @export
198+
199199
parse_inputs <- function(data_spec, metric, model) {
200200
if (is.null(metric)) {
201201
stop("metric is missing")

R/simulate_wrappers.R

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#' with the outcome (i.e., true signal features).
3030
#' @param noise_parameters Integer. Number of candidate predictors not
3131
#' associated with the outcome (noise features). Default is 0.
32-
#' @param predictor_type Character string, either `"continuous"` or `"binary"`.
33-
#' Specifies the type of simulated candidate predictors. Defaults to `"continuous"`.
32+
#' @param predictor_type Character string, currently only `"continuous"` supported, which is the default option.
33+
#' Specifies the type of simulated candidate predictors.
3434
#' @param binary_predictor_prevalence Optional numeric in (0, 1). Prevalence of
3535
#' the binary predictors when `predictor_type = "binary"`. Ignored otherwise.
3636
#' @param outcome_prevalence Numeric in (0, 1). Target prevalence of the binary
@@ -78,23 +78,23 @@
7878
#' }
7979
#' @export
8080
simulate_binary <- function(
81-
signal_parameters, # Predictors
81+
signal_parameters, # Predictors
8282
noise_parameters = 0,
83-
predictor_type = c("continuous", "binary"),
83+
predictor_type = c("continuous"),
8484
binary_predictor_prevalence = NULL,
85-
outcome_prevalence, # Outcome
85+
outcome_prevalence, # Outcome
8686
large_sample_cstatistic,
87-
model = c("glm"), # Model
87+
model = c("glm"), # Model
8888
metric = c("calibration_slope", "auc"), # Performance
8989
minimum_acceptable_performance,
90-
n_reps_total = 1000, # Engine control
90+
n_reps_total = 1000, # Engine control
9191
mean_or_assurance = c("assurance", "mean"),
9292
...
9393
) {
94-
predictor_type <- match.arg(predictor_type)
95-
model <- match.arg(model)
96-
metric <- match.arg(metric)
97-
mean_or_assurance <- match.arg(mean_or_assurance)
94+
predictor_type <- check_pmsims_args(predictor_type)
95+
model <- check_pmsims_args(model)
96+
metric <- check_pmsims_args(metric)
97+
mean_or_assurance <- check_pmsims_args(mean_or_assurance)
9898

9999
validate_metric_constraints(
100100
metric = metric,
@@ -206,7 +206,7 @@ simulate_binary <- function(
206206
simulate_continuous <- function(
207207
signal_parameters,
208208
noise_parameters = 0,
209-
predictor_type = c("continuous", "binary"),
209+
predictor_type = c("continuous"),
210210
binary_predictor_prevalence = NULL,
211211
large_sample_rsquared,
212212
model = c("lm"),
@@ -216,10 +216,10 @@ simulate_continuous <- function(
216216
mean_or_assurance = c("assurance", "mean"),
217217
...
218218
) {
219-
predictor_type <- match.arg(predictor_type)
220-
model <- match.arg(model)
221-
metric <- match.arg(metric)
222-
mean_or_assurance <- match.arg(mean_or_assurance)
219+
predictor_type <- check_pmsims_args(predictor_type)
220+
model <- check_pmsims_args(model)
221+
metric <- check_pmsims_args(metric)
222+
mean_or_assurance <- check_pmsims_args(mean_or_assurance)
223223

224224
validate_metric_constraints(
225225
metric = metric,
@@ -329,7 +329,7 @@ simulate_continuous <- function(
329329
simulate_survival <- function(
330330
signal_parameters,
331331
noise_parameters = 0,
332-
predictor_type = c("continuous", "binary"),
332+
predictor_type = c("continuous"),
333333
binary_predictor_prevalence = NULL,
334334
large_sample_cindex,
335335
baseline_hazard = 1,
@@ -341,10 +341,10 @@ simulate_survival <- function(
341341
mean_or_assurance = c("assurance", "mean"),
342342
...
343343
) {
344-
predictor_type <- match.arg(predictor_type)
345-
model <- match.arg(model)
346-
metric <- match.arg(metric)
347-
mean_or_assurance <- match.arg(mean_or_assurance)
344+
predictor_type <- check_pmsims_args(predictor_type)
345+
model <- check_pmsims_args(model)
346+
metric <- check_pmsims_args(metric)
347+
mean_or_assurance <- check_pmsims_args(mean_or_assurance)
348348

349349
validate_metric_constraints(
350350
metric = metric,

R/survival_tuning.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#' @param tolerance The tolerance in the large sample performance
66
#' @return The optimal value for the tuning parameter
77
#' @keywords internal
8-
#' @export
98
#'
109
#' @examples
1110

0 commit comments

Comments
 (0)