R/analyse_direction.R

Defines functions analyse_direction

Documented in analyse_direction

#' Analyse co-movement between two numeric variables.
#'
#' This returns the total number and proportion of pairwise co-movement in two time series variables.
#' An explanatory note is printed as a message in the console.
#'
#'@export
analyse_direction <- function(x,var1,var2){
  var1 <- enquo(var1)
  var2 <- enquo(var2)

  x %>%
    dplyr::mutate_at(vars(!!var1,!!var2),~direction(.)) %>%
    tidyr::drop_na() %>%
    dplyr::mutate(match:=(!!var1)==(!!var2)) %>%
    dplyr::summarise(n=sum(match),
              base=length(match),
              prop=sum(match)/length(match)) -> result

  prop <- paste0(round((result$prop)*100),"%")

  message(glue::glue('There are {result$n} out of {result$base} instance(s) ({prop}) where values move in the same direction.'))
  return(result)
}
martinctc/tstoolbox documentation built on Nov. 20, 2024, 3:55 p.m.