goldingn / pop

A Flexible Syntax for Population Dynamic Modelling :koala: :chart_with_upwards_trend:
Other
10 stars 2 forks source link

add continuous traits #36

Open goldingn opened 8 years ago

goldingn commented 8 years ago

This will enable age- or size-structured models, like IPMs.

This requires:

Initially this can just be a single trait, but possible to allow multiples (representing all combinations of bins for each trait as separate bins)

goldingn commented 8 years ago

This is the preferred formula syntax, for:

  1. moving to a new stage with a trait
(size | juvenile) ~ adult
  1. changing trait within a stage
(size | juvenile) ~ (size | juvenile)
  1. moving from a stage with a trait to one without
adult ~ (size | larvae)
  1. moving between two stages with traits
(size | adult) ~ (size | larvae)

will need to parse these cleanly, and use trait density (need a better name than that) transfuns for 1, 2, and 4. I.e. anything with a trait on the LHS (can just use the population vector for the RHS)

goldingn commented 8 years ago

Code to parse these, with or without braces:

stripBraces <- function (x) {
  if (as.character(x[[1]]) == '(') {
    x <- x[[2]]
    x <- stripBraces(x)
  }
  return (x)
}
getStage <- function (f, which = c('to', 'from')) {
  which <- match.arg(which)
  x <- switch(which,
              to = f[[2]],
              from = f[[3]])
  if (length(x) == 1) {
    stage <- as.character(x)
  } else {
    x <- stripBraces(x)
    if (length(x) == 3 &
               as.character(x[[1]]) == '|') {
      stage <- as.character(x[[3]])
      attr(stage, 'trait') <- as.character(x[[2]])
    } else {
      stop ('cannot parse transition formula')
    }
  }
  return (stage)
}