在foreach()中使用的R包gamlss无法找到对象

时间:2018-03-09 05:39:00

标签: r foreach doparallel gamlss

我正试图获得留一个预测值。请帮我解决这个“无法找到对象”的问题。我已经搜索过类似的问题,但还没有成功解决。这是在Windows 10上。

提前致谢

library('gamlss')
library('foreach')
library('doParallel')

registerDoParallel(cores = 4)
# Generate data
set.seed(314)
sample.size <- 30
input.processed.cut <- data.frame(TP = round(runif(sample.size) * 100), 
                                  FP = round(runif(sample.size) * 100), 
                                  x = runif(sample.size))
# Fit Beta-binomial
model3 <- gamlss(formula = cbind(TP, FP) ~ x,   
                 family = BB,  
                 data = input.processed.cut) 

# Get the leave-one-out values
loo_predict.mu <- function(model.obj, input.data) {
  yhat <- foreach(i = 1 : nrow(input.data), .packages="gamlss", .combine = rbind) %dopar% {
    updated.model.obj <- update(model.obj, data = input.data[-i, ])
    predict(updated.model.obj, what = "mu", newdata = input.data[i,], type = "response")
  }
  return(data.frame(result = yhat[, 1], row.names = NULL))
}

par.run <- loo_predict.mu(model3, input.processed.cut)
# Error in { : task 1 failed - "object 'input.data' not found" 

> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          4.3                         
year           2017                        
month          11                          
day            30                          
svn rev        73796                       
language       R                           
version.string R version 3.4.3 (2017-11-30)
nickname       Kite-Eating Tree 

1 个答案:

答案 0 :(得分:0)

我收到了gamlss团队的回复,并确认他们的解决方案有效。唯一要改变的是提供数据&#34;以及&#34; newdata&#34;预测()。

loo_predict.mu <- function(model.obj, input.data) {
  yhat <- foreach(i = 1 : nrow(input.data), .packages="gamlss", .combine = rbind) %dopar% {
    updated.model.obj <- update(model.obj, data = input.data[-i, ])
    predict(updated.model.obj, what = "mu", data = input.data[-i, ],
            newdata = input.data[i,], type = "response")
  }
  return(data.frame(result = yhat[, 1], row.names = NULL))
}