[R-sig-Geo] combining UD's in kernel denisty analysis
Clement Calenge
clement.calenge at oncfs.gouv.fr
Sun Jul 8 15:51:15 CEST 2012
Dear Louise,
[snip]
>> data<-read.csv("three.csv")
>> library(adehabitatHR)
>> gmt<-(data$gmt)
>> data$gmt<-as.POSIXct(strptime(gmt, "%d/%m/%Y%H:%M:%S"))
>> tr=as.ltraj(xy, data$gmt, id, burst = data$Burst, typeII = TRUE, slsp ="remove")
>> lik<-liker(tr, sig2=1, rangesig1=c(10, 100))
>> hrBB<- kernelbb(tr, sig1 = 6, sig2 = 1, grid = 100, byburst=TRUE)
>> hrBB
>> image(hrBB)
>> plot(getverticeshr(hrBB, 95), add=TRUE, lwd=2)
>> plot(getverticeshr(hrBB, 50), add=TRUE, lwd=2)
> Error in plot(getverticeshr(hrBB, 50), add = TRUE, lwd = 2) :
> error in evaluating the argument 'x' in selecting a method for function 'plot': Error in re[[i]] : subscript out of bounds
It is hard to understand this particular error without any reproducible
example. I don't know the origin of this particular error, but there are
other errors in your code. There are several animals/bursts in tr, so
that if you want to display the home range contour for each animal, you
should not use plot(…, add=TRUE) after image(hrBB). Rather, you should use:
par(mfrow=n2mfrow(length(hrBB)), mar=c(0,0,2,0))
lapply(1:length(hrBB), function(i) {
image(hrBB[[i]], main=names(hrBB)[i])
plot(getverticeshr(hrBB[[i]], 95), add=TRUE, lwd=2)
plot(getverticeshr(hrBB[[i]], 50), add=TRUE, lwd=2)
})
I am not sure that this will correct your error, but as indicated, it is
difficult to debug a code without any reproducible example.
[snip]
> # I then try to combine these 4 individual UD’s using the following code
>
> UDs<- lapply(1:length(hrBB), function(i) hrBB[[i]]$UD)
There is a confusion here. There is no component UD in the object of
class estUD. This kind of code would have been correct with the "old"
package adehabitat (no longer developed). The newer package adehabitatHR
uses quite different classes of objects. You should read the vignette of
adehabitatHR, by typing:
vignette("adehabitatHR")
Also, you should read documentations related to the classes of objects
of class "SpatialPixelsDataFrame" used by the package sp, on which the
package relies (the class estUD inherits from this class). Understanding
these classes may help you to write correct code.
> UDend<- liUD[[1]]
> for (i in 2:length(liUD))
> UDend<- UDend + liUD[[i]]
> nlocs<- unlist(lapply(1:length(tr), function(i) nrow(tr[[i]])))
> wi<- nlocs/sum(nlocs)
> UDt<- UDs[[1]]
> for (i in 2:length(UDs))
> UDt<- UDt+wi[i]*UDs[[i]]
>
> # And also tried:
>
> liUD<- lapply(1:length(tr), function(i) {
> hrBB[[i]]$UD * (nrow(tr[[i]]) / sum(sapply(tr, nrow)))
> })
> UDend<- liUD[[1]]
> for (i in 2:length(liUD))
> UDend<- UDend + liUD[[i]]
>
> But get this error message:
>
> Error in image.default(UDend) : argument must be matrix-like
These errors are caused by the incorrect use of hrBB[[i]]$UD above.
This code would be correct if you had used the package adehabitat
instead of adehabitatHR. For adehabitatHR, this code should work (not
tested):
UDend<- hrBB[[1]]
hrBB <- estUDm2spixdf(hrBB)
nlocs <- unlist(lapply(1:length(tr), function(i) nrow(tr[[i]])))
wi <- nlocs/sum(nlocs)
UDend[[1]] <- apply(hrBB at data, 1, function(x) sum(x*nlocs))
However, be careful that the above code averages the UDs of all bursts
and all animals. It may be sensible to modify this code so that the
average is calculated for each animal, depending on your aim.
HTH,
Clément Calenge.
--
Clément CALENGE
Cellule d'appui à l'analyse de données
Direction des Etudes et de la Recherche
Office national de la chasse et de la faune sauvage
Saint Benoist - 78610 Auffargis
tel. (33) 01.30.46.54.14
More information about the R-sig-Geo
mailing list