Calculating residual sum of squares and standard error from linearFit model [closed]

I used the linearFit() reducer to get trend analysis for NDVI (dependent variable), and time (t) is my one independent value.

What code can I use to calculate the residual sum of squares and standard error from this linearFit model in google earth engine?

• in theory this should be possible with `ee.Reducer.linearRegression` but for me this solution always times out. Commented Apr 17, 2021 at 11:06

There are two options:

1. Use `ee.Reducer.linearRegression()` instead of `ee.linearFit()`. Then you can take R2 from the band called `'residuals'`.

2. You go from the definition of and code it yourself.

For my dataset, option 1 resulted in a timeout but you might get different mileage. Hence, I'll describe how to code from it's definition. For instance over at Wikipedia the definition is given by:

For an explanation of the symbols, check out the Wikipedia page. In addition I used for the variance and for the count of datapoints. I turned this formula into the following code and got decent performance.

``````var varcount_reducer = ee.Reducer.variance().combine(ee.Reducer.count(), '', true);
image_collection = image_collection.map(function (img) {
});
var trend_img = image_collection
.select(['system:time_start', 'sigma0'])
.reduce(ee.Reducer.linearFit());

// calculate R_squared, the coefficient of determination
// R_squared = 1 - SS_res / SS_tot
var offset = trend_img.select('offset');
var scale = trend_img.select('scale');
var SS_res = image_collection.map( function (img) {
var fit = offset.add(img.select('system:time_start').multiply(scale)); // y_bar
var error = img.select('sigma0').subtract(fit); // y_i - y_bar
return error.multiply(error); // squared
}).sum();
var SS_tot = change_coll.reduce(varcount_reducer).expression(
"b('sigma0_variance')*b('sigma0_count')");