Search code examples
rtime-seriesbreakpoints

Detecting multiple change points in mean and variance using R's changepoint package


I'm trying to identify change points in daily time series of stock prices using the "changepoint" package. The package contains different methods to detect the change points such as "Binary segmentation", "segmentation neighborhood" and "Pruned Exact Linear Time (PELT)", which is good for robustness checking.

The data I'm using has 4170 entries and starts from 2000-01-03

Prices.d <- ts(EM_indices[, 2], start = c(2000,01,03), freq = 365)

First I tried to use the PELT method to detect change points in the mean with the following code:

> cpt.mean(Prices.d, pen.value = c(4,1500),penalty = "CROPS",method = "PELT")

The results should indicate the locations of the change points but it has not been reported, here's what I got in returns:

enter image description here

You can see how the changepoints locations is empty, so adjusted the code by adding the arguments of Class and parameter estimates:

 Change <- cpt.mean(Prices.d, pen.value = c(4,1500),penalty = "CROPS",method = "PELT", class=TRUE, param.estimates=TRUE)

The changepoints locations,however, are still not reported in the results, what should I do to solve this problem?


Solution

  • When penalty is set to 'penalty = "CROPS"', then cpt.mean() returns a range of segmentations. This is why you cannot see the changepoint locations in the image you have attached. To access these, you could call attributes(Change)$cpts.full. This will return a matrix of changepoint locations.

    Alternatively, if you set 'class = F', you can obtain the segmentations using Change$changepoints.