R/inflation.R

Defines functions inflation

Documented in inflation

#' @title Create tibble with the set of cumulative inflation series from start_year back to begin and forward to end year
#' @description The function used for inflation series preparation. In start year inflation = 1, to backward inflation declining according by official inflation in RF, to forward inflation increasing by official inflation till the current year and after by 4 perc per year
#' @usage inflation (start_year = 2020, begin = 2000, end = 2050)
#' @param start_year the year when inflation make equal to 0 percentages
#' @param begin the year when inflation series starts
#' @param end the year when inflation series should be stoped
#' @return tibble with 2 double type columns: year, inf
#' @importFrom readxl read_excel
#' @importFrom dplyr %>%
#' @importFrom dplyr mutate
#' @importFrom dplyr select
#' @importFrom dplyr arrange
#' @importFrom dplyr filter
#' @importFrom dplyr between
#' @importFrom dplyr lag
#' @importFrom tidyr pivot_longer
#' @export
#' @examples
#' inflation ()
#' inflation (start_year = 2020, begin = 2000, end = 2050)

inflation <- function(start_year = 2020, begin = 2000, end = 2050){

  inflation_re <- readxl::read_excel("2. Master/inflation_RF.2000-2050.xlsx") %>%
    pivot_longer(cols = where(is.double), names_to = "year") %>%
    mutate(indicator = "C715") %>% select (-1) %>%
    mutate (value = value/100)

  inf_2000 <- inflation_re %>%  # Перед прогнозированием по логистической кривой уберем инфляционную составляющую
    mutate (year = as.numeric(year)) %>%
    mutate (value1 = ifelse(year == start_year, 0, value)) %>%
    mutate (value1 = ifelse(year <  start_year, 0, value1)) %>%
    mutate (inf    = ifelse(year >  start_year,  cumprod(1 + value1), 1)) %>%
    arrange(desc(year)) %>%
    mutate (value  = lag(value)) %>%
    mutate (value2  = ifelse(year  > start_year, 0, value)) %>%
    mutate (value2  = ifelse(year >= start_year, 0, value2)) %>%
    mutate (inf2    = ifelse(year <  start_year, 1/cumprod(1 + value2), 1)) %>%
    mutate (inf     = ifelse(year >= start_year, inf, inf2)) %>%
    filter (between(year, begin, end)) %>%
    arrange(year) %>%
    select (year, inf)

  return(inf_2000)

}
St-Digital-Twin/Dtwin documentation built on Jan. 1, 2022, 8:11 p.m.