Analyzing Time Series Growth with ts_growth_rate_vec() in healthyR.ts

rtip
healthyrts
timeseries
Author

Steven P. Sanderson II, MPH

Published

October 16, 2023

Introduction

Time series data is essential for understanding trends and making forecasts in various fields, from finance to healthcare. Analyzing the growth rate of time series data is a crucial step in uncovering valuable insights. In the world of R programming, the healthyR.ts library introduces a powerful tool to calculate growth rates and log-differenced growth rates with the ts_growth_rate_vec() function. In this blog post, we’ll explore how this function works and how it can be used for effective time series analysis.

Understanding ts_growth_rate_vec():

The ts_growth_rate_vec() function is part of the healthyR.ts library, designed to work with numeric vectors or time series data. It calculates the growth rate or log-differenced growth rate of the provided data, offering valuable insights into the underlying trends and patterns.

Syntax

Here is the function syntax:

ts_growth_rate_vec(
  .x, 
  .scale = 100, 
  .power = 1, 
  .log_diff = FALSE, 
  .lags = 1
)
  • .x - A numeric vector
  • .scale - A numeric value that is used to scale the output
  • .power - A numeric value that is used to raise the output to a power
  • .log_diff - A logical value that determines whether the output is a log difference
  • .lags - An integer that determines the number of lags to use

You can find the documentation here

Examples

Let’s first take a look at the data we are going to be working with in this post, AirPassengers.

AirPassengers
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432
plot(AirPassengers)

Let’s load in the {healthyR.ts} library and see some examples to illustrate its functionality:

library(healthyR.ts)
  1. Calculating Basic Growth Rate:
ts_growth_rate_vec(AirPassengers) |> head(12)
 [1]         NA   5.357143  11.864407  -2.272727  -6.201550  11.570248
 [7]   9.629630   0.000000  -8.108108 -12.500000 -12.605042  13.461538
plot(ts(ts_growth_rate_vec(AirPassengers)))

The output provides growth rates for the AirPassengers dataset. This basic calculation can help you understand how the data is evolving over time. The growth rates are calculated from one point to the next, giving you an idea of the speed at which the values are changing.

  1. Applying Scaling and Power Transformation:
ts_growth_rate_vec(AirPassengers, .log_diff = TRUE) |> head(12)
 [1]         NA   5.218575  11.211730  -2.298952  -6.402186  10.948423
 [7]   9.193750   0.000000  -8.455739 -13.353139 -13.473259  12.629373
plot(ts(ts_growth_rate_vec(AirPassengers, .log_diff = TRUE)))

This example introduces the option to apply scaling and a power transformation. The resulting growth rates can help uncover trends that might not be apparent in the original data. Using a log-differenced growth rate is particularly useful for capturing the percentage change, making it easier to interpret the data.

  1. Handling Lagged Data:
ts_growth_rate_vec(AirPassengers, .lags = -1) |> head(12)
 [1]  -5.084746 -10.606061   2.325581   6.611570 -10.370370  -8.783784
 [7]   0.000000   8.823529  14.285714  14.423077 -11.864407   2.608696
plot.ts(ts_growth_rate_vec(AirPassengers, .lags = -1))

In this case, the function calculates the log differences of the time series with lags. This is helpful when you want to observe the changes between data points at different time intervals. It can reveal patterns that might not be apparent in the basic growth rate calculation.

  1. Combining Scaling, Transformation, and Lags:
ts_growth_rate_vec(AirPassengers, .log_diff = TRUE, .lags = -1) |> head(12)
 [1]  -5.218575 -11.211730   2.298952   6.402186 -10.948423  -9.193750
 [7]   0.000000   8.455739  13.353139  13.473259 -12.629373   2.575250
plot.ts(ts_growth_rate_vec(AirPassengers, .log_diff = TRUE, .lags = -1))

This example combines all the mentioned features to provide a comprehensive analysis of the data. It’s a powerful way to understand how the growth rate is affected by various factors, such as scaling and time lags.

Conclusion:

The ts_growth_rate_vec() function in the healthyR.ts library is a versatile tool for time series analysis. Whether you need a basic growth rate, want to apply scaling and transformation, or work with lagged data, this function has you covered. It’s a valuable asset for R programmers, helping them uncover hidden insights within time series data.

Incorporating this function into your data analysis workflow can provide you with a deeper understanding of how values change over time. Whether you’re working with financial data, healthcare data, or any other time series dataset, ts_growth_rate_vec() is a powerful addition to your R programming toolkit. Start exploring your time series data today and discover the trends and patterns that lie within.