extract_covariates: Extract covariate values

Description Usage Arguments Details Examples

View source: R/extract_covariates.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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 documentation built on Jan. 18, 2021, 5:05 p.m.