3

我有一个可以使用的函数,lapply但如果我尝试使用mclapply. 该函数的参数是一个多元 XTS。这是一个示例:

library(quantmod)
library(doMC)
registerDoMC(4)

test <- function(x){
  return(mean(x))
}

myEnv <- new.env()
getSymbols(c("^GSPC", "^RUT"), env=myEnv)
data <- do.call(merge, c(eapply(myEnv, Ad), all=TRUE))

lapply(data, test)
mclapply(data, test)

lapply按预期返回结果,但mclapply返回:

Error in `[.xts`(X, seq(i, length(X), by = cores)) : 
  subscript out of bounds

有人可以帮我吗?谢谢。

会话信息

R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] doMC_1.2.5      multicore_0.1-7 iterators_1.0.6 foreach_1.4.0   quantmod_0.3-22 TTR_0.21-1      xts_0.9-0       zoo_1.7-9      
[9] Defaults_1.1-1 

loaded via a namespace (and not attached):
[1] codetools_0.2-8 grid_2.15.2     lattice_0.20-10 Rcpp_0.9.15     tools_2.15.2   
4

1 回答 1

3

xts从某种意义上说,对象很有趣:

length(data)
# [1] 3010
data[3010]
# Error in `[.xts`(data, 3010) : subscript out of bounds

还有那个,mclapply不喜欢……

尽管文档说mclapply(as.list(data), test)

X: 向量(原子或列表)或表达式向量。其他对象(包括分类对象)将被as.list.

去想...这可能值得向作者提及。

于 2012-12-22T04:14:44.117 回答