---
title: "Fit models to estimate light respiration"
author: "Joseph R. Stinziano and Christopher D. Muir"
date: "`r Sys.Date()`"
output: rmarkdown::html_document
vignette: >
 %\VignetteIndexEntry{Fit models to estimate light respiration}
 %\VignetteEngine{knitr::rmarkdown}
 %\VignetteEncoding{UTF-8}
---

## Preferred version (**photosynthesis** >= 2.1.1)

The package currently supports fitting light respiration according to the 
slope-intercept regression extension of the Laisk method (Laisk 1977; 
Walker & Ort 2015), the Kok method (Kok 1956), and the Yin modification of the 
Kok method (Yin *et al.* 2009, 2011).

### Estimate light respiration using least-squares (ls)

#### Walker & Ort (2015) model

```{r, message = FALSE}

library(broom)
library(dplyr)
library(photosynthesis)

acq_data = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv()

fit = fit_photosynthesis(
  .data = acq_data,
  .photo_fun = "r_light",
  .model = "walker_ort_2015",
  .vars = list(.A = A, .Q = Qin, .C = Ci),
  C_upper = 300,
  # Irradiance levels used in experiment
  Q_levels =  c(1500, 750, 375, 125, 100, 75, 50, 25),
)

# The 'fit' object inherits class 'lm' and many methods can be used

## Model summary:
summary(fit)

## Estimated parameters:
coef(fit)

## 95% confidence intervals:
## n.b. these confidence intervals are not correct because the regression is fit 
## sequentially. It ignores the underlying data and uncertainty in estimates of 
## slopes and intercepts with each A-C curve. Use '.method = "brms"' to properly 
## calculate uncertainty. 
confint(fit)

## Tidy summary table using 'broom::tidy()'
tidy(fit, conf.int = TRUE, conf.level = 0.95)

## Calculate residual sum-of-squares
sum(resid(fit)^2)

```
#### Yin *et al.* (2011) model

```{r, message = FALSE}

library(broom)
library(dplyr)
library(photosynthesis)

acq_data = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv()

fit = fit_photosynthesis(
  .data = acq_data,
  .photo_fun = "r_light",
  .model = "yin_etal_2011",
  .vars = list(.A = A, .phiPSII = PhiPS2, .Q = Qin),
  Q_lower = 20,
  Q_upper = 250
)

# The 'fit' object inherits class 'lm' and many methods can be used

## Model summary:
summary(fit)

## Estimated parameters:
coef(fit)

## 95% confidence intervals:
confint(fit)

## Tidy summary table using 'broom::tidy()'
tidy(fit, conf.int = TRUE, conf.level = 0.95)

## Calculate residual sum-of-squares
sum(resid(fit)^2)

```

#### Kok (1956) model

```{r, message = FALSE}

library(broom)
library(dplyr)
library(photosynthesis)

acq_data = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv()

fit = fit_photosynthesis(
  .data = acq_data,
  .photo_fun = "r_light",
  .model = "kok_1956",
  .vars = list(.A = A, .Q = Qin),
  Q_lower = 20,
  Q_upper = 150
)

# The 'fit' object inherits class 'lm' and many methods can be used

## Model summary:
summary(fit)

## Estimated parameters:
coef(fit)

## 95% confidence intervals:
confint(fit)

## Tidy summary table using 'broom::tidy()'
tidy(fit, conf.int = TRUE, conf.level = 0.95)

## Calculate residual sum-of-squares
sum(resid(fit)^2)

```

### Bayesian estimate of light respiration with **brms** and *Stan*

