10

I need accomplish a pairwise Pearson correlation between 19 raster layers for Africa Continent extracted from WordClim database. I want to checking what are variables layers more correlated/significant to my model. For this i tried use the layerStats function from Raster package, but after execute my output not contain numerical values, all the rows and columns showed NAs values. Below is my script.

#Loading raster files from WorldClim database
rastFiles<- list.files(pattern="bil")
a<-stack(rastFiles)

# Adjusting for African Continent
newext<-c(-20, 55, -35, 45)
Africa<-crop(a,newext)
Africa

#Correlation
cor<-layerStats(Africa,'pearson')
Ricardo Adelino
  • 165
  • 1
  • 1
  • 12

1 Answers1

11

In r, simply use the code below, ensuring you have na.rm=T to deal with NAs across layers:

library(raster)    
jnk=layerStats(raster_stack, 'pearson', na.rm=T)
corr_matrix=jnk$'pearson correlation coefficient'
Lucas Fortini
  • 2,420
  • 15
  • 26
  • hello, have you any idea why my layerStats function might return "Error: Failure during raster IO"? Before stacking 2 rasters, i did have to change the extent of one to match the other. thanks – Sam Jan 11 '16 at 09:33
  • 1
    What does `jnk$'mean'` tell us? I can't find anything about this. Thanks – parallax Jun 08 '18 at 12:34