I am trying to run a correlation test in R for 761 environmental rasters, but that seems to be too many to run at one time. For context, I have average monthly SST and CHL rasters over roughly 50 years that are parameters for MaxEnt species distribution modeling, and I need to know if any correlations exist between any of the raster datasets. Is there a way to load that large number of rasters for correlation analysis that I have not tried (see code below), and if not, what other options exist for determining correlation between SST and CHL rasters?
The code I've tried:
library(ENMTools)
library(raster)
setwd(“environmental rasters location”)
rastlist <- list.files(path = “file location”, pattern = ‘.asc’, all.files = TRUE, full.names = FALSE)
allrasters <- lapply(rastlist, raster)
allrasters_stacked <- stack(allrasters)
correlations <- raster.cor.matrix(allrasters_stacked)
This didn't work so I tried:
r1 <- raster("Chl_Apr2003.1km.asc")
correlations <- cor(sampleRandom(allrasters_stacked, size = ncell(r1)*0.05), method = "spearman")
and got the error:
Cannot allocate vector of size 7.6 Gb