---
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
  coeffs: NA
author: "Author: `r params$author`"
title: |
    | ![](logo.png){width=1in}
    | 4PL bioassay evaluation
subtitle: |
  `r params$FileName`
  
  <left> Unique time: </left> <right> `r Sys.time()`</right>
date: "`r paste(params$Subway, params$Version)`"

---

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

<!-- \newpage -->

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)

library(knitr)
library(DT)
library(kableExtra)

REP <- params$REP
author <- params$author
coeffs <- params$coeffs

all_l <- REP$all_l
ANOVAXLS <- REP$ANOVAXLS
XLplot4pl <- REP$XLplot4pl
DiagnTable <- REP$DiagnTable
UnRPLAausw <- REP$UnRPLAausw
UnRPLBend <- REP$UnRPLBend
PLAausw <- REP$PLAausw
PLBend <- REP$PLBend
pottab4plXL <- REP$pottab4plXL
Lim <- REP$Lim

XLdat2 <- REP$XLdat2

CIplot <- REP$CIplot
testsTab <- REP$testsTab
relpotTestPlot <- REP$relpotTestPlot



```


# 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.  
USP<1034> recommends calculation of standard errors of ratios of the parameters using Fieller's theorem [Finney DJ 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 mathematically more stable und thus preferable compared to a ratio approach ([Franz VH 2007]).

# Raw data

All data used for the 4PL 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)

```


# Results

## Overall result

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

potFlag <- 0
if (pottab4plXL["test_result"][[1]][1]==1) potFlag <- 1
AnalysisFlag <- FALSE
if (potFlag==1 | sum(testsTab$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()`


## 4pl-regression

Relative potency (absolute and relative confidence limits) are shown in Table 2:

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

#browser()
if (pottab4plXL["test_result"][[1]][1]==1) { cat(paste("FAILED: relative potency CL result of restricted model outside limits: ", Lim[[9]], "and" ,Lim[[10]] ))}
kable(pottab4plXL, format = "markdown", caption= "Relative potency with absolute and relative CLs ", digits=3, row.names = F) %>%
  kable_styling(latex_options = "hold_position")


```


## Plot of the data and models

The following plots show the restricted and unrestricted model, respectively.


```{r XLplot, echo=FALSE, warning=FALSE, fig.height=4, fig.width=6, fig.cap="Plot of models", fig.align='left', comment=F, message=F, results='asis', fig.pos='H'}

library(cowplot)


plot_grid(XLplot4pl)


```


## ANOVA table

The ANOVA of the unconstrained model is listed in table 2:

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

kable(ANOVAXLS, format = "markdown", caption= "ANOVA table unrestricted", digits=3) %>%
  kable_styling(latex_options = "hold_position")


```

## Analysis suitability tests

The following table lists the chosen suitabilit test results with confidence limits, where applicable:


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

kable(testsTab, row.names = F, format = "markdown", caption="SST results")


```


\footnotesize

```{r Fussnote, echo=F, comment=NA}

cat("*...The estimate for F-test on regression and on non-linearity is the p-value")
cat(  "F-test on regression passes if F-value > F-crit and thus p < 0.05")
cat(  "F-test on non-linearity passes if F-value < F-crit and thus p > 0.05")
cat(  "Test results outcome:")
cat("  0 ... test passed (for EQ tests: CL within limits);")
cat("  1 ... test failed (for EQ tests: CL not within limits);")


  
```

\normalsize


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

TestsTabFlag <- FALSE
if (sum(testsTab$test_results)>0) TestsTabFlag <- TRUE
colFmt2 <- function() {
  
  outputFormat <- knitr::opts_knit$get("rmarkdown.pandoc.to")
 if(TestsTabFlag) {
    text <- paste("\\textcolor{red}{Analysis suitability tests failed}",sep="")
  } else  {
    text <- paste("\\textcolor{black}{Analysis suitability tests succeeded}",sep="")
  }  
  return(text)
}


```


`r colFmt2()`




## Fitting results of the 4 models with bend points

The results of the non-linear fitting procedure for the restricted model (5 parameters) is listed in table 4:

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

kable(PLAausw, format = "markdown", caption= "Restricted 4PL evaluation", digits=3, row.names = F)


```


A depiction of the CI and corresponding limits of relative potency is shown here:

```{r, label='relpotPlot', echo=FALSE, warning=FALSE, fig.height=2, fig.width=3.5, fig.cap="Rel potency with CIs and limits", fig.align='left', results='asis'}

print(relpotTestPlot)


```


The bend points for test and reference sample are in table 5:

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

kable(PLBend, format = "markdown", caption= "Bendpoints (Sebaugh) of restricted 4PL", digits=3)


```

The results of the non-linear fitting procedure for the unrestricted model (8 parameters) is listed in table 6:

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

kable(UnRPLAausw, format = "markdown", caption= "UNrestricted 4PL evaluation", digits=3, row.names = F)

```


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

kable(UnRPLBend, format = "markdown", caption= "Bend points of 4PL unrestricted", digits=3, row.names = F)


```



# Appendix: Formulas

## 4PL regression

$$
Y = D + \frac{A-D} {1+(\frac{C} {x})^B } + \epsilon
$$


## log-logistic 4P regression

$$
Y = D + \frac{A-D} {1+e^{(B*(C - log(x))) }} + \epsilon
$$

where: x ... concentration of the analyte

A: upper asymptote

B: slope

D: lower asymptote

C ... EC50

# 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




