3

I want to calculate the correlation coefficient and R squared between two bands for two different images. How should I proceed?

    var s1_float = ee.ImageCollection("COPERNICUS/S1_GRD_FLOAT")
        .filterMetadata ('resolution_meters', 'equals', 10)
        .filter (ee.Filter.eq ('instrumentMode', 'IW'))
        .filter (ee.Filter.eq('orbitProperties_pass', 'ASCENDING'))
        .filterDate('2020-01-02', '2020-01-04')
        .filterBounds(geometry) 
        .first()
        .clip (geometry)
    
    //second image from my assets 
    var grd = ee.Image ("users/bene96detta/SLC_to_GRD_orb_TC").clip(geometry);

    //difference between the two bands 
    var diffBandVV = grd.select('b2').subtract(s1_float.select('VV')) 
    //std deviation 
    var stdevVV = diffBandVV.reduceRegion({ 
    reducer: ee.Reducer.stdDev(),
    scale: 30,
    maxPixels: 10e9
    });

1 Answer 1

1

I was also looking to calculate R2 between two images, and couldn't find a reducer to do this -- only for the Pearson correlation coefficient. Here is my current solution showing both (Pearson correlation coefficient and coefficient of determination, i.e. R2)).

Hopefully there is either a reducer that I missed, or a more elegant solution that someone can share.

https://code.earthengine.google.com/9b66ed88a7c0d581c6d484f16dac70c1

var geometry = 
    ee.Geometry.Polygon(
        [[[-117.24411029815674, 40.07684302769587],
          [-117.24411029815674, 40.06830460341872],
          [-117.23411102294922, 40.06830460341872],
          [-117.23411102294922, 40.07684302769587]]]);

var image = ee.Image("LANDSAT/LC08/C01/T1_TOA/LC08_041032_20140108")
var image1 = image.select("B2")
var image2 = image.select("B3")

Map.addLayer(image1.clip(geometry),{min:0,max:0.3},"B2")
Map.addLayer(image2.clip(geometry),{min:0,max:0.3},"B3")
Map.centerObject(geometry,15)

image = image.select(["B2","B3"])
var values = image
.reduceRegion(ee.Reducer.toList(), geometry, 30)
print("Values: ", values)


// Pearson correlation coefficient:
var pearsonR = image.reduceRegion(
{
geometry: geometry,
reducer: ee.Reducer.pearsonsCorrelation(),
scale:30  
})
print("Pearson R(B2,B3):", pearsonR.get("correlation"))


// Coefficient of determination:
function R2(image, fitted, geometry, scale){
  image = image.rename("constant")
  fitted = fitted.rename("constant")
var SSres = image.subtract(fitted).pow(2)
.reduceRegion({
  geometry:geometry,
  reducer:ee.Reducer.sum(),
  scale:scale
})
var imageMean = image.reduceRegion({
  geometry:geometry,
  reducer:ee.Reducer.mean(),
  scale:scale
}).toImage(["constant"]);
var SStot = image.subtract(imageMean).pow(2)
.reduceRegion({
  geometry:geometry,
  reducer:ee.Reducer.sum(),
  scale:scale
})
var coeff = ee.Number(1).subtract(
  ee.Number(SSres.get("constant"))
  .divide(ee.Number(SStot.get("constant"))))
return coeff
}


// R2 using B2 as y_true and B3 as y_fitted:
var coeff = R2(image1,image2,geometry, 30);
print("R2(y_true=B2, y_fitted=B3): ", coeff)

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.