Skip to content

Commit

Permalink
Merge pull request #2 from deleip/gdp_update
Browse files Browse the repository at this point in the history
update to 2017 as currency base year
  • Loading branch information
deleip authored Aug 15, 2024
2 parents af78a7a + a5fc43d commit 4b0ae2d
Show file tree
Hide file tree
Showing 23 changed files with 121 additions and 111 deletions.
6 changes: 3 additions & 3 deletions R/calcAgEmplILO.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param inclFish boolean: should employment in fisheries be included?
#' @param inclForest boolean: should emplyoment in forestry be included?
#' @param dataVersionILO which version of the ILO input data to use. "" for the oldest version and
#' old regression, or "monthYear" (e.g. "Aug23") for newer data
#' old regression, or "monthYear" (e.g. "Aug24") for newer data
#' @return List of magpie objects with results on country level, weight on country level, unit and description.
#' @importFrom stringr str_split
#' @author Debbora Leip
Expand All @@ -16,7 +16,7 @@
#' calcOutput("AgEmplILO")
#' }

calcAgEmplILO <- function(subsectors = TRUE, inclFish = FALSE, inclForest = FALSE, dataVersionILO = "Aug23") {
calcAgEmplILO <- function(subsectors = TRUE, inclFish = FALSE, inclForest = FALSE, dataVersionILO = "Aug24") {

# read original employment dataset from ILO (convert from thous. to mil.)
dataType <- ifelse(dataVersionILO == "", "EmplByActivityModelled",
Expand All @@ -33,7 +33,7 @@ calcAgEmplILO <- function(subsectors = TRUE, inclFish = FALSE, inclForest = FALS
subtype <- ifelse(dataVersionILO == "", "AgEmplShare", paste("AgEmplShare", dataVersionILO, sep = "_"))
regCoeff <- readSource("RegressionsILO", subtype)

gdpPPPpc <- calcOutput("GDPpcPast", GDPpcPast = "WDI-MI", unit = "constant 2005 Int$PPP", aggregate = FALSE)
gdpPPPpc <- calcOutput("GDPpcPast", GDPpcPast = "WDI-MI", unit = "constant 2017 Int$PPP", aggregate = FALSE)

estShare <- (regCoeff[, , "slope", drop = TRUE] * log10(gdpPPPpc) + regCoeff[, , "intercept", drop = TRUE]) ** 2
const <- (regCoeff[, , "slope"] * log10(regCoeff[, , "threshold"]) + regCoeff[, , "intercept"]) ** 2
Expand Down
4 changes: 2 additions & 2 deletions R/calcFacReq.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ calcFacReq <- function(splitSectors = FALSE) {
}

return(list(x = facReq,
unit = "USD05MER per tDM",
unit = "USD2017MER per tDM",
weight = weight,
description = "Factor requirements for different crops (USD05MER per tDM) at regional level",
description = "Factor requirements for different crops at regional level",
isocountries = FALSE))
}
4 changes: 2 additions & 2 deletions R/calcFacReqGLO.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ calcFacReqGLO <- function() {

return(list(x = x,
weight = NULL,
unit = "USD05MER per tDM",
description = "Factor requirements for different crops (USD05MER per tDM) at global level"))
unit = "USD2017MER per tDM",
description = "Factor requirements for different crops at global level"))
}
10 changes: 5 additions & 5 deletions R/calcFactorCostsCrops.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#' }
#' @importFrom magclass setNames dimSums time_interpolate

calcFactorCostsCrops <- function(datasource = "USDA", unit="constant 2017 US$MER") {
calcFactorCostsCrops <- function(datasource = "USDA", unit = "constant 2017 US$MER") {

if (datasource == "USDA") {
# Value of Production for livestock in US$MER2017 (including FAO livst categories not mapped to MAgPIE categories)
vopCrops <- calcOutput("VoPcrops", fillGaps = TRUE, aggregate = FALSE, unit="constant 2017 US$MER")
vopCrops <- calcOutput("VoPcrops", fillGaps = TRUE, aggregate = FALSE, unit = "constant 2017 US$MER")

# no VoP data before 1991, data for 2019 incomplete
years <- setdiff(getYears(vopCrops, as.integer = TRUE), c(1960:1990, 2019))
Expand Down Expand Up @@ -56,9 +56,9 @@ calcFactorCostsCrops <- function(datasource = "USDA", unit="constant 2017 US$MER

if(unit != "constant 2017 US$MER"){
out <- convertGDP(out,
unit_in = "constant 2017 US$MER",
unit_out = unit,
replace_NAs = "no_conversion")
unit_in = "constant 2017 US$MER",
unit_out = unit,
replace_NAs = "no_conversion")
}


Expand Down
3 changes: 2 additions & 1 deletion R/calcFactorCostsLivst.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ calcFactorCostsLivst <- function(datasource = "USDA", otherLivst = FALSE, unit =

if (datasource == "USDA") {
# Value of Production for livestock in US$MER2005 (including FAO livst categories not mapped to MAgPIE categories)
vopLivst <- calcOutput("VoPlivst", fillGaps = TRUE, aggregate = FALSE, other = otherLivst, unit = "constant 2017 US$MER")
vopLivst <- calcOutput("VoPlivst", fillGaps = TRUE, aggregate = FALSE, other = otherLivst,
unit = "constant 2017 US$MER")

# no VoP data before 1991, data for 2019 incomplete
years <- setdiff(getYears(vopLivst, as.integer = TRUE), c(1960:1990, 2019))
Expand Down
8 changes: 4 additions & 4 deletions R/calcFactorIntensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' For the "CapitalStock" method only "intensities" and "requirements" outputs supported.
#' @param method "USDA" or "CapitalStock"
#' @param unit output currency unit based on the convertGDP function from the GDPuc library
#' @return magpie object of the factor requirements intensity or factor intensity in 05USDppp/tDM per crop,
#' @return magpie object of the factor requirements intensity or factor intensity in USD/tDM per crop,
#' or capital share fraction.
#' @author Edna J. Molina Bacca
#' @importFrom luscale speed_aggregate
Expand All @@ -22,15 +22,15 @@
#' a <- calcOutput("FactorIntensity")
#' }
#'
calcFactorIntensity <- function(output = "intensities", method = "USDA", unit="constant 2017 US$MER") {
calcFactorIntensity <- function(output = "intensities", method = "USDA", unit = "constant 2017 US$MER") {


if (method == "USDA") { # using USDA method

# Production of crops. mio. ton
cropProdDMall <- collapseDim(calcOutput("Production", products = "kcr", aggregate = FALSE, attributes = "dm"))

vopCrops <- calcOutput("VoPcrops", aggregate = FALSE, unit="constant 2017 US$MER")
vopCrops <- calcOutput("VoPcrops", aggregate = FALSE, unit = "constant 2017 US$MER")

gnames <- intersect(getNames(vopCrops), getNames(cropProdDMall))
gyears <- intersect(getYears(vopCrops), getYears(cropProdDMall))
Expand Down Expand Up @@ -127,7 +127,7 @@ calcFactorIntensity <- function(output = "intensities", method = "USDA", unit="c
}

units <-
if (output %in% c("intensities", "requirements")) paste0("mio ",unit) else if (output == "CapitalShare") "fraction"
if (output %in% c("intensities", "requirements")) paste0("mio ", unit) else if (output == "CapitalShare") "fraction"

if(unit != "constant 2017 US$MER" & output %in% c("intensities", "requirements")){
x <- convertGDP(x,
Expand Down
22 changes: 15 additions & 7 deletions R/calcHourlyLaborCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param datasource either raw data from "ILO" (agriculture+forestry+fishery) or data calculated based on total labor
#' costs from "USDA_FAO" (crop+livestock production).
#' @param dataVersionILO Which version of ILO data to use (for hourly labor costs if source is ILO, for ag empl. if
#' source is USDA_FAO). "" for the oldest version, or "monthYear" (e.g. "Aug23") for a newer version)
#' source is USDA_FAO). "" for the oldest version, or "monthYear" (e.g. "Aug24") for a newer version)
#' @param sector should average hourly labor costs be reported ("agriculture"), or hourly labor costs specific to
#' either "crops" or "livestock" production. For ILO only the aggregate hourly labor costs are available.
#' @param fillWithRegression boolean: should missing values be filled based on a regression between ILO hourly labor
Expand All @@ -25,7 +25,7 @@
#' @importFrom stringr str_split str_to_title


calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug23", # nolint: cyclocomp_linter
calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug24", # nolint: cyclocomp_linter
sector = "agriculture", fillWithRegression = TRUE, calibYear = 2010,
cutAfterCalibYear = TRUE, projection = FALSE) {

Expand All @@ -40,7 +40,7 @@ calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug2
items <- c("ISIC_Rev31: A Agriculture, hunting and forestry", "ISIC_Rev31: B Fishing",
"ISIC_Rev4: A Agriculture; forestry and fishing")
hourlyCosts <- readSource("ILOSTAT", datasource)[, , items]
hourlyCosts <- hourlyCosts[, , "US$MER2005", drop = TRUE]
hourlyCosts <- hourlyCosts[, , "US$MER2017", drop = TRUE]

# aggregate within rev 3.1
mapping <- data.frame(from = c("ISIC_Rev31: A Agriculture, hunting and forestry",
Expand Down Expand Up @@ -68,6 +68,10 @@ calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug2
hourlyCostsIndia <- c(0.2063, 0.2080, 0.2015, 0.1970, 0.1936, 0.1964, 0.2014, 0.2350, 0.2520,
0.2672, 0.3097, 0.3336, 0.3568, 0.3795, 0.3903, 0.3956, 0.4008)
hourlyCosts["IND", setdiff(2000:2017, 2006), ] <- hourlyCostsIndia
hourlyCosts["IND", , ] <- convertGDP(hourlyCosts["IND", , ],
unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = c("linear", "no_conversion"))

# add data for China, aggregated using production as weight (provided by Xiaoxi)
hourlyCostsChina <- readSource("HourlyLaborCostsChina", convert = FALSE)
Expand All @@ -84,12 +88,16 @@ calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug2

years <- intersect(getYears(hourlyCosts), getYears(hourlyCostsChina))
hourlyCosts["CHN", years, ] <- hourlyCostsChina[, years, ]
hourlyCosts["CHN", , ] <- convertGDP(hourlyCosts["CHN", , ],
unit_in = "constant 2005 US$MER",
unit_out = "constant 2017 US$MER",
replace_NAs = c("linear", "no_conversion"))

# remove outliers
hourlyCosts[hourlyCosts > 100] <- 0 # unreasonable high values
gdpMERpc <- calcOutput("GDPpc",
scenario = "SSPs",
unit = "constant 2005 US$MER",
unit = "constant 2017 US$MER",
average2020 = FALSE,
naming = "scenario",
aggregate = FALSE)[getItems(hourlyCosts, dim = 1), getItems(hourlyCosts, dim = 2), "SSP2"]
Expand Down Expand Up @@ -138,10 +146,10 @@ calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug2
hourlyCosts <- calcOutput("HourlyLaborCosts", dataVersionILO = dataVersionILO, datasource = datasource,
sector = sector, fillWithRegression = FALSE, aggregate = FALSE)

# calculate GDPpc [USD05MER] for regression
# calculate GDPpc [USD2017MER] for regression
gdpMERpc <- calcOutput("GDPpc",
scenario = "SSPs",
unit = "constant 2005 US$MER",
unit = "constant 2017 US$MER",
average2020 = FALSE,
naming = "scenario",
aggregate = FALSE)
Expand Down Expand Up @@ -257,6 +265,6 @@ calcHourlyLaborCosts <- function(datasource = "USDA_FAO", dataVersionILO = "Aug2

return(list(x = hourlyCosts,
weight = weight,
unit = "US$05MER",
unit = "US$2017MER",
description = "Mean nominal hourly labour cost per employee in agriculture"))
}
4 changes: 2 additions & 2 deletions R/calcIFPRIsubsidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @description Adds non-allocated subsidies to crop subsidies (as most subsidies are linked to land area), and
#' excludes NRP subsidies (as those are border measures, which are already reflected in ag. prices)
#' @param fillGaps boolean, should gaps in the dataset be filled using interpolation?
#' @return magpie object. in mio. USDMER05
#' @return magpie object. in mio. USD
#' @author Debbora Leip
#'
#' @seealso [calcOutput()]
Expand Down Expand Up @@ -35,6 +35,6 @@ calcIFPRIsubsidy <- function(fillGaps = TRUE) {

return(list(x = subsidies,
weight = NULL,
unit = "mio. USDMER05",
unit = "mio. USDMER2017",
description = "Agricultural subsidies"))
}
22 changes: 11 additions & 11 deletions R/calcLaborCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param datasource data source on which the labor costs should be based. Available are ILO, USDA (which also uses data
#' on VoP from FAO), and GTAP.
#' @param dataVersionILO If source is ILO, the version can be chosen. "" for the oldest version, or "monthYear" (e.g.
#' "Aug23") for a newer version)
#' "Aug24") for a newer version)
#' @param subsectors boolean: should output be aggregated or split into available subsectors (crops, livst, forestry,
#' fishery)
#' @param inclFish boolean: should fish labor costs be included?
Expand All @@ -24,7 +24,7 @@
#' @importFrom GDPuc convertGDP
#' @importFrom stringr str_split

calcLaborCosts <- function(datasource = "ILO", dataVersionILO = "Aug23", subsectors = TRUE, inclFish = FALSE,
calcLaborCosts <- function(datasource = "ILO", dataVersionILO = "Aug24", subsectors = TRUE, inclFish = FALSE,
inclForest = FALSE, otherLivst = TRUE, gtapVar = "NVFA", addSubsidies = FALSE) {

# get data from specified source
Expand All @@ -42,26 +42,26 @@ calcLaborCosts <- function(datasource = "ILO", dataVersionILO = "Aug23", subsect
iloEmpl <- iloEmpl[, years, ]
iloWeeklyHours <- iloWeeklyHours[, years, ]

# mean nominal hourly labor cost per employee in agriculture (US$05MER/h) based on USDA_FAO calculation
# mean nominal hourly labor cost per employee in agriculture (US$2017MER/h) based on USDA_FAO calculation
# and regression based on ILO raw data
iloLaborCosts <- calcOutput("HourlyLaborCosts", dataVersionILO = dataVersionILO, datasource = datasource,
fillWithRegression = TRUE, projection = "SSP2", # scenario has no influence until 2020
aggregate = FALSE)[, years, ]

# combine data sets to get total labor costs
iloTotalHours <- 52.1429 * iloWeeklyHours[, , , drop = TRUE] * iloEmpl # mio. hours
out <- iloTotalHours * iloLaborCosts[, , , drop = TRUE] # mio. US$MER05
out <- iloTotalHours * iloLaborCosts[, , , drop = TRUE] # mio. US$MER2017

} else if (datasource == "USDA") {

if (isTRUE(inclForest)) stop("Forest labor costs not available for this datasource")

# Value of Production for livestock in US$MER2005 (including FAO livst categories not mapped to MAgPIE categories)
vopLivst <- calcOutput("VoPlivst", other = otherLivst, fillGaps = TRUE, aggregate = FALSE) # mio. US$MER05
# Value of Production for livestock in US$MER2017 (including FAO livst categories not mapped to MAgPIE categories)
vopLivst <- calcOutput("VoPlivst", other = otherLivst, fillGaps = TRUE, aggregate = FALSE) # mio. US$MER2017
vopLivst <- setNames(dimSums(vopLivst, dim = 3), "Livestock")

# Value of Production for crops in US$MER2005
vopCrops <- calcOutput("VoPcrops", fillGaps = TRUE, aggregate = FALSE) # mio. US$MER05
# Value of Production for crops in US$MER2017
vopCrops <- calcOutput("VoPcrops", fillGaps = TRUE, aggregate = FALSE) # mio. US$MER2017
vopCrops <- setNames(dimSums(vopCrops, dim = 3), "Crops")

# no VoP data before 1991, data for 2019 incomplete (using fillGaps in calcVoP reduces years to 1991:2013 anyway)
Expand Down Expand Up @@ -180,16 +180,16 @@ calcLaborCosts <- function(datasource = "ILO", dataVersionILO = "Aug23", subsect
# aggregate if subsectors == FALSE
if (isFALSE(subsectors)) laborCosts <- dimSums(laborCosts, dim = 3)

# convert to USDMER05 (for countries missing the inflation factor, we assume no inflation)
# convert to USDMER2017 (for countries missing the inflation factor, we assume no inflation)
out <- convertGDP(laborCosts, unit_in = "current US$MER",
unit_out = "constant 2005 US$MER", replace_NAs = "no_conversion")
unit_out = "constant 2017 US$MER", replace_NAs = "no_conversion")

} else {
stop("Data source not available")
}

return(list(x = out,
weight = NULL,
unit = "mio. USD05MER",
unit = "mio. USD2017MER",
description = "labor costs in agriculture"))
}
6 changes: 3 additions & 3 deletions R/calcNonMAgPIEFactorCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param aggSubsidies boolean: if subtype is "subsidies", should crop and livestock subsidies be reported separately
#' or as aggregate?
#' @param extrapolate boolean: should values be extrapolate (by keeping constant) until 2150?
#' @return magpie object. in mio. USDMER05
#' @return magpie object. in mio. USD
#' @author Debbora Leip
#' @seealso [calcOutput()]
#' @examples
Expand All @@ -24,7 +24,7 @@ calcNonMAgPIEFactorCosts <- function(subtype = "subsidies", aggSubsidies = FALSE
fractionLivst <- setNames(dimSums(fractionLivst[, , c("Labor", "Capital")], dim = 3), "Livestock")
fractions <- mbind(fractionCrops, fractionLivst)

if (subtype == "subsidies") { # ag. subsidies in mio.USD05MER
if (subtype == "subsidies") { # ag. subsidies in mio.USD2017MER
out <- calcOutput("IFPRIsubsidy", aggregate = FALSE, fillGaps = TRUE)

# get factor cost share of subsidies
Expand Down Expand Up @@ -71,6 +71,6 @@ calcNonMAgPIEFactorCosts <- function(subtype = "subsidies", aggSubsidies = FALSE

return(list(x = out,
weight = NULL,
unit = "mio. USDMER05",
unit = "mio. USDMER2017",
description = "Factor costs affecting employment but not included in MAgPIE factor costs"))
}
8 changes: 4 additions & 4 deletions R/calcPricesProducer.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ calcPricesProducer <- function(products = "kcr", calculation = "VoP", weighting
if (products == "kcr") {
if (calculation == "FAO") {
# items for aggregation
pricesProdFAO <- readSource("FAO_online", "PricesProducerAnnual") # USD05MER per ton wet matter
pricesProdFAO <- readSource("FAO_online", "PricesProducerAnnual") # USD2017MER per ton wet matter


getNames(pricesProdFAO)[getNames(pricesProdFAO) == "254|Oil palm fruit"] <- "254|Oil, palm fruit"
Expand Down Expand Up @@ -128,7 +128,7 @@ stop("only production and consumption weights")

if (calculation == "FAO") {

pricesProdFAO <- readSource("FAO_online", "PricesProducerAnnual") # USD05 per ton
pricesProdFAO <- readSource("FAO_online", "PricesProducerAnnual") # USD2017 per ton

# get mapping
mappingFAO <- toolGetMapping("FAOitems_online.csv", type = "sectoral", where = "mappingfolder") # Reads mapping
Expand Down Expand Up @@ -203,11 +203,11 @@ stop("invalid type")
}
}

units <- "05USDMER/tDM"
units <- "2017USDMER/tDM"

return(list(x = x,
weight = weight,
mixed_aggregation = NULL,
unit = units,
description = "Producer prices in 05USDMER/tDM"))
description = "Producer prices in 2017USDMER/tDM"))
}
6 changes: 3 additions & 3 deletions R/calcProductionCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' calcOutput("ProductionCosts", source = "Vittis")
#' }
#'
calcProductionCosts <- function(datasource = "Vittis", unit="constant 2017 US$MER") {
calcProductionCosts <- function(datasource = "Vittis", unit = "constant 2017 US$MER") {
if (datasource == "Vittis") {
costs <- readSource("Vittis") # US$17/ha
areas <- calcOutput("Croparea", sectoral = "kcr", aggregate = FALSE) # "million ha"
Expand All @@ -21,15 +21,15 @@ calcProductionCosts <- function(datasource = "Vittis", unit="constant 2017 US$ME
stop("Source not available")
}

if(unit !="constant 2017 US$MER"){
if (unit != "constant 2017 US$MER") {

costs<-convertGDP(costs,
unit_in = "constant 2017 US$MER",
unit_out = unit,
replace_NAs = "no_conversion")
}

units <- paste0("mio ",unit)
units <- paste0("mio ", unit)

return(list(x = costs,
weight = NULL,
Expand Down
2 changes: 1 addition & 1 deletion R/calcRegFactorShare.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ calcRegFactorShare <- function(datasource = "USDA", factor = "cap") {
usdaShares <- usdaShares / dimSums(usdaShares, dim = 3)

# GDP (US$PPP) per capita as independent variable
gdp <- calcOutput("GDPpc", naming = "scenario", aggregate = FALSE)[, , "SSP2"]
gdp <- calcOutput("GDPpc", unit = "constant 2017 Int$PPP", naming = "scenario", aggregate = FALSE)[, , "SSP2"]
country <- intersect(getCells(gdp), unique(mapping$ISO))
mapping2 <- mapping[mapping$ISO %in% country, ]
years <- intersect(getYears(gdp), getYears(weight))
Expand Down
Loading

0 comments on commit 4b0ae2d

Please sign in to comment.