From 4738e00f9d5347a6be2bf0cf9ec655d25fb5ad5f Mon Sep 17 00:00:00 2001 From: bodirsky Date: Thu, 25 Jan 2024 17:25:26 +0100 Subject: [PATCH] adding feed conversion validation --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/calcValidFeedConversion.R | 166 ++++++++++++++++++++++++++++++++++++ R/fullVALIDATION.R | 4 + README.md | 6 +- 6 files changed, 178 insertions(+), 8 deletions(-) create mode 100644 R/calcValidFeedConversion.R diff --git a/.buildlibrary b/.buildlibrary index 7a2a111..a1278a8 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '50158744' +ValidationKey: '50354850' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 91afec3..52fce0c 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,8 +2,8 @@ cff-version: 1.2.0 message: If you use this software, please cite it using the metadata from this file. type: software title: 'mrvalidation: madrat data preparation for validation purposes' -version: 2.54.2 -date-released: '2024-01-10' +version: 2.55.0 +date-released: '2024-01-25' abstract: Package contains routines to prepare data for validation exercises. authors: - family-names: Bodirsky diff --git a/DESCRIPTION b/DESCRIPTION index a57afef..040e6ea 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrvalidation Title: madrat data preparation for validation purposes -Version: 2.54.2 -Date: 2024-01-10 +Version: 2.55.0 +Date: 2024-01-25 Authors@R: c( person("Benjamin Leon", "Bodirsky", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), person("Stephen", "Wirth", role = "aut"), diff --git a/R/calcValidFeedConversion.R b/R/calcValidFeedConversion.R new file mode 100644 index 0000000..c68192a --- /dev/null +++ b/R/calcValidFeedConversion.R @@ -0,0 +1,166 @@ +#' @title calcValidFeedConversion +#' @description calculates various feed indicators +#' +#' @param livestockSystem if TRUE, ruminant meat and milk are aggregated, and poultry meat and egg are aggregated +#' +#' @return List of magpie objects with results on country level, weight on country level, unit and description. +#' @author Benjamin Leon Bodirsky, github Copilot +#' @seealso +#' \code{\link{calcFoodSupplyPast}}, +#' \code{\link{calcValidLivestockShare}} +#' @examples +#' \dontrun{ +#' calcOutput("ValidFeed") +#' } +#' +#' @importFrom magpiesets reporthelper +#' @importFrom magclass dimOrder +calcValidFeedConversion <- function(livestockSystem = TRUE) { + ### calculate product specific feed conversion efficiency as quotient between + ### feed and animal products + + mb <- calcOutput("FAOmassbalance", aggregate = FALSE) + + mb <- mb[, , c("ge", "nr")] + x <- NULL + weight <- NULL + + ap <- mb[, , "production"][, , findset("kli")] + quotientProductspecfic <- collapseNames(ap) + # extract feed items from mb + feed <- mb[, , c("feed", "feed_livst_chick", "feed_livst_egg", + "feed_livst_milk", "feed_livst_pig", + "feed_livst_rum")] + feedGrouped <- mbind( + add_dimension( + dimSums(feed[, , c("tece", "trce", "rice_pro", "maiz", "brans")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Cereal Intensity"), + add_dimension( + dimSums(feed[, , c("soybean", "groundnut", "puls_pro", "rapeseed", "oils", "oilcakes", + "cassav_sp", "oilpalm", "sunflower")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Oilcrop intensity"), + add_dimension( + dimSums(feed[, , c("potato", "cassav_sp", "others", + "sugr_beet", "sugr_cane", "molasses")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Roots and other crops"), + add_dimension( + dimSums(feed[, , c("pasture")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Pasture intensity"), + add_dimension( + dimSums(feed[, , c("foddr", "res_cereals", "res_fibrous", "res_nonfibrous")], + dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Other roughage intensity"), + add_dimension( + dimSums(feed[, , c("livst_chick", "livst_egg", "livst_milk", "livst_pig", + "livst_rum", "fish")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Animal product intensity"), + add_dimension( + dimSums(feed[, , c("distillers_grain", "alcohol", "scp")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Other processed items intensity") + ) + + + feedProductspecific <- collapseNames(feedGrouped[, , c("feed_livst_chick", "feed_livst_egg", + "feed_livst_milk", "feed_livst_pig", + "feed_livst_rum")]) + getNames(feedProductspecific, dim = 2) <- substring(getNames(feedProductspecific, dim = 2), 6) + + if (livestockSystem == TRUE) { + feedProductspecific <- mbind( + add_dimension( + dimSums(feedProductspecific[, , c("livst_milk", "livst_rum")], dim = c("ElementShort")), + dim = 3.2, add = "ElementShort", nm = "Ruminant meat and dairy"), + add_dimension( + dimSums(feedProductspecific[, , c("livst_chick", "livst_egg")], dim = c("ElementShort")), + dim = 3.2, add = "ElementShort", nm = "Poultry meat and eggs"), + add_dimension( + dimSums(feedProductspecific[, , c("livst_pig")], dim = c("ElementShort")), + dim = 3.2, add = "ElementShort", nm = "Monogastric meat") + ) + quotientProductspecfic <- mbind( + add_dimension( + dimSums(quotientProductspecfic[, , c("livst_milk", "livst_rum")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Ruminant meat and dairy"), + add_dimension( + dimSums(quotientProductspecfic[, , c("livst_chick", "livst_egg")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Poultry meat and eggs"), + add_dimension( + dimSums(quotientProductspecfic[, , c("livst_pig")], dim = c("ItemCodeItem")), + dim = 3.1, add = "ItemCodeItem", nm = "Monogastric meat") + ) + } else { + getNames(feedProductspecific, dim = 2) <- reportingnames(getNames(feedProductspecific, dim = 2)) + getNames(quotientProductspecfic, dim = 1) <- reportingnames(getNames(quotientProductspecfic, dim = 1)) + } + + # Calculate feed conversion efficiency total + quotientTmp <- collapseNames(dimSums(quotientProductspecfic, dim = c("ItemCodeItem"))[, , "ge"]) + indicatorTmp <- collapseNames(dimSums(feedProductspecific, + dim = c("ItemCodeItem", "ElementShort"))[, , "ge"]) / quotientTmp + nameIndicator <- "Productivity|Feed conversion efficiency" + indicatorTmp <- setNames(collapseNames(indicatorTmp), paste0(nameIndicator, " (", "GE per GE", ")")) + quotientTmp <- setNames(collapseNames(quotientTmp), paste0(nameIndicator, " (", "GE per GE", ")")) + x <- mbind(x, indicatorTmp) + weight <- mbind(weight, quotientTmp) + + # calculate feed conversion efficiency Livestock specific + quotientTmp <- quotientProductspecfic[, , c("ge", "nr")] + indicatorTmp <- dimSums(feedProductspecific, dim = c("ItemCodeItem"))[, , c("ge", "nr")] / quotientTmp + prefix <- "Productivity|Feed conversion efficiency|" + nameIndicator <- paste0(prefix, getNames(indicatorTmp, dim = 1), " (", "GE per GE", ")") + x <- mbind(x, setNames(collapseNames(indicatorTmp[, , "ge"]), nameIndicator)) + weight <- mbind(weight, setNames(collapseNames(quotientTmp[, , "ge"]), nameIndicator)) + nameIndicator <- paste0(prefix, getNames(indicatorTmp, dim = 1), " (", "Nr per Nr", ")") + x <- mbind(x, setNames(collapseNames(indicatorTmp[, , "nr"]), nameIndicator)) + weight <- mbind(weight, setNames(collapseNames(quotientTmp[, , "nr"]), nameIndicator)) + + # calculate feed conversion efficiency Livestock and product specific + quotientTmp <- collapseNames(quotientProductspecfic[, , "ge"]) + indicatorTmp <- collapseNames(feedProductspecific[, , "ge"]) / quotientTmp + prefix <- "Productivity|Feed conversion efficiency|" + for (item in getNames(feedProductspecific, dim = 2)) { + nameIndicator <- paste0(prefix, item, "|+|", + getNames(indicatorTmp, dim = 1), " (", "GE per GE", ")") + x <- mbind(x, setNames(collapseNames(indicatorTmp)[, , item], nameIndicator)) + weightTmp <- collapseNames(indicatorTmp[, , item]) * NA + weightTmp[, , ] <- collapseNames(quotientTmp[, , item]) + weight <- mbind(weight, setNames(weightTmp, nameIndicator)) + } + + # calculate feed conversion efficiency Product specific + quotientTmp <- collapseNames(dimSums(quotientProductspecfic, dim = c("ItemCodeItem"))[, , "ge"]) + indicatorTmp <- collapseNames(dimSums(feedProductspecific, dim = c("ElementShort"))[, , "ge"]) / quotientTmp + prefix <- "Productivity|Feed conversion efficiency|+|" + nameIndicator <- paste0(prefix, getNames(indicatorTmp, dim = 1), " (", "GE per GE", ")") + x <- mbind(x, setNames(collapseNames(indicatorTmp), nameIndicator)) + weightTmp <- collapseNames(indicatorTmp) * NA + weightTmp[, , ] <- collapseNames(quotientTmp) + weight <- mbind(weight, setNames(weightTmp, nameIndicator)) + + # roughage share for ruminants as quotient of roughage and total feed + quotientTmp <- dimSums(feedProductspecific, dim = "ItemCodeItem") + indicatorTmp <- dimSums( + feedProductspecific[, , c("Other roughage intensity", "Pasture intensity")], + dim = "ItemCodeItem") / quotientTmp + if (livestockSystem == TRUE) { + quotientTmp <- quotientTmp[, , "Ruminant meat and dairy"] + indicatorTmp <- indicatorTmp[, , "Ruminant meat and dairy"] + } + prefix <- "Productivity|Roughage share|" + nameIndicator <- paste0(prefix, getNames(indicatorTmp, dim = 1), " (", "GE per GE", ")") + x <- mbind(x, setNames(collapseNames(indicatorTmp[, , "ge"]), nameIndicator)) + weight <- mbind(weight, setNames(quotientTmp[, , "ge"], nameIndicator)) + + x <- add_dimension(x, dim = 3.1, add = "scenario", nm = "historical") + x <- add_dimension(x, dim = 3.2, add = "model", nm = "Weindl_et_al2017") + + getNames(x) <- sub("\\|$", "", getNames(x)) + getNames(weight) <- sub("\\|$", "", getNames(weight)) + + + return(list(x = x, + weight = weight, + unit = "GE feed per GE product", + description = "Agricultural Demand") + ) +} diff --git a/R/fullVALIDATION.R b/R/fullVALIDATION.R index 3a03e54..ab58bf9 100644 --- a/R/fullVALIDATION.R +++ b/R/fullVALIDATION.R @@ -279,6 +279,10 @@ fullVALIDATION <- function(rev = 0.1) { calcOutput(type = "ValidLSUdensity", aggregate = "REG+GLO", file = valfile, append = TRUE, try = TRUE) calcOutput(type = "ValidAgriResearchIntensity", aggregate = "REG+GLO", datasource = "Pardey", file = valfile, append = TRUE, try = TRUE) + calcOutput(type = "ValidFeedConversion", aggregate = "REG+GLO", + file = valfile, append = TRUE, try = TRUE) + + # Prices calcOutput(type = "ValidPriceAgriculture", datasource = "WBGEM", aggregate = FALSE, diff --git a/README.md b/README.md index 7ad97b0..c86ada3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat data preparation for validation purposes -R package **mrvalidation**, version **2.54.2** +R package **mrvalidation**, version **2.55.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrvalidation)](https://cran.r-project.org/package=mrvalidation) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4317826.svg)](https://doi.org/10.5281/zenodo.4317826) [![R build status](https://github.com/pik-piam/mrvalidation/workflows/check/badge.svg)](https://github.com/pik-piam/mrvalidation/actions) [![codecov](https://codecov.io/gh/pik-piam/mrvalidation/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrvalidation) [![r-universe](https://pik-piam.r-universe.dev/badges/mrvalidation)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky , R package version 2.54.2, . +Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 , R package version 2.55.0, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrvalidation: madrat data preparation for validation purposes}, author = {Benjamin Leon Bodirsky and Stephen Wirth and Kristine Karstens and Florian Humpenoeder and Mishko Stevanovic and Abhijeet Mishra and Anne Biewald and Isabelle Weindl and Felicitas Beier and David Chen and Michael Crawford and Debbora Leip and Edna {Molina Bacca} and Ulrich Kreidenweis and Amsalu {W. Yalew} and Patrick {von Jeetze} and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves}, year = {2024}, - note = {R package version 2.54.2}, + note = {R package version 2.55.0}, doi = {10.5281/zenodo.4317826}, url = {https://github.com/pik-piam/mrvalidation}, }