R/get_tpts.R

Defines functions get_tpts

Documented in get_tpts

## * GET THERMAL PERFORMANCE TRAITS

#' Get thermal performance traits
#'
#' Gets the thermal performance traits from a thermal performance curve
#'
#' @param Tm A vector of temperature values
#'
#' @param P A vector of performance values
#'
#' @param Degree Degree of the polynomial regression that will be used to extract the data.
#'
#' @param Pmin The minimal thermal performance. From it CTmax & CTmin will be determined
#'
#' @return A datset of two columns: "trait" with trait ids and "value" for each trait
#'
#' @examples
#'
#'@export

 get_tpts <- function(Tm, P, Degree, Pmin){

   # Get the tpc
   C <- get_tpc(Tm, P, Degree, Pmin)

  # Select Topt
  Topt <- C[which.max(C$P),]$Tm

  # Select Pmax
  Pmax <- C[which.max(C$P),]$P

  # Select CTmax
  CTmax <- C %>% filter(Tm >= Topt) %>% filter(P == Closest(P, min(P, na.rm = T))) %>% select(Tm) %>% as.numeric()

  # Select CTmin
  CTmin <- C %>% filter(Tm <= Topt) %>% filter(P == Closest(P, min(P, na.rm = T))) %>% select(Tm) %>% as.numeric()
  # important >= / <= in both cases because there can be no values above or below Topt in some cases

  # Get Tbr
  Tbr <- CTmax - CTmin

  # Summary dataset
  TPtraits <- data.frame(trait = c("Topt","CTmax","CTmin","Tbr","Pmax"), value = c(Topt, CTmax, CTmin, Tbr, Pmax))

  return(TPtraits)

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