0

I am trying to compare the outputs of species distribution models. My current code uses the following function to calculate TSS scores.

TSS_Score<-function(TrueBinary, Raster) {

  Raster[]<-as.numeric(Raster[] > 0.5)

  ConfusionMatrix<-crosstab(TrueBinary,Raster)

  a<-ConfusionMatrix[2,2]

  b<-ConfusionMatrix[2,1]

  c<-ConfusionMatrix[1,2]

  d<-ConfusionMatrix[1,1]

  Sensitivity<-a/(a+c)

  Specificity<-d/(b+d)

  TSS<-Sensitivity+Specificity-1

  return(TSS)
}

This code works but I need a raster as the model output for it to work. I have run a ppm model from spatstat and then used the predict.ppm function. However the output of predict.ppm is an image class.

Is there anyway to get a Raster as a predict output?

Or to convert the image into a raster?

EDIT: Here is the code for my ppm model. The covariates are images files as the function doesn't seem to take rasters?

PPMmodel<-ppm(PointsPPP, ~slope, covariates = list(slope =

AMtemp,MDRtemp,Isotherm,AMprecip,SeasonPrecip, TreeCover50))

predictPPM<-predict(PPMmodel)

predictPPM

real-valued pixel image

128 x 128 pixel array (ny, nx)

enclosing rectangle: [92.192, 141.01] x [-10.999, 28.549] units

2
  • The output of predict.ppm can be all sorts of things - could you show us what yours is? Generally you can turn spatstat image classes into matrices with as.matrix and get the coordinates from the object too, which is sufficient to create raster classes. – Spacedman Feb 25 '17 at 8:56
  • @Spacedman I have added the modeling code to the question. – mlfp88 Feb 25 '17 at 17:03
0

Here's something from example(predict.ppm) that looks like your object:

> trend
real-valued pixel image
32 x 32 pixel array (ny, nx)
enclosing rectangle: [0, 1] x [0, 1] units

and here is what happens when you call raster from the raster package on it:

> raster(trend)
class       : RasterLayer 
dimensions  : 32, 32, 1024  (nrow, ncol, ncell)
resolution  : 0.03125, 0.03125  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       : layer 
values      : 61.62107, 107.6241  (min, max)

which was a very pleasant surprise.

1
  • Interesting...I wouldn't expect that at all....Thanks for looking at that! Saving that as .grd file will solve my problem! Thank you! – mlfp88 Feb 26 '17 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.