# Get the slope of a linear regression in Google Earth Engine

I have an image with 3 bands that represent the NDVI value from 3 different years:

``````// Load Landsat imagery for different years
var landsat2019 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2019-05-01', '2019-05-31')
.filterBounds(aoi)
.median();
var landsat2020 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2020-05-01', '2020-05-31')
.filterBounds(aoi)
.median();
var landsat2021 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2021-05-01', '2021-05-31')
.filterBounds(aoi)
.median();

// Calculate NDVI for each year
var ndvi2019 = landsat2019.normalizedDifference(['B5', 'B4']).rename('band1');
var ndvi2020 = landsat2020.normalizedDifference(['B5', 'B4']).rename('band2');
var ndvi2021 = landsat2021.normalizedDifference(['B5', 'B4']).rename('band3');

var combinedImage = ee.Image.cat(ndvi2019, ndvi2020, ndvi2021);
``````

For each pixel in the image, I want to compute a linear regression between the 3 ndvi values and get the slope of the linear regression as a new image.

• There is a linear fit example built into the code editor. Commented Mar 4 at 11:26
• @NoelGorelick, I reviewed that documentation (linearFit for ee.Image). However, they compute a linear regression between two bands in that example. In my case, I have 3 bands, each of them with the NDVI value for different years. I want to compute a linear regression as time vs NDVI, and then get the slope value. That is why I do not see how to implement it. thanks
– Isa
Commented Mar 4 at 11:46

I finally solved the problem with this code, using a Image Collection instead of a single image with different bands:

``````// Load Landsat imagery for different years
var landsat2019 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2019-05-01', '2019-05-31')
.filterBounds(aoi)
.median()
.clip(aoi)
.set('year', 2019);
var landsat2020 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2020-05-01', '2020-05-31')
.filterBounds(aoi)
.median()
.clip(aoi)
.set('year', 2020);
var landsat2021 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterDate('2021-05-01', '2021-05-31')
.filterBounds(aoi)
.median()
.clip(aoi)
.set('year', 2021);

var landsatCollection = ee.ImageCollection([landsat2019, landsat2020, landsat2021]);

var sortedCollection = landsatCollection.sort('year');

// Print the sorted collection to inspect the results
print(sortedCollection);

// Compute NDVI for all images in the collection
var ndviCollection = landsatCollection.map(function(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
});

// Select only the NDVI band
var ndviBandCollection = ndviCollection.select('NDVI');

// 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.
};

// Load the input image collection: projected climate data.
var collection = ndviBandCollection
// Map the time band function over the collection.
.map(createTimeBand);

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

// Display the results.
Map.centerObject(aoi, 10);