最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Error in predict.train .... type must be either "raw" or "prob" - Stack Overflow

programmeradmin1浏览0评论

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)

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)
Share Improve this question edited Feb 6 at 23:49 dthorbur 1,0253 gold badges13 silver badges25 bronze badges asked Feb 6 at 8:56 GraceGrace 233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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)
发布评论

评论列表(0)

  1. 暂无评论