library(partools)
library(randomForest)

cls <- makeCluster(2)
setclsinfo(cls)
clusterEvalQ(cls,library(randomForest))
# https://archive.ics.uci.edu/ml/datasets/covertype
cvr <- read.csv(file="/Users/robinyancey/desktop/covtype.data",sep=',',header=F)
#cvr <- as.data.table(cvr)
cvr <- cvr[,c(1:10,55)]
cvr$V55 <- as.factor(as.character(cvr$V55))
testidxs <- sample(1:nrow(cvr),10000)

cvrtest <- as.matrix(cvr[testidxs,1:10])
cvrtesttrue <- cvr[testidxs,11]
clusterExport(cls,'cvrtest')
cvrtrain <- cvr[-testidxs,]
distribsplit(cls,'cvrtrain',scramble=T)

print(system.time(clusterEvalQ(cls,rfout <- randomForest(V55 ~ .,data=cvrtrain))))
print(system.time(rfout <- randomForest(V55 ~ .,data=cvrtrain)))
yprob <- predict(rfout,cvrtest,type='prob')
dyprob <- clusterEvalQ(cls,predict(rfout,cvrtest,type='prob'))
dyprob <- Reduce('+',dyprob) / 2
z <- apply(yprob,1,which.max)
dz <- apply(dyprob,1,which.max)

print(mean(dz == cvrtesttrue))  
print(mean(z == cvrtesttrue))
