gpt4 book ai didi

R caret extractPrediction 随机森林模型 : Error: $ operator is invalid for atomic vectors

转载 作者:行者123 更新时间:2023-12-04 09:50:43 26 4
gpt4 key购买 nike

我想使用带有随机森林模型的函数 caret::extractPrediction 来提取新的未见数据的预测,但我无法弄清楚为什么我的代码会抛出错误 Error: $运算符对原子向量无效。输入参数应该如何构造才能使用此功能?

这是我的可重现代码:

library(caret)

dat <- as.data.frame(ChickWeight)
# create column set
dat$set <- rep("train", nrow(dat))
# split into train and validation set
set.seed(1)
dat[sample(nrow(dat), 50), which(colnames(dat) == "set")] <- "validation"

# predictors and response
all_preds <- dat[which(dat$set == "train"), which(names(dat) %in% c("Time", "Diet"))]
response <- dat[which(dat$set == "train"), which(names(dat) == "weight")]

# set train control parameters
contr <- caret::trainControl(method="repeatedcv", number=3, repeats=5)

# recursive feature elimination caret
set.seed(1)
model <- caret::train(x = all_preds,
y = response,
method ="rf",
ntree = 250,
metric = "RMSE",
trControl = contr)

# validation set
vali <- dat[which(dat$set == "validation"), ]

# not working
caret::extractPrediction(models = model, testX = vali[,-c(3,5,1)], testY = vali[,1])
caret::extractPrediction(models = model, testX = vali, testY = vali)

# works without problems
caret::predict.train(model, newdata = vali)

最佳答案

我通过查看extractPrediction 的文档找到了解决方案。基本上,参数 models 不需要单个模型实例,而是模型列表。所以我只是插入了 list(my_rf = model) 而不仅仅是 model

caret::extractPrediction(models = list(my_rf = model), testX = vali[,-c(3,5,1)], testY = vali[,1])

关于R caret extractPrediction 随机森林模型 : Error: $ operator is invalid for atomic vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com