Estimating movement preferences using Step Selection Analysis

library(data.table)
library(glue)
source("R/fun_read_config.R")
source("R/fun_dist_disp.R")
source("R/fun_link_landscape.R")
source("R/fun_custom_ssf_steps.R")

Prepare data

Read paths

paths = list.dirs(path = "data", recursive = F)
paths = paths[grep("sim", paths)]

# only foragers and obligate
paths = paths[grep("exploit|obligate", paths)]
mean_cue = function(l_land, layer = 4, value_name) {
  le_ = lapply(l_land, read_landscape, layer = 4, value_name = "variable")
  le_ = rbindlist(le_)
  le_ = le_[, list(
    mean = mean(variable)
  ), by = c("x", "y")]
  setnames(le_, "mean", sprintf("mean_%s", value_name))
  le_
}
for (path in paths) {

  message(
    glue("processing {path}")
  )

  # which data to read
  lt_data = glue("{path}/00250_pos.csv")
  data = fread(lt_data)

  param = read_config(path)

  # thin data to every tenth step
  data = data[t %% 10 == 0,]

  # nest by id and params
  data = data[, list(
    steps = list(.SD)
  ), by = c("id")]

  # selec SSF paths
  data[, steps := lapply(steps, prep_path_ssf, chebyshev_distance = 10,
                         n_samples = 8)]

  # bind list
  data = data[, unlist(steps, recursive = F), by = c("id")]

  # set x and y names for linking env
  setnames(data, old = c("x_", "y_"), new = c("x", "y"))

  # over list, read and link landscape
  land_files = list.files(path, pattern = "t0", full.names = T)
  names(land_files) = names(data)

  prod = "data/data_parameters/kernels32.png"
  prod = read_landscape(prod, layer = 1, value_name = "cell_r")

  # read mean value
  items = mean_cue(land_files, layer = 4, value_name = "items")
  agents_h = mean_cue(land_files, layer = 2, value_name = "handlers")
  agents_nh = mean_cue(land_files, layer = c(1, 3), value_name = "nonhand")

  # link all
  land_data = Reduce(merge, x = list(items, agents_h, agents_nh, prod))

  # link land to data
  data = merge(data, land_data, by = c("x", "y"))

  # save to file
  if (!dir.exists("data/ssf_data")) {
    dir.create("data/ssf_data")
  } else {
    data[, names(param) := list(param["growth"], param["type"], param["rep_"])]
    fwrite(
      data,
      glue("data/ssf_data/data_ssf_\\
           {param['growth']}_{param['type']}_{param['rep_']}.csv")
    )
  }

  rm(data)

}


pratikunterwegs/process-pattern documentation built on Dec. 22, 2021, 9:50 a.m.