# Handling missing conversion factors

## The `replace_NAs` argument

Use the `replace_NAs` argument in `convertGDP` to handle missing conversion factors.

### `replace_NAs` = NULL or NA

By default, `replace_NAs` is `NULL`, and NAs are returned along with a warning. Set `replace_NAs = NA` to explicitly return NAs without the warning.

Below, the `return_cfs` argument is set to `TRUE` to inspect the conversion factors, along side the result.

    library(GDPuc)

    # Test with Venezuela -> iso3c = VEN
    my_gdp <- tibble::tibble(
      iso3c = c("VEN"),
      year = 2010:2014,
      value = 100:104
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      return_cfs = TRUE
    )
    #> Warning: NAs have been generated for countries lacking conversion factors!
    x$result
    #> # A tibble: 5 × 3
    #>   iso3c  year value
    #>   <chr> <int> <dbl>
    #> 1 VEN    2010    NA
    #> 2 VEN    2011    NA
    #> 3 VEN    2012    NA
    #> 4 VEN    2013    NA
    #> 5 VEN    2014    NA

    x$cfs
    #> # A tibble: 1 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                     NA                     NA
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factor in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflator in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factor in (LCU per international $)`

To eliminate the warning:

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = NA
    )

You can also use the `GDPuc.warn` option to suppress warnings from `convertGDP` in general (see “Silence warnings”).

### `replace_NAs` = 0

If set to 0, resulting NAs are set to 0.

    my_gdp <- tibble::tibble(
      iso3c = "VEN",
      year = 2010:2014,
      value = 100:104
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = 0,
      return_cfs = TRUE
    )
    x$result
    #> # A tibble: 5 × 3
    #>   iso3c  year value
    #>   <chr> <int> <dbl>
    #> 1 VEN    2010     0
    #> 2 VEN    2011     0
    #> 3 VEN    2012     0
    #> 4 VEN    2013     0
    #> 5 VEN    2014     0

    x$cfs
    #> # A tibble: 1 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                     NA                     NA
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factor in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflator in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factor in (LCU per international $)`

### `replace_NAs` = “no_conversion”

If set to “no_conversion”, NAs are replaced with the values in the gdp argument.

    my_gdp <- tibble::tibble(
      iso3c = "VEN",
      year = 2010:2014,
      value = 100:104
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = "no_conversion",
      return_cfs = TRUE
    )
    x$result
    #> # A tibble: 5 × 3
    #>   iso3c  year value
    #>   <chr> <int> <dbl>
    #> 1 VEN    2010   100
    #> 2 VEN    2011   101
    #> 3 VEN    2012   102
    #> 4 VEN    2013   103
    #> 5 VEN    2014   104

    x$cfs
    #> # A tibble: 1 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                     NA                     NA
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factor in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflator in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factor in (LCU per international $)`

### `replace_NAs` = “linear”

If set to “linear”, missing conversion factors are inter- and extrapolated linearly. For the extrapolation, the closest 5 data points are used.

    my_gdp <- tibble::tibble(
      iso3c = "VEN",
      year = 2010:2014,
      value = 100:104
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = "linear",
      return_cfs = TRUE
    )
    x$result
    #> # A tibble: 5 × 3
    #>   iso3c  year value
    #>   <chr> <int> <dbl>
    #> 1 VEN    2010  203.
    #> 2 VEN    2011  205.
    #> 3 VEN    2012  208.
    #> 4 VEN    2013  210.
    #> 5 VEN    2014  212.

    x$cfs
    #> # A tibble: 1 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                   14.4                   5.97
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factor in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflator in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factor in (LCU per international $)`

### `replace_NAs` = “regional_average”

