diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2018-03-28 13:41:14 +0100 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2018-03-28 13:41:14 +0100 |
| commit | 75d6ed3e5d919b4e5b7bd1e81283131c92530d26 (patch) | |
| tree | 907f9de5769412c25e61d0c67a50089e6d8e1d6e /R/validate.R | |
| parent | Make sure we use "weights" everywhere instead of "weight" (diff) | |
| download | rgensvm-75d6ed3e5d919b4e5b7bd1e81283131c92530d26.tar.gz rgensvm-75d6ed3e5d919b4e5b7bd1e81283131c92530d26.zip | |
Validate params in gensvm() function
Parameter validation was only done for some parameters in the
gensvm() function and for the parameter grid in gensvm.grid()
With this commit the parameters will be tested properly for
both functions.
Diffstat (limited to 'R/validate.R')
| -rw-r--r-- | R/validate.R | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/R/validate.R b/R/validate.R new file mode 100644 index 0000000..b0f3f39 --- /dev/null +++ b/R/validate.R @@ -0,0 +1,70 @@ +#' @title [internal] Validate parameters +#' +#' @export +#' @keywords internal +gensvm.validate.params <- function(p=NULL, kappa=NULL, lambda=NULL, + epsilon=NULL, gamma=NULL, weights=NULL, + kernel=NULL, ...) +{ + the.args <- as.list(match.call()) + conditions <- gensvm.param.conditions() + for (param in names(the.args)) { + if (is.null(the.args[[param]])) + next + if (!(param %in% names(conditions))) + next + func <- conditions[[param]] + value <- eval(the.args[[param]]) + if (!func(value)) { + cat(sprintf("Error: Parameter '%s' got invalid value: %s\n", param, + toString(value))) + return(FALSE) + } + } + return(TRUE) +} + +#' @title [internal] Validate parameter grid +#' +#' @export +#' @keywords internal +gensvm.validate.param.grid <- function(df) +{ + expected.colnames <- c("kernel", "coef", "degree", "gamma", "weights", + "kappa", "lambda", "p", "epsilon", "max.iter") + for (name in colnames(df)) { + if (!(name %in% expected.colnames)) { + cat(sprintf("Error: Invalid name supplied in parameter grid: %s\n", + name)) + return(FALSE) + } + } + + conditions <- gensvm.param.conditions() + for (idx in 1:nrow(df)) { + for (param in colnames(df)) { + if (!(param %in% names(conditions))) + next + func <- conditions[[param]] + value <- df[[param]][idx] + if (!func(value)) { + cat(sprintf("Invalid value in grid for parameter: %s\n", param)) + return(FALSE) + } + } + } + return(TRUE) +} + +gensvm.param.conditions <- function() +{ + conditions <- list( + p=function(x) { x >= 1.0 && x <= 2.0 }, + kappa=function(x) { x > -1.0 }, + lambda=function(x) {x > 0.0 }, + epsilon=function(x) { x > 0.0 }, + gamma=function(x) { x != 0.0 }, + weights=function(x) { x %in% c("unit", "group") }, + kernel=function(x) { x %in% c("linear", "poly", "rbf", "sigmoid") } + ) +} |
