I want to try adpat quantile regression to caret package. I wrote below code to adapt but I get the error as below:
library(quantreg)
quantregression <- list(type='Regression', library='quantreg',loop=NULL)
param <- data.frame(parameter = c("tau"), class = c("numeric"), label = c("tau"))
quantregression$parameters <- param
quantGrid <- function(x, y, len=NULL, search="grid"){
if(search == "grid"){
out <- expand.grid(tau=c(.05, .25, .5, .75, .95))
}
out
}
quantregression$grid <- quantGrid
quantregFit <- function(x, y, wts, param, lev, last, weights, classProbs, ...){
dat <- if(is.data.frame(x)) x else as.data.frame(x)
dat$.outcome <- y
if(!is.null(wts))
{
if (param$tau)
out <- quantreg::rq(.outcome ~ ., data = dat, weights = wts,tau=tau, ...)
else
out <- quantreg::rq(.outcome ~0 + ., data = dat, weights = wts,tau=tau, ...)
} else
{
if (param$tau)
out <- quantreg::rq(.outcome ~ ., data = dat,tau=tau, ...)
else
out <- quantreg::rq(.outcome ~0 + ., data = dat,tau=tau, ...)
}
out
}
quantregression$fit <- quantregFit
quantPred <- function(modelFit, newdata, preProc=NULL, submodels=NULL){
if(!is.data.frame(newdata)) newdata <- as.data.frame(newdata)
quantreg::predict.rq(modelFit, newdata)
}
quantregression$predict <- quantPred
quantProb <- function(){
return(NULL)
}
quantregression$prob <- quantProb
I get the error as below:
Something is wrong; all the RMSE metric values are missing:
RMSE Rsquared MAE
Min. : NA Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA Median : NA
Mean :NaN Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA Max. : NA
NA's :5 NA's :5 NA's :5
Error: Stopping
In addition: There were 50 or more warnings (use warnings() to see the first 50)
warnings() Warning messages: 1: model fit failed for Fold01.Rep1: tau=0.05 Error in unique(tau) : object 'tau' not found
I want to adopt the quantile regression using "quantreg" package for caret to obtain cross validation results for quantile regression. I wrote above code but I could not find the error in the code.