Step Selection Analysis

library(data.table)
library(glue)

library(amt)
paths = list.files(path = "data/ssf_data", full.names = T)

# only foragers and obligate
paths = paths[grep("exploit|obligate", paths)]

Run SSF fits

purrr::walk(paths, function(file) {

  data = fread(file)

  data = data[, list(
    steps = list(.SD)
  ), by = c("id", "growth", "type", "rep_")]

  ssf_fit = lapply(data$steps, function(df) {
    amt::fit_ssf(
      data = df,
      method = "approximate",
      formula = case_ ~ cell_r + strata(step_id_)
    )
  })

  data[, ssf_fit := lapply(ssf_fit, function(ssf_fit){
    broom::tidy(ssf_fit$model)
  })
  ]
  data[, steps := NULL]
  # extract weights and save
  data = data[, unlist(ssf_fit, recursive = F),
       by = c("id", "growth", "type", "rep_")]
  rm(ssf_fit); gc()

  message(
    glue(
      "ssf fitted: {unique(data$type)} {unique(data$growth)}\\
        {unique(data$rep_)}"
    )
  )

  fwrite(data,
         file = glue("data/ssf_fits/ssf_fit_{unique(data$type)}_\\
         {unique(data$growth)}_{unique(data$rep_)}.csv")
  )

})


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