我正在使用两个 RasterStack 对象,每个对象由代表单个时间步长的十层组成。
# Mock data
pred.rst.stck <- do.call("stack", lapply(seq(10), function(i) {
pred.rst <- raster(nrows = 15, ncols = 15, xmn= 0, xmx = 10, ymn = 0, ymx = 10)
pred.rst[] <- rnorm(225, 50, 10)
return(pred.rst)
})
resp.rst.stck <- do.call("stack", lapply(seq(10), function(i) {
resp.rst <- raster(nrows = 10, ncols = 10, xmn = 0, xmx = 10, ymn = 0, ymx = 10)
resp.rst[] <- rnorm(100, 50, 10)
return(resp.rst)
})
pred.rst.stck
用作一组预测变量和resp.rst.stck
一组响应变量。对于预测器 RasterStack 的每个单个单元格,我想在响应 RasterStack 的每个单元格上拟合一个线性模型,为每个拟合模型提取相应的 R 平方并将它们相加。长话短说,这是我迄今为止使用 Rparallel
包的最快方法:
# Parallelization
library(parallel)
n.cores <- detectCores()
clstr <- makePSOCKcluster(n.cores)
# Extract cell values from RasterStack objects
pred.vals <- getValues(pred)
resp.vals <- getValues(resp)
clusterExport(clstr, c("pred.vals", "resp.vals"))
# Loop through all predictor cells
rsq.sums <- parLapply(clstr, seq(nrow(pred.vals)), function(i) {
# For each predictor cell, loop through all response cells,
# fit linear model, extract and sum up single R-squared
do.call("sum", lapply(seq(nrow(resp.vals)), function(j) {
summary(lm(resp.vals[j, ] ~ pred.vals[i, ]))$r.squared
}))
})
虽然parLapply
与普通相比性能要好得多lapply
,但我想知道是否有一种优雅的方式可以加快整个过程。有什么建议么?
干杯,
弗洛里安