I am calculating the SPI from the precipitation data for 12 month period of database "UCSB-CHG/CHIRPS/DAILY". I has implemented various stages to calculate the final SPI. Here the final SPI image types shows its 'ImageCollection'. I want to visualize each spi image over the map. error "linear: Layer error: Number.multiply, argument 'right': Invalid type. Expected type: Number. Actual type: Image<[precipitation]>."
//------------------code---------
var CHIRPS = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY");
var roi =
ee.Geometry.Polygon(
[[[68.16642515954577, 25.162738638764438],
[68.16642515954577, 20.21844152685595],
[74.75822203454577, 20.21844152685595],
[74.75822203454577, 25.162738638764438]]], null, false);
var StYear = 1981;var StMonth = 1;
var EnYear = 1982;var EnMonth=5;
//-----------------CALCULATION OF 12 MONTH SPAN IMAGES---------
var IMean=ee.ImageCollection([]);
var IMean=ee.ImageCollection([]);
var ILog=ee.ImageCollection([]);
var i;
var Exit=0;
while (Exit===0){
var StDate=ee.Date.fromYMD(StYear,StMonth,1);
var tEnYear=StYear+1;
var EnDate=ee.Date.fromYMD(tEnYear,StMonth,1);//##advance the end date by 12 month
print('Date='+StYear.toString()+'-'+StMonth.toString()+' to '+tEnYear.toString()+'-'+StMonth.toString());
var I1 = ee.Image((CHIRPS).filter(ee.Filter.date(StDate,EnDate)).filterBounds(roi).select('precipitation').mean());
var I1A=I1.clip(roi);
IMean=IMean.merge(I1A);//## merge image with collection
var IL=(I1A).log();
var I1L=(IL.clip(roi)).log()
ILog=ILog.merge(I1L);
if ((tEnYear==EnYear) && (StMonth==EnMonth)){
Exit=1;}
else{
Exit=0;
StMonth=StMonth+1;
if(StMonth>12){
StYear=StYear+1;
StMonth=1;
}
}
//print((i))
}
print('Mean Image Collection=',IMean,IMean.name())
//calculate mean of mean images and sum of log
var MSize=IMean.size();
var N=MSize.getInfo();
print('Size ImageCollection Mean=',N)
//--- Calculate Mean of all Monthly images
var FinalMean=ee.ImageCollection(IMean).mean();//8.86, J4
print('Mean of Images Calculated, ',FinalMean,FinalMean.name());
// Calculate Log of the sum of images
var LogofMean=(FinalMean).log();
print('Log of mean image calculated, ',LogofMean,LogofMean.name())
//-- sum of all log images
var SumofLog=ee.ImageCollection(ILog).sum();//717.33
print('Sum of log of individual calculation calculated, ',SumofLog,SumofLog.name())
//---calculate A
var A=ee.Image(LogofMean.subtract(SumofLog.divide(N)));//A
print('A Calculated,',A.name())
//#calculate alpha, beta
var s1=ee.Image((ee.Number(1).divide(ee.Number(4).multiply(A))))
var s2=ee.Image((ee.Number(1).add((ee.Number(1).add(A.multiply(4).divide(3))).sqrt())))
var alpha=ee.Image(s1.multiply(s2))
print('alpha calculated, ',alpha.name())
var beta=FinalMean.divide(alpha)
print('beta calculated, ',beta.name())
// calculate Gamma CDF
var z=FinalMean.divide(beta)
var GammaCDF=ee.Image(ee.Number(z).gammainc(alpha))
var SPI=ee.Image(ee.Number(-1).multiply(((ee.Number(2).sqrt())
.multiply((ee.Number(2).multiply(GammaCDF)).erfcInv()))));
var SPI2=ee.ImageCollection(SPI.copyProperties());
print("SPI Calculated=",SPI2.name())//to print object type
var visParams = {
//"bands":["precipitation"],
"min":0,
"max":5,
"palette":[ "FF595E","FF924C","FFCA3A","C5CA30","8AC926",
"52A675","1982C4","2E75B8","4267AC","6A4C93"]};
//var img1 = ee.Image((CHIRPS).filter(ee.Filter.date(StDate,EnDate)).filterBounds(roi).select('precipitation').mean());
var SPISlice = ee.Image(SPI2.first());
print('SPLICE Type= ',SPISlice.name())
Map.centerObject(roi,6);
Map.addLayer(SPISlice, visParams, 'linear')