Kevin
Kevin

Reputation: 143

Why does R scale{raster} function give a different output than scaling the same raster in PythonWin?

I've used both R and PythonWin to spatially apply a statistical model. I created the same prediction raster in both programs and the outputs are different (not crazy different but different). I'm wondering if the difference between the two prediction rasters could be due to a scaling issue (I had to scale the values in my rasters because I scaled the variables in the model I'm applying)?

I have large raster files that are 23546 columns x 9157 rows, 30 x 30 cell size, and a floating pixel type. If I scale the values of a raster in R using scale{raster}, the output has a mean of 7.9e-17 and stdev 1.

inputras<-raster(file)
outras1<-scale(inputras)
cellStats(outras1, mean)
# returns 7.874414e-17
cellStats(outras1, sd)
# returns 1

If I scale the values of the same raster by subtracting the mean and dividing by the standard deviation I get the same output. No surprise.

X<-cellStats(inputras, mean)
Y<-cellStats(inputras, sd)
outras2<-(inputras-X)/Y
cellStats(outras2, mean)
# returns 7.874414e-17
cellStats(outras2, sd)
#returns 1

If I do this in PythonWin using arcpy.GetRasterProperties for the same raster I get a different output.

inputras=arcpy.Raster(file)
X=float(arcpy.GetRasterProperties_management (inputras, "MEAN").getOutput(0))
Y=float(arcpy.GetRasterProperties_management (inputras, "STD").getOutput(0))
outras=(inputras-X)/Y
A=arcpy.GetRasterProperties_management (outras, "MEAN")
B=arcpy.GetRasterProperties_management (outras, "STD")
print A, B
#returns a mean of -6.8e-8 and a stdev of 0.999

I should maybe point out that the min and max are the same for all outputs.

Why are the means different for the output from R vs. PythonWin while using (to my knowledge) the same method of scaling values? Thanks in advance for any thoughts you might have on the matter.

Upvotes: 0

Views: 457

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47481

Did you check whether

X<-cellStats(inputras, mean)
Y<-cellStats(inputras, sd)

returns the same as

X=float(arcpy.GetRasterProperties_management (inputras, "MEAN").getOutput(0))
Y=float(arcpy.GetRasterProperties_management (inputras, "STD").getOutput(0))

Upvotes: 0

Related Questions