Skip to contents

This function is used to adjust the probability of a species to be sampled across the raster, so that the sampled frequency of occurrence of the species is closer to the observed

Usage

fr2prob(x, rprob = NULL)

Arguments

x

SpatRaster. A presence-absence raster (stack).

rprob

SpatRaster. A raster (stack) of probabilities.

Value

numeric vector

Examples

library(SESraster)
library(terra)
# load random species distributions
r <- load_ext_data()

# applying the function
fr2prob(r)
#>     global1     global2     global3     global4     global5     global6 
#>  0.04166623  1.27269835  0.56249121  0.92305917  0.92305917  0.66665556 
#>     global7 
#> 11.49856268 


f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
set.seed(510)
r10 <- rast(lapply(1:18,
                function(i, r, mn, mx){
                  app(r, function(x, t){
                    sapply(x, function(x, t){
                       x<max(t) & x>min(t)
                    }, t=t)
                  }, t=sample(seq(mn, mx), 2))
                }, r=r, mn=minmax(r)[1]+10, mx=minmax(r)[2]-10))

names(r10) <- paste("sp", 1:nlyr(r10))
fr2prob(r10)
#>      global1      global2      global3      global4      global5      global6 
#>  0.199633212  2.952713473  0.342161977  0.238975534  0.245336042  0.881976051 
#>      global7      global8      global9     global10     global11     global12 
#>  0.134940601  0.029713724  0.027189380  9.594385735  1.254864072  1.920729704 
#>     global13     global14     global15     global16     global17     global18 
#>  0.007872215 19.757159426  2.442036928  0.911637488  1.112258816  0.053726000 
# raw frequencies
unlist(terra::global(r10, function(x)sum(x, na.rm=TRUE)))
#>  global1  global2  global3  global4  global5  global6  global7  global8 
#>      767     3443     1175      889      908     2160      548      133 
#>  global9 global10 global11 global12 global13 global14 global15 global16 
#>      122     4174     2565     3031       36     4387     3270     2198 
#> global17 global18 
#>     2427      235