---
title: "Fitting the Michaelis-Menten Model"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Fitting the Michaelis-Menten Model}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(renz)
data(ONPG)
```


## Non-linear least square 

Biochemistry is an eminently quantitative science. The study of
the relationship between variables and the determination of the parameters governing such relation, are part of the work of our discipline.  Generally, the variables of interest are not linearly related to each other. Although exist linearizing transformations, these are not without risks since they can introduce significant biases.

The existence of programming languages such as R, allows us to easily and reliably address the non-linear fit of biochemical models. The function *dir.MM()* from the *renz* package carry out the non-linear least square fitting of kinetic data to the Michaelis-Menten equation.


\begin{equation}
\tag{1}
v = V_{max} \frac{[S]}{K_m + [S]}
\end{equation}

Regression analysis is a set of statistical processes for estimating the relationships between a dependent variable (in our case initial rate) and one or more independent variables (in our case the substrate concentration). The method least squares computes the unique line (or hyperplane) that minimizes the sum of squared differences between the true data and that line (see Figure).

```{r echo=FALSE}
So <- c(0, 2.5, 5.0, 10.0, 15.0, 20.0)
vo <- c(0, 0.25, 0.63, 0.62, 0.82, 0.8)
s <- seq(0, 20, 0.1)
v <- 1 * s / (4 + s)
v_ <- 1 * So / (4 + So)
# v <- c(0.024, 0.036, 0.053, 0.060, 0.064)
plot(s, v, ylim = c(0,1), ty = "l", xlab = "[S] (mM)", ylab = "mM/min")
points(So, vo)
segments(2.5, 0.25, 2.5, v_[2], col = "blue")
segments(5, 0.63, 5, v_[3], col = "blue")
segments(10, 0.62, 10, v_[4], col = "blue")
segments(15, 0.82, 15, v_[5], col = "blue")
segments(20, 0.8, 20, v_[6], col = "blue")
```



\begin{equation}
\tag{2}
SSQ = \sum_{i=1}^n[v_i - V_{max} \frac{[S_i]}{K_m + [S]_i}]^2
\end{equation}

To find the kinetic parameters ($K_m$ and $V_{max}$) that minimize the SSQ, we will have to solve the system of equations:

\begin{equation}
\tag{3}
\frac{\partial SSQ}{\partial V_{max}} = 0 = -2\sum_{i=1}^n[v_i - V_{max} \frac{[S]_i}{K_m + [S]_i}] \frac{[S]_i}{K_m + [S]_i}
\end{equation}

\begin{equation}
\tag{4}
\frac{\partial SSQ}{\partial K_m} = 0 = -2\sum_{i=1}^n[v_i - V_{max} \frac{[S]_i}{K_m + [S]_i}] \frac{V_{max}[S]_i}{([S]_i + K_m)^2}
\end{equation}

To achieve such purpose, the function *dir.MM()* invokes numerical method well implemented into R.

## Loading some real kinetic data

We start by loading some kinetic data obtained by students during their undergraduate laboratory training. Using $\beta$-galactosidase as an enzyme model, the students assess the effect of the substrate o-nitrophenyl-$\beta$-D-galactopynaroside (ONPG) on the initial rate (doi: 10.1002/bmb.21522). The data obtained by eight different groups of students can be loaded just typing:

```{r}
data <- ONPG
library(knitr)
kable(data)
```

The first column gives the ONPG concentrations in mM, and the remaining 8 columns correspond to the initial rates. Note that while groups 1, 2, 7 and 8 decided to express their rates as $\mu$M/min, the remaining groups opted by mM/min.  This information can be confirmed by checking the attributes of data:

```{r}
attributes(data)
```

Thus, before continuing we are going to express all the rates using the same units: $\mu$M/min:

```{r}
data[ , 4:7] <- 1000 * data[ , 4:7]
```

## First thing first: scatter plot

I strongly insist to my students that when we have to analyze data, the first thing we must do is a scatter diagram, since this will give us a first impression about our data and will guide us on how to proceed with the analysis. To lead by example, we will carry out such diagrams.

The first four groups:

```{r}
oldmar <- par()$mar
oldmfrow <- par()$mfrow
par(mfrow = c(2, 2))
par(mar = c(4, 4,1,1))
for (i in 2:5){
  plot(data$ONPG, data[, i],
       ty = 'p', ylab = 'v (uM/min)', xlab = '[ONPG] (mM)')
}
par(mar = oldmar)
par(mfrow = oldmfrow)
```

The next four groups:

```{r}
oldmar <- par()$mar
oldmfrow <- par()$mfrow
par(mfrow = c(2, 2))
par(mar = c(4, 4,1,1))
for (i in 6:9){
  plot(data$ONPG, data[, i],
       ty = 'p', ylab = 'v (uM/min)', xlab = '[ONPG] (mM)')
}
par(mar = oldmar)
par(mfrow = oldmfrow)
```

In general, the data does not provide us with any surprises. That is, the relationship between the dependent variable (initial rate) and the independent variable ([ONPG]) is what we expect: hyperbolic curve. An exception is the rate obtained by group 2 when [ONPG] = 8 mM, which is clearly an "outlier". No problem! We will remove that point from further analysis to prevent it from introducing artifacts. 

```{r}
data$v2[8] <- NA
```

## Direct Michaelis-Menten fitting


Using the data from group 7 to illustrate the use of the *dir.MM()* function:

```{r}
dir.MM(data[ , c(1,8)], unit_v = "mM/min")
```
We propose to the reader, as an exercise, to compare these results with those obtained when we use data from the remaining groups.
