extrapolate: Extrapolate

View source: R/interpolate.R

extrapolateR Documentation

Extrapolate

Description

Extrapolate to dimensions beyond the range of those which exist in your data.table. Uses linear, rate of change, or uniform extrapolation.

Usage

extrapolate(
  dt,
  id_cols,
  extrapolate_col,
  value_col,
  extrapolate_vals,
  method,
  n_groups_fit,
  n_groups_bin = NULL
)

Arguments

dt

[data.table()]
Data you would like to extrapolate.

id_cols

[characher()]
Columns which uniquely identify rows of dt. Extrapolation will be done separately by group on id_cols excluding interpolate_col.

extrapolate_col

[characher(1)]
Name of column of dt which is a numeric variable defining the groups you would like to extrapolate for.

value_col

[characher(1)]
Name of a column of dt which is a numeric variable defining the values you would like to extrapolate.

extrapolate_vals

[numeric(1)]
The values of extrapolate_col that you would like included in the outputs, including both input values and extrapolated values.

method

[characher(1)]
The method for extrapolation. Must be either 'linear', 'rate_of_change', or 'uniform'.

n_groups_fit

[numeric(1)]
The number of values of extrapolate_col to include in determining the extrapolation. For method 'linear' this is the groups used to fit a linear model. For method 'rate_of_change' this is the groups used to calculate a mean rate of change. For method 'uniform' this is the number of groups to combine into a uniform mean value which will be filled in. The groups will be selected as the first N groups or the last N groups in the data depending on the direction of extrapolation. Example: for extrapolation from years 1990:2000 up to year 2020 with n_groups_fit = 5, only years 1995:2000 will be used to fit the extrapolation model.

n_groups_bin

[numeric(1)]
TODO: add option to bin groups before fitting linear model or calculating rate of change.

Details

For reference on rate of change, see page 12 of the Preston Demography book, or, https://www.un.org/esa/sustdev/natlinfo/indicators/methodology_sheets/demographics/population_growth_rate.pdf

Consider log transforming your data prior to extrapolation if appropriate.

Value

[data.table()] dt with added rows for extrapolated values.

Examples

dt <- data.table::data.table(
  group = c(rep("a", 5), rep("b", 5)),
  x = rep(c(1:5), 2),
  y = rep(seq(10, 50, 10), 2)
)
dt <- extrapolate(dt, id_cols = c("group", "x"), extrapolate_col = "x",
                  value_col = "y", extrapolate_vals = c(1:10),
                  method = "linear", n_groups_fit = 3)


ihmeuw-demographics/demUtils documentation built on Feb. 27, 2024, 1:25 p.m.