rkillick / changepoint

A place for the development version of the changepoint package on CRAN.
127 stars 33 forks source link

cpt.mean doesn't accept a ts object #68

Closed 1lliter8 closed 2 years ago

1lliter8 commented 2 years ago

I'm trying to use the function on a ts object of 78 monthly observations but I get the Data must have atleast 2 observations to fit a changepoint model error when I do so.

The problem is in single.mean.norm, where dim(data) in line 4 returns [1] 78 1, and so ncol() is used on line 8 to calculate the number of observations instead of length() on line 5. On my ts object, ncol() returns 1, throwing the error incorrectly.

rkillick commented 2 years ago

If I run the following then it works:

data=ts(data=rnorm(100),start=c(2000,1),frequency=12)
cpt.mean(data)

Output:

class 'cpt' : Changepoint Object ~~ : S4 class containing 12 slots with names cpttype date version data.set method test.stat pen.type pen.value minseglen cpts ncpts.max param.est

Created on : Wed Aug 11 17:32:15 2021

summary(.) :

Created Using changepoint version 2.2.2 Changepoint type : Change in mean Method of analysis : AMOC Test Statistic : Normal Type of penalty : MBIC with value, 13.81551 Minimum Segment Length : 1 Maximum no. of cpts : 1 Changepoint Locations :

Can you provide a minimal working example such as the above?

1lliter8 commented 2 years ago

Ach, I constructed my ts object using a single-columned tibble rather than pull()ing to a vector. This was giving the object something to return with dim() when it shouldn't, and tripping the error. Switching select to pull resolves.

Sorry to bother you!

rkillick commented 2 years ago

No problem! I'm always open to hearing that my code my be incorrect but glad when it turns out I don't have to do anything :-)

Please do come back to me if you find anything else or have any comments for improvements. One thing I do need to do is return a ts object in the data slot for the output. Currently, we return a vector which means that the plotting doesn't use the time attributes of the input object. Clearly not desirable!