随机森林模型的Rcaret提取预测:错误:$运算符对于原子向量无效

yPennylane

我想使用caret::extractPrediction带有随机森林模型的函数来提取新的看不见的数据的预测,但我不知道为什么我的代码会引发错误Error: $ operator is invalid for atomic vectors要使用此功能,应如何构造输入参数?

这是我的可复制代码:

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])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

插入符:: train函数中的“错误:$运算符对于原子向量无效”

运算符对于函数内部的原子向量无效

闪亮:updateSelectizeInput引发错误“ $运算符对于原子向量无效”

ggplot2错误:$运算符对于原子向量无效

Apply函数返回“错误:$运算符对于原子向量无效”

plot.lm错误:$运算符对原子向量无效

rfe() 函数错误:$ 运算符对原子向量无效

model.matrix错误:$运算符对原子向量无效

方差分析:错误:$运算符对原子向量无效

错误$运算符对制作直方图R的原子向量无效

biglm - 错误:$ 运算符对原子向量无效

错误:$ 运算符对原子向量无效(r 闪亮 ggplot)

Speeglm $运算符对原子向量无效

尽管未使用原子向量或 $,但错误“$ 运算符对原子向量无效”

将函数应用于不同的数据子集时,对于3个子集中的1个,会出现“错误:$运算符对于原子向量无效”

x $ j中的错误:$运算符对文本聚类中的原子向量无效

第一个函数中的错误:$运算符对原子向量无效

模糊匹配:避免出现“错误:$运算符对原子向量无效”

caret::predict 给出错误:$ 运算符对原子向量无效

HR.Rates $ y中的错误:$运算符对原子向量无效

使用 ggsave 时遇到“错误:$ 运算符对原子向量无效”

R fromJSON - $ 运算符对原子向量无效

SQL Server中的R:$运算符对原子向量无效

$ 运算符对闪亮的 R 中的原子向量无效

随机森林预测模型

两个函数-一个很好,一个有错误:$运算符对原子向量无效

==运算符的“无效分配”错误

一元运算符的无效参数-错误消息-负dplyr ::用向量选择

随机森林模型中预测结果的差异