[R-sig-eco] Package adehabitatLT; lion telemetry data; removing intermittent 1-hr locations to create 2-hr locations

basille.web at ase-research.org basille.web at ase-research.org
Tue Nov 15 02:32:30 CET 2016


Oh well, this is actually a long-standing bug in adehabitatLT (dating from
2011 at least!). I guess you have infolocs with only one column in your
ltraj. If that's the case, before the bug gets fixed, please use the
modified 'subsample' function below.

Now, in your case, I would work first on 1h and 2h schedules separately.
Assuming you have two ltraj, one with the 1h, and one with the 2h, I would
first regularize each of them (setNA and sett0), then subsample the 1h, and
combine them (using 'c') with the 2h. The way you did it, you created NAs
every 1h in the 2h schedule, with the risk of selecting them instead of the
actual locations when you subsample...

Hope this helps!
Mathieu.


=========================================================================
subsample <- function (ltraj, dt, nlo = 1, units = c("sec", "min", "hour",
    "day"), ...)
{
    if (!inherits(ltraj, "ltraj"))
        stop("ltraj should be of class \"ltraj\"")
    if ((!is.regular(ltraj)) & (attr(ltraj, "typeII")))
        stop("ltraj should be of type I or type II regular")
    p4s <- adehabitatLT:::.checkp4(ltraj)
    if (length(nlo) == 1)
        nlo <- rep(nlo, length(ltraj))
    units <- match.arg(units)
    dt <- adehabitatLT:::.convtime(dt, units)
    dtb <- ltraj[[1]]$dt[1]
    if (dt%%dtb != 0)
        stop("dt is not a multiple of the previous time lag")
    la <- dt/dtb
    res <- lapply(1:length(ltraj), function(i) {
        x <- ltraj[[i]]
        infol <- attr(x, "infolocs")
        vec <- rep(1:la, length = nrow(x))
        x <- x[vec == nlo[i], ]
        if (!is.null(infol)) {
            infol <- infol[vec == nlo[i], , drop = FALSE]
            attr(x, "infolocs") <- infol
        }
        return(x)
    })
    class(res) <- c("ltraj", "list")
    attr(res, "typeII") <- attr(ltraj, "typeII")
    attr(res, "regular") <- is.regular(res)
    attr(res, "proj4string") <- p4s
    res <- rec(res, ...)
    return(res)
}
=========================================================================