If set to “regional_average”, the regional GDP-weighted averages will be used. Requires a region-mapping, and a column in the source object with GDP data at PPP, to be used as weight. May lead to misleading results, use with care!

    my_gdp <- tibble::tibble(
      iso3c = "VEN",
      year = 2010:2014,
      value = 100:104
    )

    my_mapping_data_frame <- tibble::tibble(
      iso3c = c("VEN", "BRA", "ARG", "COL"),
      region = "LAM"
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = "regional_average",
      with_regions = my_mapping_data_frame,
      return_cfs = TRUE
    )
    x$result
    #> # A tibble: 5 × 3
    #>   iso3c  year value
    #>   <chr> <int> <dbl>
    #> 1 VEN    2010 0.485
    #> 2 VEN    2011 0.489
    #> 3 VEN    2012 0.494
    #> 4 VEN    2013 0.499
    #> 5 VEN    2014 0.504

    x$cfs
    #> # A tibble: 1 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                   1.18                   205.
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factor in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflator in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factor in (LCU per international $)`

    # Compare the 2019 PPP with the 2005 PPP. They are not in the same order of magnitude.
    # Obviously, being a part of the same region, does not mean the currencies are of the same strength.

### `replace_NAs` = c(“linear”, “…”)

If a vector is passed, with “linear” as first element, then the operations are done in sequence. For example for c(“linear”, 0), missing conversion factors are first inter- and extrapolated linearly but if any missing conversion factors still lead to NAs, these are replaced with 0.

    # Create an imaginary country XXX, and add it to the Latin America region
    my_gdp <- tibble::tibble(
      iso3c = c("VEN", "XXX"),
      year = 2010,
      value = 100
    )

    my_mapping_data_frame <- tibble::tibble(
      iso3c = c("VEN", "BRA", "ARG", "COL", "XXX"),
      region = "LAM"
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = c("linear", 0),
      with_regions = my_mapping_data_frame,
      return_cfs = TRUE
    )
    x$result
    #> # A tibble: 2 × 3
    #>   iso3c  year value
    #>   <chr> <dbl> <dbl>
    #> 1 VEN    2010  203.
    #> 2 XXX    2010    0

    x$cfs
    #> # A tibble: 2 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                   14.4                   5.97
    #> 2 XXX                       NA                       NA                    NA
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factors in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflators in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factors in (LCU per international $)`

### Deprecated: `replace_NAs` = 1

If set to `1`, missing conversion factors are set to 1. To be deprecated, use with care!

    my_gdp <- tibble::tibble(
      iso3c = "VEN",
      year = 2010:2014,
      value = 100:104
    )

    x <- convertGDP(
      gdp = my_gdp,
      unit_in = "constant 2005 Int$PPP",
      unit_out = "constant 2019 Int$PPP",
      replace_NAs = 1,
      return_cfs = TRUE
    )
    #> Warning: The `replace_NAs` argument of `convertGDP()` should not be 1 as of GDPuc 0.7.0.
    #> ℹ The deprecated feature was likely used in the GDPuc package.
    #>   Please report the issue at <https://github.com/pik-piam/GDPuc/issues>.
    #> This warning is displayed once every 8 hours.
    #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    #> generated.
    x$result
    #> # A tibble: 5 × 3
    #>   iso3c  year value
    #>   <chr> <int> <dbl>
    #> 1 VEN    2010  12.9
    #> 2 VEN    2011  13.0
    #> 3 VEN    2012  13.1
    #> 4 VEN    2013  13.3
    #> 5 VEN    2014  13.4

    x$cfs
    #> # A tibble: 1 × 4
    #>   iso3c 2005 PPP conversion fact…¹ 2019 value of base 2…² 2019 PPP conversion …³
    #>   <chr>                      <dbl>                  <dbl>                  <dbl>
    #> 1 VEN                        0.842                  0.153                      1
    #> # ℹ abbreviated names:
    #> #   ¹​`2005 PPP conversion factor in (LCU per international $)`,
    #> #   ²​`2019 value of base 2005 GDP deflator in (constant 2019 LCU per constant 2005 LCU)`,
    #> #   ³​`2019 PPP conversion factor in (LCU per international $)`

    # Why is the deflator above not 1? That is because for VEN, only the deflator value in 2019 was set to 1.
    # In 2005 the deflator was in the order of magnitude of 100. Obviously setting the deflator to 1 in 2019 is
    # completely misleading.