-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_data.Rmd
More file actions
276 lines (217 loc) · 6.3 KB
/
explore_data.Rmd
File metadata and controls
276 lines (217 loc) · 6.3 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
title: "Expoloring the data from the CliRes database"
output:
html_document:
theme: cosmo
toc: true
editor_options:
chunk_output_type: console
css: style.css
---
```{r general_options, include = FALSE}
knitr::knit_hooks$set(
margin = function(before, options, envir) {
if (before) par(mgp = c(1.5, .5, 0), bty = "n", plt = c(.105, .97, .13, .97))
else NULL
},
prompt = function(before, options, envir) {
options(prompt = if (options$engine %in% c("sh", "bash")) "$ " else "> ")
})
knitr::opts_chunk$set(margin = TRUE, prompt = TRUE, comment = "",
collapse = TRUE, cache = FALSE, autodep = TRUE,
dev.args = list(pointsize = 11), fig.height = 3.5,
fig.width = 4.24725, fig.retina = 2, fig.align = "center")
options(width = 250)
```
## Packages
Installing the required packages:
```{r}
required <- c("dplyr", "magrittr", "readr")
to_install <- setdiff(required, row.names(installed.packages()))
if (length(to_install)) install.packages(to_install)
```
Loading `magrittr`:
```{r}
library(magrittr)
```
## Loading the data
```{r}
viparc <- readr::read_csv("https://raw.githubusercontent.com/viparc/clires_data/master/data/viparc.csv",
col_types = paste(c("cii", rep("l", 6), rep("d", 45), "lil"), collapse = ""))
```
## Numbers of farms, flocks and weeks
There are **`r length(unique(viparc$farm))` farms** in the study:
```{r}
length(unique(viparc$farm))
```
And a total of **`r nrow(unique(dplyr::select(viparc, farm, flock)))` flocks**:
```{r}
viparc %>%
dplyr::select(farm, flock) %>%
unique() %>%
nrow()
```
Of which there are **`r nrow(unique(dplyr::select(dplyr::filter(viparc, completed), farm, flock)))` completed flocks** :
```{r}
viparc %>%
dplyr::filter(completed) %>%
dplyr::select(farm, flock) %>%
unique() %>%
nrow()
```
This represents **`r nrow(viparc)` weeks** of observation:
```{r}
nrow(viparc)
```
And **`r nrow(dplyr::filter(viparc, completed))` weeks** for completed flocks:
```{r}
nrow(dplyr::filter(viparc, completed))
```
The distribution of the number of flocks per farm:
```{r}
viparc %>%
dplyr::select(farm, flock) %>%
unique() %>%
dplyr::group_by(farm) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
hist(n, 0:12, col = "grey", main = NA, xlab = "number of flocks", ylab = "number of farms")
```
or:
```{r}
viparc %>%
dplyr::select(farm, flock) %>%
unique() %>%
dplyr::group_by(farm) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
table(n)
```
The same thing, considering only completed flocks:
```{r}
viparc %>%
dplyr::filter(completed) %>%
dplyr::select(farm, flock) %>%
unique() %>%
dplyr::group_by(farm) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
hist(n, 0:12, col = "grey", main = NA, xlab = "number of completed flocks", ylab = "number of farms")
```
or:
```{r}
viparc %>%
dplyr::filter(completed) %>%
dplyr::select(farm, flock) %>%
unique() %>%
dplyr::group_by(farm) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
table(n)
```
The distribution of the number of weeks per flock:
```{r}
viparc %>%
dplyr::group_by(farm, flock) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
hist(n, 0:27, col = "grey", main = NA, xlab = "number of weeks", ylab = "number of flocks")
```
```{r}
viparc %>%
dplyr::group_by(farm, flock) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
table(n)
```
The same thing, considering only the completed flocks:
```{r}
viparc %>%
dplyr::filter(completed) %>%
dplyr::group_by(farm, flock) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
hist(n, 0:27, col = "grey", main = NA, xlab = "number of weeks", ylab = "number of flocks")
```
```{r}
viparc %>%
dplyr::filter(completed) %>%
dplyr::group_by(farm, flock) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
table(n)
```
## Number of chicken
The distribution of the flocks sizes:
```{r}
viparc %>%
dplyr::filter(week < 2) %$%
hist(nb_chicken, nclass = 30, col = "grey", main = NA, xlab = "number of chicken", ylab = "number of flocks")
```
Or:
```{r}
viparc %>%
dplyr::filter(week < 2) %$%
head(sort(nb_chicken), 15)
```
The distribution of the farms sizes:
```{r}
viparc %>%
dplyr::filter(week < 2) %>%
dplyr::group_by(farm) %>%
dplyr::summarise(size = mean(nb_chicken)) %>%
dplyr::ungroup() %$%
hist(size, col = "grey", main = NA, xlab = "number of chicken", ylab = "number of farms")
```
Or:
```{r}
viparc %>%
dplyr::filter(week < 2) %>%
dplyr::group_by(farm) %>%
dplyr::summarise(size = mean(nb_chicken)) %>%
dplyr::ungroup() %$%
head(round(sort(size)), 15)
```
## Feces samplings
Not all the flocks are sampled 3 times:
```{r}
(samplings <- viparc %>%
dplyr::select(farm, flock, completed, sampling) %>%
dplyr::group_by(farm, flock) %>%
dplyr::summarise(completed = mean(completed), sampling = sum(sampling, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
dplyr::mutate(completed = completed > 0))
```
```{r}
with(samplings, table(completed, sampling))
```
The reasons for less than 3 samplings seems to be
* the flock is uncomplete
* the flock died prematuraly
* some samplings were missed
Let's explore a bit more by focusing on the completed flocks and see whether
the flocks with less than 3 samplings tend to be short ones (i.e. suggesting
premature death of the flock):
```{r}
hist2 <- function(x, ...) hist(x, breaks = 0:30, ...)
# Let's plot the durations of the completed flocks:
viparc %>%
dplyr::filter(completed) %>%
dplyr::group_by(farm, flock) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
hist2(n, col = "grey", xlab = "duration (weeks)", ylab = "number of flocks", main = NA)
# Let's now plot the durations of the completed flocks with 1 sample only:
samplings %>%
dplyr::filter(completed, sampling == 1) %$%
purrr::map2(farm, flock, function(x, y) nrow(dplyr::filter(viparc, farm == x, flock == y))) %>%
unlist() %>%
hist2(col = adjustcolor("red", .5), add = TRUE)
# And the durations of the completed flocks with 2 samples only:
samplings %>%
dplyr::filter(completed, sampling == 2) %$%
purrr::map2(farm, flock, function(x, y) nrow(dplyr::filter(viparc, farm == x, flock == y))) %>%
unlist() %>%
hist2(col = adjustcolor("blue", .5), add = TRUE)
legend("left", c("1 sampling only", "2 samplings only"), fill = adjustcolor(c("red", "blue"), .5), bty = "n")
```