# Retrieve lineal regression from scatterplot in GEE

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?

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(),,1));
print('Linear fit output B4 vs B6',
ee.Array.cat([xValues, y2], 1).reduce(ee.Reducer.linearFit(),,1));


print(yValues.reduce(ee.Reducer.linearFit(),,1))

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