0

I'm trying to get a lineal regression from scatterplot of two bands in GEE, but i don't how to do It. I was trying to apply over the example of scatterplot available in GEE docs.

// Define an arbitrary region of interest.
var sanFrancisco = ee.Geometry.Rectangle([-122.45, 37.74, -122.4, 37.8]);

// Load a Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318');

// Get a dictionary with band names as keys, pixel lists as values.
var result = image.reduceRegion(ee.Reducer.toList(), sanFrancisco, 120);

// Convert the band data to plot on the y-axis to arrays.
var y1 = ee.Array(result.get('B5'));
var y2 = ee.Array(result.get('B6'));
// Concatenate the y-axis data by stacking the arrays on the 1-axis.
var yValues = ee.Array.cat([y1, y2], 1);

// The band data to plot on the x-axis is a List.
var xValues = result.get('B4');

// Make a band correlation chart.
var chart = ui.Chart.array.values(yValues, 0, xValues)
    .setSeriesNames(['B5', 'B6'])
    .setOptions({
      title: 'LC8 TOA B4 vs. {B5,B6}',
      hAxis: {'title': 'B4'},
      vAxis: {'title': '{B5,B6}'},
      pointSize: 3,
});

// Print the chart.
print(chart);

Any idea?

1

You probably just want to chart trendlines in your graph:

// Make a band correlation chart.
var chart = ui.Chart.array.values(yValues, 0, xValues)
    .setSeriesNames(['B5', 'B6'])
    .setOptions({
      title: 'LC8 TOA B4 vs. {B5,B6}',
      hAxis: {'title': 'B4'},
      vAxis: {'title': '{B5,B6}'},
      pointSize: 3,
      trendlines: { 0: {showR2: true, visibleInLegend: true} , 
                    1: {showR2: true, visibleInLegend: true}}
});

And you can get the values R2 values in the chart using:

print('Linear fit output B4 vs B5',
          ee.Array.cat([xValues, y1], 1).reduce(ee.Reducer.linearFit(),[0],1));
print('Linear fit output B4 vs B6',
          ee.Array.cat([xValues, y2], 1).reduce(ee.Reducer.linearFit(),[0],1));

link code

1

Since you have already compiled your values in yValues object, you can use the linear fit reducer to obtain the constants.

print(yValues.reduce(ee.Reducer.linearFit(),[0],1))

The above line should get you the value of constants for the linear fit. The reducer takes 3 parameters among which first is the reducer, second is the axis (or axes) of features and third is the axis of a single feature that contains the values for x and y of the regression. Keep in mind though, since it takes order of the columns for identifying input and output, the constants you get would be different if you did

var yValues = ee.Array.cat([y2, y1], 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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