我希望parLapply()在R6对象内的窗口上使用并注意到(至少在某些情况下)我不需要将R6函数或数据导出到节点.
这是一个我可以在其中访问私有方法的示例parLapply():
require(R6);require(parallel)
square <-
R6Class("square",
public = list(
numbers = NA,
squares = NA,
initialize = function(numbers,integer) {
self$numbers <- numbers
squares <- private$square.numbers()
}
),
private = list(
square = function(x) {
return(x^2)
},
square.numbers = function() {
cl <- makeCluster(detectCores())
self$squares <- parLapply(cl,
self$numbers,
function (x) private$square(x)
)
stopCluster(cl)
}
))
##Test
test <- square$new(list(1,2,3))
print(test$squares)
# [[1]]
# [1] 1
#
# [[2]]
# [1] 4
#
# [[3]]
# …Run Code Online (Sandbox Code Playgroud) 我在Rcpp遇到条件问题.解释我的问题的最好方法是通过一个例子.
z <- seq(from=1,to=10,by=0.1)
z[c(5,10,15,20,40,50,80)] <- NA
src <- '
Rcpp::NumericVector vecz(z);
for (int i=0;i<vecz.size();i++) {
if (vecz[i] == NA_REAL) {
std::cout << "Here is a missing value" << std::endl;
}
}
'
func <- cxxfunction(signature(z="numeric"),src,plugin="Rcpp")
func(z)
# NULL
Run Code Online (Sandbox Code Playgroud)
根据我的理解,NA_REAL表示Rcpp中的实数NA_Integer的NA值,并表示整数的NA值.我不确定为什么上面的条件永远不会返回true给出z.
函数中特征向量的符号eigen根据symmetric参数的指定而变化。考虑以下示例:
set.seed(1234)
data <- matrix(rnorm(200),nrow=100)
cov.matrix <- cov(data)
vectors.1 <- eigen(cov.matrix,symmetric=TRUE)$vectors
vectors.2 <- eigen(cov.matrix,symmetric=FALSE)$vectors
#The second and third eigenvectors have opposite sign
all(vectors.1 == vectors.2)
FALSE
Run Code Online (Sandbox Code Playgroud)
这对于主成分分析也有影响,因为该函数似乎使用设置为 的函数princomp来计算协方差矩阵的特征向量。eigensymmetricTRUE
pca <- princomp(data)
#princomp uses vectors.1
pca$loadings
Loadings:
Comp.1 Comp.2
[1,] -0.366 -0.931
[2,] 0.931 -0.366
Comp.1 Comp.2
SS loadings 1.0 1.0
Proportion Var 0.5 0.5
Cumulative Var 0.5 1.0
vectors.1
[,1] [,2]
[1,] -0.3659208 -0.9306460
[2,] 0.9306460 -0.3659208
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下差异背后的根源或原因吗?