-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
198 lines (155 loc) · 4.21 KB
/
README.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
title: "Simulating bacterial growth in water bottle"
author: "Zheer Kejlberg Al-Mashhadi"
date: "2023-11-05"
output: github_document
---
Load dependencies
```{r, message=FALSE, results='hide'}
library(ggplot2)
library(gridExtra)
```
Set some basic variables
```{r}
# Set basic vars
splits <- 1
hours <- 2*splits # two updates of bacterial number per hour
days <- 24*hours
month <- 30*days
t2 <- 12*hours # doubling time of bacterial load
multiplier <- 2^(1/t2)
no_of_sims <- 20
```
Define all the functions
```{r}
# Define function to refill bottle
refill <- function(df, time) {
vol_refilled <- 1000 - df[time-1,]$volume
last_col <- df[time-1,]$colonies
df[time,] <- data.frame(
time = time,
volume = 1000,
colonies = last_col + vol_refilled,
conc = (last_col + vol_refilled) / 1000,
step = "refill"
)
return(df)
}
# Define function for drinking
drink <- function(df, df2, time, volumes) {
last_vol <- df[time-1,]$volume
drink_vol <- min(volumes[time], last_vol)
new_vol <- last_vol - drink_vol
volume_ratio <- new_vol / last_vol
last_col <- df[time-1,]$colonies
new_col <- last_col * volume_ratio
colony_diff <- last_col - new_col
df[time,] <- data.frame(
time = time,
volume = new_vol,
colonies = new_col,
conc = new_col / new_vol,
step = "drink"
)
df2[time,] <- data.frame(
volume = drink_vol,
colonies = colony_diff
)
return(list(df,df2))
}
# Define function for bacterial spread
spread <- function(df, time) {
new_col <- df[time-1,]$colonies * multiplier
new_vol <- df[time-1,]$volume
df[time,] <- data.frame(
time = time,
volume = new_vol,
colonies = new_col,
conc = new_col / new_vol,
step = "spread"
)
return(df)
}
# Define function to pick the next function to run
pick_step <- function(bottle, drunk, time, does_drink, scenario, volumes) {
if (scenario == "continuous") {
condition <- bottle[time-1,]$step == "drink"
} else {
condition <- bottle[time-1,]$volume == 0
}
if (condition) {
bottle <- refill(bottle, time)
} else {
if (does_drink[time] == 1) {
df_list <- drink(bottle, drunk, time, volumes)
bottle <- df_list[[1]]
drunk <- df_list[[2]]
} else {
bottle <- spread(bottle,time)
}
}
return(list(bottle, drunk))
}
# Define the function to run a full simulation
run_sim <- function(scenario = FALSE) {
# Random numbers to use
does_drink <- rbinom(month, 1, 0.1)
volumes <- rnorm(month,240,25)
# Set up bottle dataframe
bottle <- data.frame(
time = 1:month,
volume = c(1000, rep(NA, month-1)),
colonies = c(1000, rep(NA, month-1)),
conc = c(1, rep(NA, month-1)),
step = c("initialize", rep(NA, month-1))
)
# Set up drink counter
drunk <- data.frame(
volume = c(0, rep(NA, month-1)),
colonies = c(0, rep(NA, month-1))
)
for (i in 2:nrow(bottle)) {
df_list <- pick_step(bottle, drunk, i, does_drink, scenario, volumes)
bottle <- df_list[[1]]
drunk <- df_list[[2]]
}
print(sum(drunk$colonies, na.rm=TRUE))
}
```
Run the simulations
```{r}
# Run the simulation with continuous refilling
t1 <- as.numeric(rep(NA,no_of_sims))
for (repetition in 1:no_of_sims) {
t1[repetition] <- run_sim("continuous")
}
# Run the simulation with refilling whenever empty
t2_summary <- as.numeric(rep(NA,no_of_sims))
for (repetition in 1:no_of_sims) {
t2_summary[repetition] <- run_sim()
}
```
Create plots of distributions of total bacterial intake
```{r}
# Create density plots
t1_df <- as.data.frame(t1)
t1_plot <- ggplot(t1_df, aes(x=t1)) +
geom_density() + labs(
title = paste("Refilling continuously - ", no_of_sims, " iterations", sep=""),
x = "Total bacteria drunk",
y = "Density"
) + xlim(min(t1_df),max(t1_df))
z1 <- as.matrix(summary(t1))
z1 <- tableGrob(z1)
grid.arrange(t1_plot, z1, heights=c(2.5,1))
t2_df <- as.data.frame(t2_summary)
t2_plot <- ggplot(t2_df, aes(x=t2_summary)) +
geom_density() + labs(
title = paste("Refilling when empty - ", no_of_sims, " iterations", sep=""),
x = "Total bacteria drunk",
y = "Density"
) + xlim(min(t2_df),max(t2_df))
z2 <- as.matrix(summary(t2_summary))
z2 <- tableGrob(z2)
grid.arrange(t2_plot, z2, heights=c(2.5,1))
```