knitr::opts_chunk$set(echo = TRUE)
devtools::load_all()

The goal of this vignette is to illustrate the logic behind thermal-performance data (TPD) generation in the context of this project. The explanation will be developed through steps starting with:

Step 1: Generating base t and p points

First we use the function gen_base_tpd to generate a vector of basic t and a vector p values starting from the set TPTs considered in this project, which are: topt, tb, skw, ctmin, ctmax, pmax and pmin. For further detail on each please check the Analyze TPD vignette. As an example for this case, we set the value for these TPTs as shown below and with them we generate a TPD of base points. below we show the values and how do they look when plotted (dashed lines indicate the values of corresponding TPTs)

# Generating basic t and p points 
base_tpd <- gen_base_tpd(topt = 30, tb = 3, skw = -1, ctmin = 20, ctmax = 35, 
                         pmax = 10, pmin = 0.25)
base_tpd
p80 <- 2.5 + (10 - 2.5)*0.8
ctmax80 <- 30 + 3/2 - 1/2
ctmin80 <- 30 - 3/2 - 1/2
plot(base_tpd, type ="p", pch = 19, cex = 1.25, xlab = "Temperature", ylab = "Performance", ylim = c(0,max(base_tpd$p)))
segments(x0 = 30, y0 = 0, x1 = 30, y1 = 10, col = "grey", lty = 2, lwd = 2)
segments(x0 = 0, y0 = 10, x1 = 30, y1 = 10, col = "grey", lty = 2, lwd = 2)
segments(x0 = 20, y0 = 0, x1 = 20, y1 = 2.5, col = "grey", lty = 2, lwd = 2)
segments(x0 = 35, y0 = 0, x1 = 35, y1 = 2.5, col = "grey", lty = 2, lwd = 2)
segments(x0 = 0, y0 = 2.5, x1 = 35, y1 = 2.5, col = "grey", lty = 2, lwd = 2)
segments(x0 = ctmin80, y0 = p80, x1 = ctmax80, y1 = p80, col = "grey", lty = 2, lwd = 2.5)
lines(base_tpd, type = "p", pch = 19, cex = 1.5)

Step 2: Fit a TPM using the base points

Next, we model the relationship between t and p using a TPM as presented in the Analyze TPD vignette. We will use the function fit_tpd to evaluate between multiple possible TPM and see which one fits the data best. Once the model is found, we will obtain the TPM AIC score, the TPM name and the parameter estimates using a nls method.

fit_base_tpd <- fit_tpd(base_tpd)
fit_base_tpd
fit_base_tpd %>% select(results) %>% unnest(cols = c(results))

Step 3: Draw a TPC from the TPM

From the parameter estimates obtained and the model we draw a predicted TPC for the base points.

tpc_base_tpd <- gen_tpc(fit_base_tpd)
plot(base_tpd, type ="p", pch = 19, cex = 1.5, xlab = "Temperature", ylab = "Performance", ylim = c(0, max(base_tpd$p)))
lines(tpc_base_tpd, lwd = 2, col = "grey")
lines(base_tpd, type = "p", pch = 19, cex = 1.5)

Step 4: Generate random TPD from the TPC

To generate the final TPD from the TPC we will use the function gen_tpd. This function takes a TPC dataset as an argument and goes through the following sub-steps:

In the plot below we obtain 10 samples from the TPC fitted from the original base points. The dashed lines represent the amount of error introduced, which in this case was error = 1.

tpc <- tpc_base_tpd %>% filter(t > 20 & t < 35) %>% filter(p > 10*0.25) # filter points of the TPC 
tpc <- tpc[1:10*(nrow(tpc)/10),] # sample 10 points
tpd <- tpc 
tpd$p <- tpd$p + rnorm(10, 0, 1) # add error 
plot(base_tpd, type = "p", pch = 19, cex = 1.5, ylim = c(0,max(tpd$p,base_tpd$p)), 
     xlab = "Temperature", ylab = "Performance")
lines(tpc_base_tpd, lwd = 2, col = "grey")
segments(x0 = c(tpd$t), y0 = c(tpc$p), y1 = c(tpd$p), lty = 2)
lines(tpd, type = "p", pch = 19, col = "red", cex = 1.25)
lines(tpc, type = "p", pch = 19, col = "grey", cex = 1.25)
legend(x = min(base_tpd$t), y = max(tpd$p,base_tpd$p), 
       legend = c("Base TP points", "Sampled points from TPC","Generated TPD"), col = c("black", "grey", "red"), pch = 19, cex = 0.85, box.lty = 0)

Final Result

The resulting TPD, and it's resulting TPC, differs slightly from the original points and if Error > 0, the result will be different every time it is run

fit_gen_tpd <- fit_tpd(tpd)
tpc_gen_tpd <- gen_tpc(fit_gen_tpd)
plot(base_tpd, type = "p", pch = 19, cex = 1.5, ylim = c(0,max(tpd$p,base_tpd$p)), col = "grey", 
     xlab = "Temperature", ylab = "Performance")
lines(tpc_base_tpd, lwd = 2, col = "lightgrey")
lines(tpc_gen_tpd, lwd = 2)
lines(tpd, type = "p", pch = 19, col = "red", cex = 1.25)

Extra: Generate a Population's TPD

We use the gen_pop_tpd function to replicate the data generating process for multiple individuals to generate a population. Below is an example of a small population (n = 5) generated using the same TPTs as in the original example. Notice that each individual has a unique id, which we generate with the function gen_id. Also important to point out the variation among individuals caused by the insertion of an error = 0.75.

# Population's TPTS
pop_tpts <- tibble(tpt = c("topt", "tb", "skw", "ctmin", "ctmax", "pmax", "pmin"),
               value = c(30, 3, -1, 20, 35, 10, 0.1))

# Generate population data
pop_tpd <- gen_pop_tpd(n = 5, tpts = pop_tpts, samples = 10, error = 0.75)
plot(pop_tpd$t, pop_tpd$p, col=pop_tpd$id, pch = 19, xlab = "Temperature", ylab = "Performance", ylim = c(0,max(pop_tpd$p)))
legend(min(pop_tpd$t),max(pop_tpd$p),unique(pop_tpd$id),col=1:length(pop_tpd$id),pch=19, bty = "n", cex = 0.85)


ggcostoya/limon documentation built on April 27, 2021, 10:09 p.m.