Parallelize training

# Create a parallel socket cluster
cl <- makeCluster(8) # use 8 workers
registerDoParallel(cl) # register the parallel backend

# Fit trees in parallel and compute predictions on the test set
predictions <- foreach(
  icount(160), 
  .packages = "rpart", 
  .combine = cbind
) %dopar% {
  # bootstrap copy of training data
  index <- sample(nrow(ames_train), replace = TRUE)
  ames_train_boot <- ames_train[index, ]  
  
  # fit tree to bootstrap copy
  bagged_tree <- rpart(
    Sale_Price ~ ., 
    control = rpart.control(minsplit = 2, cp = 0),
    data = ames_train_boot
  ) 
  
  predict(bagged_tree, newdata = ames_test)
}

# Shutdown parallel cluster
stopCluster(cl)

predictions[1:5, 1:7]
##   result.1 result.2 result.3 result.4 result.5 result.6 result.7
## 1   100000   113500   129500   140000   132500   156000   128000
## 2   181755   188000   184900   187000   173000   185000   189500
## 3   189500   237000   191000   187000   173000   150000   179000
## 4   162500   206000   240000   180000   172500   180000   180000
## 5   306000   392000   272000   193950   278000   350000   260000