#' Cross Validate Power Exponential Likelihood LASSO (Lq norm LASSO)
#'
#' @param formula a model formula
#' @param data a training data set
#' @param cv.method preferably one of "boot632" (the default), "cv", or "repeatedcv".
#' @param nfolds the number of bootstrap or cross-validation folds to use. defaults to 5.
#' @param nrep the number of repetitions for cv.method = "repeatedcv". defaults to 4.
#' @param tunlen the number of values for the unknown hyperparameter to test. defaults to 10.
#' @param crit the criterion by which to evaluate the model performance. must be one of "MAE" (the default)
#' or "MSE".
#' @param max.c the largest value of the constant for calculating lambda. defaults to 4, but
#' may be adjusted. for example, if the error metric becomes constant after a certain
#' value of C, it may be advisable to lower max.c to a smaller value to obtain
#' a more fine-grained grid over the plausible values.
#'
#' @return
#' a train object
#' @export
#'
cv_dantzig = function(formula, data, cv.method = "boot632", nfolds = 5, nrep = 4, tunlen = 10, max.c = 4, crit = "MAE"){
Dantzig <- list(type = "Regression",
library = "flare",
loop = NULL)
prm <- data.frame(parameter = c("C", "base.lambda"),
class = rep("numeric", 2),
label = c("C", "base.lambda"))
Dantzig$parameters <- prm
Dantzig$max.c <- max.c
lm.betas <- lmSolve(formula, data)
model.mat <- model.matrix(formula, data)
lm.pred <- as.vector(lm.betas) %*% t(model.mat)
lm.res <- as.vector(model.frame(formula, data)[,1]) - lm.pred
Dantzig$noiseSD <- mad(lm.res)
DantzigGrid <- function(x, y, max.c = Dantzig$max.c, noise.sd = Dantzig$noiseSD, len = NULL, search = "grid") {
D = nrow(x)
N = length(y)
lambda0 = noise.sd * sqrt((2 * log(D))/N)
C <- seq(1, max.c, length.out = len)
grid <- data.frame(C = C)
grid$base.lambda <- rep(lambda0, nrow(grid))
## use grid search:
if(search == "grid"){
search = "grid"
} else {
search = "grid"
}
out <- grid
return(out)
}
Dantzig$grid <- DantzigGrid
DantzigFit <- function(x, y, param, ...) {
flare::slim(X = x, Y = y, method = "dantzig", lambda = param$C * param$base.lambda, verbose = FALSE, res.sd = FALSE)
}
Dantzig$fit <- DantzigFit
Dantzig$prob <- DantzigFit
DantzigPred <- function(modelFit, newdata, preProc = NULL, submodels = NULL){
betas = as.vector(c(modelFit$intercept, modelFit$beta))
newx = as.matrix(cbind(y = rep(1, nrow(newdata)), newdata))
as.vector(betas %*% t(newx))
}
Dantzig$predict <- DantzigPred
postRobResamp = function(pred, obs) {
isNA <- is.na(pred)
pred <- pred[!isNA]
obs <- obs[!isNA]
if (!is.factor(obs) && is.numeric(obs)) {
if (length(obs) + length(pred) == 0) {
out <- rep(NA, 3)
}
else {
robmse <- mean((pred - obs)^2)
robmae <- mean(abs(pred - obs))
out <- c(robmse, robmae)
}
names(out) <- c("MSE", "MAE")
}
else {
if (length(obs) + length(pred) == 0) {
out <- rep(NA, 2)
}
else {
pred <- factor(pred, levels = levels(obs))
requireNamespaceQuietStop("e1071")
out <- unlist(e1071::classAgreement(table(obs, pred)))[c("diag",
"kappa")]
}
names(out) <- c("Accuracy", "Kappa")
}
if (any(is.nan(out)))
out[is.nan(out)] <- NA
out
}
basicSummary = function (data, lev = NULL, model = NULL)
{
if (is.character(data$obs))
data$obs <- factor(data$obs, levels = lev)
postRobResamp(data[, "pred"], data[, "obs"])
}
if (cv.method == "repeatedcv") {
fitControl <- trainControl(method = cv.method,
number = nfolds,
repeats = nrep,
savePredictions = "all",
summaryFunction = basicSummary,
search = "grid")
} else {
fitControl <- trainControl(method = cv.method,
number = nfolds,
savePredictions = "all",
summaryFunction = basicSummary,
search = "grid")
}
fitted.models <- train(formula, data,
method = Dantzig,
metric = crit,
tuneLength = tunlen,
maximize = FALSE,
preProcess = c("center", "scale"),
trControl = fitControl)
lambda <- fitted.models$results$C * fitted.models$results$base.lambda
fitted.models$results = cbind.data.frame(fitted.models$results[,1:2],
lambda = lambda,
fitted.models$results[,3:ncol(fitted.models$results)])
return(fitted.models)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.