I want to calculate the longterm mean NDVI anomaly at a specific geometry point buffered to 1000m. I have calculated the longterm mean NDVI for the 20 year period. Because I want to calculate NDVI anomalies per year, I have calculated the monthly mean NDVI for the year 2020 as my first observation period.

The 20 year longterm mean NDVI and the monthly mean NDVI scripts are working as can be seen from the timeseries plots generated.

I am getting an error at line 224 of the script calculating the NDVI anomalies where I am trying to write a function that returns the anomalies using the formula below;

Ndvi_anomalies = monthly mean NDVI - longterm mean NDVI

Looking forward to your help and suggestions.

Here is the link to the code; https://code.earthengine.google.com/19e96a3fcda5b52fa84f1ed96ccf1dc8


1 Answer 1


You are going about your problem the wrong way. If you look at the documentation of the subtract() function (in the Docs tab next to Scripts) you'll see that is applicable to ee.Arrays, ee.Images and ee.Numbers, but not ee.ImageCollections. If you want a result for each month, you have to map through your image collection so that it runs the operation one image at the time, for example:

var Ndvi_anomalies =  months.map(function(m) {
  return mthmeanNDVI.filterMetadata('month', 'equals', m).first()
    .subtract(ltmean.filterMetadata('month', 'equals', m).first())
    .clip(swarmpoint).set('month', m);
  • thank you so much. The explanation is clear and the results match a slightly different way I used to calculate the Ndvi anomalies. Thanks for providing the clarification.
    – Shiraz
    Aug 28, 2021 at 0:13

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.