Convert from spatialpixeldataframe to estUD

241 Views Asked by At

In short: I have a spatialpixeldataframe of a utilization distribution of an animal across a grid (udspdf) which I need to convert to estUD-class.

Background to how I got there: After calculating the KDE of a single animal, I then need to convert the estUD-class object to a spatialpixeldataframe in order to remove non-habitat cells, and rescale the utilisation distribution of the habitat-cells to 1. I then need to convert the spatialpixeldataframe back to an estUD class file so that I can run kerneloverlaphr() on it.

I have the following code, which converts it to an estUDm-class. But I need it in estUD-class, as there is only one animal.

re <- lapply(1:ncol(udspdf), function(i) {
  so <- new("estUD", udspdf[,i])
  so@h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
  so@vol <- FALSE
  return(so)
})

names(re) <- names(udspdf)
class(re) <- "estUDm"
image(re)

If I just change

class(re) <- "estUD"

This seems to work, but then I can see there is an issue because

image(re)

renders the following error: Error in is(x, "GridTopology") : trying to get slot "grid" from an object (class "estUD") that is not an S4 object

I am sorry I do not know how to provide a reproducible example for such an example as the data is quite complex. I hope that a general code exists.

Any pointers appreciated!

> str(re)
List of 1
 $ ud:Formal class 'estUD' [package "adehabitatHR"] with 9 slots
  .. ..@ h          :List of 2
  .. .. ..$ h   : num 0
  .. .. ..$ meth: chr "specified"
  .. ..@ vol        : logi FALSE
  .. ..@ data       :'data.frame':  4400000 obs. of  1 variable:
  .. .. ..$ ud: num [1:4400000] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..@ coords.nrs : num(0) 
  .. ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. .. ..@ cellcentre.offset: Named num [1:2] -70 -60
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
  .. .. .. ..@ cellsize         : Named num [1:2] 0.01 0.01
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
  .. .. .. ..@ cells.dim        : Named int [1:2] 2000 2200
  .. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
  .. ..@ grid.index : int [1:4400000] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..@ coords     : num [1:4400000, 1:2] -70 -70 -70 -70 -70 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : NULL
  .. .. .. ..$ : chr [1:2] "Var2" "Var1"
  .. ..@ bbox       : num [1:2, 1:2] -70 -60 -50 -38
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "Var2" "Var1"
  .. .. .. ..$ : chr [1:2] "min" "max"
  .. ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
 - attr(*, "class")= chr "estUDm"

my code for kerneloverlaphr(), which works with the outputs stright from kernelUD() (estUD-class) is as follows:

NWI15b, BCI15b and BCI15i are my individual animals


    library(adehabitatHR)
    
    # convert list of KDEs (estUDs) to class estUDm
    tot <- list(NWI15b=NWI15b, BCI15b=BCI15b, BCI15i=BCI15i)
    class(tot) <- "estUDm"
    
    #calculate overlap using kerneloverlaphr
    kerneloverlaphr(tot, method = c("BA"), percent = 95, conditional = FALSE)
dput(head(as.data.frame.estUD(re), n = 10))
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'head': no method or default for coercing “estUDm” to “SpatialPixelsDataFrame”
> unique(re@data$ud)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'unique': trying to get slot "data" from an object (class "estUDm") that is not an S4 object 

structure of object fed to kernelUD


    str(track_sp)
    
    Formal class 'SpatialPoints' [package "sp"] with 3 slots
      ..@ coords     : num [1:9790, 1:2] -59.2 -59.2 -59.2 -59.2 -59.2 ...
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : chr [1:9790] "1" "2" "3" "4" ...
      .. .. ..$ : chr [1:2] "x" "y"
      ..@ bbox       : num [1:2, 1:2] -65.8 -55.6 -56.1 -52.2
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : chr [1:2] "x" "y"
      .. .. ..$ : chr [1:2] "min" "max"
      ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
      .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"

1

There are 1 best solutions below

0
On

Where we have

re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so@h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so@vol <- FALSE
return(so)
})

> names(re) <- names(udspdf)
> class(re) <- "estUDm"
> image(re)

We then need to simply add this:

colonyA <- re[[1]]
#and then do the same after running the code for the next colony
colonyB<- re[[1]]