---
output:
  pdf_document:
    extra_dependencies: ["float"]
    number_sections: true
    toc: true
    toc_depth: 3
header_includes:
  -\usepackage{fancyheadr}
  -\setlength{\headheight}{22pt}%
  -\usepackage{lastpage}
  -\pagestyle{fancy}
  -\usepackage{pdflscape}
  -\usepackage{longtable}
  -\rhead{\includegraphics[width=.15\textwidth]{`r getwd()`/logo.png}}
params:
  FileName: NA
  newTitle: NA
  author: NA
  REP: NA
  REPlin: NA
  coeffsLin: NA
author: "Author: `r params$author`"
title: |
    | ![](logo.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)`"

---

<!-- \fancyfoot[C]{\thepage\ of \pageref{LastPage}} -->
<!-- \newpage -->

<!-- \newpage -->

```{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




```


# 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. Because biological responses are inherently variable, affected by assay conditions, cell systems or organisms, and measurement noise, the 4-parametric logistic regression is used to obtain reliable potency values. The variance for confidence interval calculation is coming from the regression procedure itself and is an excellent predictor for the variability of any future potency determinations.  
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]). However, the presented gradient approach using the differences on the log-scale is methematically more stable und thus preferable compared to any ratio approach ([Franz, V.H. 2007]).

# 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)

```

All data used linerar regression is shown 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 ANOVA of the unconstrained model is listed in table 3.

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

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


```

The assay suitability tests are shown in table 4.


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

kable(LinTests, 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 (for EQ tests: CI within limits);
  
  1 ... test failed (for EQ tests CI not within limits);



## Fitting results

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

```{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 6.

```{r SuSSSI, 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




# Appendix: Formulas

## Potency of linear PLA

$$
 rel Potency = \frac{I_{ref} - I_{test}{k}
$$
where: I... intercept of reference or test
k ... common slope



# 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




