Why do i get this error here. when i run the function without parallel threads it works…
- User
Why do i get this error here. when i run the function without parallel threads it works fine. the code also runs fine on a mac but doesn't on a ubuntu:
Progress interrupted by simpleError condition: number of items to replace is not a multiple of replacement length Error in importance_scores[, i] <- importance[, "MeanDecreaseGini"] : number of items to replace is not a multiple of replacement length
Here is the code:
RF_bagging <- function(feature_matrix, positive_set, negative_pool, ntrees = 1000, n_models = 100, track_iterations = TRUE, model_data_output_dir = NULL) {
suppressMessages({ library(progressr)
library(foreach) library(doParallel) library(caret) library(randomForest) library(pROC) })handlers(global = TRUE) # Set up progress handlers
cl <- makeCluster(detectCores() - 1) registerDoParallel(cl)
predictions <- matrix(0, nrow = nrow(feature_matrix), ncol = n_models) # For storing predictions n_features <- ncol(feature_matrix) - 3 # For storing importance scores importance_scores <- matrix(0, nrow = n_features, ncol = n_models)
tune_grid <- expand.grid( mtry = seq(2, 10, by = 1) # Only include 'mtry' )
Use progressr to track the loop progress
with_progress({ p <- progressor(along = 1:n_models) # Create a progressor
# Train multiple RF models using random negative samples for (i in 1:n_models) { p(sprintf("Running model %d/%d", i, n_models)) # Update progress set.seed(i) # Different seed for each iteration # Resample negatives for each iteration negative_set <- negative_pool[sample(1:nrow(negative_pool), nrow(positive_set), replace = FALSE), ] training_data <- rbind(positive_set, negative_set) training_labels <- training_data$approved training_features <- training_data[, !names(training_data) %in% c("approved", "Protein", "clinical")] # Perform hyperparameter tuning tune_control <- trainControl(method = "cv", number = 5, verboseIter = FALSE) rf_tuned <- train( x = training_features, y = as.factor(training_labels), method = "rf", metric = "Accuracy", tuneGrid = tune_grid, # Only 'mtry' trControl = tune_control, ntree = ntrees # Pass 'ntree' directly here ) # Use best tuned parameters best_mtry <- rf_tuned$bestTune$mtry # Train Random Forest with tuned parameters rf_model <- randomForest( x = training_features, y = as.factor(training_labels), mtry = best_mtry, ntree = ntrees, importance = TRUE ) # Predict on the entire dataset predictions[, i] <- predict(rf_model, feature_matrix[, !names(feature_matrix) %in% c("approved", "Protein", "clinical")], type = "prob")[, 2] # Extract feature importance importance <- importance(rf_model) importance_scores[, i] <- importance[, "MeanDecreaseGini"] }})
Remaining part of the function unchanged...
Attach tracked predictions to the dataset
- Assistant
- User
what does this warning mean:
Warning messages: 1: In for (i in iArgs) { ... : closing unused connection 33 (<-localhost:11414)
- Assistant
This warning indicates that R has closed a connection that was opened during your session (commonly as part of parallel processing or other socket connections) but ultimately was not used (or no longer in use).
In many parallel-processing workflows, R spins up multiple connections (sockets or ports) to manage tasks across worker processes. Sometimes a connection remains open or is automatically opened by an underlying function but never actually handles any data. When R garbage-collects or explicitly shuts down, it closes those idle connections and issues this warning.
The key points are:
- It’s not an error and often not harmful.
- It simply informs you that an unused connection was closed.
- It can happen frequently in parallel or network-related tasks where R creates and disposes of connections.
If these warnings are flooding your logs or you suspect you have too many unused connections, you may want to:
- Explicitly stop and clear your parallel clusters at the end of your script (e.g.,
stopCluster(cl)). - Reduce the number of parallel processes if they are not needed.
- Make sure functions you call don’t leave open connections behind if they do not need them.