R/sim_mtime.R

Defines functions sim_mtime

Documented in sim_mtime

#* SIMULATE MULTIPLE TIME INTERVALS

#' Simulate multiple time intervals
#'
#' Simulates survival and reproduction of a population for multiple time intervals based on TPD
#'
#' @param TPD A population's thermal-performance data.
#'
#' @param Te A vector of envirnomental temperature values. It's length determines the number of time intervals.
#'
#' @param Sp The relationship between performance and survival
#'
#' @param Rp The relationship between performance and reproduction.
#'
#' @param Mu Mutation rate
#'
#' @param Mum Mean percentage of change on a TPT due to mutation
#'
#' @param Musd SD of the percentage of change on a TPT due to mutation
#'
#' @param Degree The degree to fit the polynomial regressions relating Tm to P.
#'
#' @param Pmin The mimum performance to be considered.
#'
#' @param Samples Samples to be taken from each new individual.
#'
#' @param Error Amount of error to be introduced in offspring samples.
#'
#' @return A new TP population dataset with survivors and offspring.
#'
#' @examples
#'
#' @export

 sim_mtime <- function(TPD, Te, Sp, Rp, Mu, Mum, Musd, Pmin, Error, Samples, Degree){

# prepare the TPD original dataset

TPD$Parent <- rep(NA, nrow(TPD)) # add the Parent column

TPD$t <- rep(0, nrow(TPD)) # add the time column

# run the loop for every temperature value
for(i in 1:length(Te)){

  # select the last time interval on the population dataset
  TPD_maxt <- TPD %>% filter(t == max(t)) %>% select(ID, Tm, P, Parent)

  # simulate survival and reproduction
  TPD_next <- sim_time(TPD = TPD_maxt, Te = Te[i], Sp = Sp, Rp = Rp, Mu = Mu, Mum = Mum, Musd = Musd,
                       Degree = Degree, Pmin = Pmin, Samples = Samples, Error = Error)

  # determine the t for this iteration
  this_time <- as.numeric(max(TPD$t)) + 1

  # add the time column
  TPD_next$t <- rep(this_time, nrow(TPD_next))

  # bind the nex data to the original TPD
  TPD <- rbind(TPD, TPD_next)

}

# define return
return(TPD)

}
ggcostoya/tpcurves2 documentation built on Jan. 1, 2021, 2:19 a.m.