I have 30 raster layers (.tiff) on oceanic and climatic environmental variables from all over the world downloaded from global databases.
I need to analyze the relationship that may exist between these layers of information, prior to carrying out other analyzes (species distribution models). I have read that I could perform two tests: Pearson Correlation and Variance Inflaction Factor (VIF). The idea is to stay only with the variables (layers) that have a Pearson correlation < 0.7 and a VIF < 10.
The aim is to output a correlation matrix that shows the coefficient for each of the combinations.
I have tried this code:
# Load the physical variables and saved as raster brick
list <- list.files(path="D:/layers", pattern='.tif$', full.names=TRUE)
predsGlobal.raw <- brick(stack(list))
save(predsGlobal.raw,file="D:/Correlation/predsGlobal.raw.Rda")
layersGlob <- predsGlobal.raw@data@names
# Applied a threshold of 0.7 for Pearson correlation
vc <- vifcor(predsGlobal.raw,th=.7)
correl.groups <- cor(predsGlobal.raw) # matrix
vc@results
ex <- exclude(predsGlobal.raw,vc)
save(ex,file="D:/Correlacion/ex.Rda")
# Save in an excel
wb <- createWorkbook()
addWorksheet(wb, "predsGlobal")
writeDataTable(wb, 'predsGlobal', ex, startCol = 1, startRow = 1, colNames = TRUE, rowNames = F)
saveWorkbook(wb, file = 'D:/Correlation/predsGlobal.xlsx')
But it has not worked for me, I get the following errors:
Error in cor (predsGlobal.raw):
supply both 'x' and 'y' or a matrix-like 'x'
Error in writeDataTable(wb, "predsGlobal", ex, startCol = 1, startRow = 1, :
x must be a data.frame.