R/gen_tpd.R

Defines functions gen_tpd

Documented in gen_tpd

##* GENERATE THERMAL PERFORMANCE DATA

#' Generate a thermal-performance data set
#'
#' Generates a thermal-performance data set from a list of thermal performance traits
#'
#' @param Topt The thermal optimum.
#'
#' @param CTmax The critical thermal maximum. Needs to be > Topt
#'
#' @param CTmin The critical thermal minimum. Needs to be < Topt
#'
#' @param Pmax The maximal performance capacity.
#'
#' @param Pmin The mimimal performance capacity. Needs to be < Pmax
#'
#' @param Error The amount of error/noise to be introduced to generate unique data. In units of standard deviation.
#'
#' @param Samples Number of samples to be extracted.
#'
#' @param Degree Degree of the polynomial regression that will be created to extract the data.
#'
#' @return A dataset of two columns: "T" for temperature and "P" for performance.
#'
#' @examples
#'
#' data <- gen_tpd(Topt = 25, CTmax = 30, CTmin = 20, Pmax = 10, Pmin = 0, Error = 0.5, Samples = 10, Degree = 3)
#'
#' data
#'
#' plot(data)
#'
#' @export

gen_tpd <- function(Topt, CTmax, CTmin, Pmax, Pmin, Error, Samples, Degree){

  # Generate guide performance values.
  guide_P <- c(rnorm( 10, Pmin, sd = 10^-5), rnorm( 10, Pmax, sd = 10^-5), rnorm( 10, Pmin, sd = 10^-5))

  # Generate guide temperature values
  guide_T <- c(rnorm( 10, CTmin, sd = 10^-5), rnorm( 10, Topt, sd = 10^-5), rnorm( 10, CTmax, sd = 10^-5))

  # Get the polynomial regression from the guide values and specified degree
  fit <- lm(guide_P ~ poly(guide_T, degree = Degree))

  # Generate definitive temperature values
  Tm <- data.frame(guide_T = sort(sample(seq( CTmin, CTmax, by = (CTmax - CTmin) / Samples), size = Samples), decreasing = FALSE))

  # Generate uncertainties
  U <- rnorm(Samples, mean = 0, sd = Error)

  # Generate definitive perforamance values based on fit (base with Degree = 2)
  P <- predict(fit, newdata = Tm) + U

  # Only keep values above Pmin, if lower then switch them to Pmin
  P <- ifelse(P < Pmin, Pmin, P)

  # Summarize everything in a dataset and make it look pretty
  TPdata <- data.frame(Tm = Tm$guide_T, P = P)

  return(TPdata)

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