-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.Rmd
204 lines (156 loc) · 3.93 KB
/
tests.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
199
200
201
---
title: "Testing the ViParc dataset"
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")
options(width = 137)
```
## Packages
Installing required packages:
```{r}
required <- c("dplyr", "magrittr", "tibble")
to_install <- which(! required %in% row.names(installed.packages()))
if (length(to_install) > 0) install.packages(to_install)
```
Loading `magrittr`:
```{r}
library(magrittr)
```
## Utilitary functions
A utilitary function that completes a `table` vector with integer names, making
it start at 0 and progress by step of 1:
```{r}
complete_vector <- function(x) {
the_names <- as.integer(names(x))
new_names <- as.character(0:max(the_names))
out <- setNames(rep(0, length(new_names)), new_names)
out[names(x)] <- x
out
}
```
## Loading the dataset
Loading the dataset:
```{r}
download.file("https://raw.github.com/viparc/prophylactic/master/data/viparc.csv", "viparc.csv")
viparc <- "viparc.csv" %>%
read.csv(stringsAsFactors = FALSE) %>%
tibble::as_tibble()
file.remove("viparc.csv")
```
## Farms, cycles and weeks
Number of farms:
```{r}
length(unique(viparc$USUBJID))
```
The total number of cycles:
```{r}
viparc %>%
dplyr::select(USUBJID, FLOCKSEQUENCE) %>%
unique() %>%
nrow()
```
The total number of weeks:
```{r}
nrow(viparc)
```
The distribution of the number of cycles per farm:
```{r}
viparc %>%
dplyr::select(USUBJID, FLOCKSEQUENCE) %>%
unique() %>%
dplyr::group_by(USUBJID) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
table(n)
```
The distribution of the number of weeks per cycle:
```{r}
viparc %>%
dplyr::group_by(USUBJID, FLOCKSEQUENCE) %>%
dplyr::tally() %>%
dplyr::ungroup() %$%
table(n) %>%
complete_vector() %>%
print() %T>%
barplot()
```
Checking for missing weeks:
```{r}
viparc %>%
dplyr::group_by(USUBJID, FLOCKSEQUENCE) %>%
dplyr::arrange(WEEK) %>%
dplyr::summarise(d = length(unique(diff(WEEK)))) %>%
dplyr::ungroup() %>%
dplyr::filter(d > 1)
```
## Drugs
The drugs:
```{r}
ab <- dplyr::select(viparc, dplyr::matches("^[a-z]", FALSE))
```
The names of drugs, sorted alphabetically:
```{r}
ab %>%
names() %>%
sort()
```
The drugs ordered by usage:
```{r}
sort(colSums(ab), TRUE)
```
The same in proportion of the number of weeks:
```{r}
round(100 * sort(colSums(ab), TRUE) / nrow(viparc))
```
A histogram of the drug usage:
```{r}
barplot(sort(colSums(ab), TRUE))
```
The number of drugs per week:
```{r}
table(rowSums(ab))
```
In proportion of the total number of weeks:
```{r}
round(100 * table(rowSums(ab)) / nrow(viparc))
```