interpolate_missing_periods: Interpolate missing periods

View source: R/interpolate_missing_periods.R

interpolate_missing_periodsR Documentation

Interpolate missing periods

Description

Adds missing periods to data frame and interpolates missing values linearly or using splines from adjacent existing ones. Values for periods smaller/bigger than the existing ones can be filled with the values for the first/last available period in the case of linear interpolation.

Usage

interpolate_missing_periods(
  data,
  ...,
  value = "value",
  expand.values = FALSE,
  method = "linear",
  combinations = "nesting"
)

interpolate_missing_periods_(
  data,
  periods,
  value = "value",
  expand.values = FALSE,
  method = "linear",
  combinations = "nesting"
)

Arguments

data

A data frame or a quitte object.

...

A name-value pair of periods to fill. If unnamed, defaults to 'period'. If empty (but possibly named) uses only periods present in data.

value

Name of the column to fill, defaults to 'value'.

expand.values

If FALSE (the default), values are not expanded beyond the range of available data. If TRUE values at the closest extreme is used for linear interpolation. Results for spline interpolation are possibly nonsensical.

method

Specifies the interpolation method. Either 'linear' for linear interpolation or 'spline', 'spline_fmm', or 'spline_natural' for spline interpolation. 'spline' is an alias for 'spline_fmm'. See spline() for details.

combinations

Specifies the method with which other columns are treated. They are either preserved as-is ('nesting', the default), or are expanded to all unique combinations ('crossing'). See tidyr::expand() for details.

periods

A named list of periods to fill.

Value

A data frame or a quitte object, the same as data.

Author(s)

Michaja Pehl

Examples

require(dplyr)

# generate some test data with explicit (A-y-2025) and implicit (B-x-2030)
# missing values
(data <- tibble(
    group  = rep(c('A', 'B'), c(8, 4)),
    item   = c(rep('x', 4), rep('y', 4), rep('x', 4)),
    period = rep(c(2015, 2025, 2030, 2035), 3),
    value  = c(2, 4, 5, 6, 20, NA, 50, 60, NA, 400, 500, NA)))

# fill values for already existing periods
interpolate_missing_periods(data)

# fill values for existing periods, with full combinations of other columns
interpolate_missing_periods(data, combinations = 'crossing')

# add additional periods and fill values
interpolate_missing_periods(data, period = seq(2010, 2035, 5))

# also fill values outside the original data range
interpolate_missing_periods(data, seq(2010, 2035, 5), expand.values = TRUE)

# works on data frames with different column names
(data <- data %>%
        rename(year = period, coeff = value))

interpolate_missing_periods(data, year, value = 'coeff')

# works on quitte objects too
(quitte <- data %>%
        rename(model = group, scenario = item, period = year, value = coeff) %>%
        mutate(variable = 'Var 1', unit = 'u1') %>%
        as.quitte())

interpolate_missing_periods(quitte, expand.values = TRUE)

# and works with POSIXct periods
(quitte <- quitte %>%
        mutate(period = ISOyear(period)))

interpolate_missing_periods(quitte, period = ISOyear(seq(2010, 2035, 5)))

# standard evaluation example
interpolate_missing_periods_(data, periods = list(year = seq(2010, 2035, 5)),
                             value = 'coeff', expand.values = TRUE)

pik-piam/quitte documentation built on April 26, 2024, 12:58 a.m.