for eachを使用してrandomForestを並行して実行する際に問題があります。この例を参照してください。いくつかのデータを作成してから、数式表記を作成します。この式は、randomForest 自体で機能します。しかし、foreach 並列ループで使用すると失敗します...?
# rf on big training set
# use parallel foreach
library(foreach)
library(doMC)
registerDoMC(4) #change the 2 to your number of CPU cores
# info on parrallell backend
getDoParName()
getDoParWorkers()
# bogus data
set.seed(123)
ssize <- 100000
x1 <- sample( LETTERS[1:9], ssize, replace=TRUE, prob=c(0.1, 0.2, 0.15, 0.05,0.1, 0.2, 0.05, 0.05,0.1) )
x2 <- rlnorm(ssize,0,0.25)
x3 <- rlnorm(ssize,0,0.5)
y <- sample( c("Y","N"), ssize, replace=TRUE, prob=c(0.05, 0.95))
df <- data.frame(x1,x2,x3,y)
df$p_y <- as.numeric(df$y)-1
# use strata to sample whole dataset
library(sampling)
s1 = strata(df,stratanames = "y", size = c(2500,2500))
s2 = strata(df,stratanames = "y", size = c(2500,2500))
s3 = strata(df,stratanames = "y", size = c(2500,2500))
s4 = strata(df,stratanames = "y", size = c(2500,2500))
s_list <- list(s1$ID_unit, s2$ID_unit, s3$ID_unit, s4$ID_unit)
# model function
rf.formula <- as.formula(paste("y","~",paste("x1","x2",sep="+")))
library(randomForest)
# simple stuff works but takes some time
model.rf <-randomForest(y ~ x1 + x2, df, ntree=100, nodesize = 50)
# build rf with dopar on explicit formula works and is quick
model.rf.dopar <- foreach(subset=s_list, .combine=combine, .packages='randomForest') %dopar%
randomForest(y ~ x1 + x2, df, ntree=100, nodesize = 50, subset=subset)
# build rf with dopar on rf.formula fails
model.rf.s.b2 <- foreach(subset=s_list, .combine=combine, .packages='randomForest') %dopar%
randomForest(rf.formula, df, ntree=100, nodesize = 50, subset=subset)
# > model.rf.s.b2 <- foreach(subset=s_list, .combine=combine, .packages='randomForest') %dopar%
# + randomForest(rf.formula, df, ntree=100, nodesize = 50, subset=subset)
# Error in randomForest(rf.formula, df, ntree = 100, nodesize = 50, subset = subset) :
# task 1 failed - "invalid subscript type 'closure'"
エラー:
model.rf.s.b2 <- foreach(subset=s_list, .combine=combine, .packages='randomForest') %dopar%
+ randomForest(rf.formula, df, ntree=100, nodesize = 50, subset=subset)
Error in randomForest(rf.formula, df, ntree = 100, nodesize = 50, subset = subset) :
task 1 failed - "invalid subscript type 'closure'"
助言がありますか?
送信