I am attempting to create a map of R2 values illustrating the correlation between radiance and NO2 concentrations in Asia. Each pixel on the map should have its own R2.
As the first step of this process, I tried to find the residuals of each pixel by applying ee.Reducer.linearRegression() to a collection of 5 images. It seems like each pixel in the resulting image should have 5 residuals associated with it, since each image would be a separate data point, but there is only one residual given. Why is this the case? How is this single residual representative of all 5 images?
var scale = 10000
var asia = ["Sri Lanka"]//,"Bangladesh","India","Afghanistan","Pakistan","Bhutan","Nepal","Cambodia","Indonesia","Laos","Malaysia","Burma","Philippines","Singapore","Thailand","Vietnam"]
var dates = ["2018","2019","2020","2021","2022","2023"]
var counter = 0
while(counter < asia.length){
var years = []
var countryName = asia[counter]
// shapefile of national borders, filtered to just our region of interest
var countries = ee.FeatureCollection("USDOS/LSIB/2017").filter(ee.Filter.eq('COUNTRY_NA', countryName))
var counter2 = 0
while(counter2 < dates.length-1){
// grab all the images for one year
var startDate = dates[counter2]
var endDate = dates[counter2+1]
var rad = ee.ImageCollection('NOAA/VIIRS/DNB/MONTHLY_V1/VCMSLCFG')
.select('avg_rad')
.filterDate(startDate, endDate)
.filterBounds(countries)
// reduce to image
var radImage = rad.reduce(ee.Reducer.median())
var no2 = ee.ImageCollection("COPERNICUS/S5P/NRTI/L3_NO2")
.select('tropospheric_NO2_column_number_density')
.filterDate(startDate, endDate)
.filterBounds(countries)
// reduce to image
var no2Image = no2.reduce(ee.Reducer.median())
// resample both images to be the same resolution
var radProj = radImage.reproject({crs: "EPSG:4326",scale: scale})
var no2Proj = no2Image.reproject({crs: "EPSG:4326",scale: scale})
// addBands
var combo = radProj.addBands(no2Proj)
years.push(combo)
counter2 += 1
}
print(countryName)
// create image collection from yearly images
var allTime = ee.ImageCollection(years)
// perform linear regression
var linear = allTime.reduce(ee.Reducer.linearRegression(1,1)).clip(countries)
// coefficients irrelevant; select residuals
var residuals = linear.select("residuals")
print(residuals)
Map.addLayer(residuals)
counter += 1
}