如何在for循环中定义模型名称?
我想在不同的样本上训练n个随机森林。样本1给出rf1,样本2给出rf2,等等 但这类代码不起作用(类型为“closure”的错误对象不可子集)如何在for循环中定义模型名称?,r,R,我想在不同的样本上训练n个随机森林。样本1给出rf1,样本2给出rf2,等等 但这类代码不起作用(类型为“closure”的错误对象不可子集) for(1:3中的i){ rf$i它不起作用,因为rf还不存在,您无法将其子集 1.将列表用作容器 以下几点应该行得通 # define the length of your random forest trials N = 3 rf = vector( "list", N) for (i in seq_len( N ) { rf[[ i ]] &
for(1:3中的i){
rf$i它不起作用,因为rf
还不存在,您无法将其子集
1.将列表用作容器
以下几点应该行得通
# define the length of your random forest trials
N = 3
rf = vector( "list", N)
for (i in seq_len( N ) {
rf[[ i ]] <- train( Y ~. , data = trainingData, method = "rf",
ntree = 100,
tuneGrid=data.frame(mtry = mtry),
trControl = controle,
metric='ROC')
}
这将在您的环境中存储三个对象rf1
、rf2
和rf3
,您可以独立处理它们。它不起作用,因为rf
尚不存在,并且您无法将其子集
1.将列表用作容器
以下几点应该行得通
# define the length of your random forest trials
N = 3
rf = vector( "list", N)
for (i in seq_len( N ) {
rf[[ i ]] <- train( Y ~. , data = trainingData, method = "rf",
ntree = 100,
tuneGrid=data.frame(mtry = mtry),
trControl = controle,
metric='ROC')
}
这将在您的环境中存储三个对象rf1
、rf2
和rf3
,您可以独立处理它们。$
不处理$
之后的变量。假设rf
是一个列表(使用rf初始化名为rf=vector('list',',3)的列表)
,然后使用rf[[i]]$
不处理$
后面的变量。假设rf
是一个列表(使用rf初始化名为rf=vector('list',3)
,然后使用rf[[i]]
# define the length of your random forest trials
N = 3
for (i in seq_len( N ) {
assign( paste0( "rf", i) ,
train( Y ~. ,
data = trainingData, method = "rf",
ntree = 100,
tuneGrid=data.frame(mtry = mtry),
trControl = controle,
metric='ROC')
}