lundi 1 novembre 2021

R package amt: How to calculate different amounts of random locations for different individuals

My data set consists of more than 50.000 locations ($ longitude and $ latitude) of 25 GPS collard animals with a date time stamp ($ acquisition_time) and some additional info (age class, sex, study area). I am working with the package amt for the first time and I am stuck trying to derive the random points for each individual within its home range.

My code so far:

library(purrr)
library(amt)
library(tidyverse)
library(dplyr) 
library(lubridate)
library(sf) 
library(sp)
library(plyr)
library(mapview)
library(maptools)
library(rgdal)

#load data
dat <- read.csv('wildcat_data_ready.csv')

#prepare data
dat$acquisition_time = as.POSIXct(dat$acquisition_time, 
                                  tz = "UTC", format = "%Y-%m-%d %H:%M:%S")

dat1 <- as_tibble(dat) %>% dplyr::select (x = longitude, y = latitude,
                  t = acquisition_time,id=animals_id, sex=animals_sex, 
                  age = animals_age_class, area = study_area_id)

#change projection to LAEA & add day and night
trk <- dat1 %>% make_track(x, y, t, id = id, sex = sex, age = age, area = area, 
                  crs = sp::CRS("+init=epsg:4326")) %>% 
                  transform_coords(sp::CRS("+init=epsg:3035")) %>% 
                  amt::time_of_day(include.crepuscule = FALSE)

#get information about time resolution of fixes
info_trk <- trk %>% summarize_sampling_rate_many(c("id"), time_unit="hour")
info_trk 

#ensure locations are recorded 6 hours apart
trk1 <- trk %>% nest(data = -"id")

trk1 <- trk1 %>% mutate(resample = map(data, function(a) {
                  a %>% amt::track_resample(rate = hours(6),
                  tolerance = minutes(20))
  }))

# calculate kde 95% home range
trk2 <- trk1 %>% mutate(hr = map(resample, function(x) {
                  x %>% hr_kde(levles = 0.95)}))

trk2 <- trk2 %>% mutate(hr1 = map(hr, function(y) {
                  y%>% hr_isopleths()
  }))

# calculate random points in individuals home range 
trk_id <- trk2 %>% mutate(random_used = map(hr1, function(z) {
                  z %>% random_points(n=100)
  }))

Here is a sample from my original data:

> dat[886:898,]
    animals_id    acquisition_time longitude latitude      projection collar_type
886          1 2011-12-27 23:59:00  7.588487 47.91844 EPSG:4326-WGS48         gps
887          1 2011-12-28 21:59:00  7.589968 47.92188 EPSG:4326-WGS48         gps
888          1 2011-12-29 19:59:00  7.586910 47.93048 EPSG:4326-WGS48         gps
889          1 2011-12-30 17:59:00  7.596179 47.92308 EPSG:4326-WGS48         gps
890          1 2011-12-31 04:59:00  7.587268 47.91989 EPSG:4326-WGS48         gps
891          1 2012-01-01 02:59:00  7.586433 47.91984 EPSG:4326-WGS48         gps
892          1 2012-01-04 07:59:00  7.612507 47.95379 EPSG:4326-WGS48         gps
893          4 2010-05-01 02:59:00  7.620057 47.99007 EPSG:4326-WGS48         gps
894          4 2010-05-01 23:59:00  7.622088 47.98821 EPSG:4326-WGS48         gps
895          4 2010-05-02 20:59:00  7.623225 47.99002 EPSG:4326-WGS48         gps
896          4 2010-05-04 00:59:00  7.630547 47.97274 EPSG:4326-WGS48         gps
897          4 2010-05-04 21:59:00  7.623501 47.98742 EPSG:4326-WGS48         gps
898          4 2010-05-08 09:59:00  7.626074 47.98340 EPSG:4326-WGS48         gps
    study_area_id animals_age_class animals_sex
886            13                 a           f
887            13                 a           f
888            13                 a           f
889            13                 a           f
890            13                 a           f
891            13                 a           f
892            13                 a           f
893            13                 a           f
894            13                 a           f
895            13                 a           f
896            13                 a           f
897            13                 a           f
898            13                 a           f

The structure of the object I use to calculate the random points with, looks like this (object trk2):

# A tibble: 65 x 5
   id    data                    resample              hr        hr1         
   <chr> <list>                  <list>                <list>    <list>      
 1 M25   <track_xyt [867 x 7]>   <track_xyt [210 x 8]> <kde [7]> <sf [1 x 4]>
 2 M23   <track_xyt [2,998 x 7]> <track_xyt [518 x 8]> <kde [7]> <sf [1 x 4]>
 3 M24   <track_xyt [868 x 7]>   <track_xyt [236 x 8]> <kde [7]> <sf [1 x 4]>
 4 M27   <track_xyt [466 x 7]>   <track_xyt [466 x 8]> <kde [7]> <sf [1 x 4]>
 5 M28   <track_xyt [676 x 7]>   <track_xyt [676 x 8]> <kde [7]> <sf [1 x 4]>
 6 M12   <track_xyt [346 x 7]>   <track_xyt [189 x 8]> <kde [7]> <sf [1 x 4]>
 7 M14   <track_xyt [297 x 7]>   <track_xyt [131 x 8]> <kde [7]> <sf [1 x 4]>
 8 M7    <track_xyt [645 x 7]>   <track_xyt [215 x 8]> <kde [7]> <sf [1 x 4]>
 9 M13   <track_xyt [395 x 7]>   <track_xyt [198 x 8]> <kde [7]> <sf [1 x 4]>
10 M41   <track_xyt [131 x 7]>   <track_xyt [64 x 8]>  <kde [7]> <sf [1 x 4]>
# ... with 55 more rows

The problem is, that I don't want 100 random points (n=100) for every individual but the amount of random points should be equal to the number of observations of the individuals. The observations are stored in the lists resample. I don't know how to change this piece of code to achieve this:

# calculate random points in individuals home range 
trk_id <- trk2 %>% mutate(random_used = map(hr1, function(z) {
                  z %>% random_points(n=100)
  }))

I appreciate every tip.




Aucun commentaire:

Enregistrer un commentaire