[AniMov] kNNCH code questions?

Sander Oom slist at oomvanlieshout.net
Wed Jan 19 11:38:31 CET 2005

Dear Clement,

A while ago we exchanged emails about the kNNHC function from Wayne 
Getz. I have copied the relevant bit at the bottom. I am diving into the 
functions now to do some preliminary analysis of my sable movements, 
using the following code:

x <- rnorm(100)
y <- rnorm(100)

XYnnch <- data.frame(X=x, Y=y)

aa1<-NNCH(XYnnch, k=5)
aa2<-NNCH(XYnnch, k=10)
aa3<-NNCH(XYnnch, k=15)
par(mfrow=c(2,2)) # a 2 by 2 matrix of plots
plot(aa1, add.points=FALSE)
plot(aa2, add.points=FALSE)
plot(aa3, add.points=FALSE)

Problems, suggestions and questions:
1) I would like to get insight into the data by plotting several plots 
in a single graph, but it won't work (had the same problem with plots 
from hexbin package as well). Maybe I am missing something basic about R 
2) The method is indeed rather slow (for big data sets), but I can just 
make myself some coffee.
3) I would like to save the kNNCH polygons to a GIS file. Are the 
polygons available somewhere? Preferably I would be able to save 
individual layers for each k, or the polygons would have the k level as 
one of their attributes. This would allow for powerful GIS analysis 
later on.
4) I assume that the NNCH.area function puts out various percentages of 
the total area covered by all polygons in the kNNCH. Would it be 
possible to provide the area covered by each individual level of k? I 
think there might be some ecological relevance in the the way area 
increases with increasing k! Following Figure 3 in Getz et al 2004 
Ecography 27:489.
Alternatively I could write a loop for each k and store the total area, 
but than I recalculate all the previous k's each time as well. With the 
method being slow, that is quite a penalty.
5) I also think there will be ecological relevance in the changes in 
home range size through time. Thus I have been trying to run the NNCH 
analysis in a moving window function. The aim is to end up with a 
dataframe with the window ID, the associated NNCH area, and the mean 
'date' of the moving window, so I can plot NNCH area against time. 
However, hacking other people's code is risky if you have no clue what 
you are hacking. Can you help me out with this (maybe you know of a 
better moving window function. I came across this one on the R list):

movingWindowNNHC <- function(data, width, k, units) {
    mCall = match.call()
    mCall$width = NULL
    mCall[[1]] = as.name("lm")
    mCall$x = mCall$y = TRUE
    bigfit = eval(mCall, parent.frame())
    ncoef = length(coef(bigfit))
    nr = nrow(data)
    width = as.integer(width)[1]
    stopifnot(width >= ncoef, width <= nr)
    y = bigfit$y
    x = bigfit$x
    terms = bigfit$terms
    inds = embed(seq(nr), width)[, rev(seq(width))]
    multiNNCH <- lapply(seq(nrow(inds)),
                     function(st) {
                         aa1<-NNCH(data, k=k, units=units)
                         areaNNCH <- NNCH.area(aa1, percent=100)

# parameters: data, width of window, k, units
test <- movingWindowNNHC(XYnnch, 5, 5, "m")

I'm sure I will come up with more later on, but this is it for now!

Thanks for any help and suggestions,



> 3) I met Wayne Getz earlier this year. His new home range method looks 
> very promising. It should fit in very well with AdeHabitat, as it is 
> spatially explicit. Problem is to get it programmed into R. Please 
> have a look at his paper! What do you think?
> http://www.cnr.berkeley.edu/%7Egetz/Reprints04/Getz&WilmersEcography04. 
> <http://www.cnr.berkeley.edu/%7Egetz/Reprints04/Getz%26WilmersEcography04.pdf>pdf 
> <http://www.cnr.berkeley.edu/%7Egetz/Reprints04/Getz%26WilmersEcography04.pdf>

I have heard from this method, and I find it very interesting. Many 
biologists of Lyon working with Wayne Getz
have pointed out its attractive characteristics to me. Other studies are 
of course needed on this method,
especially concerning the effect of the number of relocations on the 
estimates and the choice of the
number of neighbours to take into account in the estimation. But I agree 
with you that such a method
would fit well in adehabitat, at least as an exploratory tool of the 
features of a point pattern (see the good
results on the data set chamois of the package adehabitat, below).
I have programed it some time ago to investigate its properties, but I 
never took the time to make the functions general
enough to include it in the package, i.e. several questions are still 
unresolved (still very slow in R, how to use it
concretely? should home ranges be stored as objects of class "area" ? 
which functions are needed to deal
with the estimations ? etc.).
And above all, I never took the time to do the longer part of the work 
for a non-english speaker, when
writing a function for a package: write the help page to describe the 
use and the principle of the method.
Please find attached the R file containing the code of the functions (it 
needs the package gpclib) as well as an example
of use of the functions with the data set "chamois" of the package.
Can you have a look at the attached files and tell me your opinion on it ?


Clément Calenge

More information about the AniMov mailing list