-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampling Contamination.Rmd
138 lines (97 loc) · 4.61 KB
/
Sampling Contamination.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
---
title: 'Sampling: Contamination'
author: "Xianbin Cheng"
date: "August 29, 2018"
output: html_document
---
# Method #
1. Load in libraries, visualization functions and check the session info.
```{r, warning = FALSE, message= FALSE}
source(file = "Sampling_libraries.R")
source(file = "Sampling_contamination.R")
source(file = "Sampling_visualization.R")
#library(plotly)
```
```{r}
sessionInfo()
```
2. Define the input paramaters and create a simulation function.
* `n_contam` = the number of contamination points
* `x_lim` = the limits of the horizontal axis
* `y_lim` = the limits of the vertical axis
* `x` = the horizontal coordinate of the contamination center, which follows a uniform distribution (`U(0,10)`)
* `y` = the vertical coordinate of the contamination center, which follows a uniform distribution(`U(0,10)`)
* `cont_level` = a vector that indicates the mean contamination level (logCFU/g or logCFU/mL) and the standard deviation in a log scale, assuming contamination level follows a log normal distribution $ln(cont\_level)$~$N(\mu, \sigma^2)$.
**Mode 1: Discrete Spread**
* `n_affected` = the number of affected plants near the contamination spot, which follows a Poisson distribution ($Pois(\lambda = 5)$)
* `covar_mat` = covariance matrix of `x` and `y`, which defines the spread of contamination. Assume the spread follows a 2D normal distribution with var(X) = 0.25, var(Y) = 0.25 and cov(X, Y) = 0
**Mode 2: Continuous Spread**
* `spread_radius` = the radius of the contamination spread
* `LOC` = the limit of contribution of contamination. By default, it is set at 0.001.
```{r}
## The input parameters
n_contam = rpois(n = 1, lambda = 3)
x_lim = c(0, 10)
y_lim = c(0, 10)
cont_level = c(7, 1)
### Mode 1
n_affected = rpois(n = 1, lambda = 5)
covar_mat = matrix(data = c(0.25, 0, 0, 0.25), nrow = 2, ncol = 2)
### Mode 2
spread_radius = 2.5
LOC = 10^(-3)
```
3. Simulate the contamination center and its spread.
a) Generate `n_contam` contamination spots whose coordinates follow a uniform distribution.
b) If `n_affected` > 0, then generate contamination spread points that distribute around the contamination spots. The contamination spread points follow a bivariate normal distribution. If `n_affected` = 0, then skip this step.
c) Generate contamination levels for contamination spots. The contamination levels follow a log normal distribution ($ln(X)$~$N(`r cont_level[1]`, `r cont_level[2]`)$). The spread of contamination level is defined by a decay function. This function is affected by distance, `spread_radius` and `LOC`.
i) `f_exp()`: $$ f(d) = exp(- \frac {d} {\theta}) $$, where d = distance, $\theta = - \frac {spread\_radius} {ln(LOC)}$
ii) `f_norm()`: $$ f(d) = exp(- \frac {d^2} {\sigma^2})$$, where d = distance, $\sigma = \sqrt {- {\frac {spread\_radius^2} {ln(LOC)}}}$.
d) Check for any points that fall outside the perimeter of the field.
e) Remove those outliers.
```{r}
sim_contam
```
```{r}
# Run this if we want reproducibility
#set.seed(123)
```
```{r}
contam_xy = sim_contam(n_contam = n_contam, xlim = x_lim, ylim = y_lim, covariance = covar_mat, n_affected = n_affected, radius = spread_radius, cont_level = cont_level)
```
```{r}
summary(contam_xy$label)
```
# Results #
1. Visualization of contamination spots.
**Mode 1: Discrete Spread**
```{r}
plot_contam_dis = contam_draw(data = contam_xy, spread = "discrete", xlim = x_lim, ylim = y_lim)
```
```{r}
plot_contam_dis
```
**Mode 2: Continuous Spread**
```{r}
plot_contam_cont = contam_draw(data = contam_xy, spread = "continuous", xlim = x_lim, ylim = y_lim)
```
```{r}
plot_contam_cont
```
```{r}
## Choose an exponential function to describe the contamination spread
contam_level_draw(dimension = "2d", method = "exp", spread_radius = spread_radius, LOC = LOC)
contam_level_draw(dimension = "3d", method = "exp", spread_radius = spread_radius, LOC = LOC, df_contam = contam_xy, xlim = x_lim, ylim = y_lim, interactive = FALSE)
## Choose a normal distribution-like function to describe the contamination spread
contam_level_draw(dimension = "2d", method = "norm", spread_radius = spread_radius, LOC = LOC)
contam_level_draw(dimension = "3d", method = "norm", spread_radius = spread_radius, LOC = LOC, df_contam = contam_xy, xlim = x_lim, ylim = y_lim, interactive = FALSE)
```
```{r, eval = FALSE, echo = FALSE}
### Visualize the covariance matrix
test = mvrnorm(n = 10000, mu = c(5,5), Sigma = covar_mat)
test = as.data.frame(test)
colnames(test) = c("X", "Y")
kernel_density = kde2d(x = test$X, y = test$Y, n = 50)
image(kernel_density)
contour(kernel_density, add = TRUE)
```