Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to calculate growth rate #459

Closed
spsanderson opened this issue Oct 12, 2023 · 0 comments
Closed

Function to calculate growth rate #459

spsanderson opened this issue Oct 12, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Oct 12, 2023

need a function to calculate growth rate of a series.

Function:

ts_growth_rate_vec <- function(.x, .scale = 100, .power = 1, .log_diff = FALSE,
                               .lags = 1){
  
  # Variables
  x <- as.vector(as.numeric(.x))
  s <- as.numeric(.scale)
  p <- as.numeric(.power)
  l <- as.numeric(.lags)
  ld <- as.logical(.log_diff)
  
  # Checks
  if (!is.vector(x) | !is.numeric(x)){
    rlang::abort(
      message = ".x must be a numeric vector",
      use_cli_format = TRUE
    )
  }
  
  if (!is.numeric(s) | !is.numeric(p) | !is.numeric(l)){
    rlang::abort(
      message = ".scale, .power and .lags must all be numeric",
      use_cli_format = TRUE
    )
  }
  
  if (!is.logical(ld)){
    rlang::abort(
      message = ".log_diff must be either TRUE or FALSE",
      use_cli_format = TRUE
    )
  }
  
  if (l == 0){
    rlang::abort(
      message = ".lags must be an integer that is either greater than or less than 0",
      use_cli_format = TRUE
    )
  }
  
  # Calculation
  if (l < 0){
    if (ld) {
      return(log(x / dplyr::lead(x, -l)) * s)
    } else {
      return(((x / dplyr::lead(x, -l))^p - 1) * s)
    }
  } else if (ld){
    return(log(x/dplyr::lag(x, l)) * s)
  } else {
    return(((x / dplyr::lag(x, l))^p - 1) * s)
  }
}

Example:

> x <- AirPassengers
> ts_growth_rate_vec(x) |> head(12)
 [1]         NA   5.357143  11.864407  -2.272727  -6.201550  11.570248   9.629630   0.000000
 [9]  -8.108108 -12.500000 -12.605042  13.461538
> ts_growth_rate_vec(x, .log_diff = TRUE) |> head(12)
 [1]         NA   5.218575  11.211730  -2.298952  -6.402186  10.948423   9.193750   0.000000
 [9]  -8.455739 -13.353139 -13.473259  12.629373
> ts_growth_rate_vec(x, .lags = 2) |> head(12)
 [1]          NA          NA  17.8571429   9.3220339  -8.3333333   4.6511628  22.3140496
 [8]   9.6296296  -8.1081081 -19.5945946 -23.5294118  -0.8403361
> ts_growth_rate_vec(x, .lags = -1) |> head(12)
 [1]  -5.084746 -10.606061   2.325581   6.611570 -10.370370  -8.783784   0.000000   8.823529
 [9]  14.285714  14.423077 -11.864407   2.608696
> ts_growth_rate_vec(x, .lags = -2) |> head(12)
 [1] -15.1515152  -8.5271318   9.0909091  -4.4444444 -18.2432432  -8.7837838   8.8235294
 [8]  24.3697479  30.7692308   0.8474576  -9.5652174  -6.3492063

ts_growth_rate_vec(AirPassengers, 100, 1) |>
  plot(type = "l")

image

@spsanderson spsanderson added the enhancement New feature or request label Oct 12, 2023
@spsanderson spsanderson added this to the healthyR.ts 0.2.11 milestone Oct 12, 2023
@spsanderson spsanderson self-assigned this Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant