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 }) 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", fig.path = "README-")
library(viparc) library(dplyr)
Heatmap of the surveillance disease and drug usage in several farms for a number of weeks:
data("chcknfarms") chcknfarms %>% filter(CYCLE == 1) %>% transmute(space = FARMCODE, time = WEEK, disease = SRespiratory, drug = AntibioticUse) %>% heat_map()
Note that the input data frame to heat_map
should have 4 variables named
space
, time
, disease
and drug
.
Let's consider for example the following vector of AMU:
amu <- rep(rep(c(TRUE, FALSE), 6), c(7, 2, 1, 2, 3, 1, 2, 1, 1, 1, 1, 3))
See this example:
data.frame(amu) %>% mutate( with_amu = amu, wout_amu = no_amu(amu, 1)) %>% select(-amu)
Note that, as expected, the only combination that we cannot have on any row is
c(TRUE, TRUE)
. Then, if one wants to discriminate any week either into AMU or
no-AMU, the code will have to be:
data.frame(amu) %>% mutate( with_amu = amu, wout_amu = no_amu(amu, 1)) %>% select(-amu) %>% filter(with_amu + wout_amu > 0) %>% transmute(amu = with_amu)
Let's consider a data set that could look like this:
(dataset <- data.frame( amu = c(rep(TRUE, 4), rep(FALSE, 5), TRUE, rep(FALSE, 6), TRUE, FALSE, TRUE, rep(FALSE, 4), TRUE, FALSE), disease = c(rep(TRUE, 3), FALSE, TRUE, FALSE, rep(TRUE, 2), FALSE, rep(TRUE, 2), rep(FALSE, 3), rep(TRUE, 2), rep(FALSE, 3), TRUE, rep(FALSE, 2), rep(TRUE, 3)) ))
Among the weeks with no diseases, we want to compare 2 groups of weeks:
and for each of these 2 groups we want to count the number of weeks $j$ for which a disease was observed at week $j + 1$ or $j + 2$.
To make it clear, note that here we are thus eliminating from the analysis
The R code for that would be:
dataset %>% mutate(no_amu = no_amu(amu, 1), # no AMU on weeks i-1 and i diseas = any_disease(disease, 2)) %>% # disease on weeks i+1 or i+2 na.exclude() %>% filter(! disease, # removes weeks with disease amu + no_amu ) %>% # removes weeks with no AMU but... mutate(amu = ! no_amu) %>% # ... with AMU the week before. select(-disease, -no_amu) %>% table() %>% fisher.test()
These piece of code in basically included in the amu_causes_disease()
function. See the examples below:
amu_with_diseases %>% amu_causes_disease()
amu_with_diseases %>% amu_causes_disease(disease = "DIARRHOEA")
amu_with_diseases %>% mutate(Sick_yes = DIARRHOEA | RESPIRATORY ) %>% amu_causes_disease()
amu_with_diseases %>% mutate(Sick_yes = DIARRHOEA | RESPIRATORY ) %>% amu_causes_disease(week_disease = 3)
See how functions time_index()
and fill_gaps()
can be used to do the job:
dataset %>% mutate(amu_week = time_index(amu), disease2 = fill_gaps(disease), disease2_week = time_index(disease2)) %>% select(amu, amu_week, disease, disease2, disease2_week)
Then, you can extend this in a longer pipeline:
dataset %>% mutate(amu_week = time_index(amu), disease = fill_gaps(disease), disease_week = ifelse(disease, time_index(disease), NA), disease_start = disease_week < 2, amu_event = ifelse(amu, ifelse(amu_week > 1, "D", "S"), ifelse(amu_week > 1, "N", "A")))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.