jmsigner / amt

37 stars 13 forks source link

simulating the steady-state UD for prediction maps #93

Closed kflorko closed 5 months ago

kflorko commented 1 year ago

Hello! I am trying to simulate a steady-state UD from a SSF by reproducing the code described in Signer et al. 2019, but the functions habitat_kernel(), movement_kernel(), and simulate_ud() are not in the current version of amt (0.2.1.0). Is there another way to simulate the UD?

Here is a small reproducible example showing what I'd like to do and how we get held up at the habitat_kernel(), movement_kernel(), and simulate_ud() steps:

# grab animal movement data
data("deer")

# grab environmental covariate data
data("sh_forest")

# prepare data 
data_ssf <- deer |>
  steps_by_burst() |>
  random_steps(n_control = 15) |>
  extract_covariates(sh_forest) |>
  mutate(forest = factor(forest, levels = 1:0, labels = c("forest", "non-forest")), 
         cos_ta = cos(ta_), 
         log_sl = log(sl_))

# fit model
m1 <- data_ssf |> 
  fit_clogit(case_ ~ forest*cos_ta + forest*log_sl + strata(step_id_))

# grab shape and scale
shape <- sl_distr(m1)$params$shape     # note the 2019 paper has this as: shape <- sl_shape(m1)
scale <- sl_distr(m1)$params$scale     # note the 2019 paper has this as: scale <- sl_scale(m1)

# create a habitat kernel
hk <- habitat_kernel(coef = list(forest = coef(m1["forest"]), resources = sh_forest))

# create a movement kernel
mk <- movement_kernel(scale = scale, shape = shape, template = sh_forest)

# simulate the steady-state UD
ssud <- simulate_ud(movement_kernel = mk,
                    habitat_kernel = hk,
                    start = as.numeric(data_ssf[1, c("x1_", "y1_")]), n = 1e7)

Many thanks in advance! Katie

note: cross-posted on stack overflow

jmsigner commented 1 year ago

Hi Katie,

there is new functionality to simulate in amt. Here is a brief example:

First fit a model

library(amt)
data("deer")
forest <- get_sh_forest() # this needed because of terra

# prepare data 
data_ssf <- deer |>
  steps_by_burst() |>
  random_steps(n_control = 15) |>
  extract_covariates(forest, where = "both") 

# fit model
m1 <- data_ssf |> 
  fit_clogit(case_ ~ forest_end +  forest_start:cos(ta_) + 
               forest_start:sl_ + strata(step_id_))

Start with simulations

# First generate a redistribution kernel

start <- make_start(deer[1, ]) # This is the starting location (we could sample it randomly from within the area we want to )
k1 <- redistribution_kernel(m1, map = forest, start = start)

# Now simulate a path of length 30 for 50 times. This will lead a transient UD. For a steady state UD, either increase `n` or choose many more random starting points. 
n <- 50
p1 <- replicate(n, simulate_path(k1, n = 30), simplify = FALSE) # Takes about 2 min

uds <- lapply(c(5, 30), function(i) {
  tibble(
    rep = 1:n, 
    path = map(p1, ~ dplyr::slice(.x, i))
  ) |> unnest(cols = path) |> filter(!is.na(x_)) |> 
    make_track(x_, y_) |> hr_kde(trast = forest)
})

terra::plot(hr_ud(uds[[1]]))
terra::plot(hr_ud(uds[[2]]))

Hopefully, this helps. The advantage is that the redistribution_kernel() is now dynamic and not static as it was before.

All the best, Johannes