From a5fc43d2e4438437d8694de676d8d2e124567514 Mon Sep 17 00:00:00 2001 From: Debbora Leip Date: Thu, 15 Aug 2024 18:42:17 +0200 Subject: [PATCH] update to 2017 as currency base year --- R/calcAgEmplILO.R | 6 +++--- R/calcFacReq.R | 4 ++-- R/calcFacReqGLO.R | 4 ++-- R/calcFactorCostsCrops.R | 10 +++++----- R/calcFactorCostsLivst.R | 3 ++- R/calcFactorIntensity.R | 8 ++++---- R/calcHourlyLaborCosts.R | 22 +++++++++++++++------- R/calcIFPRIsubsidy.R | 4 ++-- R/calcLaborCosts.R | 22 +++++++++++----------- R/calcNonMAgPIEFactorCosts.R | 6 +++--- R/calcPricesProducer.R | 8 ++++---- R/calcProductionCosts.R | 6 +++--- R/calcRegFactorShare.R | 2 +- R/calcRegressionsILO.R | 14 +++++++------- R/calcVoPAFF.R | 23 ++++++++--------------- R/calcVoPcrops.R | 12 ++++++------ R/calcVoPlivst.R | 14 +++++++------- R/calcWeeklyHoursILO.R | 4 ++-- R/convertIFPRIsubsidy.R | 4 ++-- R/convertILOSTAT.R | 27 +++++++++++++-------------- R/convertVittis.R | 6 +++--- R/readRegressionsILO.R | 17 ++++++++++++++++- R/readVittis.R | 6 ------ 23 files changed, 121 insertions(+), 111 deletions(-) diff --git a/R/calcAgEmplILO.R b/R/calcAgEmplILO.R index 8634024..4db5393 100644 --- a/R/calcAgEmplILO.R +++ b/R/calcAgEmplILO.R @@ -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 @@ -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", @@ -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 diff --git a/R/calcFacReq.R b/R/calcFacReq.R index 447db73..6809c30 100644 --- a/R/calcFacReq.R +++ b/R/calcFacReq.R @@ -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)) } diff --git a/R/calcFacReqGLO.R b/R/calcFacReqGLO.R index 8e9d4f4..3b383e9 100644 --- a/R/calcFacReqGLO.R +++ b/R/calcFacReqGLO.R @@ -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")) } diff --git a/R/calcFactorCostsCrops.R b/R/calcFactorCostsCrops.R index 40e6dc5..614db7b 100644 --- a/R/calcFactorCostsCrops.R +++ b/R/calcFactorCostsCrops.R @@ -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)) @@ -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") } diff --git a/R/calcFactorCostsLivst.R b/R/calcFactorCostsLivst.R index 9252f5e..9951622 100644 --- a/R/calcFactorCostsLivst.R +++ b/R/calcFactorCostsLivst.R @@ -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)) diff --git a/R/calcFactorIntensity.R b/R/calcFactorIntensity.R index ddfd2e4..1dce131 100644 --- a/R/calcFactorIntensity.R +++ b/R/calcFactorIntensity.R @@ -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 @@ -22,7 +22,7 @@ #' 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 @@ -30,7 +30,7 @@ calcFactorIntensity <- function(output = "intensities", method = "USDA", unit="c # 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)) @@ -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, diff --git a/R/calcHourlyLaborCosts.R b/R/calcHourlyLaborCosts.R index 39d7859..d8b6ad1 100644 --- a/R/calcHourlyLaborCosts.R +++ b/R/calcHourlyLaborCosts.R @@ -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 @@ -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) { @@ -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", @@ -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) @@ -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"] @@ -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) @@ -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")) } diff --git a/R/calcIFPRIsubsidy.R b/R/calcIFPRIsubsidy.R index 61fff2f..9001626 100644 --- a/R/calcIFPRIsubsidy.R +++ b/R/calcIFPRIsubsidy.R @@ -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()] @@ -35,6 +35,6 @@ calcIFPRIsubsidy <- function(fillGaps = TRUE) { return(list(x = subsidies, weight = NULL, - unit = "mio. USDMER05", + unit = "mio. USDMER2017", description = "Agricultural subsidies")) } diff --git a/R/calcLaborCosts.R b/R/calcLaborCosts.R index 42a7ccc..f28de1f 100644 --- a/R/calcLaborCosts.R +++ b/R/calcLaborCosts.R @@ -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? @@ -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 @@ -42,7 +42,7 @@ 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 @@ -50,18 +50,18 @@ calcLaborCosts <- function(datasource = "ILO", dataVersionILO = "Aug23", subsect # 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) @@ -180,9 +180,9 @@ 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") @@ -190,6 +190,6 @@ calcLaborCosts <- function(datasource = "ILO", dataVersionILO = "Aug23", subsect return(list(x = out, weight = NULL, - unit = "mio. USD05MER", + unit = "mio. USD2017MER", description = "labor costs in agriculture")) } diff --git a/R/calcNonMAgPIEFactorCosts.R b/R/calcNonMAgPIEFactorCosts.R index c1e170d..e8c4809 100644 --- a/R/calcNonMAgPIEFactorCosts.R +++ b/R/calcNonMAgPIEFactorCosts.R @@ -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 @@ -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 @@ -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")) } diff --git a/R/calcPricesProducer.R b/R/calcPricesProducer.R index b9f9673..e6fde8f 100644 --- a/R/calcPricesProducer.R +++ b/R/calcPricesProducer.R @@ -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" @@ -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 @@ -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")) } diff --git a/R/calcProductionCosts.R b/R/calcProductionCosts.R index 5a3c6b2..fd28622 100644 --- a/R/calcProductionCosts.R +++ b/R/calcProductionCosts.R @@ -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" @@ -21,7 +21,7 @@ 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", @@ -29,7 +29,7 @@ calcProductionCosts <- function(datasource = "Vittis", unit="constant 2017 US$ME replace_NAs = "no_conversion") } - units <- paste0("mio ",unit) + units <- paste0("mio ", unit) return(list(x = costs, weight = NULL, diff --git a/R/calcRegFactorShare.R b/R/calcRegFactorShare.R index a0af29e..8544658 100644 --- a/R/calcRegFactorShare.R +++ b/R/calcRegFactorShare.R @@ -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)) diff --git a/R/calcRegressionsILO.R b/R/calcRegressionsILO.R index 24f0e6f..a53a5f2 100755 --- a/R/calcRegressionsILO.R +++ b/R/calcRegressionsILO.R @@ -7,7 +7,7 @@ #' "HourlyLaborCosts" for a regression between mean nominal hourly labor cost per employee in #' agriculture and GDP pc MER05. #' @param dataVersionILO which version of the ILO input data and regression to use. "" for the oldest version and -#' old regression, or "monthYear" (e.g. "Aug23") for newer data with the new regression type +#' old regression, or "monthYear" (e.g. "Aug24") for newer data with the new regression type #' @param thresholdWage only relevant for linear hourly labor cost regression: for low GDP pc MER, the regression #' between hourly labor costs and GDP pc MER can lead to unreasonably low or even negative hourly labor costs. #' Therefore, we set all hourly labor costs below this threshold to the threshold. @@ -23,7 +23,7 @@ #' a <- calcOutput("RegressionsILO", subtype = "HourlyLaborCosts") #' } -calcRegressionsILO <- function(subtype = "AgEmplShare", dataVersionILO = "Aug23", +calcRegressionsILO <- function(subtype = "AgEmplShare", dataVersionILO = "Aug24", thresholdWage = 0.1, forceWageIntercept = TRUE, wageRegrType = NULL, recalculate = FALSE) { @@ -71,8 +71,8 @@ calcRegressionsILO <- function(subtype = "AgEmplShare", dataVersionILO = "Aug23" share <- iloEmpl[, getYears(pop), "Total", drop = TRUE] / pop[, , , drop = TRUE] getNames(share) <- "share_empl_ag" - # GDP per capita as independent variable (sqrt(share of people in ag.) ~ log_10(GDPpcPPP)) - gdpPc <- calcOutput("GDPpcPast", GDPpcPast = "WDI-MI", unit = "constant 2005 Int$PPP", aggregate = FALSE) + # GDP per capita as independent variable (updated currency baseyear since Aug 24) + gdpPc <- calcOutput("GDPpcPast", GDPpcPast = "WDI-MI", unit = "constant 2017 Int$PPP", aggregate = FALSE) years <- intersect(getYears(share), getYears(gdpPc)) gdpPc <- gdpPc[, years, ] @@ -96,7 +96,7 @@ calcRegressionsILO <- function(subtype = "AgEmplShare", dataVersionILO = "Aug23" data$transformedGDPpcPPP <- log(data$GDPpcPPP, base = 10) data$transformedshareEmplAg <- sqrt(data$shareEmplAg) - # regression + # regression (sqrt(share of people in ag.) ~ log_10(GDPpcPPP)) reg <- lm(transformedshareEmplAg ~ transformedGDPpcPPP, data = data, weights = pop) intercept <- reg$coefficients["(Intercept)"][[1]] slope <- reg$coefficients["transformedGDPpcPPP"][[1]] @@ -122,8 +122,8 @@ calcRegressionsILO <- function(subtype = "AgEmplShare", dataVersionILO = "Aug23" fillWithRegression = FALSE, aggregate = FALSE) hourlyLaborCosts[hourlyLaborCosts == 0] <- NA - ## GDP PER CAPITA IN US$MER AS DEPENDENT VARIABLE - gdpPcMER <- calcOutput("GDPpcPast", GDPpcPast = "WDI-MI", unit = "constant 2005 US$MER", aggregate = FALSE) + ## GDP PER CAPITA IN US$MER AS DEPENDENT VARIABLE (updated currency baseyear since Aug 24) + gdpPcMER <- calcOutput("GDPpcPast", GDPpcPast = "WDI-MI", unit = "constant 2017 US$MER", aggregate = FALSE) ## combining data years <- intersect(getItems(hourlyLaborCosts, dim = 2), getItems(gdpPcMER, dim = 2)) diff --git a/R/calcVoPAFF.R b/R/calcVoPAFF.R index 0fa90d4..1d814dd 100644 --- a/R/calcVoPAFF.R +++ b/R/calcVoPAFF.R @@ -3,7 +3,7 @@ #' forestry and fisheries sectors. Forestry and Fisheries are calculated from exports values. #' #' @param unit output currency unit based on the convertGDP function from the GDPuc library -#' @return magpie object. in mio. 05USD MER units +#' @return magpie object. in mio. USD #' @author Edna J. Molina Bacca, Debbora Leip #' @importFrom dplyr intersect #' @importFrom magclass dimSums @@ -15,11 +15,11 @@ #' a <- calcOutput("VoPAFF") #' } #' -calcVoPAFF <- function(unit="constant 2017 US$MER") { +calcVoPAFF <- function(unit = "constant 2017 US$MER") { #### Value of production for Agriculture (crops and livestock) - vopCrops <- calcOutput("VoPcrops", aggregate = FALSE,unit="constant 2017 US$MER") - vopLivst <- calcOutput("VoPlivst", other = TRUE, aggregate = FALSE, unit="constant 2017 US$MER") + vopCrops <- calcOutput("VoPcrops", aggregate = FALSE, unit = unit) + vopLivst <- calcOutput("VoPlivst", other = TRUE, aggregate = FALSE, unit = unit) vopAg <- setNames(dimSums(vopCrops, dim = 3) + dimSums(vopLivst, dim = 3), "Agriculture") @@ -47,7 +47,7 @@ calcVoPAFF <- function(unit="constant 2017 US$MER") { prodFishTonNet[cellsFish, yearsFish, ] / 1000 # mio. current USD vopFish <- convertGDP(vopFish, unit_in = "current US$MER", - unit_out = "constant 2017 US$MER", + unit_out = unit, replace_NAs = "no_conversion") vopFish[!is.finite(vopFish)] <- 0 @@ -68,7 +68,7 @@ calcVoPAFF <- function(unit="constant 2017 US$MER") { # Base year change for exports value priceForestry <- convertGDP(priceForestry, unit_in = "current US$MER", - unit_out = "constant 2017 US$MER", + unit_out = unit, replace_NAs = "no_conversion") priceForestry[!is.finite(priceForestry)] <- 0 @@ -89,15 +89,8 @@ calcVoPAFF <- function(unit="constant 2017 US$MER") { x <- mbind(vopAg[cellsVoP, yearsVoP, ], vopFish[cellsVoP, yearsVoP, ], vopForestry[cellsVoP, yearsVoP, ]) x[!is.finite(x)] <- 0 - - if(unit != "constant 2017 US$MER"){ - x<-convertGDP(x, - unit_in = "constant 2017 US$MER", - unit_out = unit, - replace_NAs = "no_conversion") - } - - units <- paste0("mio ",unit) + + units <- paste0("mio ", unit) return(list(x = x, weight = NULL, diff --git a/R/calcVoPcrops.R b/R/calcVoPcrops.R index e49df24..8fcdb03 100644 --- a/R/calcVoPcrops.R +++ b/R/calcVoPcrops.R @@ -4,7 +4,7 @@ #' #' @param fillGaps boolean: should gaps be filled using production * prices (where production data is available)? #' @param unit output currency unit based on the convertGDP function from the GDPuc library -#' @return magpie object. in mio. USD05 MER or fraction +#' @return magpie object. in mio. USD or fraction #' @author Edna J. Molina Bacca, Debbora Leip #' @importFrom dplyr intersect #' @importFrom GDPuc convertGDP @@ -16,7 +16,7 @@ #' a <- calcOutput("VoPcrops") #' } #' -calcVoPcrops <- function(fillGaps = TRUE, unit="constant 2017 US$MER") { +calcVoPcrops <- function(fillGaps = TRUE, unit = "constant 2017 US$MER") { # Value of production of individual items (US$MER17) item <- "Gross_Production_Value_(USDMER17)_(1000_US$)" vopAll <- readSource("FAO_online", "ValueOfProd")[, , item] / 1000 # mio. US$MER17 @@ -96,10 +96,10 @@ calcVoPcrops <- function(fillGaps = TRUE, unit="constant 2017 US$MER") { weight <- NULL if(unit != "constant 2017 US$MER"){ - vopKcrAggregated<-convertGDP(vopKcrAggregated, - unit_in = "constant 2017 US$MER", - unit_out = unit, - replace_NAs = "no_conversion") + vopKcrAggregated <- convertGDP(vopKcrAggregated, + unit_in = "constant 2017 US$MER", + unit_out = unit, + replace_NAs = "no_conversion") } diff --git a/R/calcVoPlivst.R b/R/calcVoPlivst.R index cfbe580..b3b9dbb 100644 --- a/R/calcVoPlivst.R +++ b/R/calcVoPlivst.R @@ -5,7 +5,7 @@ #' silkworms, and honey) be reported as "livst_other"? #' @param fillGaps boolean: should gaps be filled using production * prices (where production data is available)? #' @param unit output currency unit based on the convertGDP function from the GDPuc library -#' @return magpie object. in mio. USDMER05 +#' @return magpie object #' @author Debbora Leip #' @importFrom GDPuc convertGDP #' @importFrom magpiesets findset @@ -16,7 +16,7 @@ #' a <- calcOutput("VoPlivst") #' } #' -calcVoPlivst <- function(other = FALSE, fillGaps = TRUE, unit="constant 2017 US$MER") { +calcVoPlivst <- function(other = FALSE, fillGaps = TRUE, unit = "constant 2017 US$MER") { # Value of production of individual items (US$MER05) item <- "Gross_Production_Value_(USDMER17)_(1000_US$)" @@ -72,13 +72,13 @@ calcVoPlivst <- function(other = FALSE, fillGaps = TRUE, unit="constant 2017 US$ if(unit != "constant 2017 US$MER"){ - vopLivst<-convertGDP(vopLivst, - unit_in = "constant 2017 US$MER", - unit_out = unit, - replace_NAs = "no_conversion") + vopLivst <- convertGDP(vopLivst, + unit_in = "constant 2017 US$MER", + unit_out = unit, + replace_NAs = "no_conversion") } - units <- paste0("mio ",unit) + units <- paste0("mio ", unit) return(list(x = vopLivst, weight = NULL, diff --git a/R/calcWeeklyHoursILO.R b/R/calcWeeklyHoursILO.R index 08dfe2a..e3314fa 100644 --- a/R/calcWeeklyHoursILO.R +++ b/R/calcWeeklyHoursILO.R @@ -3,7 +3,7 @@ #' fishery based on ILO dataset #' @param projections boolean, should weekly hours be projected (by keeping constant) up to 2150? #' @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. #' @author Debbora Leip #' @examples @@ -12,7 +12,7 @@ #' } #' @importFrom magclass getNames<- getYears getRegions where time_interpolate dimSums -calcWeeklyHoursILO <- function(projections = FALSE, dataVersionILO = "Aug23") { +calcWeeklyHoursILO <- function(projections = FALSE, dataVersionILO = "Aug24") { dataType <- ifelse(dataVersionILO == "", "WeeklyHoursByActivity", paste("WeeklyHoursByActivity", dataVersionILO, sep = "_")) diff --git a/R/convertIFPRIsubsidy.R b/R/convertIFPRIsubsidy.R index fc8c043..4c538bb 100644 --- a/R/convertIFPRIsubsidy.R +++ b/R/convertIFPRIsubsidy.R @@ -30,10 +30,10 @@ convertIFPRIsubsidy <- function(x) { x <- toolCountryFill(x, fill = 0) x[is.na(x)] <- 0 - # convert to USDMER05 (for years with missing conversion factors we assume no inflation) + # convert to USDMER2017 (for years with missing conversion factors we assume no inflation) x <- GDPuc::convertGDP(x, 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" ) return(x) diff --git a/R/convertILOSTAT.R b/R/convertILOSTAT.R index aa220e4..740897c 100644 --- a/R/convertILOSTAT.R +++ b/R/convertILOSTAT.R @@ -14,23 +14,22 @@ convertILOSTAT <- function(x) { # transform currencies if applicable if ("currency" %in% getSets(x)) { - x[, , "2017 PPP $"] <- convertGDP( - x[, , "2017 PPP $"], - unit_in = "constant 2017 Int$PPP", - unit_out = "constant 2005 Int$PPP", - replace_NAs = c("linear", "no_conversion") - ) - x[, , "US dollars"] <- convertGDP( - x[, , "US dollars"], - unit_in = "current US$MER", - unit_out = "constant 2005 US$MER", - replace_NAs = c("linear", "no_conversion") - ) + if ("2021 PPP $" %in% getNames(x, dim = "currency")) { + x[, , "2021 PPP $"] <- convertGDP(x[, , "2021 PPP $"], + unit_in = "constant 2021 Int$PPP", + unit_out = "constant 2017 Int$PPP", + replace_NAs = c("linear", "no_conversion")) + } + + x[, , "US dollars"] <- convertGDP(x[, , "US dollars"], + unit_in = "current US$MER", + unit_out = paste("constant 2017 US$MER"), + replace_NAs = c("linear", "no_conversion")) # update unit description getNames(x, dim = "currency")[getNames(x, dim = "currency") == "Local currency"] <- "current LCU" - getNames(x, dim = "currency")[getNames(x, dim = "currency") == "2017 PPP $"] <- "US$PPP2005" - getNames(x, dim = "currency")[getNames(x, dim = "currency") == "US dollars"] <- "US$MER2005" + getNames(x, dim = "currency")[getNames(x, dim = "currency") == "2021 PPP $"] <- "US$PPP2017" + getNames(x, dim = "currency")[getNames(x, dim = "currency") == "US dollars"] <- "US$MER2017" } # set missing values to 0 diff --git a/R/convertVittis.R b/R/convertVittis.R index 8fe9faa..094cc39 100644 --- a/R/convertVittis.R +++ b/R/convertVittis.R @@ -1,6 +1,6 @@ #' @title convertVittis #' @description Aggregate production costs from Vittis dataset to MAgPIE crop -#' categories and change unit from US$2000 to US$2005. +#' categories and change unit from USD PPP 2000 to USD MER 2017. #' @param x MAgPIE object to be converted #' @return A MAgPIE object containing national-scale costs of production for #' 10 crops, disaggregated in 9 distinct cost elements @@ -17,8 +17,8 @@ convertVittis <- function(x) { toolAggregate(mapping, from = "ProductionItem", to = "Vittis", dim = 3) x <- toolAggregate(x, mapping, weight = weights, from = "Vittis", to = "kcr", dim = 3.2) - # Convert from "constant 2000 Int$PPP" to "constant 2005 Int$PPP" - x <- GDPuc::convertGDP(x, "constant 2000 Int$PPP", "constant 2005 Int$PPP", replace_NAs = c("linear", 0)) + # Convert from "constant 2000 Int$PPP" to "constant 2017 US$MER" + x <- GDPuc::convertGDP(x, "constant 2000 Int$PPP", "constant 2017 US$MER", replace_NAs = c("linear", 0)) # fill missing countries with average over corresponding world region mapping <- toolGetMapping("regionmappingH12.csv", type = "regional", where = "mappingfolder") diff --git a/R/readRegressionsILO.R b/R/readRegressionsILO.R index a9d1cb2..04ff01b 100644 --- a/R/readRegressionsILO.R +++ b/R/readRegressionsILO.R @@ -7,7 +7,7 @@ #' log(hourly labor costs) ~ log(GDP pc MER) (new version) #' } #' The version of regression and underlying data can be chosen by adding a suffix to the subtype, "" for the oldest -#' version, or "_monthYear" (e.g. "_Aug23") for newer version +#' version, or "_monthYear" (e.g. "_Aug24") for newer version #' @return regression coefficients as MAgPIE object #' @author Debbora Leip #' @examples @@ -22,6 +22,21 @@ readRegressionsILO <- function(subtype = "AgEmplShare") { regrType <- str_split(subtype, "_")[[1]][1] if (!(regrType %in% c("AgEmplShare", "HourlyLaborCosts"))) stop("Invalid subtype.") + # data version + dataVersion <- str_split(subtype, "_")[[1]][2] + if (!is.na(dataVersion)) { + month <- match(substr(dataVersion, 1, 3), month.abb) + month <- ifelse(month < 10, paste0("0", month), as.character(month)) + dataVersion <- as.numeric(paste0(substr(dataVersion, 4, 5), month)) + } else { + dataVersion <- 0 + } + if (dataVersion < 2408) { + stop("This version of the regression has been created before the GDP update in August 2024 and is thus based ", + "on GDP using 2005 instead of 2017 as base year. It is incompatible with the data used in this version of ", + "mrfactors. Please used mrfactors 0.4.0 or ealier when using this ILO data version.") + } + # valid version? if (!file.exists(paste0(subtype, ".csv"))) { stop(paste0("This version of the regression (", subtype, ") has not been added to the source folder yet.")) diff --git a/R/readVittis.R b/R/readVittis.R index aad0c98..56c2f8e 100644 --- a/R/readVittis.R +++ b/R/readVittis.R @@ -27,11 +27,5 @@ readVittis <- function() { "Seeds", "Financing", "Fuel_Lubricant", "Fertilizer", "Irrigation", "Labor") - - data<-convertGDP(data, - unit_in = "constant 2005 US$MER", - unit_out = "constant 2017 US$MER", - replace_NAs = "no_conversion") - return(data) }