0

現在、BLB ブートストラップを使用してモデル推定量を計算しようとしていますが、並行して実行したいと考えています。私のコードは、並行して実行していないときに正常に動作します。並列計算を行っているときの問題は、各コアから取得した結果に NA 値が含まれていることです。アイリス データ セットの値に NA がまったく含まれていないのに、NA 値を取得する方法がわかりません。ここに私が使用しているコードがあります:

library(doParallel)
library(itertools)

 num_of_cores <- detectCores()
 cl <- makePSOCKcluster(num_of_cores)
 registerDoParallel(cl)

 attach(iris)
 data <- iris
 coeftmp <- data.frame()
 system.time(
 r <- foreach(dat = isplitRows(data, chunks=num_of_cores),
             .combine = cbind) %dopar% {

                 BLBsize = round(nrow(dat)^0.6)
                 for (i in 1:400){
                         set.seed(i)

                         # sampling B(n) data points from the original data set without replacement
                         sample_BOFN <- dat[sample(nrow(dat), size = BLBsize, replace = FALSE), ]

                          # sampling from the subsample with replacment
                         sample_bootstrap <- sample_BOFN[sample(nrow(sample_BOFN), size = nrow(sample_BOFN), replace = TRUE), ]

                         bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)
                         coeftmp <- rbind(coeftmp, bootstrapModel$coefficients)

                 }
                 #calculating the estimators of the model with mean
                  colMeans(coeftmp)

         })
4

1 に答える 1

1

これを解決するには、デバッガーを数回繰り返す必要があると思います。しかし、あなたはNAこの行から得ています

bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)

sample_bootstrap特異点は NA 係数を与えるため、あなたの の 1 つから特異点が得られると思います。しかし、他の何かがこのエラーを引き起こしている可能性がありますが、それは間違いなくこのコード行からのものです..それを特定するには、デバッガーをステップスルーする必要があります.

...つまり、これは完全な答えではありません。ただし、これにより、独自の問題を解決できるはずです。

これは、調査することで確認できます。

r2 <- foreach(dat = isplitRows(data, chunks=1)) %dopar% {

     BLBsize = round(nrow(dat)^0.6)
     for (i in 1:400){
       set.seed(i)

       # sampling B(n) data points from the original data set without replacement
       sample_BOFN <- dat[sample(nrow(dat), size = BLBsize, replace = FALSE), ]

       # sampling from the subsample with replacment
       sample_bootstrap <- sample_BOFN[sample(nrow(sample_BOFN), size = nrow(sample_BOFN), replace = TRUE), ]

       bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)
       coeftmp <- rbind(coeftmp, bootstrapModel$coefficients)

     }
     #calculating the estimators of the model with mean
     # return a list, not just the colMeans -- for debugging purposes
     return(list(coeftmp= coeftmp, result= colMeans(coeftmp)))

   }

   sum(is.na(r2[[1]][[1]])) # no missing coefficients with 1 core

r <- foreach(dat = isplitRows(data, chunks=num_of_cores)) %dopar% {

     BLBsize = round(nrow(dat)^0.6)
     for (i in 1:400){
       set.seed(i)

       # sampling B(n) data points from the original data set without replacement
       sample_BOFN <- dat[sample(nrow(dat), size = BLBsize, replace = FALSE), ]

       # sampling from the subsample with replacment
       sample_bootstrap <- sample_BOFN[sample(nrow(sample_BOFN), size = nrow(sample_BOFN), replace = TRUE), ]

       bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)
       coeftmp <- rbind(coeftmp, bootstrapModel$coefficients)

     }
     #calculating the estimators of the model with mean
     # return a list, not just the colMeans -- for debugging purposes
     return(list(coeftmp= coeftmp, result= colMeans(coeftmp)))

   }

 # lots of missing values in your coeftmp results.
 lapply(r, function(l) {sum(is.na(l[[1]]))}) 
于 2015-11-16T18:08:09.910 に答える