Le 14/11/2016 à 18:20, Lisanne Petracca a écrit :
> Hi Mathieu,
> Thank you for responding to my inquiry.
> I think I am very, very close to figuring this out, thanks to your suggestion of the subsample function-- but I got an error in the final step.
> First, I have my ltraj object, in which there are 8158 bursts, each separated by a maximum of two hours (name = finaldatastep_pre_2hr).
> 
> I used setNA to create regular, hourly trajectories.
> refda <- strptime("2004-01-01 00:00", "%Y-%m-%d %H:%M", tz="Africa/Harare")finaldatastep_pre_hourly <- setNA(finaldatastep_pre_2hr, refda, 1, units = "hour")
> 
> I then used subsample to resample the ltraj object to every two hours (7200 seconds), 
> x <- rep(1, times=8158) 
> finaldatastep_final <- subsample(finaldatastep_pre_hourly, dt = 7200, nlo=x, units=c("sec"))
> but got this error message: "Error in res[, names(res) %in% which, drop = FALSE] : 
>   incorrect number of dimensions"
> Thoughts on what this error message means?  I re-checked number of bursts, and it is 8158.
> Many thanks if you can assist on this.
> Lisanne
> 
>       From: "basille.web at ase-research.org" <basille.web at ase-research.org>
>  To: Lisanne Petracca <lisanne.petracca at yahoo.com> 
> Cc: "r-sig-ecology at r-project.org" <r-sig-ecology at r-project.org>
>  Sent: Monday, November 14, 2016 3:32 PM
>  Subject: Re: [R-sig-eco] Package adehabitatLT; lion telemetry data; removing intermittent 1-hr locations to create 2-hr locations
>    
> Dear Lisanne,
> 
> You want to play around with the function 'subsample' (see the help page
> for that), assuming that you can 'isolate' the different schedules as
> different bursts (potentially same ID, but different bursts when dt = 1h or
> 2h). To achieve that, you may be able to use 'cultraj' or 'hab::subset'.
> 'hab' is a package I developed, and is available on GitHub:
> 
> https://github.com/basille/hab
> 
> Note also that there are a couple of functions for SSFs in there...
> 
> Also, adehabitatLT provides two functions to regularize trajectories:
> 'setNA' (that adds NAs for missing locations) and 'sett0' (that rounds the
> timestamps, similarly to what you already did).
> 
> Look up the vignette for more details:
> 
> https://cran.r-project.org/web/packages/adehabitatLT/vignettes/adehabitatLT.pdf
> 
> Hope this helps,
> Mathieu.
> 
> 
> Le 14/11/2016 à 11:37, Lisanne Petracca a écrit :
>> Hi all,
>>
>> This is my first time posting to this mailing list, so I apologize in advance if I fail to describe my problem adequately.
>>
>> I am working with a lion telemetry database, containing ~125,000 locations of 14 individuals.
>>
>> The issue is that the database is messy. There are different collaring schedules between and within individuals (as some lions were collared multiple times), and my goal is to extract all points that are two hours apart for a step-selection function.
>>
>> Using the package adehabitatLT, I was able to round all telemetry points to the nearest hour, and remove lion--time duplicates that may have resulted from this rounding. I first converted to ltraj class, a class suitable for analysis of movement paths. Code below (or you can skip ahead to next part):
>>
>>
>> alldatastep_pre <- as.data.frame(subset(dt, select=c("Name", "LocalTime", "lat", "long")))
>>
>> #convert time column to POSIXct object
>> time <- as.POSIXct(strptime(alldatastep_pre$LocalTime,"%Y-%m-%d %H:%M:%S"), tz="Africa/Harare")
>> time2 <- round(time, units="hours")
>> alldatastep_pre$NewTime <- as.POSIXct(strptime(time2,format="%Y-%m-%d %H:%M"), tz="Africa/Harare")
>>
>> #removing duplicates of Lion ID---Time due to rounding
>> alldatastep2_pre <- alldatastep_pre[!duplicated(paste(alldatastep_pre$Name, alldatastep_pre$NewTime)), ]
>> time <- as.POSIXct(strptime(alldatastep2_pre$NewTime,format="%Y-%m-%d %H:%M"), tz="Africa/Harare")
>>
>> #extract WGS coordinate columns from alldata
>> WGSCoords <- data.frame(X = alldatastep2_pre$long, Y = alldatastep2_pre$lat)
>> names(WGSCoords) <- c("X","Y")
>>
>> #convert it from data frame to sp object
>> coordinates(WGSCoords) <- ~ X + Y # longitude first
>>
>> #add a coordinate reference system (in this case WGS84)
>> proj4string(WGSCoords) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
>>
>> #project using spTransform
>> UTMCoords <- as.data.frame(spTransform(WGSCoords, CRS("+proj=utm +zone=35 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs")))
>> head(UTMCoords)
>>
>> #conversion to class ltraj
>> finaldatastep_pre <- as.ltraj(xy = UTMCoords[,c("X","Y")], date= time, id = alldatastep2_pre$Name)
>>
>> I then cut all trajectories such that each "burst" of points is separated by a maximum of two hours:
>>
>> #function "foo2" will return TRUE when time lag btw 2 relocs is >2 hours
>> foo2 <- function(dt) 
>>
>> {
>>   return(dt> (1*3600*2))
>> }
>>
>> #cuts ltraj object into "bursts" separated by maximum of 2 hours
>> finaldatastep_pre_2hr <- cutltraj(finaldatastep_pre, "foo2(dt)", nextr = TRUE)
>>
>> So, this is all great, BUT it leaves me with locations that are separated by one AND two hours, rather than just those locations separated by two hours, which is my goal. I am unable to figure out how to use R to convert a data frame like the one below such that ONLY points separated by two hours (dt=7200) are retained. So, essentially, in the case of two subsequent locations separated by 3600 seconds (one hour), I want to only retain the second one such that all points are separated by 7200 seconds (two hours).
>>
>> Here is a sample of my data frame:
>>
>>
>> | x | y | date | dist | dt | id |
>> | 530042.6 | 7907562 | 9/9/2011 19:00 | 563.124484 | 3600 | Bhubesi |
>> | 529550.4 | 7907288 | 9/9/2011 20:00 | 391.955558 | 3600 | Bhubesi |
>> | 529235.2 | 7907055 | 9/9/2011 21:00 | 4.425924 | 3600 | Bhubesi |
>> | 529235.2 | 7907051 | 9/9/2011 22:00 | 457.910135 | 7200 | Bhubesi |
>> | 528913.5 | 7906725 | 9/10/2011 0:00 | 2.105879 | 3600 | Bhubesi |
>> | 528911.4 | 7906725 | 9/10/2011 1:00 | 614.954651 | 7200 | Bhubesi |
>> | 529519.1 | 7906819 | 9/10/2011 3:00 | 2308.861303 | 3600 | Bhubesi |
>> | 531726.2 | 7907497 | 9/10/2011 4:00 | 764.682101 | 3600 | Bhubesi |
>> | 532321.3 | 7907017 | 9/10/2011 5:00 | 282.912376 | 3600 | Bhubesi |
>> | 532142 | 7906798 | 9/10/2011 6:00 | 705.010909 | 3600 | Bhubesi |
>>
>>
>> Any guidance would be appreciated.
>> Best,Lisanne
>>     [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-ecology mailing list
>> R-sig-ecology at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>>
> 

-- 

Mathieu Basille

basille at ufl.edu | http://ase-research.org/basille
+1 954-577-6314 | University of Florida FLREC

  « Le tout est de tout dire, et je manque de mots
  Et je manque de temps, et je manque d'audace. »
  — Paul Éluard

This message is signed to guarantee its authenticity.
For a true private correspondence, use my public key
to encrypt your messages:

  http://mathieu.basille.net/pub.asc

Learn more: http://mzl.la/1BsOGiZ



More information about the R-sig-ecology mailing list