-1

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.

1 Answer 1

0

Perhaps you should try taking the values from the rasterbrick using:

data<-values(predsGlobal.raw)

Then removing any NAs and turning it into a dataframe as: data<-as.data.frame(data)

Then take the correlation: cor(data) given that it seems to only work with matrix-like data, and you can select the x and y variables from the dataframe to test for colinearity, as it seems to not work effectively with rasterbricks.

Then you can use writeDataTable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.