-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pooling paper.Rmd
142 lines (116 loc) · 3.45 KB
/
Pooling paper.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
title: 'Pooling: Paper'
author: "Xianbin Cheng"
date: "February 15, 2019"
output: html_document
---
# Objective
Clean up data for the pooling paper
```{r, warning = FALSE, message = FALSE}
library(tidyverse)
source("STD_simulation.R")
library(mc2d)
library(MASS)
```
```{r}
sessionInfo()
```
```{r}
df = read.csv("Texas Conc and Class 04_12_2018.csv", header = TRUE, row.names = 1)
str(df)
```
```{r}
df1 = df %>%
mutate(plate = str_split(string = .$Kernel_ID, pattern = "-", simplify = TRUE)[,1] %>% as.numeric(),
kernel = str_split(string = .$Kernel_ID, pattern = "-", simplify = TRUE)[,2] %>% as.numeric()) %>%
arrange(plate, kernel) %>%
dplyr::filter(! plate %in% c(102, 902))
df1$AF_class = factor(x = df1$AF_class, levels = c("H", "M", "L"))
str(df1)
```
```{r}
summary(df1$AF_class)
summary(subset(x = df1, subset = AF_class == "L" , select = AF.ppb., drop = TRUE))
summary(subset(x = df1, subset = AF_class %in% c("M", "H"), select = AF.ppb., drop = TRUE))
```
```{r}
##### How many NAs?
count_na = function(data, type){
if(type == "AF"){
a = "AF.ppb."
} else if (type == "FM"){
a = "FM.ppm."
}
sum(is.na(data[[a]]))
}
# How many NA's in aflatoxin?
df1 %>%
split(x = ., f = df1$plate) %>%
map(.x = ., .f = count_na, type = "AF") %>%
unlist()
```
```{r}
n = 1000000
prev = 0.04
set.seed(123)
# PERT for healthy kernels and Gamma for contaminated kernels
neg = rpert(n = n * (1 - prev), min = 0, mode = 0.7, max = 19.99, shape = 80)
pos = rgamma_lim(n = n * prev, alpha = 2, mode = 40000, lb = 20, ub = NULL)
sim_data = data.frame(conc = c(neg, pos), class = c(rep("neg", times = length(neg)), rep("pos", times = length(pos)))) %>%
mutate(func = "PERT")
```
```{r}
ggplot(data = sim_data) +
geom_histogram(aes(x = conc, fill = class), bins = 100) +
scale_x_log10(breaks = c(1e-1, 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6)) +
scale_fill_discrete(h = c(190, 15)) +
labs(x = "AF concentration") +
theme_bw()
```
```{r}
# Real data
quantile(x = df1$AF.ppb., na.rm = TRUE)
neg_exp = subset(x = df1, subset =AF_class == "L", select = AF.ppb.)
pos_exp = subset(x = df1, subset = AF_class == "H", select = AF.ppb.)
# PERT + Gamma
quantile(x = sim_data$conc)
```
```{r}
# Real data VS PERT + Gamma
wilcox.test(x = df1$AF.ppb., y = sim_data$conc, alternative = "two.sided")
ks.test(x = neg_exp$AF.ppb., y = neg)
ks.test(x = pos_exp$AF.ppb., y = pos)
ks.test(x = df1$AF.ppb., y = sim_data$conc)
```
```{r}
AF_hist = ggplot(data = df1)+
geom_histogram(aes(x = AF.ppb., fill = AF_class), bins = 50) +
labs(x = "Aflatoxin concentration (ppb)", y = "Number of Kernels")+
scale_x_log10(breaks = c(1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4, 1e5)) +
scale_fill_discrete(name = "Single Kernel Aflatoxin Level", labels = c("[AF]>=20", "[AF]<20")) +
theme_classic() +
theme(legend.position = "top")
```
```{r}
combined = ggplot() +
geom_histogram(data = df1, aes(x = AF.ppb., y = ..density..), binwidth = 0.1) +
geom_density(data = sim_data, aes(x = conc), fill = "grey", alpha = 0.3) +
labs(x = "Aflatoxin concentration (ppb)", y = "Density")+
scale_x_log10(breaks = c(1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4, 1e5)) +
#scale_y_sqrt() +
theme_classic() +
theme(legend.position = "top")
```
```{r}
AF_hist
combined
```
```{r, eval = FALSE}
pdf("plot.pdf")
AF_hist
combined
dev.off()
```
```{r, echo= FALSE}
sessionInfo()
```