---
title: "Proportion test and CI calculation"
author: ""
date: "`r format(Sys.time(), '%H:%M, %d %B, %Y')`"
output:
   html_document:
      toc: true
      toc_float: true
      toc_depth: 4
      theme: cerulean
      highlight: kate
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 1. Introduction

The analysis reported here is part of the manuscript Romand et al. 2021.  The script performs statistical analysis on categorical/nominal data: comparison of dead cotyledon counts between different plant lines grown under N limiting conditions. 

The analysis requires the rstatix package and the rcompanion package.

#### Setup R packages

```{r message= FALSE, warning=FALSE}
if (!require(rstatix)) { install.packages("rstatix", repos = "http://cran.us.r-project.org") }
if (!require(rcompanion)) { install.packages("rcompanion", repos = "http://cran.us.r-project.org") }
if (!require(knitr)) { install.packages("knitr", repos = "http://cran.us.r-project.org") }
if (!require(tidyverse)) { install.packages("tidyverse", repos = "http://cran.us.r-project.org") }
if (!require(devtools)) { install.packages("devtools", repos = "http://cran.us.r-project.org") }


library(rstatix)
library(rcompanion)
library(knitr)
library(tidyverse)
library(devtools)
```

## 2. Data

Data is inputted manually into a table. In this case it is the summed counts from three biological replicates of the same experiment. Dead is for plants with dead cotyledons, and all is the total number of plants evaluated.

```{r}

# Create data matrix for analysis

xtab <- matrix(c(13, 280, 51, 234, 17, 281, 8, 287, 55, 240 ), nrow = 5, ncol = 2, byrow = TRUE,
               dimnames = list(c("Col", "OXRSH1", "qrt", "rsh1-1","QM"),
                               c("dead", "all")))

kable(xtab)

```

## 3. Data analysis

A proportion test is performed using the proportion test (prop_test) from the rstatix package.

```{r }
table<-prop_test(xtab, conf.level = 0.95, detailed=TRUE, correct=TRUE)
kable(table)

```

Now a post-hoc pairwaise Fisher test is performed to identify which differences are signficant using the rcompanion package. See `https://rcompanion.org/handbook/H_04.html` for more information.

```{r }
PT = pairwiseNominalIndependence(xtab,
                                 compare = "row",
                                 fisher  = TRUE,
                                 gtest   = FALSE,
                                 chisq   = FALSE,
                                 method  = "fdr",  # False Discovery Rate, see ?p.adjust for options
                                 digits  = 3)

# Add a significance column

PT<-PT %>%
  mutate(Significance = case_when(
    p.adj.Fisher <=0.01 ~ "**",
    p.adj.Fisher <=0.05 ~ "*",
    p.adj.Fisher >=0.05 ~ "ns",
    ))

kable(PT)

```



Next confidence intervals are calculated using the binomial test.

```{r}

data<-as.data.frame(xtab)

# Add columns to show line names and proportion dead 

data$Lines <- row.names(data)

data$dead_proportion = data$dead / data$all

data<-data[,c(3,1,2,4)]

# Perform binom test and extract confidence intervals

data<-data %>% 
    rowwise() %>% 
    mutate(CI= list(enframe(binom.test(dead, all, alternative =  "two.sided", conf.level = 0.95)$conf.int))) %>% 
    unnest(CI) %>% 
    spread(name, value) %>% 
    rename("LCB" = "1", "UCB" = "2")



kable(data, digits= 3)

```


## 4. R session

```{r}
InfoSession <- devtools::session_info()
print(InfoSession)
```

## 5. Citations

1. Kassambara, Alboukadel. 2021. Rstatix: Pipe-Friendly Framework for Basic Statistical Tests. https://CRAN.R-project.org/package=rstatix.

2. Mangiafico, Salvatore. 2021. Rcompanion: Functions to Support Extension Education Program Evaluation. https://CRAN.R-project.org/package=rcompanion.

3. R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

4. Wickham et al., (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686,
https://doi.org/10.21105/joss.01686

5. Wickham, Hadley, Hester, Jim and Chang, Winston (2021). devtools: Tools to Make Developing R Packages Easier. R package version 2.4.2. https://CRAN.R-project.org/package=devtools

6. Yihui Xie (2021). knitr: A General-Purpose Package for Dynamic Report Generation in R. R
package version 1.33.