-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.Rmd
More file actions
189 lines (153 loc) · 4.59 KB
/
analysis.Rmd
File metadata and controls
189 lines (153 loc) · 4.59 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
---
title: "Feeds"
csl: the-american-naturalist.csl
output:
html_document:
theme: cerulean
toc: yes
pdf_document:
toc: yes
<!-- bibliography: references.bib -->
editor_options:
chunk_output_type: console
---
<!--
IMAGES:
Insert them with: 
You can also resize them if needed: convert image.png -resize 50% image.png
If you want to center the image, go through HTML code:
<div style="text-align:center"><img src ="image.png"/></div>
REFERENCES:
For references: Put all the bibTeX references in the file "references.bib"
in the current folder and cite the references as @key or [@key] in the text.
Uncomment the bibliography field in the above header and put a "References"
title wherever you want to display the reference list.
-->
<style type="text/css">
.main-container {
max-width: 1370px;
margin-left: auto;
margin-right: auto;
}
</style>
```{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",
message = FALSE)
options(width = 137)
```
## Required packages
```{r}
library(magrittr)
library(dplyr)
library(tidyr)
library(purrr)
library(readr)
```
## Reading and transforming the data a bit
The trick here is to replace the `concentration` column by two columns `minc`
and `maxc` where `maxc = amount_anti_mg/kg/week`. Whenever there is only 1
antibiotic, `minc` is equal to `amount_anti_mg/kg/week` as well but equal to `0`
otherwise. Whenever there is no antibiotics at all, both `minc` and `maxc` are
set to `0`. For simplicity, we also replace missing value of `anti_name` by the
character string `no_anti`:
```{r}
(feeds <- read_csv("Feed_Chapter4_data_10Dec.csv") %>%
mutate(minc = if_else(option > 0, 0, `amount_anti_mg/kg/week`),
maxc = `amount_anti_mg/kg/week`) %>%
mutate_at(vars(minc, maxc), replace_na, 0) %>%
mutate_at(vars(anti_name), replace_na, "no_anti") %>%
group_by(farmid, flockid, weekid, anti_name) %>%
summarise_at(vars(minc, maxc), sum) %>%
ungroup())
```
Note: `option == 1` means there is a label problem.
This being done, it's now fairly easy to compute, the range of the mean
concentration of, for example, colistin:
```{r}
feeds %>%
filter(anti_name %in% c("colistin", "no_anti")) %>%
summarise(minc = mean(minc), maxc = mean(maxc))
```
Similarly, the range of the mean concentration of colistin per flock:
```{r}
feeds %>%
filter(anti_name %in% c("colistin", "no_anti")) %>%
group_by(flockid) %>%
summarise(minc = mean(minc), maxc = mean(maxc))
```
And the range of the mean concentration of colistin per farm:
```{r}
feeds %>%
filter(anti_name %in% c("colistin", "no_anti")) %>%
group_by(farmid) %>%
summarise(minc = mean(minc), maxc = mean(maxc))
```
## Computing the ranges for all antibiotics
This is the list of all antibiotics:
```{r}
anti_list <- setdiff(unique(feeds$anti_name), "no_anti")
```
The following function computes the ranges for all the antibiotics:
```{r}
compute_range <- function(x) {
anti_list %>%
map_dfr(~ filter(x, anti_name %in% c(.x, "no_anti")) %>%
summarise(minc = mean(minc), maxc = mean(maxc))) %>%
mutate(antibiotic = anti_list) %>%
select(antibiotic, everything())
}
```
Let's try it for all the weeks pooled together:
```{r}
compute_range(feeds)
```
By flock:
```{r}
feeds %>%
group_by(flockid) %>%
group_modify(~ compute_range(.x)) %>%
ungroup()
```
By farm:
```{r}
feeds %>%
group_by(farmid) %>%
group_modify(~ compute_range(.x)) %>%
ungroup()
```
## Frequencies
If now we are interested in frequencies of usage instead of concentration, it's
basically the same as above, replacing `minc` and `maxc` by presence / absence
data. For all weeks pooled together:
```{r}
feeds %>%
mutate_at(vars(minc, maxc), as.logical) %>%
compute_range()
```
By flock:
```{r}
feeds %>%
mutate_at(vars(minc, maxc), as.logical) %>%
group_by(flockid) %>%
group_modify(~ compute_range(.x)) %>%
ungroup()
```
By farm:
```{r}
feeds %>%
mutate_at(vars(minc, maxc), as.logical) %>%
group_by(farmid) %>%
group_modify(~ compute_range(.x)) %>%
ungroup()
```