Skip to main content
edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

How to do Doing spatial correlation between two sets of rasters in R?

Post Reopened by aldo_tapia, tinlyx, whyzar, xunilk, Fezter
edited body
Source Link
user2543
  • 1.2k
  • 4
  • 16
  • 32

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.

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.

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.

I added the sample dataset for replication.
Source Link
user2543
  • 1.2k
  • 4
  • 16
  • 32

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.

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")

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.

added 336 characters in body
Source Link
user2543
  • 1.2k
  • 4
  • 16
  • 32
Loading
deleted 156 characters in body
Source Link
user2543
  • 1.2k
  • 4
  • 16
  • 32
Loading
Post Closed as "Needs more focus" by Spacedman, aldo_tapia, JGH, ArMoraer, whyzar
Source Link
user2543
  • 1.2k
  • 4
  • 16
  • 32
Loading