I want to run a linear regression (and plot out a correlation plot) in Earth Engine. To start with, I'd like to predict one image from another. So, that's one dependent variable and one independent variable (if I understand GEE's implementation properly - for some reason all the examples use a constant as a dependent variable, and I'm unclear why).
The code below (and here: https://code.earthengine.google.com/e20ba3ec3189bcc388920fbef2a03ec7) results in an error "ReduceBands reducer must have one input". I want to run this globally, and not reduce it to a region.
Any suggestions?
var era5_mon = ee.ImageCollection("ECMWF/ERA5/MONTHLY"),
era5_mon_land = ee.ImageCollection("ECMWF/ERA5_LAND/MONTHLY");
// See https://github.com/gee-community/ee-palettes for palettes
var palettes = require('users/gena/packages:palettes');
var era5_2020 = era5_mon_land.filter(ee.Filter.date('2020-01-01', '2020-12-31'));
var vizParams = {
min: 0,
max: 40,
palette: palettes.matplotlib.magma[7]
};
Map.addLayer(era5_2020.select('temperature_2m').mean().subtract(ee.Number(273.15)), vizParams, 'temp')
var vizParams = {
min: 0,
max: .5,
palette: palettes.colorbrewer.RdYlBu[7]
};
Map.addLayer(era5_2020.select('volumetric_soil_water_layer_1').mean(), vizParams, 'water')
var era5_regress = era5_2020.select(['temperature_2m', 'volumetric_soil_water_layer_1']).mean();
var linearRegression = era5_regress.reduce({
reducer: ee.Reducer.linearRegression({
numX: 1,
numY: 1
})
});
print('linearRegression',linearRegression)
// Convert the coefficients array to a list.
var coefList = ee.Array(linearRegression.get('coefficients')).toList();
// Extract the y-intercept and slope.
var b0 = ee.List(coefList.get(0)).get(0); // y-intercept
var b1 = ee.List(coefList.get(1)).get(0); // slope
// Extract the residuals.
var residuals = ee.Array(linearRegression.get('residuals')).toList().get(0);
// Inspect the results.
print('OLS estimates', linearRegression);
print('y-intercept:', b0);
print('Slope:', b1);
print('Residuals:', residuals);