You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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){
# Variablesx<- as.vector(as.numeric(.x))
s<- as.numeric(.scale)
p<- as.numeric(.power)
l<- as.numeric(.lags)
ld<- as.logical(.log_diff)
# Checksif (!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
)
}
# Calculationif (l<0){
if (ld) {
return(log(x/dplyr::lead(x, -l)) *s)
} else {
return(((x/dplyr::lead(x, -l))^p-1) *s)
}
} elseif (ld){
return(log(x/dplyr::lag(x, l)) *s)
} else {
return(((x/dplyr::lag(x, l))^p-1) *s)
}
}
need a function to calculate growth rate of a series.
Function:
Example:
The text was updated successfully, but these errors were encountered: