Ask Your Question
0

What is the process to obtain the number of local extreme points in a time-series dataset using R?

asked 2021-08-21 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2022-10-15 07:00:00 +0000

david gravatar image
  1. Load the necessary R packages, such as ‘stats’.
  2. Import the time-series data and convert it to a time-series object using the ts() or zoo() function.
  3. Smooth the time-series data using functions like the loess() or the smooth.spline() function.
  4. Retrieve the local extrema points using functions such as the diff() and sign() functions to calculate the first derivative and changes in the sign of the data, respectively.
  5. Count the number of local extreme points using a function like length().

Example code:

# Load packages
library(stats)

# Import and convert data to a time-series object
ts_data <- ts(data, start = c(2010, 1), end = c(2020, 12), frequency = 12)

# Smooth the data
smooth_data <- loess(ts_data ~ time(ts_data))

# Calculate first derivative and change in sign
diff_data <- diff(sign(diff(smooth_data)))

# Count local extreme points
num_extreme_points <- length(which(diff_data < 0))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-21 11:00:00 +0000

Seen: 14 times

Last updated: Oct 15 '22