Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

extract_covariates

Extract covariate values


Description

Extract the covariate values at relocations, or at the beginning or end of steps.

Usage

extract_covariates(x, ...)

## S3 method for class 'track_xy'
extract_covariates(x, covariates, ...)

## S3 method for class 'random_points'
extract_covariates(x, covariates, ...)

## S3 method for class 'steps_xy'
extract_covariates(x, covariates, where = "end", ...)

extract_covariates_along(x, ...)

## S3 method for class 'steps_xy'
extract_covariates_along(x, covariates, ...)

extract_covariates_var_time(x, ...)

## S3 method for class 'track_xyt'
extract_covariates_var_time(
  x,
  covariates,
  when = "any",
  max_time,
  name_covar = "time_var_covar",
  ...
)

## S3 method for class 'steps_xyt'
extract_covariates_var_time(
  x,
  covariates,
  when = "any",
  max_time,
  name_covar = "time_var_covar",
  where = "end",
  ...
)

Arguments

x

[track_xy, track_xyt, steps]
Either a track created with mk_track or track, or steps.

...

Further arguments, none implemented.

covariates

[RasterLayer,RasterStack,RasterBrick]
The (environmental) covariates. For extract_covariates_var_time the argument covariates need to have a z-column (i.e. the time stamp).

where

[character(1)="end"]{"start", "end", "both"}
For steps this determines if the covariate values should be extracted at the beginning or the end of a step. or end.

when

[character(1)="any"]{"any", "before", "after"}
Specifies for for extract_covariates_var_time whether to look before, after or in both direction (any) for the temporally closest environmental raster.

max_time

[Period(1)]
The maximum time difference between a relocation and the corresponding raster. If no rasters are within the specified max. distance NA is returned.

name_covar

[character(1)="time_var_covar"]
The name of the new column.

Details

extract_covariates_along extracts the covariates along a straight line between the start and the end point of a (random) step. It returns a list, which in most cases will have to be processed further.

Examples

data(deer)
data(sh_forest)
deer %>% extract_covariates(sh_forest)
deer %>% steps %>% extract_covariates(sh_forest)
deer %>% steps %>% extract_covariates(sh_forest, where = "start")
## Not run: 
data(deer) # relocation
data("sh_forest") # env covar

p1 <- deer %>% steps() %>% random_steps() %>%
  extract_covariates(sh_forest) %>% # extract at the endpoint
  mutate(for_path = extract_covariates_along(., sh_forest))  %>%
  # 1 = forest, lets calc the fraction of forest along the path
  mutate(for_per = purrr::map_dbl(for_path, ~ mean(. == 1)))

## End(Not run)

# Simulate some dummy data
# Hourly data for 10 days: 24 * 10
set.seed(123)
path <- data.frame(x = cumsum(rnorm(240)),
              y = cumsum(rnorm(240)),
              t = lubridate::ymd("2018-01-01") + hours(0:239))
trk <- make_track(path, x, y, t)

# dummy env data
rs <- raster::raster(xmn = -50, xmx = 50, ymn = -50, ymx = 50, res = 1)

# create dummy covars for each day
rs <- raster::stack(lapply(1:10, function(i)
  raster::setValues(rs, runif(1e4, i - 1, i))))

# Env covariates are always taken at noon
rs <- raster::setZ(rs, lubridate::ymd_hm("2018-01-01 12:00") + days(0:9))

# Allow up to 2 hours after
trk %>% extract_covariates_var_time(rs, max_time = hours(2), when = "after") %>%
  print(n = 25)
trk %>% extract_covariates_var_time(rs, max_time = hours(2), when = "before") %>%
  print(n = 25)
trk %>% extract_covariates_var_time(rs, max_time = hours(2), when = "any") %>%
  print(n = 25)

# We can use different time scales
trk %>%
  extract_covariates_var_time(
    rs, max_time = hours(2), when = "any", name_covar = "env_2h") %>%
  extract_covariates_var_time(
    rs, max_time = hours(4), when = "any", name_covar = "env_4h") %>%
  extract_covariates_var_time(
    rs, max_time = hours(6), when = "any", name_covar = "env_6h") %>%
  print(n = 25)

# We can use different time scales: after
trk %>%
  extract_covariates_var_time(
    rs, max_time = hours(2), when = "after", name_covar = "env_2h") %>%
  extract_covariates_var_time(
    rs, max_time = hours(4), when = "after", name_covar = "env_4h") %>%
  extract_covariates_var_time(
    rs, max_time = hours(6), when = "after", name_covar = "env_6h") %>%
  print(n = 25)

# We can use different time scales: before
trk %>%
  extract_covariates_var_time(
    rs, max_time = hours(2), when = "before", name_covar = "env_2h") %>%
  extract_covariates_var_time(
    rs, max_time = hours(4), when = "before", name_covar = "env_4h") %>%
  extract_covariates_var_time(
    rs, max_time = hours(6), when = "before", name_covar = "env_6h") %>%
  print(n = 25)

# The same works also for steps
trk %>%
  steps() %>%
  extract_covariates_var_time(
    rs, max_time = hours(2), when = "before", name_covar = "env_2h") %>%
  print(n = 25)

# also with start and end
trk %>%
  steps() %>%
  extract_covariates_var_time(
    rs, max_time = hours(2), when = "before", name_covar = "env_2h",
    where = "both") %>%
  print(n = 25)

amt

Animal Movement Tools

v0.1.4
GPL-3
Authors
Johannes Signer [aut, cre], Brian Smith [ctb], Bjoern Reineking [ctb], Ulrike Schlaegel [ctb], John Fieberg [ctb], Scott LaPoint [dtc]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.