Calculate percentage change over a time period for the given column pattern.

calculate_percentage_change(
  df,
  pattern,
  compare_period = c(2, 2),
  time_filter = NULL
)

Arguments

df

A tibble or Data frame

pattern

A string representing column pattern

compare_period

A numeric vector of comparison period

time_filter

A numeric vector of time filter

Value

A vector with calculated percentage change

Details

This function aims to generate features such as percentage change in last month versus last to last month to capture trend in data for a given comparison period. For example, In Telecommunication it can be used to generate features useful for predicting subscribers who are more likely to decline their revenue next month/week. In Finance it can be used to generate features useful for predicting customers who are going to default next month/week.

Examples

df <- data.frame(
    week_payment1 = c(1, 2, 3),
    week_payment2 = c(4, 5, 6),
    week_payment3 = c(7, 8, 9),
    week_payment4 = c(10, 11, 12)
)
calculate_percentage_change(df, "week_payment")
#> [1] -70.58824 -63.15789 -57.14286