Here we create two data sets. One that expresses AMU as presence (TRUE
) / absence (FALSE
) variables and one that expresses AMU in g / kg of chicken.
Installing the required packages:
> required <- c("dplyr", "magrittr", "mgcv", "readr")
> to_install <- which(! required %in% row.names(installed.packages()))
> if (length(to_install)) install.packages(to_install)
Loading magrittr
:
> library(magrittr)
The CliRes data can be loaded directly from here:
> 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 = ""))
If you want to transform the data regarding AMU into qualitative information, here is the way to do:
> if (!dir.exists("data")) dir.create("data")
> viparc %>%
+ dplyr::mutate_at(dplyr::vars(dplyr::ends_with("_g")), list(~ . > 0)) %>%
+ dplyr::rename_at(dplyr::vars(dplyr::ends_with("_g")), function(x) sub("_g$", "_use", x)) %>%
+ write.csv("data/viparc_qualitative.csv", FALSE, row.names = FALSE)
If you want to express AMU in g / kg of chicken, you first need to retrieve data on weight as a function of age. These data are available here:
> age_weight <- readr::read_csv("https://raw.githubusercontent.com/viparc/amu_metrics/master/data/age_weight.csv",
+ col_types = "iiid")
We use these data in a Generalized Additive Model in order to efficiently extrapolate the weight-age relationship outside the age range of the data. After merging with viparc
, we can then express the AMU as a function of g / kg of chicken:
> if (!dir.exists("data")) dir.create("data")
> weeks <- 1:max(viparc$week)
> age_weight %>%
+ mgcv::gam(weight.kg ~ s(week), Gamma(link = log), .) %>%
+ mgcv::predict.gam(data.frame(week = weeks), "response") %>%
+ list(weeks) %>%
+ as.data.frame() %>%
+ setNames(c("individual_weight_kg", "week")) %>%
+ dplyr::right_join(viparc, "week") %>%
+ dplyr::mutate(farm_weight_kg = individual_weight_kg * nb_chicken) %>%
+ dplyr::mutate_at(dplyr::vars(dplyr::ends_with("_g")), list(~ . / farm_weight_kg)) %>%
+ dplyr::select(farm, flock, week, respiratory:sampling, individual_weight_kg, farm_weight_kg) %>%
+ dplyr::rename_at(dplyr::vars(dplyr::ends_with("_g")), function(x) sub("_g$", "_g.kg", x)) %>%
+ write.csv("data/viparc_quantitative.csv", FALSE, row.names = FALSE)