rmachine-learningregressioncaret

Error in predict.train .... type must be either "raw" or "prob"


I am using caret with ranger for regression but I need to set quantreg = TRUE as I need this to compute quantiles (see below) some extract of my code. But I am getting this error:

Error in predict.train(object = rf_fit, data = df_testing, type = "quantile", : type must be either "raw" or "prob"

library(tidyverse)
library(ranger)
library(caret)

# Get data
data(iris)

## Specify split rate
split <- 0.8

#### Partition the table into training and testing 
set.seed(20)
trainIndex <- createDataPartition(c(iris["Sepal.Length"], 
                                  recursive=T), p=split, list = F)

#### Training samples
df_training <- iris[trainIndex,1:ncol(iris)]
y_train = as.numeric(unlist(df_training['Sepal.Length']))
X_train = df_training[2:4]

#### Testing samples
df_testing <- iris[-trainIndex,1:ncol(iris)]

# Control and grid
ctrl<- trainControl(method="repeatedcv",
                       repeats = 3,
                       savePredictions = TRUE)

tgrid <-  expand.grid(mtry = length(X_train),
                    splitrule = "extratrees",
                    min.node.size = c(1,2,3))

# modelling
rf_fit <- train(X_train,
             y_train,
             method="ranger",
             seed = 20, 
             metric="RMSE",
             tuneGrid=tgrid,
             trControl = ctrl,
             num.trees=500,
             quantreg = TRUE, 
             num.threads = 12,
             importance = "permutation")

# Predict quantile on test set  
qtile=c( 0.05,  0.50, 0.95)
predict(object = rf_fit, data = df_testing, 
                      type = "quantile", 
                      quantiles = qtile,
                      na.rm=TRUE)

Solution

  • The issue arises because you are using predict.train() from the caret package, which does not support type = "quantile". The predict() function in caret only allows type = "raw" or type = "prob", while quantile prediction is a feature of ranger::predict().

    To fix this, use predict() directly from the ranger package instead of caret:

    # Extract the final ranger model from caret
    rf_ranger <- rf_fit$finalModel
    
    # Define quantiles
    qtile <- c(0.05, 0.50, 0.95)
    
    # Predict quantiles using ranger directly
    preds <- predict(rf_ranger, data = df_testing[, 2:4], type = "quantiles", quantiles = qtile)
    
    # Print predictions
    print(preds$predictions)