We added an option to estimate light respiration ($R_\mathrm{d}$) and the CO$_2$ compensation point ($C_\mathrm{i}^*$ or $\Gamma^*$) using the amazing [**brms**](https://CRAN.R-project.org/package=brms) package which fits models in [*Stan*](https://mc-stan.org/). This method has the advantage in that it properly estimates confidence intervals, but requires some post-processing. The example below shows how to do that.

#### Walker & Ort (2015) model

The code below is not run, but copy-and-paste into your console to see output.

```{r, message = FALSE, warning=FALSE, results='hide', eval = FALSE}

library(dplyr)
library(photosynthesis)
library(tidyr)

acq_data = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv()

fit = fit_photosynthesis(
  .data = acq_data,
  .photo_fun = "r_light",
  .model = "walker_ort_2015",
  .method = "brms",
  .vars = list(.A = A, .Q = Qin, .C = Ci),
  C_upper = 300,
  # Irradiance levels used in experiment
  Q_levels =  c(1500, 750, 375, 125, 100, 75, 50, 25),
  brm_options = list(chains = 1, seed = 20221118)
)

# The 'fit' object inherits class 'brmsfit' and many methods can be used

# The following code will extract parameter estimates and 95% CIs from the 
# posterior distribution
ests = fit |>
  as.data.frame() |>
  dplyr::select(
    r_xy = `cor_.Q_level__Intercept__.C`,
    sd_x = `sd_.Q_level__Intercept`,
    sd_y = `sd_.Q_level__.C`,
    mu_x = b_Intercept,
    mu_y = b_.C
  ) |>
  dplyr::mutate(
    gamma_star = -r_xy * sd_x / sd_y,
    Rd = mu_x + gamma_star * mu_y
  ) |>
  dplyr::summarise(dplyr::across(gamma_star:Rd, list(
    estimate = median,
    lower = ~quantile(., probs = 0.25),
    upper = ~quantile(., probs = 0.975)
  ))) |>
  pivot_longer(
    everything(),
    names_to = c("parameter", ".value"),
    names_pattern = "(gamma_star|Rd)_(estimate|lower|upper)"
  )

```

#### Yin *et al.* (2011) model

The code below is not run, but copy-and-paste into your console to see output.

```{r, message = FALSE, eval = FALSE}

library(photosynthesis)

acq_data = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv()

fit = fit_photosynthesis(
  .data = acq_data,
  .photo_fun = "r_light",
  .model = "yin_etal_2011",
  .method = "brms",
  .vars = list(.A = A, .phiPSII = PhiPS2, .Q = Qin),
  Q_lower = 20,
  Q_upper = 250,
  brm_options = list(chains = 1)
)

# The 'fit' object inherits class 'brmsfit' and many methods can be used
summary(fit)

```

#### Kok (1956) model

The code below is not run, but copy-and-paste into your console to see output.

```{r, message = FALSE, eval = FALSE}

library(photosynthesis)

acq_data = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv()

fit = fit_photosynthesis(
  .data = acq_data,
  .photo_fun = "r_light",
  .model = "kok_1956",
  .method = "brms",
  .vars = list(.A = A, .Q = Qin),
  Q_lower = 20,
  Q_upper = 150,
  brm_options = list(chains = 1)
)

# The 'fit' object inherits class 'brmsfit' and many methods can be used
summary(fit)

```

## Deprecated version (**photosynthesis** <= 2.1.1)

The functions to estimate light respiration in the original version (`fit_r_light_kok()`, `fit_r_light_yin()`, `fit_r_light_WalkerOrt()`) will no longer be updated and may phase it out of future releases. Use one of:

* `fit_photosynthesisi(..., .photo_fun = 'r_light', .model = 'kok_1956')` 
* `fit_photosynthesisi(..., .photo_fun = 'r_light', .model = 'walker_ort_1956')` 
* `fit_photosynthesisi(..., .photo_fun = 'r_light', .model = 'yin_etal_2011')` 

```{r, message = FALSE, warning=FALSE}
library(dplyr)
library(photosynthesis)

round_to_nearest = function(x, values) {
  sapply(x, function(y, values) {
    values[which.min(abs(y - values))]
  }, values = values)
}

# Read in your data
dat = system.file("extdata", "A_Ci_Q_data_1.csv", package = "photosynthesis") |> 
  read.csv() |>
  mutate(group = photosynthesis:::round_to_nearest(CO2_s, values = c(50, 100, 200, 300, 410, 600, 820, 1200, 1600))) |>
  rename(A_net = A, PPFD = Qin, phi_PSII = PhiPS2, C_i = Ci)

# Fit light respiration with Yin method
r_light = fit_r_light_yin(data = dat, PPFD_lower = 20, PPFD_upper = 250)

# Fit light respiration with Kok method
r_light = fit_r_light_kok(data = dat, PPFD_lower = 20, PPFD_upper = 150)

# Fit light respiration across groups with Yin method
r_lights = fit_many(
  data = dat,
  funct = fit_r_light_yin,
  group = "group",
  PPFD_lower = 20, PPFD_upper = 250,
  progress = FALSE
)

#Fit the Walker-Ort method for GammaStar and light respiration
walker_ort = fit_r_light_WalkerOrt(dat)

# View model output
summary(walker_ort[[1]])

# View graph
# walker_ort[[2]]

# View coefficients
walker_ort[[3]]

```

# References

Kok B. 1956. On the inhibition of photosynthesis by intense light. *Biochimica et Biophysica Acta* 21: 234–244

Laisk A. 1977. Kinetics of photosynthesis and photorespiration in C3 plants. Nauka, Moscow.

Walker BJ, Ort DR. 2015. Improved method for measuring the apparent CO2 photocompensation point resolves the impact of multiple internal conductances to CO2 to net gas exchange. *Plant, Cell & Environment* 38:2462-
2474

Yin X, Struik PC, Romero P, Harbinson J, Evers JB, van der Putten  PEL, Vos J. 2009. Using combined measurements of gas exchange and chlorophyll fluorescence to estimate parameters of a biochemical C3 photosynthesis model: a critical appraisal and a new integrated approach applied to leaves in a wheat (*Triticum aestivum*) canopy. *Plant, Cell & Environment* 32:448-464

Yin X, Sun Z, Struik PC, Gu J. 2011. Evaluating a new method to estimate the rate of leaf respiration in the light by analysis of combined gas exchange and chlorophyll fluorescence measurements. *Journal of Experimental Botany* 62: 3489–3499
