---
output:
  pdf_document:
    extra_dependencies: ["float"]
    number_sections: true
    toc: true
    toc_depth: 3
header_includes:
  -\setlength{\headheight}{22pt}%
  -\usepackage{pdflscape}
  -\usepackage{longtable}
  -\usepackage{fancyheadr}
  -\pagestyle{fancy}
  -\fancyhf{}
  -\fancyfoot[C]{Page \thepage \ of \pageref{LastPage}}
  -\usepackage{lastpage}
  -\rhead{\includegraphics[width=.15\textwidth]{`r getwd()`/logov2.png}}
params:
  FileName: NA
  newTitle: NA
  author: NA
  REP: NA
  REPlin: NA
  coeffsLin: NA
  NoP: NA
  Assay: NA
author: "Author: `r params$author`"
title: |
    | ![](logov2.png){width=1in}
    | Linear bioassay evaluation
subtitle: |
  `r params$FileName`
  
  <left> Unique time: </left> <right> `r Sys.time()`</right>
date: "`r paste(params$NoP, params$Assay)`"

---



```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)

library(knitr)
library(DT)

REP <- params$REP
REPlin <- params$REPlin
author <- params$author
coeffsLin <- params$coeffsLin

all_l <- REP$all_l
circles <- REPlin$circles
#ANOVAXLS <- REP$ANOVAXLS
SuModAB <- REPlin$SuModAB
SuModABu <- REPlin$SuModABu
LinTests <-  REPlin$LinTests
XLplotLin <- REPlin$pLin
LinPotTab <- REPlin$LinPotTab

XLdat2 <- REP$XLdat2

LinTests1 <- LinTests[,1:3]
ANOVAlin <- LinTests[,4:ncol(LinTests)]


```


\newpage

# Introduction

Bioassay potency estimation uses statistical methods to quantify the strength of a biological product or drug by comparing its response to that of a reference standard. Biological responses are inherently variable, affected by assay conditions, cell systems or organisms, and measurement noise. To control this variability, a linear regression approach is used to obtain reliable potency values. Three consecutive dilution steps showing the steepest slope are used for linear fitting.   
USP<1034> recommends calculation of standard errors of ratios of the parameters using Fieller's theorem [Finney D.J. 1978] or using the "delta" method (for a discussion about the "delta" method see [Ver Hoef 2012]). The present analysis calculated the relative potency with the "delta" method. The formula of the relative potency is in the Appendix.

# Raw data

All data used for evaluation is shown in table 1.

```{r Alll, echo=FALSE, warning=FALSE, results='asis'}

kable(XLdat2, format = "markdown", caption= "Uploaded data (test and reference) ", digits=3)

```

The linerar regression is calculated on the readout listed in table 2.

```{r Circles, echo=FALSE, warning=FALSE, results='asis'}

kable(circles, format = "markdown", caption= "Concentrations and readout used for linear regression", digits=3, row.names = F)

```



# Results

## Overall result

```{r Over_all, echo=FALSE, comment=NA, warning=NA, message=NA}

#browser()
potFlag <- 0
if (LinPotTab[1,"test_result"]==1) potFlag <- 1
AnalysisFlag <- FALSE
if (potFlag==1 | sum(LinTests$test_results)>0) AnalysisFlag <- TRUE

colFmt <- function() {
  
  outputFormat <- knitr::opts_knit$get("rmarkdown.pandoc.to")
 if(AnalysisFlag) {
    text <- paste("\\textcolor{red}{Analysis failed}",sep="")
  } else  {
    text <- paste("\\textcolor{black}{Analysis succeeded}",sep="")
  }  
  return(text)
}


```

`r colFmt()`


## Plots and ANOVA

Plots in Figure 1 show the restricted and unrestricted model, respectively.

```{r LinPlot, echo=FALSE, warning=FALSE, fig.height=4, fig.width=6, fig.cap="Plot of models", fig.align='left'}

library(cowplot)


plot_grid(XLplotLin)



```


The relative potency can be read from tabale 3.

```{r LinPotTab, echo=FALSE, warning=FALSE, results='asis'}

kable(LinPotTab, format = "markdown", caption= "Potency table", digits=3)


```

  0 ... test passed;
  
  1 ... test failed);


The ANOVA of the unconstrained model is listed in table 4.

```{r anovaxls, echo=FALSE, warning=FALSE, results='asis'}

kable(ANOVAlin, format = "markdown", caption= "ANOVA table unrestricted", digits=3)

RMSE <- sqrt(ANOVAlin[5,4])

```

The standard deviation of the model is `r RMSE`. 

The assay suitability tests are shown in table 5.


```{r SST_ergebn, echo=FALSE, cache=FALSE, warning=FALSE, message=FALSE, tidy=TRUE}

kable(LinTests1, row.names = F, format = "markdown", caption="Assay suitability test results", digits=3)


```

The estimate is the p-value of the test.  
F-tests on regression, significance of slopes, and preparation need to have a p-value <0.05 to pass.
All other tests pass if p-value > 0.05.

  0 ... test passed;
  
  1 ... test failed);

(NOTE: F-tests are sensitive, when the residual variability of the method is small. On the other hand effects may not be detected if residual variability is high.)

## Fitting results

The results of the linear fitting procedure for the restricted model is listed in table 6:

```{r SumCSSI, echo=FALSE, warning=FALSE, results='asis'}

kable(SuModAB, format = "markdown", caption= "Restricted linear regression (CSSI)", digits=3, row.names = F)


```

CSSI: common slope, separate intercept

The results of the linear fitting procedure for the unrestricted model is listed in table 7.


```{r SumSSSI, echo=FALSE, warning=FALSE, results='asis'}

kable(SuModABu, format = "markdown", caption= "Restricted linear regression (SSSI)", digits=3, row.names = F)


```

SSSI: separate slope, separate intercept

# Signature

<!-- Signature and date:\\ -->
<!-- \noindent\framebox(200,50) -->
<!--   \begin{minipage}[t][40pt][c]{190pt} -->
<!--     \centering -->
<!--     % Leave this blank for a physical signature -->
<!--   \end{minipage} -->


\vspace{1.5cm} 
\noindent
\begin{tabular}{p{6cm}p{1cm}p{6cm}}
  \cline{1-1} \cline{3-3}
  Date & & Signature 
\end{tabular}



# Appendix: Formulas

## Potency of linear PLA

Relative potency of the test sample to the reference is calculated as:
$$
 relPot_{log} = \frac{I_{ref} - I_{test}}{k}
$$
where: \\ I... intercept of reference or test\\
k ... common slope

The standard error of the linear restricted model is used to get the confidence interval of the relative potency with the formula:
$$
 CI_{rel Pot} = exp(relPot_{log} \pm se(relPot_{log})*q^{t_{n-p}}_{1-\frac{\alpha}{2}})
$$
In general, the confidence intervals are calculated as follows:
$$
 CI = \hat\theta\pm se(\hat\theta)*q^{t_{n-p}}_{1-\frac{\alpha}{2}}
$$
…where $\hat\theta$ is a fitted parameter or a linear combination thereof, q is the 1-alpha/2 quantile of the Student’s t-distribution with n-p degrees of freedom and se is the standard error derived from any covariance matrix.


# Literature

Finney, D.J.: (1978) Statistical Method in Biological Assay, London: Charles Griffin House, 3rd edition (pp. 80-82)

Franz, V.H.: Ratios: A short guide to confidence limits and proper use. arXiv:0710.2024v1, 10 Oct 2007

VerHoef, J.M.: Who invented the Delta Method? The American Statistician, 2012, 66:2, 124-127 DOI: 10.1080/00031305.2012.687494




