-1

How can apply Linear Fit with Google Earth Engine? I have the next code but I cannot see the values of the slope (scale) because are masked.

var region = table

var start = '2000-01-01';
var end = '2014-12-31';


// This function adds a time band to the image.
var createTimeBand = function(image) {
  // Scale milliseconds by a large constant to avoid very small slopes
  // in the linear regression output.
  return image.addBands(image.metadata('system:time_start').divide(1e18));
};

// Load the input image collection: projected climate data.
var collection = ee.ImageCollection("MODIS/MOD13Q1")
  .filter(ee.Filter.calendarRange(1,1,'month'))
  .filterBounds(region)
  .map(createTimeBand)
  .filterDate(ee.Date('2000-01-01'), ee.Date('2014-12-31'))
  .map(function(image) {
    var date = ee.Date(image.get('system:time_start'));
    var yearOffset = date.difference(ee.Date(start), 'year');
    var savi = image.expression(
      '(1 + L) * float(nir - red)/ (nir + red + L)',
      {
        'nir': image.select('sur_refl_b02'),
        'red': image.select('sur_refl_b01'),
        'L': 0.5
      });
    return ee.Image(1).addBands(yearOffset).addBands(savi).toDouble();
  });

Map.addLayer(collection)


print (collection)


// Reduce the collection with the linear fit reducer.
// Independent variable are followed by dependent variables.
var linearFit = collection.select(['constant', 'constant_2'])
  .reduce(ee.Reducer.linearFit());

Map.addLayer(linearFit)
2

You were on the right track, but used an incorrect band name in the collection that you applied the linear fit reducer to. Here is a working version that renames the bands in the interest of readability.

var start = ee.Date('2000-01-01');
var end = ee.Date('2014-12-31');

// This function adds a time band to the image.
var createTimeBand = function(image) {
  // Scale milliseconds by a large constant to avoid very small slopes
  // in the linear regression output.
  return image.addBands(image.metadata('system:time_start').subtract(start.millis()));
};

// Load the input image collection: projected climate data.
var collection = ee.ImageCollection("MODIS/MOD13Q1")
  .filter(ee.Filter.calendarRange(1,1,'month'))
  .map(createTimeBand)
  .filterDate(ee.Date('2000-01-01'), ee.Date('2014-12-31'))
  .map(function(image) {
    var date = ee.Date(image.get('system:time_start'));
    var yearOffset = ee.Image(date.difference(ee.Date(start), 'year')).rename('year_offset');
    var savi = image.expression(
      '(1 + L) * float(nir - red)/ (nir + red + L)',
      {
        'nir': image.select('sur_refl_b02'),
        'red': image.select('sur_refl_b01'),
        'L': 0.5
      }).rename('savi');
    return ee.Image(1).addBands(yearOffset).addBands(savi).toDouble();
  });

Map.addLayer(collection, {}, 'collection', false);

// Reduce the collection with the linear fit reducer.
// Independent variable are followed by dependent variables.
var linearFit = collection.select(['year_offset', 'savi'])
  .reduce(ee.Reducer.linearFit());

Map.addLayer(
  linearFit.addBands(ee.Image(0).rename('dummy_band')),
  {bands:['scale', 'offset', 'dummy_band'], min:[0], max:[0.01,1,0]},
  'linearFit'
);

If you map the linear fit model's scale and offset to the red and green color channels, the result is rather interesting.

enter image description here

  • I am comparing the LinearFit from GEE with a Simple Linear Regression in Excel for some random points and are completely different. I extract one pixel value from the result of the LinearFit in GEE and lets say that it is 0.012. Then, I plot a chart with SAVI collection and I extract (.csv) every single SAVI pixel value for each image (28 in total) and I do the LinearRegression in Excel and the result of the slope (scale) is completely different. – Juan TB Feb 8 '18 at 10:39
  • I do the same with the LinearRegression function from GEE and the result is exactly the same as with the LinearFit. I cannot understand why with GEE I get one result and with SPSS and EXCEL I get other results. – Juan TB Feb 8 '18 at 10:43

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.