I have two sets of rasters from 2001-2016. Obviously, these two are correlated to each other but I want to produce a map showing the degree of their correlation. The Correlation Coefficient will be the output raster. Is this possible in R? This is my code which is not working. I am guessing around the looping of rasters inside RED and BLUE.
library (raster)
r <- raster()
userpath <- "F:\\somepath"
#list files
#Read RED and BLUE in different objects:
RED <- list.files(path = userpath, pattern = 'RED*.*.tif', full.names = T)
BLUE <- list.files(path = userpath, pattern = 'BLUE*.*.tif', full.names = T)
redtemp<-stack(RED)
bluetemp<-stack(BLUE)
redtempvalues<-values(redtemp)
bluetempvalues<-valued(bluetemp)
corValues<-vector(mode='numeric')
for (i in 1:dim(redtempvalues)[1]){
corValues[i]<-cor(x=redtemp[i,], y=bluetemp[i,], method='pearson')
}
correlationRaster<-setValues(r, values=corValues)
plot(correlationRaster)
writeRaster(correlationRaster, ".tif")
This is the sample dataset I want to create a correlation map.This is the sample dataset I want to create a correlation map.