I've created a regression model on my data using random forests in R. The output is quite large, I'm wondering if there's any way to reduce this to only the necessary pieces to make a prediction?

The training data set contains 20 variables and ~45,000 rows, which is also large. My code is listed below.

data <- readRDS("data.Rds")


train <- data[ which(set == "train")]
test <- data[ which(set == "test")]

x <- data.table(train[, 2:21, with=FALSE])
y <- as.vector(as.matrix(train[, 23, with=FALSE]))

cl <- makeCluster(detectCores())
registerDoParallel(cl, cores=4)
time <- system.time({rf.fit <- foreach(ntree=rep(500, 6),
                               .packages="randomForest") %dopar% 
                   {randomForest(x, y, ntree=ntree)}})

saveRDS(rf.fit, "rf.fit.Rds")

The output of this is ~230 MB. Once I have the model, is it possible to reduce the size to make it easier to work with? My goals with this are to identify the important variables, and make a prediction on new data.

  • $\begingroup$ What is your outcome variable of interest? Is it dichotomous, discrete, or continuous? $\endgroup$ Commented Jun 9, 2014 at 14:40
  • $\begingroup$ Are you working on windows? If you are on, for example, linux; the paralellized version is much more memory efficient: r-bloggers.com/… $\endgroup$
    – JEquihua
    Commented Jun 9, 2014 at 17:37
  • $\begingroup$ My output for this round is dichotomous (number of sales). A future iteration will be continuous (revenue). My system is running Windows. I'll take a look at parallelRandomForest, is it possible to get that to run on Windows? $\endgroup$
    – Lenwood
    Commented Jun 9, 2014 at 23:37
  • 1
    $\begingroup$ I've installed parallelRandomForest on my system. It does run MUCH faster, but it doesn't solve this problem. The output file is still over 200 MB. Is it possible to reduce the size of the randomForest model and still be able to use it to make a prediction? $\endgroup$
    – Lenwood
    Commented Jun 19, 2014 at 16:21

1 Answer 1


I used this function to reduce my default caret-output from 137 MB to 3 MB. You can still use this model for prediction with $finalModel

## Clean Model to Save Memory

## http://stats.stackexchange.com/questions/102667/reduce-random-forest-model-memory-size
stripRF <- function(cm) {
  cm$finalModel$predicted <- NULL 
  cm$finalModel$oob.times <- NULL 
  cm$finalModel$y <- NULL
  cm$finalModel$votes <- NULL
  cm$control$indexOut <- NULL
  cm$control$index    <- NULL
  cm$trainingData <- NULL

  attr(cm$terms,".Environment") <- c()
  attr(cm$formula,".Environment") <- c()

  • $\begingroup$ that is nice. what was the train method in your case? I looked at object.size() of the $finalModel produced by method="ranger" and the forest itself is 65% of the total object size (i.e. not reducible beyond that). $\endgroup$
    – coulminer
    Commented Mar 17, 2017 at 10:16
  • $\begingroup$ with the method = "rf", the forest was at 69% of the total object size $\endgroup$
    – coulminer
    Commented Mar 20, 2017 at 9:29
  • $\begingroup$ version of this in python? $\endgroup$ Commented Oct 24, 2019 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.