I am trying to do a Linear Regression about Evapotranspiration using MODIS and I get this error when I try to inspect the value of the pixels in the result layer: "Error Array index 1 out of bounds. Expected value between 0 and -1, found, 0."

The code is the next:

// Simple regression of year versus NDVI.

// Define the start date and position to get images covering TDPS,
// Bolivia, from 2000-2010.
var start = '2000-01-01';
var end = '2014-12-31';

var region = table2

// Filter to Collection
// time of year to avoid seasonal affects, and for each image create the bands
// we will regress on:
// 1. A 1, so the resulting array has a column of ones to capture the offset.
// 2. Fractional year past 2000-01-01.
// 3. SAVI.
// 4. Mask Clouds
var col = ee.ImageCollection("MODIS/NTSG/MOD16A2/105")
  .filterDate(start, end)
  .map(function(image) {
    var date = ee.Date(image.get('system:time_start'));
    var yearOffset = date.difference(ee.Date(start), 'year');
    return ee.Image(1).addBands(yearOffset).toDouble();

// Convert to an array. Give the axes names for more readable code.
var array = col.toArray();
var imageAxis = 0;
var bandAxis = 1;

// Slice off the year and ndvi, and solve for the coefficients.
var x = array.arraySlice(bandAxis, 0, 2);
var y = array.arraySlice(bandAxis, 2);
var fit = x.matrixSolve(y).clip(region);

// Get the coefficient for the year, effectively the slope of the long-term
// NDVI trend.
var slope = fit.arrayGet([1, 0]).clip(region);


// Export the image, specifying scale and region.
  image: slope,
  description: 'ET_slope_MODIS',
  scale: 1000,
  maxPixels: 1e9,
  region: geometry

Does anyone know which is the main issue?

1 Answer 1


The main issue is that you should not use arrays when you don't need to. If I'm interpreting your intention correctly, here it is the better way:

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

var col = ee.ImageCollection("MODIS/NTSG/MOD16A2/105").select('ET')
  .filterDate(start, end)
  .map(function(image) {
    var date = ee.Date(image.get('system:time_start'));
    var yearOffset = date.difference(ee.Date(start), 'year');
    return ee.Image(1).addBands(yearOffset).float().addBands(image);

var regression = col.reduce(ee.Reducer.linearRegression(2));

var coeffsImage = regression
    .arrayFlatten([['constant', 'timeCoeff']]);

Map.addLayer(coeffsImage, {bands: 'timeCoeff'}, 'timeCoeff');

For more on linear regression, see this page.

  • Is it possible that GEE takes a distance equidistant for the independent variable (time)? If images are daily will take (day1,day2,day3...) but if there is a gap of 37 days between image3 and image4, does GEE take that gap?
    – Juan TB
    Mar 21, 2018 at 11:42

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.