Skip to content

Commit

Permalink
adding feed conversion validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bodirsky committed Jan 25, 2024
1 parent cc29c77 commit 4738e00
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '50158744'
ValidationKey: '50354850'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
person("Stephen", "Wirth", role = "aut"),
Expand Down
166 changes: 166 additions & 0 deletions R/calcValidFeedConversion.R
Original file line number Diff line number Diff line change
@@ -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")
)
}
4 changes: 4 additions & 0 deletions R/fullVALIDATION.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky <bodirsky@

To cite package **mrvalidation** in publications use:

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 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.54.2, <https://github.com/pik-piam/mrvalidation>.
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 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.55.0, <https://github.com/pik-piam/mrvalidation>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
Expand Down

0 comments on commit 4738e00

Please sign in to comment.