R中的高尔距离并行计算

时间:2018-08-13 19:05:38

标签: r parallel-processing doparallel

我正在尝试计算单个数据集观测值之间的高尔距离。我在以下链接中找到了一个有用的函数:https://www.kaggle.com/olivermeyfarth/parallel-computation-of-gower-distance?scriptVersionId=46656,其中包含以下使用并行计算的代码。

代码如下:

computeDistance <- function(dt1, dt2, nThreads = 4) {
  # Determine chunk-size to be processed by different threads
  s <- floor(nrow(dt1) / nThreads)

  # Setup multi-threading
  modelRunner <- makeCluster(nThreads)
  registerDoParallel(modelRunner)

  # For numeric variables, build ranges (max-min) to be used in gower-distance.
  # Ensure that the ranges is computed on the overall data and not on
  # the chunks in the parallel threads. Also, note that function 'gower.dist()'
  # seems to be buggy in regards to missing values (NA), which can be fixed by
  # providing ranges for all numeric variables in the function-call

  dt <- rbind(dt1, dt2)
  rngs <- rep(NA, ncol(dt))
  for (i in 1:ncol(dt)) {
   col <- dt[[i]]
   if (is.numeric(col)) {
     rngs[i] <- max(col, na.rm = T) - min(col, na.rm = T)
   }
  }

  # Compute distance in parallel threads; note that you have to include packages
  # which must be available in the different threads
  distanceMatrix <-
    foreach(
      i = 1:nThreads, .packages = c("StatMatch"), .combine = "rbind",
      .export = "computeDistance", .inorder = TRUE
    ) %dopar% {
      # Compute chunks
      from <- (i - 1) * s + 1
      to <- i * s
      if (i == nThreads) {
        to <- nrow(dt1)
      }

      # Compute distance-matrix for each chunk
      # distanceMatrix <- daisy(dt1[from:to,],metric = "gower")
      distanceMatrix <- gower.dist(dt1[from:to,], dt2, rngs = rngs)
    }

  # Clean-up
  stopCluster(modelRunner)
  return(distanceMatrix)
}    

但是,当我尝试使用数据集运行代码时(以下数据只是一个简单的示例),如下所示:

Distance_data <- data.frame(cbind(c(4,234,6,1),c(4,1,6,4),c(3,75,23,1)))
distances <- computeDistance(Distance_data,Distance_data)  

我在R中收到以下错误:

Error in e$fun(obj, substitute(ex), parent.frame(), e$data) : 
worker initialization failed: package ‘clue’ could not be loaded

我尝试将线索包添加到foreach函数中的.packages参数,但这没有成功。任何帮助表示赞赏!

注意:您需要下载以下软件包才能运行功能

library(StatMatch)
library(doParallel) 

0 个答案:

没有答案