├── src ├── Makevars ├── changepoint_init.c ├── cost_general_functions.c └── PELT_one_func_minseglen.c ├── data ├── HC1.RData ├── ftse100.RData ├── Lai2005fig3.RData ├── Lai2005fig4.RData └── wave.c44137.RData ├── tests ├── testthat.R ├── testthat │ ├── Rplots.pdf │ ├── test-plot.R │ └── _snaps │ │ └── plot │ │ └── diagnostic-plot-default.svg └── figs │ └── plot-diagnostic-true-tests │ └── diagnostic-plot-default.svg ├── .gitignore ├── .Rbuildignore ├── R ├── zzz.R ├── SegNeigh_one_func_minseglen.R ├── decision.R ├── data_input.R ├── CROPS.R ├── PELT_one_func_minseglen.R ├── BinSeg_one_func_minseglen.R ├── class_input.R ├── range_of_penalties.R ├── penalty_decision.R ├── fit.R ├── single.nonparametric.R ├── poisson.R └── CptReg.R ├── man ├── HC1.Rd ├── cpts.full-methods.Rd ├── cpts.full--methods.Rd ├── ftse100.Rd ├── pen.value.full--methods.Rd ├── pen.value.full-methods.Rd ├── method-methods.Rd ├── wave.c44137.Rd ├── method--methods.Rd ├── cpttype-methods.Rd ├── cpttype--methods.Rd ├── data.set-methods.Rd ├── pen.type-methods.Rd ├── data.set--methods.Rd ├── ncpts.max-methods.Rd ├── param.est-methods.Rd ├── pen.type--methods.Rd ├── pen.value-methods.Rd ├── test.stat-methods.Rd ├── ncpts.max--methods.Rd ├── param.est--methods.Rd ├── pen.value--methods.Rd ├── test.stat--methods.Rd ├── coef-methods.Rd ├── Lai2005fig3.Rd ├── distribution-methods.Rd ├── likelihood-methods.Rd ├── distribution--methods.Rd ├── ncpts-methods.Rd ├── data.set.ts-methods.Rd ├── method.Rd ├── Lai2005fig4.Rd ├── cpttype.Rd ├── data.set.Rd ├── ncpts.Rd ├── pen.type.Rd ├── minseglen.Rd ├── ncpts.max.Rd ├── param.est.Rd ├── pen.value.Rd ├── test.stat.Rd ├── cpts.full.Rd ├── distribution.Rd ├── data.set.ts.Rd ├── minseglen-methods.Rd ├── minseglen--methods.Rd ├── pen.value.full.Rd ├── cpts-.Rd ├── nseg.Rd ├── cpts.ts.Rd ├── method-.Rd ├── cpttype-.Rd ├── show-methods.Rd ├── pen.value-.Rd ├── minseglen-.Rd ├── pen.type-.Rd ├── ncpts.max-.Rd ├── data.set-.Rd ├── test.stat-.Rd ├── seg.len.Rd ├── param.est-.Rd ├── cpts.Rd ├── distribution-.Rd ├── pen.value.full-.Rd ├── cpts.full-.Rd ├── nseg-methods.Rd ├── summary-methods.Rd ├── cpts-methods.Rd ├── cpts--methods.Rd ├── logLik-methods.Rd ├── cpts.ts-methods.Rd ├── likelihood.Rd ├── seg.len-methods.Rd ├── param.Rd ├── fitted-methods.Rd ├── param-methods.Rd ├── residuals-methods.Rd ├── changepoint-package.Rd ├── plot-methods.Rd ├── class_input.Rd ├── PELT.Rd ├── BINSEG.Rd ├── penalty_decision.Rd ├── cpt.range-class.Rd ├── cpt.reg-class.Rd ├── decision.Rd ├── cpt.reg.Rd ├── cpt-class.Rd ├── cpt.mean.Rd └── cpt.meanvar.Rd ├── changepoint.Rproj ├── NAMESPACE ├── inst └── CITATION ├── DESCRIPTION ├── .github └── workflows │ └── R-CMD-check.yaml └── CITATION.cff /src/Makevars: -------------------------------------------------------------------------------- 1 | PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) 2 | -------------------------------------------------------------------------------- /data/HC1.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkillick/changepoint/HEAD/data/HC1.RData -------------------------------------------------------------------------------- /data/ftse100.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkillick/changepoint/HEAD/data/ftse100.RData -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(changepoint) 3 | 4 | test_check("changepoint") 5 | -------------------------------------------------------------------------------- /data/Lai2005fig3.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkillick/changepoint/HEAD/data/Lai2005fig3.RData -------------------------------------------------------------------------------- /data/Lai2005fig4.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkillick/changepoint/HEAD/data/Lai2005fig4.RData -------------------------------------------------------------------------------- /data/wave.c44137.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkillick/changepoint/HEAD/data/wave.c44137.RData -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | src/*.o 6 | src/*.so 7 | src/*.dll 8 | -------------------------------------------------------------------------------- /tests/testthat/Rplots.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkillick/changepoint/HEAD/tests/testthat/Rplots.pdf -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^README\.Rmd$ 4 | ^LICENSE\.md$ 5 | ^\.github$ 6 | ^.*\.cff$ 7 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onAttach <- function(libname, pkgname) 2 | { 3 | f <- read.dcf(file.path(libname, pkgname, "DESCRIPTION"), 4 | c("Version", "Date")) 5 | packageStartupMessage('Successfully loaded changepoint package version ', 6 | f[1,1],'\n WARNING: From v.2.3 the default method in cpt.* functions has changed from AMOC to PELT.', 7 | '\n See NEWS for details of all changes.') 8 | } 9 | -------------------------------------------------------------------------------- /man/HC1.Rd: -------------------------------------------------------------------------------- 1 | \name{HC1} 2 | \docType{data} 3 | \alias{HC1} 4 | \title{ 5 | G+C Content in Human Chromosome 1 6 | } 7 | \description{ 8 | This dataset gives the G+C content in 3kb windows along the Human Chromosome from 10Mb to 33Mb (no missing data). 9 | } 10 | \usage{ 11 | HC1 12 | } 13 | \format{ 14 | A vector of length 23553. 15 | } 16 | \source{http://www.ncbi.nlm.nih.gov/mapview/map_search.cgi?taxid=9606&build=previous} 17 | 18 | \keyword{datasets} 19 | -------------------------------------------------------------------------------- /changepoint.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageCheckArgs: --as-cran --no-tests 22 | -------------------------------------------------------------------------------- /man/cpts.full-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts.full-methods} 2 | \docType{methods} 3 | \alias{cpts.full-methods} 4 | \alias{cpts.full,cpt.range-method} 5 | \title{ ~~ Methods for Function cpts.full ~~} 6 | \description{ 7 | ~~ Methods for function \code{cpts.full} ~~ 8 | } 9 | \section{Methods}{ 10 | \describe{ 11 | 12 | \item{\code{signature(object = "cpt.range")}}{ 13 | Retrieves cpts.full slot from an object of class cpt.range. 14 | } 15 | }} 16 | \keyword{methods} 17 | \keyword{cpt} 18 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpts.full--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts.full<--methods} 2 | \docType{methods} 3 | \alias{cpts.full<--methods} 4 | \alias{cpts.full<-,cpt.range-method} 5 | \title{ ~~ Methods for Function cpts.full<- ~~} 6 | \description{ 7 | ~~ Methods for function \code{cpts.full<-} ~~ 8 | } 9 | \section{Methods}{ 10 | \describe{ 11 | 12 | \item{\code{signature(x = "cpt.range")}}{ 13 | Assigns the value following <- to the cpts.full slot in x. This is required to be a matrix. 14 | } 15 | }} 16 | \keyword{methods} 17 | \keyword{cpt} 18 | \keyword{internal} -------------------------------------------------------------------------------- /man/ftse100.Rd: -------------------------------------------------------------------------------- 1 | \name{ftse100} 2 | \docType{data} 3 | \alias{ftse100} 4 | \title{ 5 | FTSE 100 Daily Returns: 2nd April 1984 -- 13th September 2012 6 | } 7 | \description{ 8 | This dataset gives the daily returns \eqn{(c_{t+1}/c_t -1)} of the UK FTSE 100 index from 2nd April 1984 until the 13th September 2012. 9 | } 10 | \usage{ 11 | ftse100 12 | } 13 | \format{ 14 | A matrix of dimension 7187 x 2 where the first column is the Date and the second column is the Daily Return. 15 | } 16 | \source{Yahoo! Finance} 17 | 18 | \keyword{datasets} 19 | -------------------------------------------------------------------------------- /man/pen.value.full--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value.full<--methods} 2 | \docType{methods} 3 | \alias{pen.value.full<--methods} 4 | \alias{pen.value.full<-,cpt.range-method} 5 | \title{ ~~ Methods for Function pen.value.full<- ~~} 6 | \description{ 7 | ~~ Methods for function \code{pen.value.full<-} ~~ 8 | } 9 | \section{Methods}{ 10 | \describe{ 11 | 12 | \item{\code{signature(x = "cpt.range")}}{ 13 | Assigns the value following <- to the pen.value.full slot in x 14 | } 15 | 16 | }} 17 | \keyword{methods} 18 | \keyword{cpt} 19 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value.full-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value.full-methods} 2 | \docType{methods} 3 | \alias{pen.value.full-methods} 4 | \alias{pen.value.full,cpt.range-method} 5 | \title{ ~~ Methods for Function pen.value.full ~~} 6 | \description{ 7 | ~~ Methods for function \code{pen.value.full} ~~ 8 | } 9 | \section{Methods}{ 10 | \describe{ 11 | 12 | \item{\code{signature(object = "cpt.range")}}{ 13 | Retrieves pen.value.full slot from an object of class cpt.range. 14 | } 15 | 16 | }} 17 | \keyword{methods} 18 | \keyword{cpt} 19 | \keyword{internal} -------------------------------------------------------------------------------- /man/method-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{method-methods} 2 | \docType{methods} 3 | \alias{method-methods} 4 | \alias{method,cpt-method} 5 | \alias{method,cpt.reg-method} 6 | \title{ ~~ Methods for Function method ~~} 7 | \description{ 8 | ~~ Methods for function \code{method} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves method slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves method slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/wave.c44137.Rd: -------------------------------------------------------------------------------- 1 | \name{wave.c44137} 2 | \docType{data} 3 | \alias{wave.c44137} 4 | \title{ 5 | Wave data from buoy c44137 6 | } 7 | \description{ 8 | This dataset gives the significant wave heights from buoy c44137 obtained from the Fisheries and Oceans Canada, East Scotian Slop. The data are taken at hourly intervals from January 2005 until September 2012. 9 | } 10 | \usage{ 11 | wave.c44137 12 | } 13 | \format{ 14 | A vector of length 63651. 15 | } 16 | \source{http://www.meds-sdmm.dfo-mpo.gc.ca/isdm-gdsi/waves-vagues/search-recherche/list-liste/data-donnees-eng.asp?medsid=C44137} 17 | 18 | \keyword{datasets} 19 | -------------------------------------------------------------------------------- /man/method--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{method<--methods} 2 | \docType{methods} 3 | \alias{method<--methods} 4 | \alias{method<-,cpt-method} 5 | \alias{method<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function method<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{method<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the method slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the method slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpttype-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpttype-methods} 2 | \docType{methods} 3 | \alias{cpttype-methods} 4 | \alias{cpttype,cpt-method} 5 | \alias{cpttype,cpt.reg-method} 6 | \title{ ~~ Methods for Function cpttype ~~} 7 | \description{ 8 | ~~ Methods for function \code{cpttype} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves cpttype slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves cpttype slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} 24 | -------------------------------------------------------------------------------- /man/cpttype--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpttype<--methods} 2 | \docType{methods} 3 | \alias{cpttype<--methods} 4 | \alias{cpttype<-,cpt-method} 5 | \alias{cpttype<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function cpttype<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{cpttype<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the cpttype slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the cpttype slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/data.set-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{data.set-methods} 2 | \docType{methods} 3 | \alias{data.set-methods} 4 | \alias{data.set,cpt-method} 5 | \alias{data.set,cpt.reg-method} 6 | \title{ ~~ Methods for Function data.set ~~} 7 | \description{ 8 | ~~ Methods for function \code{data.set} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | \item{\code{signature(object = "cpt")}}{ 13 | Retrieves the data.set slot from objects with class cpt 14 | } 15 | 16 | \item{\code{signature(object = "cpt.reg")}}{ 17 | Retrieves the data.set slot from objects with class cpt.reg 18 | } 19 | }} 20 | \keyword{methods} 21 | \keyword{cpt} 22 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.type-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.type-methods} 2 | \docType{methods} 3 | \alias{pen.type-methods} 4 | \alias{pen.type,cpt-method} 5 | \alias{pen.type,cpt.reg-method} 6 | \title{ ~~ Methods for Function pen.type ~~} 7 | \description{ 8 | ~~ Methods for function \code{pen.type} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves pen.type slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves pen.type slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/data.set--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{data.set<--methods} 2 | \docType{methods} 3 | \alias{data.set<--methods} 4 | \alias{data.set<-,cpt-method} 5 | \alias{data.set<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function data.set<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{data.set<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the data.set slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the data.set slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/ncpts.max-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{ncpts.max-methods} 2 | \docType{methods} 3 | \alias{ncpts.max-methods} 4 | \alias{ncpts.max,cpt-method} 5 | \alias{ncpts.max,cpt.reg-method} 6 | \title{ ~~ Methods for Function ncpts.max ~~} 7 | \description{ 8 | ~~ Methods for function \code{ncpts.max} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves ncpts.max slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves ncpts.max slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/param.est-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{param.est-methods} 2 | \docType{methods} 3 | \alias{param.est-methods} 4 | \alias{param.est,cpt-method} 5 | \alias{param.est,cpt.reg-method} 6 | \title{ ~~ Methods for Function param.est ~~} 7 | \description{ 8 | ~~ Methods for function \code{param.est} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves param.est slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves param.est slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.type--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.type<--methods} 2 | \docType{methods} 3 | \alias{pen.type<--methods} 4 | \alias{pen.type<-,cpt-method} 5 | \alias{pen.type<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function pen.type<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{pen.type<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the pen.type slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the pen.type slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value-methods} 2 | \docType{methods} 3 | \alias{pen.value-methods} 4 | \alias{pen.value,cpt-method} 5 | \alias{pen.value,cpt.reg-method} 6 | \title{ ~~ Methods for Function pen.value ~~} 7 | \description{ 8 | ~~ Methods for function \code{pen.value} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves pen.value slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves pen.value slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/test.stat-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{test.stat-methods} 2 | \docType{methods} 3 | \alias{test.stat-methods} 4 | \alias{test.stat,cpt-method} 5 | \alias{test.stat,cpt.reg-method} 6 | \title{ ~~ Methods for Function test.stat ~~} 7 | \description{ 8 | ~~ Methods for function \code{test.stat} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves test.stat slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves test.stat slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/ncpts.max--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{ncpts.max<--methods} 2 | \docType{methods} 3 | \alias{ncpts.max<--methods} 4 | \alias{ncpts.max<-,cpt-method} 5 | \alias{ncpts.max<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function ncpts.max<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{ncpts.max<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the ncpts.max slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the ncpts.max slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/param.est--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{param.est<--methods} 2 | \docType{methods} 3 | \alias{param.est<--methods} 4 | \alias{param.est<-,cpt-method} 5 | \alias{param.est<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function param.est<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{param.est<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the param.est slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the param.est slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value<--methods} 2 | \docType{methods} 3 | \alias{pen.value<--methods} 4 | \alias{pen.value<-,cpt-method} 5 | \alias{pen.value<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function pen.value<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{pen.value<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the pen.value slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the pen.value slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/test.stat--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{test.stat<--methods} 2 | \docType{methods} 3 | \alias{test.stat<--methods} 4 | \alias{test.stat<-,cpt-method} 5 | \alias{test.stat<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function test.stat<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{test.stat<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the test.stat slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the test.stat slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/coef-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{coef-methods} 2 | \docType{methods} 3 | \alias{coef-methods} 4 | \alias{coef,cpt-method} 5 | \alias{coef,cpt.reg-method} 6 | \title{ ~~ Methods for Function coef ~~} 7 | \description{ 8 | ~~ Methods for function \code{coef} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves parameter estimates (i.e. param.est slot) from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves parameter estimates (i.e. param.est slot) from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} 24 | -------------------------------------------------------------------------------- /man/Lai2005fig3.Rd: -------------------------------------------------------------------------------- 1 | \name{Lai2005fig3} 2 | \docType{data} 3 | \alias{Lai2005fig3} 4 | \title{ 5 | Normalized glioblastoma profile for chromosome 13 6 | } 7 | \description{ 8 | This dataset is taken from Lai W, Johnson MJ, Kucherlapati R, Park PJ, Bioinformatics , 2005. The paper states that the original source of the data is from Bredel et al. (2005). The data is Chromosome 13 in GBM31. 9 | } 10 | \usage{ 11 | Lai2005fig3 12 | } 13 | \format{ 14 | A matrix of dimensions 797 x 5. The columns are Spot, CH, POS.start, POS.end, GBM31. 15 | } 16 | \source{http://compbio.med.harvard.edu/Supplements/Bioinformatics05b/Profiles/Chrom_13_GBM31.xls} 17 | 18 | \keyword{datasets} 19 | -------------------------------------------------------------------------------- /man/distribution-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{distribution-methods} 2 | \docType{methods} 3 | \alias{distribution-methods} 4 | \alias{distribution,cpt-method} 5 | \alias{distribution,cpt.reg-method} 6 | \title{ ~~ Methods for Function distribution ~~} 7 | \description{ 8 | ~~ Methods for function \code{distribution} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves distribution slot from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves distribution slot from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/likelihood-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{likelihood-methods} 2 | \docType{methods} 3 | \alias{likelihood-methods} 4 | \alias{likelihood,cpt-method} 5 | \title{ ~~ Methods for Function likelihood ~~} 6 | \description{ 7 | ~~ Methods for function \code{likelihood} ~~ 8 | } 9 | \section{Methods}{ 10 | \describe{ 11 | 12 | \item{\code{signature(object = "cpt")}}{ 13 | Returns the likelihood of the data with the fitted changepoints, two values are returned, the raw scaled negative likelihood and the scaled negative likelihood + penalty. Only valid for cpttype="mean","variance" or "mean and variance". 14 | } 15 | }} 16 | \keyword{methods} 17 | \keyword{cpt} 18 | \keyword{internal} -------------------------------------------------------------------------------- /man/distribution--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{distribution<--methods} 2 | \docType{methods} 3 | \alias{distribution<--methods} 4 | \alias{distribution<-,cpt-method} 5 | \alias{distribution<-,cpt.reg-method} 6 | \title{ ~~ Methods for Function distribution<- ~~} 7 | \description{ 8 | ~~ Methods for function \code{distribution<-} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(x = "cpt")}}{ 14 | Assigns the value following <- to the distribution slot in x 15 | } 16 | 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the distribution slot in x 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /tests/testthat/test-plot.R: -------------------------------------------------------------------------------- 1 | context("plot(diagnostic = TRUE) tests") 2 | 3 | # Generate cpt.range object 4 | testdata <- changepoint::ftse100$V2 5 | obj.cpt.range <- cpt.var(testdata, method = "PELT", 6 | penalty = "CROPS", pen.value = c(5, 500)) 7 | 8 | # For code coverage 9 | plot(obj.cpt.range, diagnostic = TRUE) 10 | plot(obj.cpt.range, diagnostic = TRUE, type = "h") 11 | 12 | # Tests for plots 13 | vdiffr::expect_doppelganger("Diagnostic plot (default)", 14 | plot(obj.cpt.range, diagnostic = TRUE)) 15 | vdiffr::expect_doppelganger("Diagnostic plot (histogram)", 16 | plot(obj.cpt.range, diagnostic = TRUE, type = "h")) 17 | -------------------------------------------------------------------------------- /man/ncpts-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{ncpts-methods} 2 | \docType{methods} 3 | \alias{ncpts-methods} 4 | \alias{ncpts,cpt-method} 5 | \alias{ncpts,cpt.reg-method} 6 | \title{ ~~ Methods for Function ncpts ~~} 7 | \description{ 8 | ~~ Methods for function \code{ncpts} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Returns the number of changepoints (i.e. length of the cpts slot minus 1) from an object of class cpt 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Returns the number of changepoints (i.e. length of the cpts slot minus 1) from an object of class cpt.reg 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/data.set.ts-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{data.set.ts-methods} 2 | \docType{methods} 3 | \alias{data.set.ts-methods} 4 | \alias{data.set.ts,cpt-method} 5 | \alias{data.set.ts,cpt.reg-method} 6 | \title{ ~~ Methods for Function data.set.ts ~~} 7 | \description{ 8 | ~~ Methods for function \code{data.set.ts} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | \item{\code{signature(object = "cpt")}}{ 13 | Retrieves the data.set slot from objects with class cpt. This returns a ts class object. 14 | } 15 | 16 | \item{\code{signature(object = "cpt.reg")}}{ 17 | Retrieves the data.set slot from objects with class cpt.reg. This returns a ts class object. 18 | } 19 | }} 20 | \keyword{methods} 21 | \keyword{cpt} 22 | \keyword{internal} -------------------------------------------------------------------------------- /man/method.Rd: -------------------------------------------------------------------------------- 1 | \name{method} 2 | \alias{method} 3 | \title{ 4 | Generic Function - method 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | method(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{method-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | method(x) # retrieves the method slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/Lai2005fig4.Rd: -------------------------------------------------------------------------------- 1 | \name{Lai2005fig4} 2 | \docType{data} 3 | \alias{Lai2005fig4} 4 | \title{ 5 | Normalized glioblastoma profile for an excerpt of chromosome 7, the EGFR locus. 6 | } 7 | \description{ 8 | This dataset is taken from Lai W, Johnson MJ, Kucherlapati R, Park PJ, Bioinformatics , 2005. The paper states that the original source of the data is from Bredel et al. (2005). The data is an excerpt of chromosome 7 in GBM29 from 40 to 65 Mb. 9 | } 10 | \usage{ 11 | Lai2005fig4 12 | } 13 | \format{ 14 | A matrix of dimensions 193 x 5. The columns are Spot, CH, POS.start, POS.end, GBM31. 15 | } 16 | \source{http://compbio.med.harvard.edu/Supplements/Bioinformatics05b/Profiles/Chrom_7_from40_to65Mb_GBM29.xls} 17 | 18 | \keyword{datasets} 19 | -------------------------------------------------------------------------------- /man/cpttype.Rd: -------------------------------------------------------------------------------- 1 | \name{cpttype} 2 | \alias{cpttype} 3 | \title{ 4 | Generic Function - cpttype 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpttype(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{cpttype-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | cpttype(x) # retrieves the cpttype slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/data.set.Rd: -------------------------------------------------------------------------------- 1 | \name{data.set} 2 | \alias{data.set} 3 | \title{ 4 | Generic Function - data.set 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | data.set(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{data.set-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | data.set(x) # retrieves the data.set slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/ncpts.Rd: -------------------------------------------------------------------------------- 1 | \name{ncpts} 2 | \alias{ncpts} 3 | \title{ 4 | Generic Function - ncpts 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | ncpts(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{ncpts-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | ncpts(x) # returns the number of changepoints (i.e. length of the cpts slot in x minus 1) 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} -------------------------------------------------------------------------------- /man/pen.type.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.type} 2 | \alias{pen.type} 3 | \title{ 4 | Generic Function - pen.type 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | pen.type(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{pen.type-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | pen.type(x) # retrieves the pen.type slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | import(methods) 2 | import(graphics) 3 | import(zoo) 4 | import(stats) 5 | 6 | export("cpt.mean", "cpt.meanvar", "cpt.var","cpt.reg") 7 | export("BINSEG", "decision", "penalty_decision","PELT","class_input") 8 | exportClasses(cpt,cpt.reg,cpt.range) 9 | exportMethods(plot,summary,show,nseg,logLik,likelihood,fitted,residuals,data.set,data.set.ts,cpts,cpts.full, cpts.ts,cpttype,distribution,test.stat, 10 | method,ncpts,ncpts.max,param.est,param, coef,pen.type,pen.value,pen.value.full, minseglen, seg.len,"cpts<-","cpttype<-","data.set<-","distribution<-","test.stat<-","ncpts.max<-","param.est<-","pen.type<-", 11 | "pen.value<-", "method<-", "cpts.full<-", "minseglen<-","pen.value.full<-") 12 | 13 | useDynLib(changepoint, .registration=TRUE) 14 | -------------------------------------------------------------------------------- /man/minseglen.Rd: -------------------------------------------------------------------------------- 1 | \name{minseglen} 2 | \alias{minseglen} 3 | \title{ 4 | Generic Function - minseglen 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | minseglen(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{minseglen-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | minseglen(x) # retrieves the minseglen slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/ncpts.max.Rd: -------------------------------------------------------------------------------- 1 | \name{ncpts.max} 2 | \alias{ncpts.max} 3 | \title{ 4 | Generic Function - ncpts.max 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | ncpts.max(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic function. 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{ncpts.max-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | ncpts.max(x) # retrieves the ncpts.max slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/param.est.Rd: -------------------------------------------------------------------------------- 1 | \name{param.est} 2 | \alias{param.est} 3 | \title{ 4 | Generic Function - param.est 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | param.est(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{param.est-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | param.est(x) # retrieves the param.est slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value} 2 | \alias{pen.value} 3 | \title{ 4 | Generic Function - pen.value 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | pen.value(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{pen.value-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | pen.value(x) # retrieves the pen.value slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/test.stat.Rd: -------------------------------------------------------------------------------- 1 | \name{test.stat} 2 | \alias{test.stat} 3 | \title{ 4 | Generic Function - test.stat 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | test.stat(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{test.stat-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | test.stat(x) # retrieves the test.stat slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpts.full.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts.full} 2 | \alias{cpts.full} 3 | \title{ 4 | Generic Function - cpts.full 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpts.full(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic function. 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{cpts.full-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt.range") # new cpt.range object 33 | cpts.full(x) # retrieves the cpts.full slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/distribution.Rd: -------------------------------------------------------------------------------- 1 | \name{distribution} 2 | \alias{distribution} 3 | \title{ 4 | Generic Function - distribution 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | distribution(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{distribution-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | distribution(x) # retrieves the distribution slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/data.set.ts.Rd: -------------------------------------------------------------------------------- 1 | \name{data.set.ts} 2 | \alias{data.set.ts} 3 | \title{ 4 | Generic Function - data.set.ts 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | data.set.ts(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{data.set.ts-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | data.set.ts(x) # retrieves the data.set slot from x. This is a ts object. 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/minseglen-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{minseglen-methods} 2 | \docType{methods} 3 | \alias{minseglen-methods} 4 | \alias{minseglen,cpt-method} 5 | \alias{minseglen,cpt.reg-method} 6 | \alias{minseglen,cpt.range-method} 7 | \title{ ~~ Methods for Function minseglen ~~} 8 | \description{ 9 | ~~ Methods for function \code{minseglen} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(object = "cpt")}}{ 15 | Retrieves minseglen slot from an object of class cpt 16 | } 17 | \item{\code{signature(object = "cpt.range")}}{ 18 | Retrieves minseglen slot from an object of class cpt.range 19 | } 20 | \item{\code{signature(object = "cpt.reg")}}{ 21 | Retrieves minseglen slot from an object of class cpt.reg 22 | } 23 | 24 | }} 25 | \keyword{methods} 26 | \keyword{cpt} 27 | \keyword{internal} -------------------------------------------------------------------------------- /man/minseglen--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{minseglen<--methods} 2 | \docType{methods} 3 | \alias{minseglen<--methods} 4 | \alias{minseglen<-,cpt-method} 5 | \alias{minseglen<-,cpt.reg-method} 6 | \alias{minseglen<-,cpt.range-method} 7 | \title{ ~~ Methods for Function minseglen<- ~~} 8 | \description{ 9 | ~~ Methods for function \code{minseglen<-} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(x = "cpt")}}{ 15 | Assigns the value following <- to the minseglen slot in x 16 | } 17 | \item{\code{signature(x = "cpt.reg")}}{ 18 | Assigns the value following <- to the minseglen slot in x 19 | } 20 | \item{\code{signature(x = "cpt.range")}}{ 21 | Assigns the value following <- to the minseglen slot in x 22 | } 23 | 24 | }} 25 | \keyword{methods} 26 | \keyword{cpt} 27 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value.full.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value.full} 2 | \alias{pen.value.full} 3 | \title{ 4 | Generic Function - pen.value.full 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | pen.value.full(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{pen.value.full-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt.range") # new cpt.range object 33 | pen.value.full(x) # retrieves the pen.value.full slot from x 34 | } 35 | 36 | \keyword{methods} 37 | \keyword{cpt} 38 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpts-.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts<-} 2 | \alias{cpts<-} 3 | \title{ 4 | Generic Function - cpts<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpts(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic function. 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{cpts<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | cpts(x)<-10 # replaces the vector of changepoint in object x with 10 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/nseg.Rd: -------------------------------------------------------------------------------- 1 | \name{nseg} 2 | \alias{nseg} 3 | \title{ 4 | Generic Function - nseg 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | nseg(object,...) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{...}{ 17 | Other optional arguments used by some methods. 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{nseg-methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | nseg(x) # returns the number of segments (i.e. length of the cpts slot) 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | -------------------------------------------------------------------------------- /man/cpts.ts.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts.ts} 2 | \alias{cpts.ts} 3 | \title{ 4 | Generic Function - cpts.ts 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpts.ts(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic function. 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{cpts.ts-methods}} 30 | } 31 | \examples{ 32 | x=new("cpt") # new cpt object 33 | cpts.ts(x) # retrieves the cpts slot from x but unlike cpts, displays the "date" index rather than 34 | #the position within the vector 35 | } 36 | 37 | \keyword{methods} 38 | \keyword{cpt} 39 | \keyword{internal} -------------------------------------------------------------------------------- /man/method-.Rd: -------------------------------------------------------------------------------- 1 | \name{method<-} 2 | \alias{method<-} 3 | \title{ 4 | Generic Function - method<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | method(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{method<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | method(x)<-"mean" # replaces the existing method slot in x with "mean" 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpttype-.Rd: -------------------------------------------------------------------------------- 1 | \name{cpttype<-} 2 | \alias{cpttype<-} 3 | \title{ 4 | Generic Function - cpttype 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpttype(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{cpttype<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | cpttype(x)<-"mean" # replaces the existing cpttype in object x with "mean" 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/show-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{show-methods} 2 | \docType{methods} 3 | \alias{show-methods} 4 | \alias{show,ANY-method} 5 | \alias{show,cpt-method} 6 | \alias{show,cpt.reg-method} 7 | \title{ ~~ Methods for Function print in Package `base' ~~} 8 | \description{ 9 | ~~ Methods for function \code{show} in Package `base' ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(x = "ANY")}}{ 15 | Generic print function, see base package description using ?print 16 | } 17 | 18 | \item{\code{signature(x = "cpt")}}{ 19 | Prints out information contained within the object x including a summary 20 | } 21 | \item{\code{signature(x = "cpt.reg")}}{ 22 | Prints out information contained within the object x including a summary 23 | } 24 | }} 25 | \keyword{methods} 26 | \keyword{show} 27 | \keyword{cpt} 28 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value-.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value<-} 2 | \alias{pen.value<-} 3 | \title{ 4 | Generic Function - pen.value 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | pen.value(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{pen.value<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | pen.value(x)<-5 # replaces the existing pen.value slot in x with 5 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/minseglen-.Rd: -------------------------------------------------------------------------------- 1 | \name{minseglen<-} 2 | \alias{minseglen<-} 3 | \title{ 4 | Generic Function - minseglen<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | minseglen(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{minseglen<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | minseglen(x)<-5 # replaces the existing minseglen slot in x with 5 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.type-.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.type<-} 2 | \alias{pen.type<-} 3 | \title{ 4 | Generic Function - pen.type<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | pen.type(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{pen.type<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | pen.type(x)<-"SIC" # replaces the existing pen.type slot in x with "SIC" 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/ncpts.max-.Rd: -------------------------------------------------------------------------------- 1 | \name{ncpts.max<-} 2 | \alias{ncpts.max<-} 3 | \title{ 4 | Generic Function - ncpts.max<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | ncpts.max(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic function. 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{ncpts.max<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | ncpts.max(x)<-10 # replaces the vector of changepoint in object x with 10 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/data.set-.Rd: -------------------------------------------------------------------------------- 1 | \name{data.set<-} 2 | \alias{data.set<-} 3 | \title{ 4 | Generic Function - data.set<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | data.set(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{data.set<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | data.set(x)<-c(1,2,3,4,5) # replaces the existing data.set slot in x with c(1,2,3,4,5) 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/test.stat-.Rd: -------------------------------------------------------------------------------- 1 | \name{test.stat<-} 2 | \alias{test.stat<-} 3 | \title{ 4 | Generic Function - test.stat<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | test.stat(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{test.stat<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | test.stat(x)<-"normal" # replaces the current test.stat slot of x with "normal" 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/seg.len.Rd: -------------------------------------------------------------------------------- 1 | \name{seg.len} 2 | \alias{seg.len} 3 | \title{ 4 | Generic Function - seg.len 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | seg.len(object,...) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{...}{ 17 | Other optional arguments used by some methods. 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{seg.len-methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | seg.len(x) # returns the length of each segment in the data (i.e. no. of obs between changepoints) 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | -------------------------------------------------------------------------------- /man/param.est-.Rd: -------------------------------------------------------------------------------- 1 | \name{param.est<-} 2 | \alias{param.est<-} 3 | \title{ 4 | Generic Function - param.est<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | param.est(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{param.est<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | param.est(x)<-list(mean=0) # replaces the current param.est list in x with list(mean=0) 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /R/SegNeigh_one_func_minseglen.R: -------------------------------------------------------------------------------- 1 | dataToString <- function(data){ 2 | return(paste0("c(", paste(data, collapse=", "), ")")) 3 | } 4 | 5 | 6 | 7 | SEGNEIGH <- function(data, pen.value, costfunc, Q, var=0, shape=1){ 8 | 9 | #split the costfunction by . and then use the first two elements in eval(parse()) 10 | if(Q<=0){stop(paste('Q is the maximum number of changepoints so should be greater than 0'))} 11 | 12 | split=strsplit(costfunc, "\\.") 13 | if(var!=0){ 14 | text = c("segneigh",".", split[[1]][1],".", split[[1]][2],"(",dataToString(data),",",Q,",",pen.value,",","know.mean=",TRUE,",","mu=",var,")") 15 | }else{ 16 | text = c("segneigh",".", split[[1]][1],".", split[[1]][2],"(",dataToString(data),",",Q,",",pen.value,")") 17 | } 18 | 19 | 20 | 21 | out = eval(parse(text=paste(text, collapse=""))) 22 | 23 | return(out) 24 | } -------------------------------------------------------------------------------- /man/cpts.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts} 2 | \alias{cpts} 3 | \title{ 4 | Generic Function - cpts 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpts(object,...) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{...}{ 17 | Depending on the class of \code{object} depends on the method used and whether ... is needed/warranted (and if one exists) 18 | } 19 | } 20 | \details{ 21 | Generic function. 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{cpts-methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | cpts(x) # retrieves the cpts slot from x 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/distribution-.Rd: -------------------------------------------------------------------------------- 1 | \name{distribution<-} 2 | \alias{distribution<-} 3 | \title{ 4 | Generic Function - distribution<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | distribution(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{distribution<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt") # new cpt object 36 | distribution(x)<-"normal" # replaces the current distribution slot of x with "normal" 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/pen.value.full-.Rd: -------------------------------------------------------------------------------- 1 | \name{pen.value.full<-} 2 | \alias{pen.value.full<-} 3 | \title{ 4 | Generic Function - pen.value.full<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | pen.value.full(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic Function 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{pen.value.full<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt.range") # new cpt.range object 36 | pen.value.full(x)<-5 # replaces the existing pen.value.full slot in x with 5 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpts.full-.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts.full<-} 2 | \alias{cpts.full<-} 3 | \title{ 4 | Generic Function - cpts.full<- 5 | } 6 | \description{ 7 | Generic function 8 | } 9 | \usage{ 10 | cpts.full(object)<-value 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used (and if one exists) 15 | } 16 | \item{value}{ 17 | Replacement value 18 | } 19 | } 20 | \details{ 21 | Generic function. 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{cpts.full<--methods}} 33 | } 34 | \examples{ 35 | x=new("cpt.range") # new cpt.range object 36 | cpts.full(x)<-matrix(c(10,20,10,NA),nrow=2,byrow=TRUE) 37 | # replaces the vector of changepoint in object x with the given matrix 38 | } 39 | 40 | \keyword{methods} 41 | \keyword{cpt} 42 | \keyword{internal} -------------------------------------------------------------------------------- /man/nseg-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{nseg-methods} 2 | \docType{methods} 3 | \alias{nseg-methods} 4 | \alias{nseg,cpt-method} 5 | \alias{nseg,cpt.range-method} 6 | \alias{nseg,cpt.reg-method} 7 | \title{ ~~ Methods for Function nseg ~~} 8 | \description{ 9 | ~~ Methods for function \code{nseg} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(object = "cpt")}}{ 15 | Returns the number of segments (i.e. length of the cpts slot) from an object of class cpt 16 | } 17 | \item{\code{signature(object = "cpt.range",ncpts=NA)}}{ 18 | Returns the number of segments from an object of class cpt.range. If ncpts is not specified, this the length of the cpts slot of the cpt.range object. If ncpts is specified, this is (trivially) ncpts+1. 19 | } 20 | \item{\code{signature(object = "cpt.reg")}}{ 21 | Returns the number of segments (i.e. length of the cpts slot) from an object of class cpt.reg 22 | } 23 | }} 24 | \keyword{methods} 25 | \keyword{cpt} 26 | \keyword{internal} 27 | -------------------------------------------------------------------------------- /man/summary-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{summary-methods} 2 | \docType{methods} 3 | \alias{summary-methods} 4 | \alias{summary,ANY-method} 5 | \alias{summary,cpt-method} 6 | \alias{summary,cpt.range-method} 7 | \alias{summary,cpt.reg-method} 8 | \title{ ~~ Methods for Function summary in Package `base' ~~} 9 | \description{ 10 | ~~ Methods for function \code{summary} in Package `base' ~~ 11 | } 12 | \section{Methods}{ 13 | \describe{ 14 | 15 | \item{\code{signature(object = "ANY")}}{ 16 | Generic summary function, see base package description using ?summary 17 | } 18 | 19 | \item{\code{signature(object = "cpt")}}{ 20 | Prints out a summary of the object to the terminal. 21 | } 22 | \item{\code{signature(object = "cpt.range")}}{ 23 | Prints out a summary of the object to the terminal. 24 | } 25 | \item{\code{signature(object = "cpt.reg")}}{ 26 | Prints out a summary of the object to the terminal. 27 | } 28 | }} 29 | \keyword{methods} 30 | \keyword{summary} 31 | \keyword{cpt} 32 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpts-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts-methods} 2 | \docType{methods} 3 | \alias{cpts-methods} 4 | \alias{cpts,cpt-method} 5 | \alias{cpts,cpt.reg-method} 6 | \alias{cpts,cpt.range-method} 7 | \title{ ~~ Methods for Function cpts ~~} 8 | \description{ 9 | ~~ Methods for function \code{cpts} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(object = "cpt")}}{ 15 | Retrieves cpts slot from an object of class cpt, from version 1.0 this no longer prints the length of the dataset. 16 | } 17 | 18 | \item{\code{signature(object = "cpt.reg")}}{ 19 | Retrieves cpts slot from an object of class cpt.reg, from version 1.0 this no longer prints the length of the dataset. 20 | } 21 | 22 | \item{\code{signature(object = "cpt.range",ncpts=NA)}}{ 23 | Retrieves the row in the cpts.full slot from an object of class cpt.range that has length ncpts, from version 1.0 this no longer prints the length of the dataset. 24 | } 25 | }} 26 | \keyword{methods} 27 | \keyword{cpt} 28 | \keyword{internal} -------------------------------------------------------------------------------- /R/decision.R: -------------------------------------------------------------------------------- 1 | decision<-function(tau,null,alt=NA,penalty="MBIC",n=0,diffparam=1,pen.value=0){ 2 | if(sum(is.na(alt))){ 3 | if(length(tau)!=length(null)){ 4 | stop("Lengths of tau and null do not match") 5 | } 6 | } 7 | else{ 8 | if((length(tau)!=length(null))||(length(tau)!=length(alt))){ 9 | stop("Lengths of tau, null and alt do not match") 10 | } 11 | } 12 | single.decision=function(tau,null,alt,n=0,diffparam=1,pen.value=0){ 13 | if(is.na(alt)|!is.finite(alt)){teststat=null} 14 | else{teststat=null-alt} 15 | if(teststat>=pen.value){return(tau)} 16 | else{return(n)} 17 | } 18 | if(length(tau)==1){ 19 | out=single.decision(tau,null,alt,n,diffparam,pen.value) 20 | names(out)="cpt" 21 | return(list(cpt=out,pen=pen.value)) 22 | } 23 | else{ 24 | rep=length(tau) 25 | out=NULL 26 | for(i in 1:rep){ 27 | out[i]=single.decision(tau[i],null[i],alt[i],n,diffparam,pen.value) 28 | } 29 | names(out)=rep("cpt",rep) 30 | return(list(cpt=out,pen=pen.value)) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /man/cpts--methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts<--methods} 2 | \docType{methods} 3 | \alias{cpts<--methods} 4 | \alias{cpts<-,cpt-method} 5 | \alias{cpts<-,cpt.reg-method} 6 | \alias{cpts<-,cpt.range-method} 7 | \title{ ~~ Methods for Function cpts<- ~~} 8 | \description{ 9 | ~~ Methods for function \code{cpts<-} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(x = "cpt")}}{ 15 | Assigns the value following <- to the cpts slot in x, from version 1.0 this no longer requires the last entry to be the length of the dataset. 16 | } 17 | 18 | \item{\code{signature(x = "cpt.reg")}}{ 19 | Assigns the value following <- to the cpts slot in x, from version 1.0 this no longer requires the last entry to be the length of the dataset. 20 | } 21 | \item{\code{signature(x = "cpt.range")}}{ 22 | Assigns the value following <- to the cpts slot in x, from version 1.0 this no longer requires the last entry to be the length of the dataset. 23 | } 24 | }} 25 | \keyword{methods} 26 | \keyword{cpt} 27 | \keyword{internal} -------------------------------------------------------------------------------- /man/logLik-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{logLik-methods} 2 | \docType{methods} 3 | \alias{logLik-methods} 4 | \alias{logLik,cpt-method} 5 | \alias{logLik,cpt.reg-method} 6 | \alias{logLik,cpt.range-method} 7 | \title{ ~~ Methods for Function logLik ~~} 8 | \description{ 9 | ~~ Methods for function \code{logLik} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(object = "cpt")}}{ 15 | Returns the -2*log-likelihood of the data with the fitted changepoints, two values are returned, the raw twice negative log-likelihood and twice negative log-likelihood + penalty. Only valid for cpttype="mean","variance" or "mean and variance". 16 | } 17 | }} 18 | \examples{ 19 | set.seed(1) 20 | x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1)) 21 | out=cpt.var(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5) 22 | logLik(out) # returns the raw scaled negative likelihood (925.8085) and the scaled negative 23 | #likelihood + penalty (957.5984) 24 | } 25 | 26 | \keyword{methods} 27 | \keyword{cpt} 28 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpts.ts-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{cpts.ts-methods} 2 | \docType{methods} 3 | \alias{cpts.ts-methods} 4 | \alias{cpts.ts,cpt-method} 5 | \alias{cpts.ts,cpt.reg-method} 6 | \title{ ~~ Methods for Function cpts.ts ~~} 7 | \description{ 8 | ~~ Methods for function \code{cpts.ts} ~~ 9 | } 10 | \section{Methods}{ 11 | \describe{ 12 | 13 | \item{\code{signature(object = "cpt")}}{ 14 | Retrieves cpts slot from an object of class cpt, from version 1.0 this no longer prints the length of the dataset. Contrary to the \code{cpts} function, cpts.ts displays the changepoints using the "date" index according to the start and frequency of the time series data.set(object) 15 | } 16 | 17 | \item{\code{signature(object = "cpt.reg")}}{ 18 | Retrieves cpts slot from an object of class cpt.reg, from version 1.0 this no longer prints the length of the dataset. Contrary to the \code{cpts} function, cpts.ts displays the changepoints using the "date" index according to the start and frequency of the time series data.set(object) 19 | } 20 | }} 21 | \keyword{methods} 22 | \keyword{cpt} 23 | \keyword{internal} -------------------------------------------------------------------------------- /man/likelihood.Rd: -------------------------------------------------------------------------------- 1 | \name{likelihood} 2 | \alias{likelihood} 3 | \title{ 4 | Generic Function - likelihood 5 | } 6 | \description{ 7 | Generic function to calculate the likelihood 8 | } 9 | \usage{ 10 | likelihood(object) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used to calculate the likelihood (and if one exists) 15 | } 16 | } 17 | \details{ 18 | Generic Function to calculate the likelihood. 19 | } 20 | \value{ 21 | Depends on the class of \code{object}, see individual methods 22 | } 23 | \author{ 24 | Rebecca Killick 25 | } 26 | 27 | 28 | \seealso{ 29 | \code{\link{likelihood-methods}}, \code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 30 | } 31 | \examples{ 32 | set.seed(1) 33 | x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1)) 34 | out=cpt.var(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5) 35 | likelihood(out) # returns the raw scaled negative likelihood (925.8085) and the scaled negative 36 | #likelihood + penalty (957.5984) 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{cpt} 41 | \keyword{internal} -------------------------------------------------------------------------------- /man/seg.len-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{seg.len-methods} 2 | \docType{methods} 3 | \alias{seg.len-methods} 4 | \alias{seg.len,cpt-method} 5 | \alias{seg.len,cpt.range-method} 6 | \alias{seg.len,cpt.reg-method} 7 | \title{ ~~ Methods for Function seg.len ~~} 8 | \description{ 9 | ~~ Methods for function \code{seg.len} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(object = "cpt")}}{ 15 | Returns the length of segments from an object of class cpt, i.e. the number of observations between changepoints 16 | } 17 | \item{\code{signature(object = "cpt.range",ncpts=NA)}}{ 18 | Returns the length of segments from an object of class cpt.range, i.e. the number of observations between changepoints. If ncpts is not specified, this is called on the cpts slot of the cpt.range object. If ncpts is specified, this is called on the cpts.full slot and calculated this for the specific row that gives ncpts. 19 | } 20 | 21 | \item{\code{signature(object = "cpt.reg")}}{ 22 | Returns the length of segments from an object of class cpt.reg, i.e. the number of observations between changepoints 23 | } 24 | }} 25 | \keyword{methods} 26 | \keyword{cpt} 27 | \keyword{internal} 28 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | year <- sub("-.*", "", meta$Date) 2 | note <- sprintf("R package version %s", meta$Version) 3 | 4 | citHeader("The following are references to the package. You should also reference the individual methods used, as detailed in the reference section of the help files for each function.") 5 | 6 | bibentry(bibtype="article", 7 | title = "{changepoint}: An {R} Package for Changepoint Analysis", 8 | author = c(person("Rebecca", "Killick"),person(c("Idris", "A."), "Eckley")), 9 | journal = "Journal of Statistical Software", 10 | year = "2014", 11 | volume = "58", 12 | number = "3", 13 | pages = "1--19", 14 | url = "https://www.jstatsoft.org/article/view/v058i03", 15 | ) 16 | 17 | bibentry(bibtype="Manual", 18 | title = "{changepoint}: An {R} package for changepoint analysis", 19 | author = c(person("Rebecca", "Killick"),person("Kaylea", "Haynes"), person(c("Idris", "A."), "Eckley")), 20 | year = year, 21 | url = "https://CRAN.R-project.org/package=changepoint", 22 | note = note, 23 | ) 24 | 25 | citFooter('To get Bibtex entries use: x<-citation("changepoint"); toBibtex(x)') 26 | -------------------------------------------------------------------------------- /R/data_input.R: -------------------------------------------------------------------------------- 1 | data_input <- function(data, method, pen.value, costfunc, minseglen, Q, var=0, shape=1){ 2 | if(var !=0){ 3 | mu<-var 4 | }else{ 5 | mu <- mean(data) 6 | } 7 | sumstat=cbind(c(0,cumsum(coredata(data))),c(0,cumsum(coredata(data)^2)),cumsum(c(0,(coredata(data)-mu)^2))) 8 | if(method=="PELT"){ 9 | #out=PELT.meanvar.norm(coredata(data),pen.value) 10 | out=PELT(sumstat,pen=pen.value,cost_func = costfunc,minseglen=minseglen, shape=shape) ## K NEW ## 11 | #cpts=out[[2]] 12 | } 13 | else if(method=="BinSeg"){ 14 | out=BINSEG(sumstat,pen=pen.value,cost_func = costfunc,minseglen=minseglen,Q=Q, shape=shape) ## K NEW ## 15 | #cpts=out[[2]] 16 | # out=binseg.meanvar.norm(coredata(data),Q,pen.value) 17 | # if(out$op.cpts==0){cpts=n} 18 | # else{cpts=c(sort(out$cps[1,1:out$op.cpts]),n)} 19 | # the above is now inside the BINSEG function 20 | } 21 | else if(method=="SegNeigh"){ 22 | #out=segneigh.meanvar.norm(coredata(data),Q,pen.value) 23 | out=SEGNEIGH(data=data, pen.value=pen.value, Q=Q, costfunc=costfunc, var=var, shape=shape) 24 | # if(out$op.cpts==0){cpts=n} 25 | # else{cpts=c(sort(out$cps[out$op.cpts+1,][out$cps[out$op.cpts+1,]>0]),n)} 26 | } 27 | return(out) 28 | 29 | } -------------------------------------------------------------------------------- /man/param.Rd: -------------------------------------------------------------------------------- 1 | \name{param} 2 | \alias{param} 3 | \title{ 4 | Generic Function - param 5 | } 6 | \description{ 7 | Generic function that returns parameter estimates. 8 | } 9 | \usage{ 10 | param(object,...) 11 | } 12 | \arguments{ 13 | \item{object}{ 14 | Depending on the class of \code{object} depends on the method used to find the parameter estimates (and if one exists) 15 | } 16 | \item{...}{ 17 | Other variables that may be required depending on the class of \code{object}, see individual methods. 18 | } 19 | } 20 | \details{ 21 | Generic Function that returns parameter estimates 22 | } 23 | \value{ 24 | Depends on the class of \code{object}, see individual methods 25 | } 26 | \author{ 27 | Rebecca Killick 28 | } 29 | 30 | 31 | \seealso{ 32 | \code{\link{param-methods}},\code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 33 | } 34 | \examples{ 35 | set.seed(1) 36 | x=c(rnorm(100,0,1),rnorm(100,0,10)) 37 | ans=cpt.var(x,penalty="Asymptotic",pen.value=0.01,method="AMOC",param.estimates=FALSE) 38 | # if estimates were not requested in the analysis then they can be created at a later stage if 39 | #required 40 | ans=param(ans) # fills the param.est slot with the parameter estimes. 41 | param.est(ans) # variance is 0.8067621 42 | } 43 | 44 | \keyword{methods} 45 | \keyword{cpt} 46 | \keyword{internal} -------------------------------------------------------------------------------- /man/fitted-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{fitted-methods} 2 | \docType{methods} 3 | \alias{fitted-methods} 4 | \alias{fitted,ANY-method} 5 | \alias{fitted,cpt-method} 6 | \alias{fitted,cpt.range-method} 7 | \alias{fitted,cpt.reg-method} 8 | \title{ ~~ Methods for Function fitted in Package `stats' ~~} 9 | \description{ 10 | ~~ Methods for function \code{fitted} in Package `stats' ~~ 11 | } 12 | \section{Methods}{ 13 | \describe{ 14 | 15 | \item{\code{signature(x = "ANY")}}{ 16 | Generic fitted function, see stats package description using ?fitted 17 | } 18 | 19 | \item{\code{signature(x = "cpt")}}{ 20 | Returns the fitted values for the model fit to x. Adapts to the specific model type. 21 | } 22 | \item{\code{signature(x = "cpt.range")}}{ 23 | As for the \code{cpt} objects except for one optional arguments, \code{ncpts}. The \code{ncpts} option allows you to specify the fitted values for \code{ncpts} changepoints in, i.e. the optimal may be specified as 10 changes but you want to get the fitted values for the segmentation with 5 changes (provided a segmentation with 5 changes is listed in \code{cpts.full(x)}. 24 | } 25 | \item{\code{signature(x = "cpt.reg")}}{ 26 | Returns the fitted values for the model fit to x. Adapts to the specific model type. 27 | } 28 | 29 | }} 30 | \keyword{methods} 31 | \keyword{fitted} 32 | \keyword{cpt} 33 | \keyword{internal} 34 | -------------------------------------------------------------------------------- /man/param-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{param-methods} 2 | \docType{methods} 3 | \alias{param-methods} 4 | \alias{param,cpt-method} 5 | \alias{param,cpt.range-method} 6 | \alias{param,cpt.reg-method} 7 | \title{ ~~ Methods for Function param ~~} 8 | \description{ 9 | ~~ Methods for function \code{param} ~~ 10 | } 11 | \section{Methods}{ 12 | \describe{ 13 | 14 | \item{\code{signature(object = "cpt")}}{ 15 | Replaces the \code{param.value} slot in object with the parameter estimates that are appropriate for the changepoint type (\code{cpttype} slot). If the Gamma test statistic is used then the shape parameter is required as a variable. 16 | } 17 | \item{\code{signature(object = "cpt.range")}}{ 18 | Returns a new (blank) cpt.range object where the \code{param.value} slot contains the parameter estimates for a segmentation with the \code{ncpts} specified number of changepoints. This is also tailored to the changepoint type (\code{cpttype} slot) of the original object. If the Gamma test statistic is used then the shape parameter is required as a variable. An example of use is given in the \link{cpt.mean} help file. 19 | } 20 | \item{\code{signature(object = "cpt.reg")}}{ 21 | Replaces the \code{param.value} slot in object with the parameter estimates that are appropriate for the changepoint test statistic (\code{test.stat} slot). 22 | } 23 | }} 24 | \keyword{methods} 25 | \keyword{cpt} 26 | \keyword{internal} -------------------------------------------------------------------------------- /R/CROPS.R: -------------------------------------------------------------------------------- 1 | CROPS <- function(data, penalty="CROPS", pen.value, method="PELT", test.stat="Normal", class=TRUE, param.est=TRUE, minseglen, shape, func){ 2 | if(method != "PELT"){stop('CROPS is a valid penalty choice only if method="PELT", please change your method or your penalty.')} 3 | mu <- mean(data) 4 | sumstat=cbind(c(0,cumsum(coredata(data))),c(0,cumsum(coredata(data)^2)),cumsum(c(0,(coredata(data)-mu)^2))) 5 | 6 | switch(test.stat, 7 | "Normal" = {stat = "norm"}, 8 | "Exponential" = {stat = "exp"}, 9 | "Gamma" = {stat = "gamma"}, 10 | "Poisson" = {stat = "poisson"}, 11 | {stop("Only Normal, Exponential, Gamma and Poisson are valid test statistics")} 12 | ) 13 | costfunc = paste0(func, ".", stat) 14 | 15 | out = range_of_penalties(sumstat, cost=costfunc, min_pen=pen.value[1], max_pen=pen.value[2], minseglen=minseglen) 16 | 17 | if(func=="var"){ 18 | cpttype="variance" 19 | }else if(func=="meanvar"){ 20 | cpttype="mean and variance" 21 | }else{ 22 | cpttype="mean" 23 | } 24 | 25 | if(class==TRUE){ 26 | ans = class_input(data=data,cpttype=cpttype, method="PELT", test.stat=test.stat, penalty=penalty, pen.value=pen.value, minseglen=minseglen, param.estimates=param.est, out=out,shape=shape) 27 | if(func=="var"){ 28 | param.est(ans)=c(param.est(ans),mean=mu) 29 | } 30 | return(ans) 31 | }else{return(out)} 32 | } -------------------------------------------------------------------------------- /R/PELT_one_func_minseglen.R: -------------------------------------------------------------------------------- 1 | PELT = function(sumstat,pen=0, cost_func = "norm.mean", shape = 1, minseglen = 1){ 2 | # function that uses the PELT method to calculate changes in mean where the segments in the data are assumed to be Normal 3 | n = length(sumstat[,1]) - 1 4 | if(n<2){stop('Data must have atleast 2 observations to fit a changepoint model.')} 5 | 6 | storage.mode(sumstat) = 'double' 7 | error=0 8 | 9 | lastchangelike = array(0,dim = n+1) 10 | lastchangecpts = array(0,dim = n+1) 11 | numchangecpts = array(0,dim = n+1) 12 | 13 | cptsout=rep(0,n) # sets up null vector for changepoint answer 14 | storage.mode(cptsout)='integer' 15 | 16 | answer=list() 17 | answer[[6]]=1 18 | on.exit(.C("FreePELT",answer[[6]])) 19 | 20 | storage.mode(lastchangelike) = 'double' 21 | storage.mode(lastchangecpts) = 'integer' 22 | storage.mode(numchangecpts) = 'integer' 23 | 24 | # answer=.C('PELT',cost_func, y3, y2,y,as.integer(n),as.double(pen),cptsout,as.integer(error),as.double(shape)) 25 | answer=.C('PELTC',cost_func, sumstat,as.integer(n),as.double(pen),cptsout,as.integer(error),as.double(shape), as.integer(minseglen), lastchangelike, lastchangecpts,numchangecpts) 26 | 27 | if(answer[[6]]>0){ 28 | stop("C code error:",answer[[6]],call.=F) 29 | } 30 | return(list(lastchangecpts=answer[[10]],cpts=sort(answer[[5]][answer[[5]]>0]), lastchangelike=answer[[9]], ncpts=answer[[11]])) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /man/residuals-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{residuals-methods} 2 | \docType{methods} 3 | \alias{residuals-methods} 4 | \alias{residuals,ANY-method} 5 | \alias{residuals,cpt-method} 6 | \alias{residuals,cpt.range-method} 7 | \alias{residuals,cpt.reg-method} 8 | \title{ ~~ Methods for Function residuals in Package `stats' ~~} 9 | \description{ 10 | ~~ Methods for function \code{residuals} in Package `stats' ~~ 11 | } 12 | \section{Methods}{ 13 | \describe{ 14 | 15 | \item{\code{signature(x = "ANY")}}{ 16 | Generic residuals function, see stats package description using ?residuals 17 | } 18 | 19 | \item{\code{signature(x = "cpt")}}{ 20 | Returns the residuals values from the model fit to x. Adapts to the specific model type. 21 | } 22 | \item{\code{signature(x = "cpt.range")}}{ 23 | As for the \code{cpt} objects except for one optional arguments, \code{ncpts}. The \code{ncpts} option allows you to specify the residuals values for \code{ncpts} changepoints in, i.e. the optimal may be specified as 10 changes but you want to get the residuals values for the segmentation with 5 changes (provided a segmentation with 5 changes is listed in \code{cpts.full(x)}. 24 | } 25 | \item{\code{signature(x = "cpt.reg")}}{ 26 | Returns the residuals values from the model fit to x. Adapts to the specific model type. 27 | } 28 | 29 | }} 30 | \keyword{methods} 31 | \keyword{residuals} 32 | \keyword{cpt} 33 | \keyword{internal} 34 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: changepoint 2 | Type: Package 3 | Title: Methods for Changepoint Detection 4 | Version: 2.3 5 | Date: 2024-11-02 6 | Authors@R: c(person("Rebecca", "Killick", 7 | role=c("aut","cre"),email="r.killick@lancs.ac.uk"), 8 | person("Kaylea", "Haynes", role="ctb"), 9 | person("Harjit","Hullait",role="ctb"), 10 | person("Idris", "Eckley", role=c("ths")), 11 | person("Paul","Fearnhead",role=c("ctb","ths")), 12 | person("Robin", "Long", role="ctb"),person("Jamie","Lee",role="ctr")) 13 | Maintainer: Rebecca Killick 14 | BugReports: https://github.com/rkillick/changepoint/issues 15 | URL: https://github.com/rkillick/changepoint/ 16 | Description: Implements various mainstream and specialised changepoint methods for finding single and multiple changepoints within data. Many popular non-parametric and frequentist methods are included. The cpt.mean(), cpt.var(), cpt.meanvar() functions should be your first point of call. 17 | Depends: R(>= 3.2), methods, stats, zoo(>= 0.9-1) 18 | Suggests: testthat, vdiffr 19 | License: GPL 20 | LazyData: true 21 | Packaged: 2024-11-02 14:14:13 UTC; killick 22 | NeedsCompilation: yes 23 | Repository: CRAN 24 | Date/Publication: 2024-11-02 15:50:02 UTC 25 | Author: Rebecca Killick [aut, cre], 26 | Kaylea Haynes [ctb], 27 | Harjit Hullait [ctb], 28 | Idris Eckley [ths], 29 | Paul Fearnhead [ctb, ths], 30 | Robin Long [ctb], 31 | Jamie Lee [ctr] 32 | -------------------------------------------------------------------------------- /R/BinSeg_one_func_minseglen.R: -------------------------------------------------------------------------------- 1 | BINSEG = function(sumstat, pen = 0, cost_func = "norm.mean", shape = 1, minseglen = 2, Q=5){ 2 | 3 | n = length(sumstat[,1]) - 1 4 | if(n<2){stop('Data must have at least 2 observations to fit a changepoint model.')} 5 | if(Q>((n/minseglen)+1)){stop(paste('Q is larger than the maximum number of segments',(n/minseglen)+1))} 6 | if(Q>n){stop(paste("Q is larger than the length of the data length"))} 7 | if(Q<=0){stop(paste('Q is the maximum number of changepoints so should be greater than 0'))} 8 | 9 | storage.mode(sumstat) = 'double' 10 | 11 | cptsout=rep(0,Q) # sets up null vector for changepoint answer 12 | likeout=rep(0,Q) # sets up null vector for likelihood of changepoints in cptsout 13 | storage.mode(cptsout)='integer' 14 | storage.mode(likeout)='double' 15 | op_cps = 0 16 | 17 | answer=.C('binseg', cost_func = cost_func, sumstat = sumstat, n = as.integer(n), pen = as.double(pen), Q = as.integer(Q), cptsout = cptsout, minseglen = as.integer(minseglen), likeout = likeout, op_cps = as.integer(op_cps), shape = as.double(shape)) 18 | if((answer$op_cps == Q)&(pen!=0)){warning('The number of changepoints identified is Q, it is advised to increase Q to make sure changepoints have not been missed.')} 19 | if(answer$op_cps == 0){cpts=n} 20 | else{cpts=c(sort(answer$cptsout[1:answer$op_cps]),n)} 21 | return(list(cps=rbind(answer$cptsout,2*answer$likeout),cpts=cpts,op.cpts=answer$op_cps,pen=pen)) 22 | } 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/changepoint_init.c: -------------------------------------------------------------------------------- 1 | #include // for NULL 2 | #include 3 | 4 | /* FIXME: 5 | Check these declarations against the C/Fortran source code. 6 | */ 7 | 8 | /* .C calls */ 9 | extern void binseg(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 10 | extern void FreePELT(void *); 11 | extern void PELTC(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 12 | extern void CptReg_Normal_PELT(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 13 | extern void Free_CptReg_Normal_PELT(void *); 14 | extern void CptReg_Normal_AMOC(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *, void *); 15 | extern void Free_CptReg_Normal_AMOC(void *); 16 | 17 | static const R_CMethodDef CEntries[] = { 18 | {"binseg", (DL_FUNC) &binseg, 10}, 19 | {"FreePELT", (DL_FUNC) &FreePELT, 1}, 20 | {"PELTC", (DL_FUNC) &PELTC, 11}, 21 | {"CptReg_Normal_PELT", (DL_FUNC) &CptReg_Normal_PELT, 13}, 22 | {"Free_CptReg_Normal_PELT", (DL_FUNC) &Free_CptReg_Normal_PELT, 1}, 23 | {"CptReg_Normal_AMOC", (DL_FUNC) &CptReg_Normal_AMOC, 13}, 24 | {"Free_CptReg_Normal_AMOC", (DL_FUNC) &Free_CptReg_Normal_AMOC,1}, 25 | {NULL, NULL, 0} 26 | }; 27 | 28 | void R_init_changepoint(DllInfo *dll) 29 | { 30 | R_registerRoutines(dll, CEntries, NULL, NULL, NULL); 31 | R_useDynamicSymbols(dll, FALSE); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master, '**'] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macOS-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v2 33 | 34 | - uses: r-lib/actions/setup-pandoc@v2 35 | 36 | - uses: r-lib/actions/setup-r@v2 37 | with: 38 | r-version: ${{ matrix.config.r }} 39 | http-user-agent: ${{ matrix.config.http-user-agent }} 40 | use-public-rspm: true 41 | 42 | - uses: r-lib/actions/setup-r-dependencies@v2 43 | with: 44 | extra-packages: any::rcmdcheck 45 | needs: check 46 | 47 | - uses: r-lib/actions/check-r-package@v2 48 | with: 49 | upload-snapshots: true 50 | -------------------------------------------------------------------------------- /R/class_input.R: -------------------------------------------------------------------------------- 1 | class_input <- function(data, cpttype, method, test.stat, penalty, pen.value, minseglen, param.estimates, out=list(), Q=NA, shape=NA){ 2 | if(method=="BinSeg" || method=="SegNeigh" || penalty=="CROPS"){ 3 | ans=new("cpt.range") 4 | }else{ 5 | ans=new("cpt") 6 | } 7 | 8 | data.set(ans)=data;cpttype(ans)=cpttype;method(ans)=method; test.stat(ans)=test.stat;pen.type(ans)=penalty;pen.value(ans)=pen.value;minseglen(ans)=minseglen;ans@date=date(); 9 | if(penalty!="CROPS"){ # crops is only one that doesn't give a single set of cpts 10 | cpts(ans)=out[[2]] 11 | 12 | if(param.estimates==TRUE){ 13 | if(test.stat == "Gamma"){ 14 | ans=param(ans, shape) 15 | }else{ 16 | ans=param(ans) 17 | } 18 | } 19 | } 20 | 21 | if(method=="PELT"){ 22 | ncpts.max(ans)=Inf 23 | } 24 | else if(method=="AMOC"){ 25 | ncpts.max(ans)=1 26 | } 27 | else{ 28 | ncpts.max(ans)=Q 29 | } 30 | 31 | if(method=="BinSeg"){ 32 | l=list() 33 | for(i in 1:(length(out$cps)/2)){ 34 | l[[i]] = out$cps[1,1:i] 35 | } 36 | m = t(sapply(l, '[', 1:max(sapply(l, length)))) 37 | 38 | cpts.full(ans)=m 39 | pen.value.full(ans)=out$cps[2,] 40 | }else if(method=="SegNeigh"){ 41 | cpts.full(ans)=out$cps[-1,] 42 | pen.value.full(ans)=-diff(out$like.Q) 43 | }else if(penalty=="CROPS"){ 44 | m = t(sapply(out[[2]], '[', 1:max(sapply(out[[2]], length)))) 45 | 46 | cpts.full(ans) = m 47 | pen.value.full(ans) = c(out[[1]][1,],pen.value[2]) # add in the final penalty in the range as this is removed as a duplicate set of changepoints 48 | if(test.stat=="Gamma"){param.est(ans)$shape=shape} 49 | } 50 | 51 | return(ans) 52 | } 53 | -------------------------------------------------------------------------------- /CITATION.cff: -------------------------------------------------------------------------------- 1 | abstract: | 2 | Implements various mainstream and specialised changepoint methods 3 | for finding single and multiple changepoints within data. Many 4 | popular non-parametric and frequentist methods are included. The 5 | cpt.mean(), cpt.var(), cpt.meanvar() functions should be your first 6 | point of call. 7 | authors: 8 | - family-names: Killick 9 | given-names: Rebecca 10 | email: r.killick at lancs.ac.uk 11 | orcid: "https://orcid.org/0000-0003-0583-3960" 12 | - family-names: Haynes 13 | given-names: Kaylea 14 | - family-names: Eckley 15 | given-names: Idris 16 | - family-names: Fearnhead 17 | given-names: Paul 18 | - family-names: Lee 19 | given-names: Jamie 20 | cff-version: 1.2.0 21 | date-released: "2016-10-04" 22 | license: 23 | - GPL-3.0 24 | - GPL-2.0 25 | repository-code: "https://github.com/rkillick/changepoint" 26 | version: 2.2.2 27 | title: "Changepoint: Methods for Changepoint Detection" 28 | message: | 29 | If you use this software, please cite both the article from 30 | preferred-citation and the software itself. 31 | preferred-citation: 32 | authors: 33 | - family-names: Killick 34 | given-names: Rebecca 35 | orcid: "https://orcid.org/0000-0003-0583-3960" 36 | - family-names: Haynes 37 | given-names: Kaylea 38 | - family-names: Eckley 39 | given-names: Idris 40 | title: "changepoint: An R package for changepoint analysis" 41 | type: software 42 | version: 1.0 43 | year: 2019 44 | url: "https://CRAN.R-project.org/package=changepoint" 45 | notes: "R package version 2.2.2" 46 | references: 47 | - authors: 48 | - family-names: Killick 49 | given-names: Rebecca 50 | orcid: "https://orcid.org/0000-0003-0583-3960" 51 | - family-names: Eckley 52 | given-names: Idris 53 | title: "changepoint: An R Package for Changepoint Analysis" 54 | type: article 55 | journal: "Journal of Statistical Software" 56 | year: 2014 57 | volume: 58 58 | number: 3 59 | pages: "1--19" 60 | url: "http://www.jstatsoft.org/v58/i03/" 61 | -------------------------------------------------------------------------------- /man/changepoint-package.Rd: -------------------------------------------------------------------------------- 1 | \name{changepoint-package} 2 | \alias{changepoint-package} 3 | \alias{changepoint} 4 | \docType{package} 5 | \title{ 6 | Methods for Changepoint Detection 7 | } 8 | \description{ 9 | Implements various mainstream and specialised changepoint methods for finding single and multiple changepoints within data. Many popular non-parametric and frequentist methods are included. Users should start by looking at the documentation for cpt.mean(), cpt.var() and cpt.meanvar(). 10 | } 11 | \details{ 12 | \tabular{ll}{ 13 | Package: \tab changepoint\cr 14 | Type: \tab Package\cr 15 | Version: \tab 2.3 \cr 16 | Date: \tab 2024-11-02\cr 17 | License: \tab GPL\cr 18 | LazyLoad: \tab yes\cr 19 | } 20 | 21 | } 22 | \author{ 23 | Rebecca Killick , with contributions from Kaylea Haynes, Harjit Hullait, Paul Fearnhead, Robin Long and Jamie Lee. 24 | 25 | Maintainer: Rebecca Killick 26 | } 27 | 28 | \references{ 29 | Chen, J. and Gupta, A. K. (2000) \emph{Parametric statistical change point analysis}, Birkhauser 30 | 31 | PELT Algorithm: Killick R, Fearnhead P, Eckley IA (2012) Optimal detection of changepoints with a linear computational cost, \emph{JASA} \bold{107(500)}, 1590--1598 32 | 33 | Binary Segmentation: Scott, A. J. and Knott, M. (1974) A Cluster Analysis Method for Grouping Means in the Analysis of Variance, \emph{Biometrics} \bold{30(3)}, 507--512 34 | 35 | Segment Neighbourhoods: Auger, I. E. And Lawrence, C. E. (1989) Algorithms for the Optimal Identification of Segment Neighborhoods, \emph{Bulletin of Mathematical Biology} \bold{51(1)}, 39--54 36 | 37 | } 38 | \keyword{changepoint} 39 | \keyword{segmentation} 40 | \seealso{ 41 | \code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 42 | } 43 | \examples{ 44 | # change in variance 45 | set.seed(1) 46 | x=c(rnorm(100,0,1),rnorm(100,0,10)) 47 | ansvar=cpt.var(x) 48 | plot(ansvar) 49 | print(ansvar) # identifies 1 changepoint at 100 50 | 51 | # change in mean 52 | y=c(rnorm(100,0,1),rnorm(100,5,1)) 53 | ansmean=cpt.mean(y) 54 | plot(ansmean,cpt.col='blue') 55 | print(ansmean) 56 | 57 | # change in mean and variance 58 | z=c(rnorm(100,0,1),rnorm(100,2,10)) 59 | ansmeanvar=cpt.meanvar(z) 60 | plot(ansmeanvar,cpt.width=3) 61 | print(ansmeanvar) 62 | } 63 | -------------------------------------------------------------------------------- /man/plot-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{plot-methods} 2 | \docType{methods} 3 | \alias{plot-methods} 4 | \alias{plot,ANY-method} 5 | \alias{plot,cpt-method} 6 | \alias{plot,cpt.range-method} 7 | \alias{plot,cpt.reg-method} 8 | \title{ ~~ Methods for Function plot in Package `graphics' ~~} 9 | \description{ 10 | ~~ Methods for function \code{plot} in Package `graphics' ~~ 11 | } 12 | \section{Methods}{ 13 | \describe{ 14 | 15 | \item{\code{signature(x = "ANY")}}{ 16 | Generic plot function, see graphics package description using ?plot 17 | } 18 | 19 | \item{\code{signature(x = "cpt")}}{ 20 | Plots the data and identifies the changepoints using vertical lines (change in variance), horizontal lines (change in mean). Optional arguments to control the lines: \code{cpt.col} equivilent to \code{col} to change the colour of the changepoint line; \code{cpt.width} equivilent to \code{lwd} to change the width of the changepoint line; \code{cpt.style} equivilent to \code{lty} to change the style of the line. 21 | } 22 | \item{\code{signature(x = "cpt.range")}}{ 23 | As for the \code{cpt} objects except for two optional arguments, \code{ncpts} and \code{diagnostic}. The \code{ncpts} option allows you to specify a plot of the segmentation with \code{ncpts} changepoints in, i.e. the optimal may be specified as 10 changes but you want to plot the segmentation with 5 changes (provided a segmentation with 5 changes is listed in \code{cpts.full(x)}. The \code{diagnostic} option when set to \code{TRUE} plots the number of changepoints in each segmentation against the penalty values that give that number of changepoints. This can aide the decision on the number of changepoints as when a true changepoint is added the cost decreases considerably so it creates a stable region where several penalty values give the same number of changepoints, but when a changepoint due to noise is added the change in cost is small and so a small change in penalty value can vary the number of changes a lot. This is akin to a scree plot in principal component analysis. The idea is that someone may choose to create a plot using \code{diagnostic=TRUE}, identify the appropriate number of changes and then replot using \code{ncpts} to visualize that segmentation. 24 | } 25 | \item{\code{signature(x = "cpt.reg")}}{ 26 | Plots the combined regressors (model fit) against the response and identifies the changepoints using vertical lines. Optional arguments to control the lines: \code{cpt.col} equivilent to \code{col} to change the colour of the changepoint line; \code{cpt.width} equivilent to \code{lwd} to change the width of the changepoint line; \code{cpt.style} equivilent to \code{lty} to change the style of the line. 27 | } 28 | 29 | }} 30 | \keyword{methods} 31 | \keyword{plot} 32 | \keyword{cpt} 33 | \keyword{internal} 34 | -------------------------------------------------------------------------------- /man/class_input.Rd: -------------------------------------------------------------------------------- 1 | \name{class_input} 2 | \alias{class_input} 3 | \title{ 4 | Input all required arguments into cpt classes - Only intended for developer use. 5 | } 6 | \description{ 7 | This function helps to input all the necessary information into the correct format for \code{cpt} and \code{cpt.range} classes. 8 | 9 | This function is called by \code{cpt.mean}, \code{cpt.var} and \code{cpt.meanvar} when \code{class=TRUE}. This is not intended for use by regular users of the package. It is exported for developers to call directly for speed and convenience. 10 | 11 | WARNING: No checks on arguments are performed! 12 | } 13 | \usage{ 14 | class_input(data, cpttype, method, test.stat, penalty, pen.value, minseglen, 15 | param.estimates, out=list(), Q=NA, shape=NA) 16 | } 17 | \arguments{ 18 | \item{data}{ 19 | Data used in changepoint analysis, see \code{\link{cpt.mean}} for further details. 20 | } 21 | \item{cpttype}{ 22 | Type of changepoint analysis performed as a text string, e.g. "Mean", "Mean and Variance". 23 | } 24 | \item{method}{ 25 | Method used as a text string, see \code{\link{cpt.mean}} for further details. 26 | } 27 | \item{test.stat}{ 28 | The assumed test statistic / distribution of the data as a text string. , see \code{\link{cpt.mean}}, \code{\link{cpt.meanvar}} or \code{\link{cpt.var}} for further details. 29 | } 30 | \item{penalty}{ 31 | Penalty used as a text string, see \code{\link{cpt.mean}} for further details. 32 | } 33 | \item{pen.value}{ 34 | Numerical penalty value used in the analysis (positive). 35 | } 36 | \item{minseglen}{ 37 | Minimum segment length used in the analysis (positive integer). 38 | } 39 | \item{param.estimates}{ 40 | Logical. If TRUE then parameter estimates are calculated. If FALSE no parameter estimates are calculated and the slot is blank in the returned object. 41 | } 42 | \item{out}{ 43 | List of output from \code{\link{BINSEG}}, \code{\link{PELT}} or other \code{method} used. Function assumes that \code{method} and format of \code{out} match. 44 | } 45 | \item{Q}{ 46 | The value of \code{Q} used in the \code{BinSeg} or \code{SegNeigh} methods. 47 | } 48 | \item{shape}{ 49 | Value of the assumed known shape parameter required when test.stat="Gamma". 50 | } 51 | } 52 | \details{ 53 | This function takes all the input required for the \code{cpt} or \code{cpt.range} classes and enters it into the object. 54 | 55 | This function is exported for developer use only. It does not perform any checks on inputs and is simply a convenience function for converting the output of the worker functions into a nice format for the \code{cpt} and \code{cpt.range} classes. 56 | } 57 | \value{ 58 | An object of class \code{cpt} or \code{cpt.range} as appropriate filled with the given attributes. 59 | } 60 | \author{ 61 | Rebecca Killick 62 | } 63 | \seealso{ 64 | \code{\link{cpt.var}},\code{\link{cpt.mean}},\code{\link{plot-methods}},\code{\linkS4class{cpt}} 65 | } 66 | \examples{ 67 | #This function should only be used by developers, see its use in cpt.mean, cpt.var and cpt.meanvar. 68 | } 69 | 70 | \keyword{methods} 71 | \keyword{univar} 72 | \keyword{models} 73 | \keyword{ts} 74 | -------------------------------------------------------------------------------- /man/PELT.Rd: -------------------------------------------------------------------------------- 1 | \name{PELT} 2 | \alias{PELT} 3 | \title{ 4 | PELT (Pruned Exact Linear Time) - Only intended for developer use. 5 | } 6 | \description{ 7 | Implements the PELT method for identifying changepoints in a given set of summary statistics for a specified cost function and penalty. 8 | 9 | This function is called by \code{cpt.mean}, \code{cpt.var} and \code{cpt.meanvar} when \code{method="PELT"}. This is not intended for use by regular users of the package. It is exported for developers to call directly for speed increases or to fit alternative cost functions. 10 | 11 | WARNING: No checks on arguments are performed! 12 | } 13 | \usage{ 14 | PELT(sumstat, pen = 0, cost_func = "norm.mean", shape = 1, minseglen = 1) 15 | } 16 | \arguments{ 17 | \item{sumstat}{ 18 | A matrix containing the summary statistics of data within which you wish to find a changepoint. Currently assumes 3 columns and uses the number of rows as the length of the data +1 (initial value of 0). 19 | } 20 | \item{pen}{ 21 | Default choice is 0, this should be evaluated elsewhere and a numerical value entered. This should be positive - this isn't checked but results are meaningless if it isn't. 22 | } 23 | \item{cost_func}{ 24 | The friendly name of the cost function to be called in C. If using your own cost function, this must be the name of the C function to use. 25 | } 26 | \item{shape}{ 27 | Only required for cost_func="Gamma",default is 1. Must be a positive value, this isn't checked. 28 | } 29 | \item{minseglen}{ 30 | Positive integer giving the minimum segment length (no. of observations between changes), default is 1. No checks are performed on the input value so it could be larger than feasible to have changes in the data. 31 | } 32 | } 33 | \details{ 34 | This function is used as a wrapper function to implement the PELT algorithm in C. It simply creates the necessary worker vectors, ensures all inputs are the correct type, and passes everything to the C function. 35 | 36 | This function is exported for developer use only. It does not perform any checks on inputs (other than type coersion) and is simply a wrapper function for the C code. 37 | } 38 | \value{ 39 | A list is returned with elements: 40 | \item{lastchangecpts}{Vector of length n containing the last changepoint prior to each timepoint.} 41 | \item{cpts}{Ordered list of optimal number of changepoints ending with n.} 42 | \item{lastchangelike}{Vector of lenght n containing the likelihood of the optimal segmentation up to each timepoint.} 43 | \item{ncpts}{Number of changes identified.} 44 | } 45 | \references{ 46 | PELT Algorithm: Killick R, Fearnhead P, Eckley IA (2012) Optimal detection of changepoints with a linear computational cost, \emph{JASA} \bold{107(500)}, 1590--1598 47 | 48 | CROPS: Haynes K, Eckley IA, Fearnhead P (2014) Efficient penalty search for multiple changepoint problems (in submission), arXiv:1412.3617 49 | } 50 | \author{ 51 | Rebecca Killick 52 | } 53 | 54 | 55 | \seealso{ 56 | \code{\link{cpt.mean}},\code{\link{cpt.meanvar}},\code{\link{plot-methods}},\code{\linkS4class{cpt}} 57 | } 58 | \examples{ 59 | #This function should only be used by developers, see its use in cpt.mean, cpt.var and cpt.meanvar. 60 | } 61 | 62 | \keyword{methods} 63 | \keyword{univar} 64 | \keyword{models} 65 | \keyword{ts} 66 | -------------------------------------------------------------------------------- /man/BINSEG.Rd: -------------------------------------------------------------------------------- 1 | \name{BINSEG} 2 | \alias{BINSEG} 3 | \title{ 4 | Binary Segmentation - Only intended for developer use. 5 | } 6 | \description{ 7 | Implements the Binary Segmentation method for identifying changepoints in a given set of summary statistics for a specified cost function and penalty. 8 | 9 | This function is called by \code{cpt.mean}, \code{cpt.var} and \code{cpt.meanvar} when \code{method="BinSeg"}. This is not intended for use by regular users of the package. It is exported for developers to call directly for speed increases or to fit alternative cost functions. 10 | 11 | WARNING: No checks on arguments are performed! 12 | } 13 | \usage{ 14 | BINSEG(sumstat, pen = 0, cost_func = "norm.mean", shape = 1, minseglen = 2, Q=5) 15 | } 16 | \arguments{ 17 | \item{sumstat}{ 18 | A matrix containing the summary statistics of data within which you wish to find a changepoint. Currently assumes 3 columns and uses the number of rows as the length of the data +1 (initial value of 0). 19 | } 20 | \item{pen}{ 21 | Default choice is 0, this should be evaluated elsewhere and a numerical value entered. This should be positive - this isn't checked but results are meaningless if it isn't. 22 | } 23 | \item{cost_func}{ 24 | The friendly name of the cost function to be called in C. If using your own cost function, this must be the name of the C function to use. 25 | } 26 | \item{shape}{ 27 | Only required for cost_func="Gamma",default is 1. Must be a positive value, this isn't checked. 28 | } 29 | \item{minseglen}{ 30 | Positive integer giving the minimum segment length (no. of observations between changes), default is 2. No checks are performed on the input value so it could be larger than feasible to have changes in the data. 31 | } 32 | \item{Q}{ 33 | The maximum number of changepoints to search for (positive integer). No checks are performed and so a number larger than allowed can be input. 34 | } 35 | } 36 | \details{ 37 | This function is used as a wrapper function to implement the Binary Segmentation algorithm in C. It simply creates the necessary worker vectors, ensures all inputs are the correct type, and passes everything to the C function. 38 | 39 | This function is exported for developer use only. It does not perform any checks on inputs (other than type coersion) and is simply a wrapper function for the C code. 40 | } 41 | \value{ 42 | A list is returned with elements: 43 | \item{cps}{2xQ Matrix containing the changepoint positions on the first row and the test statistic on the second row in the order identified.} 44 | \item{cpts}{Ordered list of optimal number of changepoints ending with n.} 45 | \item{op.cpts}{The optimal number changepoint locations for the penalty supplied.} 46 | \item{pen}{Penalty used to find the optimal number of changepoints.} 47 | } 48 | \references{ 49 | Binary Segmentation: Scott, A. J. and Knott, M. (1974) A Cluster Analysis Method for Grouping Means in the Analysis of Variance, \emph{Biometrics} \bold{30(3)}, 507--512 50 | } 51 | \author{ 52 | Rebecca Killick 53 | } 54 | 55 | 56 | \seealso{ 57 | \code{\link{cpt.mean}},\code{\link{cpt.meanvar}},\code{\link{plot-methods}},\code{\linkS4class{cpt}} 58 | } 59 | \examples{ 60 | #This function should only be used by developers, see its use in cpt.mean, cpt.var and cpt.meanvar. 61 | } 62 | 63 | \keyword{methods} 64 | \keyword{univar} 65 | \keyword{models} 66 | \keyword{ts} 67 | -------------------------------------------------------------------------------- /src/cost_general_functions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // RK addition 4 | #include // RK addition 5 | #include // RK addition 6 | #include // RK addition 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define SWAP(a,b) { int t; t=a; a=b; b=t; } // Macro for swapping 14 | 15 | // Cost functions 16 | 17 | double mll_mean(double x, double x2, double x3, int n, double shape){ 18 | return(x2-(x*x)/n); 19 | } 20 | 21 | double mll_var(double x, double x2, double x3, int n, double shape){ 22 | if(x3<=0){x3=0.00000000001;} 23 | return(n*(log(2*M_PI)+log(x3/n)+1)); /* M_PI is in Rmath.h */ 24 | } 25 | 26 | double mll_meanvar(double x, double x2, double x3, int n, double shape){ 27 | double sigsq=(x2-((x*x)/n))/n; 28 | if(sigsq<=0){sigsq=0.00000000001;} 29 | return(n*(log(2*M_PI)+log(sigsq)+1)); /* M_PI is in Rmath.h */ 30 | } 31 | 32 | 33 | double mll_meanvar_exp(double x, double x2, double x3, int n, double shape){ 34 | return(2*n*(log(x)-log(n))); 35 | } 36 | 37 | double mll_meanvar_gamma(double x, double x2, double x3, int n, double shape){ 38 | return(2*n*shape*(log(x)-log(n*shape))); 39 | } 40 | 41 | double mll_meanvar_poisson(double x, double x2, double x3, int n, double shape){ 42 | if(x==0){return(0);} 43 | else{return(2*x*(log(n)-log(x)));} 44 | } 45 | 46 | double mbic_var(double x, double x2, double x3, int n, double shape){ 47 | if(x3<=0){x3=0.00000000001;} 48 | return(n*(log(2*M_PI)+log(x3/n)+1)+log(n)); /* M_PI is in Rmath.h */ 49 | } 50 | 51 | double mbic_meanvar(double x, double x2, double x3, int n, double shape){ 52 | double sigsq=(x2-((x*x)/n))/n; 53 | if(sigsq<=0){sigsq=0.00000000001;} 54 | return(n*(log(2*M_PI)+log(sigsq)+1)+log(n)); /* M_PI is in Rmath.h */ 55 | } 56 | 57 | 58 | double mbic_mean(double x, double x2, double x3, int n, double shape){ 59 | return(x2-(x*x)/n+log(n)); 60 | } 61 | 62 | double mbic_meanvar_exp(double x, double x2, double x3, int n, double shape){ 63 | return(2*n*(log(x)-log(n))+log(n)); 64 | } 65 | 66 | double mbic_meanvar_gamma(double x, double x2, double x3, int n, double shape){ 67 | return(2*n*shape*(log(x)-log(n*shape))+log(n)); 68 | } 69 | 70 | double mbic_meanvar_poisson(double x, double x2, double x3, int n, double shape){ 71 | if(x==0){return(0);} 72 | else{return(2*x*(log(n)-log(x))+log(n));} 73 | } 74 | 75 | 76 | void max_which(double *array,int n,double *maxout,int *whichout){ 77 | // Function to find maximum of an array with n elements that is put in max 78 | *maxout=*array; 79 | *whichout=0; 80 | int i; 81 | for(i=1;i *maxout){ 83 | *maxout= *(array+i); 84 | *whichout=i; 85 | } 86 | } 87 | } 88 | 89 | void min_which(double *array,int n,double *minout,int *whichout){ 90 | // Function to find minimum of an array with n elements that is put in min 91 | *minout=*array; 92 | *whichout=0; 93 | int i; 94 | for(i=1;i a[j]) // If the the first number is greater, swap it 107 | SWAP(a[j-1],a[j]); 108 | } 109 | } 110 | } 111 | 112 | -------------------------------------------------------------------------------- /man/penalty_decision.Rd: -------------------------------------------------------------------------------- 1 | \name{penalty_decision} 2 | \alias{penalty_decision} 3 | \title{ 4 | Penalty Decision Function - Only intended for developer use. 5 | } 6 | \description{ 7 | Evaluates the arguments to give a numeric value for the penalty. 8 | 9 | This function is called by \code{cpt.mean}, \code{cpt.var} and \code{cpt.meanvar}. This is not intended for use by regular users of the package. It is exported for developers to call directly for speed increases or to fit alternative cost functions. 10 | 11 | WARNING: No checks on arguments are performed! 12 | } 13 | \usage{ 14 | penalty_decision(penalty, pen.value, n, diffparam, asymcheck, method) 15 | } 16 | \arguments{ 17 | \item{penalty}{ 18 | Choice of "None", "SIC", "BIC", "MBIC", AIC", "Hannan-Quinn", "Asymptotic" and "Manual" penalties. If Manual is specified, the manual penalty is contained in the pen.value parameter. If Asymptotic is specified, the theoretical type I error is contained in the pen.value parameter. The predefined penalties listed DO count the changepoint as a parameter, postfix a 0 e.g."SIC0" to NOT count the changepoint as a parameter. 19 | } 20 | \item{pen.value}{ 21 | The theoretical type I error e.g.0.05 when using the Asymptotic penalty. The value of the penalty when using the Manual penalty option - this can be a numeric value or text giving the formula to use. Available variables are, n=length of original data, null=null likelihood, alt=alternative likelihood, tau=proposed changepoint, diffparam=difference in number of alternatve and null parameters. 22 | } 23 | \item{n}{ 24 | The length of the original data, required to give sensible "no changepoint" output. 25 | } 26 | \item{diffparam}{ 27 | The difference in the number of parameters (degrees of freedom) when a change is added, required for the SIC, BIC, AIC, Hanna-Quinn and possibly Manual penalties. Do NOT include the changepoint when calculating this number as this is automatically added. 28 | } 29 | \item{asymcheck}{ 30 | A text string which translates to the asymptotic formula for a specific cost function. Currently implemented values are: \code{mean.norm}, \code{var.norm}, \code{meanvar.norm}, \code{reg.norm}, \code{var.css}, \code{mean.cusum}, \code{meanvar.gamma}, \code{meanvar.exp}, \code{meanvar.poisson}. 31 | } 32 | \item{method}{ 33 | Method used as a text string, see \code{\link{cpt.mean}} for further details. 34 | } 35 | } 36 | \details{ 37 | This function takes the text string input and converts it to a numerical value for the specific length of data specified by n. 38 | 39 | This function is exported for developer use only. It does not perform any checks on inputs and is included for convenience and speed for those who are developing their own cost functions. 40 | } 41 | \value{ 42 | The numeric value of the penalty. 43 | } 44 | \references{ 45 | SIC/BIC: Schwarz, G. (1978) Estimating the Dimension of a Model, \emph{The Annals of Statistics} \bold{6(2)}, 461--464 46 | 47 | MBIC: Zhang, N. R. and Siegmund, D. O. (2007) A Modified Bayes Information Criterion with Applications to the Analysis of Comparative Genomic Hybridization Data. \emph{Biometrics} \bold{63}, 22-32. 48 | 49 | AIC: Akaike, H. (1974) A new look at the statistical model identification, \emph{Automatic Control, IEEE Transactions on} \bold{19(6)}, 716--723 50 | 51 | Hannan-Quinn: Hannan, E. J. and B. G. Quinn (1979) The Determination of the Order of an Autoregression, \emph{Journal of the Royal Statistical Society, B} \bold{41}, 190--195 52 | } 53 | \author{ 54 | Rebecca Killick 55 | } 56 | 57 | 58 | \seealso{ 59 | \code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 60 | } 61 | \examples{ 62 | # Example of finding a change 63 | out=c(100,765.1905,435.6529) # tau, null, alt 64 | decision(out[1],out[2],out[3],penalty="SIC",n=200,diffparam=1) # returns 100 as a true changepoint 65 | 66 | # Example of no change found 67 | out=c(53,-22.47768,-24.39894) # tau, null, alt 68 | decision(out[1],out[2],out[3],penalty="Manual",n=200,diffparam=1,pen.value="2*log(n)") 69 | } 70 | 71 | \keyword{methods} 72 | \keyword{univar} 73 | \keyword{models} 74 | \keyword{ts} 75 | -------------------------------------------------------------------------------- /R/range_of_penalties.R: -------------------------------------------------------------------------------- 1 | ######## Function to run PELT for a Range Of Penalty values. ############ 2 | 3 | range_of_penalties <- function(sumstat,cost = "mean.norm",PELT = T,min_pen=log(length(sumstat)/3-1),max_pen=10*log(length(sumstat)/3-1), shape = 1, minseglen) { 4 | 5 | NCALC=0 6 | pen_interval <- c(min_pen,max_pen) 7 | n = length(sumstat)/3 - 1 8 | 9 | test_penalties <- NULL 10 | numberofchangepoints <- NULL 11 | penal <- NULL 12 | overall_cost <- array() 13 | segmentations <- NULL 14 | b_between <- array() 15 | 16 | ##### Want to store and use Func, M and CP in PELT 17 | 18 | count <- 0 19 | 20 | while (length(pen_interval) > 0){ 21 | 22 | new_numcpts <- array() 23 | new_penalty <- array() 24 | new_cpts <- array() 25 | 26 | for (b in 1:length(pen_interval)) { 27 | 28 | ans<- PELT(sumstat,pen=pen_interval[b], cost_func = cost , shape = shape, minseglen = minseglen) 29 | resultingcpts <- ans[[2]] 30 | new_numcpts[b] <- length(resultingcpts) 31 | new_cpts[b] <- list(resultingcpts[-length(resultingcpts)]) 32 | new_penalty[b] <- ans[[3]][n+1]-(ans[[4]][n+1]-1)*pen_interval[b] 33 | } 34 | 35 | if (count == 0){ 36 | print(paste("Maximum number of runs of algorithm = ", new_numcpts[1] - new_numcpts[2] + 2, sep = "")) 37 | count <- count + length(new_numcpts) 38 | print(paste("Completed runs = ", count, sep = "")) 39 | } 40 | 41 | else{ 42 | count <- count + length(new_numcpts) 43 | print(paste("Completed runs = ", count, sep = "")) 44 | } 45 | 46 | 47 | ## Add the values calculated to the already stored values 48 | test_penalties <- unique((sort(c(test_penalties,pen_interval)))) 49 | new_numcpts <- c(numberofchangepoints,new_numcpts) 50 | new_penalty <- c(penal,new_penalty) 51 | 52 | new_cpts <- c(segmentations,new_cpts) 53 | numberofchangepoints <- -sort(-new_numcpts) ##can use sort to re-order 54 | penal <- sort(new_penalty) 55 | 56 | ls <- array() 57 | 58 | for (l in 1:length(new_cpts)){ 59 | ls[l] <- length(new_cpts[[l]]) 60 | } 61 | 62 | 63 | ls1 <- sort(ls,index.return = T, decreasing = T) 64 | ls1 <- ls1$ix 65 | 66 | 67 | segmentations <- new_cpts[c(ls1)] 68 | 69 | pen_interval <- NULL 70 | tmppen_interval <- NULL 71 | 72 | for (i in 1:(length(test_penalties)-1)){ 73 | if(abs(numberofchangepoints[i]-numberofchangepoints[i+1])>1){ ##only need to add a beta if difference in cpts>1 74 | j <- i+1 75 | tmppen_interval <- (penal[j] - penal[i]) * (((numberofchangepoints[i]) - (numberofchangepoints[j]))^-1) 76 | pen_interval <- c(pen_interval, tmppen_interval ) 77 | } 78 | } 79 | 80 | if(length(pen_interval)>0){ 81 | for(k in length(pen_interval):1){ 82 | index <- which.min(abs(pen_interval[k]-test_penalties)) 83 | if (isTRUE(all.equal(pen_interval[k], test_penalties[index]))){ 84 | pen_interval=pen_interval[-k] 85 | } 86 | } 87 | } 88 | } 89 | 90 | 91 | ##PRUNE VALUES WITH SAME num_cp 92 | for(j in length(test_penalties):2){ 93 | if(numberofchangepoints[j]==numberofchangepoints[j-1]){ 94 | numberofchangepoints=numberofchangepoints[-j] 95 | test_penalties=test_penalties[-j] 96 | penal=penal[-j] 97 | segmentations = segmentations[-j] 98 | } 99 | } 100 | 101 | 102 | 103 | ###calculate beta intervals 104 | nb=length(test_penalties) 105 | beta.int=rep(0,nb) 106 | beta.e=rep(0,nb) 107 | for(k in 1:nb){ 108 | if(k==1){ 109 | beta.int[1]=test_penalties[1] 110 | }else{ 111 | beta.int[k]=beta.e[k-1] 112 | } 113 | if(k==nb){ 114 | beta.e[k]=test_penalties[k] 115 | }else{ 116 | beta.e[k]=(penal[k]-penal[k+1])/(numberofchangepoints[k+1]-numberofchangepoints[k]) 117 | } 118 | 119 | } 120 | 121 | return(list(cpt.out = rbind(beta_interval = beta.int,numberofchangepoints,penalised_cost = penal),changepoints = segmentations)) 122 | #segmentations is output matrix 123 | #beta.int 124 | } -------------------------------------------------------------------------------- /R/penalty_decision.R: -------------------------------------------------------------------------------- 1 | penalty_decision = function(penalty, pen.value, n, diffparam, asymcheck, method){ 2 | 3 | 4 | if((penalty=="SIC0") || (penalty=="BIC0")){ 5 | pen.return=diffparam*log(n) 6 | } 7 | else if((penalty=="SIC") || (penalty=="BIC")){ 8 | pen.return=(diffparam+1)*log(n) 9 | } 10 | else if(penalty=="MBIC"){ 11 | pen.return=(diffparam+2)*log(n) 12 | } 13 | else if((penalty=="SIC1") || (penalty=="BIC1")){ 14 | stop("SIC1 and BIC1 have been depreciated, use SIC or BIC for the same result.") 15 | } 16 | else if(penalty=="AIC0"){ 17 | pen.return=2*diffparam 18 | } 19 | else if(penalty=="AIC"){ 20 | pen.return=2*(diffparam+1) 21 | } 22 | else if(penalty=="AIC1"){ 23 | stop("AIC1 has been depreciated, use AIC for the same result.") 24 | } 25 | else if(penalty=="Hannan-Quinn0"){ 26 | pen.return=2*diffparam*log(log(n)) 27 | } 28 | else if(penalty=="Hannan-Quinn"){ 29 | pen.return=2*(diffparam+1)*log(log(n)) 30 | } 31 | else if(penalty=="Hannan-Quinn1"){ 32 | stop("Hannan-Quinn1 has been depreciated, use Hannan-Quinn for the same result.") 33 | } 34 | else if(penalty=="None"){ 35 | pen.return=0 36 | } 37 | else if((penalty!="Manual")&&(penalty!="Asymptotic")){ 38 | stop('Unknown Penalty') 39 | } 40 | if((penalty=="Manual")&&(is.numeric(pen.value)==FALSE)){ 41 | pen.value=try(eval(parse(text=paste(pen.value))),silent=TRUE) 42 | if(inherits(pen.value,'try-error')){ 43 | stop('Your manual penalty cannot be evaluated') 44 | }else{ 45 | pen.return=pen.value 46 | } 47 | } 48 | 49 | if((penalty=="Manual")&&(is.numeric(pen.value)==TRUE)){ 50 | pen.return=pen.value 51 | } 52 | 53 | 54 | if(penalty=="Asymptotic"){ 55 | if((pen.value <= 0) || (pen.value > 1)){ 56 | stop('Asymptotic penalty values must be > 0 and <= 1') 57 | } 58 | if(method != "AMOC"){ 59 | warning('Asymptotic penalty value is not accurate for multiple changes, it should be treated the same as a manual penalty choice.') 60 | } 61 | if(asymcheck == "mean.norm"){ 62 | alpha=pen.value 63 | alogn=(2*log(log(n)))^(-(1/2)) 64 | blogn=(alogn^(-1))+(1/2)*alogn*log(log(log(n))) 65 | pen.return=(-alogn*log(log((1-alpha+exp(-2*(pi^(1/2))*exp(blogn/alogn)))^(-1/(2*(pi^(1/2))))))+blogn)^2 66 | }else if(asymcheck == "var.norm"){ 67 | alpha=pen.value 68 | alogn=sqrt(2*log(log(n))) 69 | blogn=2*log(log(n))+ (log(log(log(n))))/2 - log(gamma(1/2)) 70 | pen.return=(-(log(log((1-alpha+exp(-2*exp(blogn)))^(-1/2))))/alogn + blogn/alogn)^2 71 | }else if(asymcheck == "meanvar.norm"){ 72 | alpha=pen.value 73 | alogn=sqrt(2*log(log(n))) 74 | blogn=2*log(log(n))+ log(log(log(n))) 75 | pen.return=(-(log(log((1-alpha+exp(-2*exp(blogn)))^(-1/2))))/alogn + blogn/alogn)^2 76 | }else if(asymcheck == "reg.norm"){ 77 | alpha=pen.value 78 | top=-(log(log((1 - alpha + exp(-2*exp(2*(log(log(n)))+(diffparam/2)*(log(log(log(n))))- log(gamma(diffparam/2)))))^(-1/2)))) + 2*(log(log(n)))+(diffparam/2)*(log(log(log(n))))- log(gamma(diffparam/2)) 79 | bottom=(2*log(log(n)))^(1/2) 80 | pen.return=(top/bottom)^2 81 | }else if(asymcheck == "var.css"){ 82 | if(pen.value==0.01){pen.return=1.628} 83 | else if(pen.value==0.05){pen.return=1.358} 84 | else if(pen.value==0.1){pen.return=1.224} 85 | else if(pen.value==0.25){pen.return=1.019} 86 | else if(pen.value==0.5){pen.return=0.828} 87 | else if(pen.value==0.75){pen.return=0.677} 88 | else if(pen.value==0.9){pen.return=0.571} 89 | else if(pen.value==0.95){pen.return=0.520} 90 | else{stop('Only alpha values of 0.01,0.05,0.1,0.25,0.5,0.75,0.9,0.95 are valid for CSS')} 91 | }else if(asymcheck == "mean.cusum"){ 92 | stop('Asymptotic penalties have not been implemented yet for CUSUM') 93 | }else if(asymcheck == "meanvar.gamma"){ 94 | stop('Asymptotic penalties for the Gamma test statistic are not defined, please choose an alternative penalty type') 95 | }else if(asymcheck == "meanvar.exp"){ 96 | alpha=pen.value 97 | an=(2*log(log(n)))^(1/2) 98 | bn=2*log(log(n))+(1/2)*log(log(log(n)))-(1/2)*log(pi) 99 | pen.return=(-1/an)*log(-0.5*log(1-alpha))+bn 100 | if(alpha==1){pen.return=1.42417} # value of 1 gives log(0), this is alpha=0.99999999999999993 101 | }else if(asymcheck == "meanvar.poisson"){ 102 | stop('Asymptotic penalties for the Poisson test statistic are not available yet, please choose an alternative penalty type') 103 | } 104 | } 105 | #if(method=="AMOC"){ 106 | # pen.return=pen.value 107 | #} 108 | if(pen.return < 0){ 109 | stop('pen.value cannot be negative, please change your penalty value') 110 | }else{ 111 | return(pen.return) 112 | } 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /R/fit.R: -------------------------------------------------------------------------------- 1 | # Fitting functions for each model form 2 | fit.mean=function(object,cpts=NULL){ 3 | if(is.null(cpts)){cpts=c(0,object@cpts)} 4 | data=data.set(object) 5 | tmpmean=NULL 6 | for(j in 1:(length(cpts)-1)){ # quicker to use this than nseg for cpt.range, -1 as cpts includes 0 and n 7 | tmpmean[j]=mean(data[(cpts[j]+1):(cpts[j+1])]) 8 | } 9 | return(tmpmean) 10 | } 11 | fit.var=function(object,cpts=NULL){ 12 | if(is.null(cpts)){cpts=c(0,object@cpts)} 13 | data=data.set(object) 14 | seglen=diff(cpts) # not using seg.len as needs ncpts for cpt.range, might aswell use diff(cpts) as less flops 15 | tmpvar=NULL 16 | for(j in 1:length(seglen)){ 17 | tmpvar[j]=var(data[(cpts[j]+1):(cpts[j+1])]) 18 | } 19 | tmpvar=tmpvar*(seglen-1)/seglen # correctly for the fact that the MLE estimate is /n but the var function is /n-1 20 | return(tmpvar) 21 | } 22 | fit.scale=function(object,shape,cpts=NULL){ 23 | if(is.null(cpts)){cpts=c(0,object@cpts)} 24 | data=data.set(object) 25 | y=c(0,cumsum(data)) 26 | tmpscale=NULL 27 | for(j in 1:(length(cpts)-1)){ # quicker than nseg for cpt.range 28 | tmpscale[j]=(y[(cpts[j+1]+1)]-y[(cpts[j]+1)])/((cpts[j+1]-cpts[j])*shape) 29 | } 30 | return(tmpscale) 31 | } 32 | fit.trend=function(object,cpts=NULL){ 33 | if(is.null(cpts)){cpts=c(0,object@cpts)} 34 | seglen=diff(cpts) # not using seg.len as needs ncpts for cpt.range, might aswell use diff(cpts) as less flops 35 | data=data.set(object) 36 | n=length(data) 37 | sumstat=cbind(cumsum(c(0,data)),cumsum(c(0,data*c(1:n)))) 38 | cptsumstat=matrix(sumstat[object@cpts+1,]-sumstat[c(0,cpts(object))+1,],ncol=2) 39 | cptsumstat[,2]=cptsumstat[,2]-cptsumstat[,1]*c(0,cpts(object)) # i.e. creating newx3 40 | 41 | thetaS=(2*cptsumstat[,1]*(2*seglen + 1) - 6*cptsumstat[,2]) / (2*seglen*(2*seglen + 1) - 3*seglen*(seglen+1)) 42 | thetaT=(6*cptsumstat[,2])/((seglen+1)*(2*seglen+1)) + (thetaS * (1-((3*seglen)/((2*seglen)+1)))) 43 | return(cbind(thetaS,thetaT)) 44 | } 45 | fit.meanar=function(object,cpts=NULL){ 46 | if(is.null(cpts)){cpts=c(0,object@cpts)} 47 | seglen=diff(cpts) # not using seg.len as needs ncpts for cpt.range, might aswell use diff(cpts) as less flops 48 | data=data.set(object) 49 | n=length(data)-1 50 | sumstat=cbind(cumsum(c(0,data[-1])),cumsum(c(0,data[-(n+1)])),cumsum(c(0,data[-1]*data[-(n+1)])),cumsum(c(0,data[-1]^2)),cumsum(c(0,data[-(n+1)]^2))) 51 | cptsumstat=matrix(sumstat[cpts[-1],]-sumstat[cpts[-length(cpts)]+1,],ncol=5) 52 | beta2=(2*seglen*cptsumstat[,3]-cptsumstat[,1]*cptsumstat[,2])/(2*seglen*cptsumstat[,5]*(1-cptsumstat[,2]^2)); 53 | beta1=(2*cptsumstat[,1]-beta2*cptsumstat[,2])/(2*seglen); 54 | 55 | return(cbind(beta1,beta2)) 56 | } 57 | fit.trendar=function(object,cpts=NULL){ 58 | if(is.null(cpts)){cpts=c(0,object@cpts)} 59 | seglen=diff(cpts) # not using seg.len as needs ncpts for cpt.range, might aswell use diff(cpts) as less flops 60 | data=data.set(object) 61 | n=length(data)-1 62 | sumstat=cbind(cumsum(c(0,data[-1])),cumsum(c(0,data[-(n+1)])),cumsum(c(0,data[-1]*data[-(n+1)])),cumsum(c(0,data[-1]*c(1:n))),cumsum(c(0,data[-(n+1)]*c(0:(n-1)))),cumsum(c(0,data[-1]^2)),cumsum(c(0,data[-(n+1)]^2))) 63 | cptsumstat=matrix(sumstat[cpts[-1]+1,]-sumstat[cpts[-length(cpts)]+1,],ncol=7) 64 | cptsumstat[,4]=cptsumstat[,4]-cptsumstat[,1]*cpts[-length(cpts)] # i.e. creating newx4 65 | cptsumstat[,5]=cptsumstat[,5]-cptsumstat[,2]*cpts[-length(cpts)] # i.e. creating newx5 66 | betatop=seglen*(seglen-1)*(seglen*(seglen-1)*cptsumstat[,3] + 2*(2*seglen+1)*cptsumstat[,1]*(cptsumstat[,5]-seglen*cptsumstat[,2]) + 6*cptsumstat[,4]*(cptsumstat[,2]-cptsumstat[,5])) 67 | betabottom=seglen*(seglen-1)*cptsumstat[,7] + 2*(2*seglen+1)*cptsumstat[,2]*(seglen*cptsumstat[,2]-cptsumstat[,5]) + 6*cptsumstat[,5]*(cptsumstat[,5]-cptsumstat[,2]); 68 | beta=betatop/betabottom; 69 | thetajpo=(6*(seglen+2)*(cptsumstat[,4]-beta*cptsumstat[,5]))/((seglen+1)*(2*seglen+1)) - 2*(cptsumstat[,1]-beta*cptsumstat[,2]) 70 | thetaj=(2*(2*seglen+1)*(cptsumstat[,1]-beta*cptsumstat[,2])-6*(cptsumstat[,4]-beta*cptsumstat[,5]))/(seglen-1) 71 | 72 | return(cbind(beta,thetajpo,thetaj)) 73 | } 74 | fit.reg=function(object,cpts=NULL){ 75 | if(is.null(cpts)){cpts=c(0,object@cpts)} 76 | data=data.set(object) 77 | p=ncol(data)-1 78 | tmpbeta=matrix(NA,ncol=p,nrow=nseg(object)) 79 | tmpsigma=rep(NA,nseg(object)) 80 | for(j in 1:(length(cpts)-1)){ 81 | formula=paste('-1+data[',cpts[j]+1,':',cpts[j+1],',2]',sep='') 82 | if(p>1){ 83 | for(i in 2:p){ 84 | formula=paste(formula,'+data[',(cpts[j]+1),':',cpts[j+1],',',i+1,']',sep='') 85 | } 86 | } 87 | tmpfit=eval(parse(text=paste('lm(data[',(cpts[j]+1),':',cpts[j+1],',1]~',formula,')',sep=''))) 88 | tmpbeta[j,]=tmpfit$coefficients 89 | tmpsigma[j]=sum(tmpfit$residuals^2)/(length(tmpfit$residuals)-length(tmpfit$coefficients)) ##var(tmpfit$residuals) 90 | } 91 | return(list(beta=tmpbeta,sig2=tmpsigma)) 92 | } 93 | 94 | -------------------------------------------------------------------------------- /man/cpt.range-class.Rd: -------------------------------------------------------------------------------- 1 | \name{cpt.range-class} 2 | \Rdversion{1.1} 3 | \docType{class} 4 | \alias{cpt.range-class} 5 | 6 | \title{Class "cpt.range"} 7 | \description{ 8 | A class for changepoint objects that return more than 1 segmentation. Inherits from cpt class. 9 | } 10 | \section{Objects from the Class}{ 11 | Objects can be created by calls of the form \code{new("cpt.range", ...)}. 12 | \describe{ 13 | \item{\code{new("cpt.range", ...)}:}{creates a new object with class cpt.range } 14 | } 15 | } 16 | \section{Slots}{ 17 | \describe{ 18 | \item{\code{cpts.full}:}{Object of class \code{"matrix"}, each row of the matrix is a different segmentation of the data (different set of changepoints).} 19 | \item{\code{pen.value.full}:}{Object of class \code{"vector"}, each element is the penalty used to create the set of changepoints in the corresponding row of \code{cpts.full}.} 20 | The remaining slots are inherited from the \code{cpt} class. 21 | \item{\code{data.set}:}{Object of class \code{"ts"}, a coerced time series of the original data. Inherited from cpt class. } 22 | \item{\code{cpttype}:}{Object of class \code{"character"}, the type of changepoint that was identified. Inherited from cpt class. } 23 | \item{\code{method}:}{Object of class \code{"character"}, the method that was used to search for changepoints. Inherited from cpt class. } 24 | \item{\code{test.stat}:}{Object of class \code{"character"}, the test statistic for the analysis of the data. Inherited from cpt class. } 25 | \item{\code{pen.type}:}{Object of class \code{"character"}, the penalty type specified in the analysis. Inherited from cpt class.} 26 | \item{\code{pen.value}:}{Object of class \code{"numeric"}, the value of the penalty used in the analysis. Inherited from cpt class.} 27 | \item{\code{minseglen}:}{Object of class \code{"numeric"}, the minimum segment length (no. of observations between changepoints) used in the analysis. Inherited from cpt class.} 28 | \item{\code{cpts}:}{Object of class \code{"numeric"}, vector of optimal changepoints identified. Inherited from cpt class. } 29 | \item{\code{ncpts.max}:}{Object of class \code{"numeric"}, maximum number of changepoint that can be identified. Inherited from cpt class. } 30 | \item{\code{param.est}:}{Object of class \code{"list"}, list where each element is a vector of parameter estimates, if requested. Inherited from cpt class. } 31 | \item{\code{date}:}{Object of class \code{"character"}, date and time the changepoint analysis was run. Inherited from cpt class. } 32 | \item{\code{version}:}{Object of class \code{"character"}, version number of the package used when the analysis was run. Inherited from cpt class.} 33 | } 34 | } 35 | \section{Methods}{ 36 | \describe{ 37 | \item{cpts.full}{\code{signature(object = "cpt.range")}: retrieves cpts.full slot } 38 | \item{pen.value.full}{\code{signature(object = "cpt.range")}: retrieves pen.value.full slot } 39 | \item{cpts.full<-}{\code{signature(object = "cpt.range")}: replaces cpts.full slot } 40 | \item{param}{\code{signature(object="cpt.range",ncpts=NA)}: creates parameter estimates for the segmentation with \code{ncpts} number of changepoints. If ncpts=NA then the optimal set of changepoints according to the set penalty is used.} 41 | \item{pen.value.full<-}{\code{signature(object = "cpt.range")}: replaces pen.value.full slot } 42 | \item{plot}{\code{signature(object="cpt.range",ncpts=NA,diagnostic=FALSE)}: by default plots the optimal segmentation as for \code{class="cpt"}. If ncpts is specified then plots the segmentation for \code{ncpts} number of changepoints. If \code{diagnostic=TRUE} then produces a diagnostic plot to aide selection of the number of changes.} 43 | \item{print}{\code{signature(object = "cpt.range")}: prints details of the cpt.range object including summary} 44 | \item{summary}{\code{signature(object = "cpt.range")}: prints a summary of the cpt.range object } 45 | } 46 | } 47 | 48 | \author{ 49 | Rebecca Killick 50 | } 51 | 52 | \seealso{ 53 | \code{\link{cpts.full-methods}},\code{\linkS4class{cpt}},\code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 54 | } 55 | \examples{ 56 | showClass("cpt.range") # shows the structure of the cpt.range class 57 | 58 | x=new("cpt.range") # creates a new object with the cpt.range class defaults 59 | cpts(x) # retrieves the cpts slot from x 60 | cpts(x)<-c(10,50,100) # replaces the cpts slot from x with c(10,50,100) 61 | 62 | # Example of multiple changes in variance at 50,100,150 in simulated normal data 63 | set.seed(1) 64 | x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1)) 65 | out=cpt.var(x,pen.value=c(log(length(x)),10*log(length(x))),penalty="CROPS",method="PELT") 66 | print(out) # prints details of the analysis including a summary 67 | summary(out) 68 | plot(out,diagnostic=TRUE) # a diagnostic plot to identify number of changepoints 69 | # looks like the segmentation with 3 changepoints, 50,99,150 is the most appropriate 70 | plot(out,ncpts=3) # plots the segmentation for 3 changes 71 | logLik(out,ncpts=3) 72 | # raw likelihood of the data with changepoints, second value is likelihood + penalty 73 | } 74 | \keyword{classes} 75 | \keyword{cpt} 76 | \keyword{internal} -------------------------------------------------------------------------------- /man/cpt.reg-class.Rd: -------------------------------------------------------------------------------- 1 | \name{cpt.reg-class} 2 | \Rdversion{1.1} 3 | \docType{class} 4 | \alias{cpt.reg-class} 5 | \alias{cpt.reg-method,cpts} 6 | \alias{cpt.reg-method,cpttype} 7 | \alias{cpt.reg-method,data.set} 8 | \alias{cpt.reg-method,test.stat} 9 | \alias{cpt.reg-method,method} 10 | \alias{cpt.reg-method,ncpts.max} 11 | \alias{cpt.reg-method,param.est} 12 | \alias{cpt.reg-method,pen.type} 13 | \alias{cpt.reg-method,pen.value} 14 | \alias{cpt.reg-method,cpts<-} 15 | \alias{cpt.reg-method,cpttype<-} 16 | \alias{cpt.reg-method,data.set<-} 17 | \alias{cpt.reg-method,test.stat<-} 18 | \alias{cpt.reg-method,method<-} 19 | \alias{cpt.reg-method,ncpts.max<-} 20 | \alias{cpt.reg-method,param.est<-} 21 | \alias{cpt.reg-method,pen.type<-} 22 | \alias{cpt.reg-method,pen.value<-} 23 | \alias{cpt.reg-method,print} 24 | \alias{cpt.reg-method,summary} 25 | \alias{cpt.reg-method,param} 26 | 27 | \title{Class "cpt.reg"} 28 | \description{ 29 | A class for changepoint objects, specifically change in regression. 30 | } 31 | \section{Objects from the Class}{ 32 | Objects can be created by calls of the form \code{new("cpt", ...)}. 33 | \describe{ 34 | \item{\code{new("cpt", ...)}:}{creates a new object with class cpt } 35 | } 36 | } 37 | \section{Slots}{ 38 | \describe{ 39 | \item{\code{data.set}:}{Object of class \code{"numeric"}, the original vector of data } 40 | \item{\code{cpttype}:}{Object of class \code{"character"}, the type of changepoint that was identified } 41 | \item{\code{method}:}{Object of class \code{"character"}, the method that was used to search for changepoints, default change in regression } 42 | \item{\code{test.stat}:}{Object of class \code{"character"}, the test statistic used to analyse the data } 43 | \item{\code{pen.type}:}{Object of class \code{"character"}, the penalty type specified in the analysis} 44 | \item{\code{pen.value}:}{Object of class \code{"numeric"}, the value of the penalty used in the analysis} 45 | \item{\code{minseglen}:}{Object of class \code{"numeric"}, the minimum segment length (no. of observations between changepoints) used in the analysis.} 46 | \item{\code{cpts}:}{Object of class \code{"numeric"}, vector of changepoints identified } 47 | \item{\code{ncpts.max}:}{Object of class \code{"numeric"}, maximum number of changepoint that can be identified } 48 | \item{\code{param.est}:}{Object of class \code{"list"}, list where each element is a vector of parameter estimates, if requested } 49 | \item{\code{date}:}{Object of class \code{"character"}, date and time the changepoint analysis was run } 50 | \item{\code{version}:}{Object of class \code{"character"}, version number of the package used when the analysis was run.} 51 | } 52 | } 53 | \section{Methods}{ 54 | \describe{ 55 | \item{cpts}{\code{signature(object = "cpt.reg")}: retrieves cpts slot } 56 | \item{cpttype}{\code{signature(object = "cpt.reg")}: retrieves cpttype slot } 57 | \item{data.set}{\code{signature(object = "cpt.reg")}: retrieves data.set slot } 58 | \item{test.stat}{\code{signature(object = "cpt.reg")}: retrieves test.stat slot } 59 | \item{ncpts.max}{\code{signature(object = "cpt.reg")}: retrieves ncpts.max slot } 60 | \item{method}{\code{signature(object = "cpt.reg")}: retrieves method slot } 61 | \item{minseglen}{\code{signature(object = "cpt")}: retrieves minseglen slot } 62 | \item{param.est}{\code{signature(object = "cpt.reg")}: retrieves param.est slot } 63 | \item{pen.type}{\code{signature(object = "cpt.reg")}: retrieves pen.type slot } 64 | \item{pen.value}{\code{signature(object = "cpt.reg")}: retrieves pen.value slot } 65 | \item{cpts<-}{\code{signature(object = "cpt.reg")}: replaces cpts slot } 66 | \item{cpttype<-}{\code{signature(object = "cpt.reg")}: replaces cpttype slot } 67 | \item{data.set<-}{\code{signature(object = "cpt.reg")}: replaces data.set slot } 68 | \item{test.stat<-}{\code{signature(object = "cpt.reg")}: replaces test.stat slot } 69 | \item{ncpts.max<-}{\code{signature(object = "cpt.reg")}: replaces ncpts.max slot } 70 | \item{method<-}{\code{signature(object = "cpt.reg")}: replaces method slot } 71 | \item{param.est<-}{\code{signature(object = "cpt.reg")}: replaces param.est slot } 72 | \item{pen.type<-}{\code{signature(object = "cpt.reg")}: replaces pen.type slot } 73 | \item{pen.value<-}{\code{signature(object = "cpt.reg")}: replaces pen.value slot } 74 | \item{print}{\code{signature(object = "cpt.reg")}: prints details of the cpt object including summary} 75 | \item{summary}{\code{signature(object = "cpt.reg")}: prints a summary of the cpt object } 76 | \item{param}{\code{signature(object = "cpt.reg")}: calculates the parameter estimates for the cpt object} 77 | } 78 | } 79 | \author{ 80 | Rebecca Killick 81 | } 82 | 83 | \seealso{ 84 | \code{\link{plot-methods}},\code{\link{cpts-methods}},\code{\linkS4class{cpt}} 85 | } 86 | \examples{ 87 | showClass("cpt.reg") 88 | 89 | x=new("cpt.reg") # creates a new object with the cpt.reg class defaults 90 | data.set(x) # retrieves the data.set slot from x 91 | data.set(x)<-matrix(1:10,nrow=5,ncol=2) # replaces the data.set slot from x with a matrix 92 | 93 | } 94 | \keyword{classes} 95 | \keyword{cpt} 96 | \keyword{internal} -------------------------------------------------------------------------------- /man/decision.Rd: -------------------------------------------------------------------------------- 1 | \name{decision} 2 | \alias{decision} 3 | \title{ 4 | Decision Function - Only intended for developer use. 5 | } 6 | \description{ 7 | Uses the function parameters to decide if a proposed changepoint is a true changepoint or due to random variability. Test is conducted using the user specified penalty. 8 | 9 | This function is called by \code{cpt.mean}, \code{cpt.var} and \code{cpt.meanvar} when \code{method="AMOC"}. This is not intended for use by regular users of the package. It is exported for developers to call directly for speed increases or to fit alternative cost functions. 10 | 11 | WARNING: No checks on arguments are performed! 12 | } 13 | \usage{ 14 | decision(tau,null,alt=NA,penalty="MBIC",n=0,diffparam=1,pen.value=0) 15 | } 16 | \arguments{ 17 | \item{tau}{ 18 | A numeric value or vector specifying the proposed changepoint location(s). 19 | } 20 | \item{null}{ 21 | The value of the null test statistic. If tau is a vector, so is null. If the test statistic is already known (i.e. doesn't have null and alternative components), replace the null argument with the test statistic. 22 | } 23 | \item{alt}{ 24 | The value of the alternative test statistic (at tau). If tau is a vector, so is alt. If the test statistic is already known, then it is used in replacement of the null argument and the alternative should not be specified (default NA to account for this) 25 | } 26 | \item{penalty}{ 27 | Choice of "None", "SIC", "BIC", "MBIC", AIC", "Hannan-Quinn", "Asymptotic" and "Manual" penalties. If Manual is specified, the manual penalty is contained in the pen.value parameter. If Asymptotic is specified, the theoretical type I error is contained in the pen.value parameter. The predefined penalties listed DO count the changepoint as a parameter, postfix a 0 e.g."SIC0" to NOT count the changepoint as a parameter. 28 | } 29 | \item{n}{ 30 | The length of the original data, required to give sensible "no changepoint" output. 31 | } 32 | \item{diffparam}{ 33 | The difference in the number of parameters in the null and alternative hypotheses, required for the SIC, BIC, AIC, Hanna-Quinn and possibly Manual penalties. 34 | } 35 | \item{pen.value}{ 36 | The theoretical type I error e.g.0.05 when using the Asymptotic penalty. The value of the penalty when using the Manual penalty option - this can be a numeric value or text giving the formula to use. Available variables are, n=length of original data, null=null likelihood, alt=alternative likelihood, tau=proposed changepoint, diffparam=difference in number of alternatve and null parameters. 37 | } 38 | } 39 | \details{ 40 | This function is used to test whether tau is a true changepoint or not. This test uses the null-alternative as the test statistic and performs the test where the null hypothesis is no change point and the alternative hypothesis is a single changepoint at tau. The test is (null-alt)>=penalty, if TRUE then the changepoint is deemed a true changepoint, if FALSE then n (length of data) is returned. 41 | 42 | If the test statistic is already known then it replaces the null value and the alternative is not required (default NA). In this case the test is null>=penalty, if TRUE then the changepoint is deemed a true changepoint, if FALSE then n (length of data) is returned. 43 | 44 | This function is exported for developer use only. It does not perform any checks on inputs and is included for convenience and speed for those who are developing their own cost functions. 45 | } 46 | \value{ 47 | A list is returned with two elements, cpt and pen. 48 | \item{cpt}{ If tau is a single value then a single value is returned: Either the value of the true changepoint location or n (length of data) if no changepoint is found. 49 | 50 | If tau is a vector of length m then a vector of length m is returned:Each element is either the value of the true changepoint location or n (length of data) if no changepoint is found. The first element is for the first value of tau and the final element is for the final value of tau. 51 | } 52 | \item{pen}{The numeric value of the penalty used for the test(s).} 53 | } 54 | \references{ 55 | SIC/BIC: Schwarz, G. (1978) Estimating the Dimension of a Model, \emph{The Annals of Statistics} \bold{6(2)}, 461--464 56 | 57 | MBIC: Zhang, N. R. and Siegmund, D. O. (2007) A Modified Bayes Information Criterion with Applications to the Analysis of Comparative Genomic Hybridization Data. \emph{Biometrics} \bold{63}, 22-32. 58 | 59 | AIC: Akaike, H. (1974) A new look at the statistical model identification, \emph{Automatic Control, IEEE Transactions on} \bold{19(6)}, 716--723 60 | 61 | Hannan-Quinn: Hannan, E. J. and B. G. Quinn (1979) The Determination of the Order of an Autoregression, \emph{Journal of the Royal Statistical Society, B} \bold{41}, 190--195 62 | } 63 | \author{ 64 | Rebecca Killick 65 | } 66 | 67 | 68 | \seealso{ 69 | \code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 70 | } 71 | \examples{ 72 | # Example of finding a change 73 | out=c(100,765.1905,435.6529) # tau, null, alt 74 | decision(out[1],out[2],out[3],penalty="SIC",n=200,diffparam=1) # returns 100 as a true changepoint 75 | 76 | # Example of no change found 77 | out=c(53,-22.47768,-24.39894) # tau, null, alt 78 | decision(out[1],out[2],out[3],penalty="Manual",n=200,diffparam=1,pen.value="2*log(n)") 79 | } 80 | 81 | \keyword{methods} 82 | \keyword{univar} 83 | \keyword{models} 84 | \keyword{ts} 85 | -------------------------------------------------------------------------------- /man/cpt.reg.Rd: -------------------------------------------------------------------------------- 1 | \name{cpt.reg} 2 | \alias{cpt.reg} 3 | \title{ 4 | Identifying Changes in Regression 5 | } 6 | \description{ 7 | Calculates the optimal position and (potentially) number of changepoints in regression structure for data using the user specified method. 8 | } 9 | \usage{ 10 | cpt.reg(data, penalty="MBIC", pen.value=0, method="PELT", test.stat="Normal", 11 | class=TRUE, param.estimates=TRUE, shape=0, minseglen=3, tol=1e-07) 12 | } 13 | \arguments{ 14 | \item{data}{ 15 | A matrix/array or ts object containing the data to fit the models to. 16 | Col1: the dependent variable, Col2+: regressors. A check is performed 17 | validate (or include if not) that an intercept regressor is included. 18 | } 19 | \item{penalty}{ 20 | Choice of penalty, see \code{\link[changepoint]{penalty_decision}}. 21 | } 22 | \item{pen.value}{ 23 | Additional values to be used in evaluating the penalty. 24 | } 25 | \item{method}{ 26 | Choice changepoint algorithm. Either "AMOC" (at least one changepoint) or 27 | "PELT" (pruned exact linear time) method. Default is "PELT". 28 | } 29 | \item{test.stat}{ 30 | Test statistic used for regression fit. Currently only "Normal" is supported which assumes a Normal distribution for the errors and fits using Residual Sum of Squares. 31 | } 32 | \item{class}{ 33 | Logical. If \code{TRUE} then an oblect of class 'cpt.reg' is returned. 34 | } 35 | \item{param.estimates}{ 36 | Logical. If \code{TRUE} and \code{class=TRUE} then parameter estimates are 37 | returned. 38 | } 39 | \item{shape}{ 40 | Additional parameters used in the cost function. If \code{dist="Normal"}, then 41 | \code{shape} is a single numeric variable that define the cost function to be: 42 | 43 | * \code{shape} < 0 : the residual sum of squares (i.e. quadratic cost). 44 | 45 | * \code{shape} = 0 : -2 * logLik (i.e. -2 * maximum likelihood value). Default. 46 | 47 | * \code{shape} > 0 : -2 * maximum likelihood value with variance=\code{shape}. 48 | } 49 | \item{minseglen}{ 50 | Positive integer giving the minimum segment length (no. of observations 51 | between changes). Default is set at 3, however checks (and adjustements 52 | where applicable) are performed to ensure this is not smaller than the 53 | number of regressors. 54 | } 55 | \item{tol}{ 56 | Tolerance for the 'qr' decomposition. Default is 1e-7. 57 | See \code{\link[stats]{lm.fit}} 58 | } 59 | } 60 | \details{ 61 | This function is used to find change in linear regression structure for 62 | \code{data}. The changes are found using the method supplied wihich can be 63 | single changepoint (AMOC) or multiple changepoints (PELT). A changepoint is 64 | denoted as the last observation of the segment / regime. 65 | } 66 | \value{ 67 | If \code{class=TRUE} then an object of S4 class \code{"cpt.reg"} is returned. 68 | The slot \code{cpts} contains the changepoints that are returned. For 69 | \code{class=FALSE} the changepoint positions are returned, along with 70 | supplementary information about the fit detailed below. (This info is mainly 71 | used for bug fixing.) 72 | 73 | If \code{data} is a matrix, then a vector/list/cpt.reg is returned depending 74 | on the of \code{method}. If \code{data} is a 3D array (multiple data sets, 75 | with total number of data sets = dim1 and each data set of the same size) 76 | then a list is returned where each element is either a vector/list/cpt.reg 77 | corresponding to the fit on each data set in the order they appear in the array. 78 | 79 | If \code{method="AMOC"} & \code{dist="Normal"} then a list is returned with: 80 | 81 | cpts: changepoint position. 82 | 83 | pen.value: penalty value. 84 | 85 | If \code{method="PELT"} & \code{dist="Normal"} then a list is returned with: 86 | 87 | cpts: changepoint positions. 88 | 89 | lastchangecpts: index of last changepoint according to optimal sequential fit. 90 | 91 | lastchangelike: cost at last changepont according to optimal sequential fit. 92 | 93 | ncpts: number of changepoints according to optimal squential fit. 94 | 95 | } 96 | \references{ 97 | PELT Algorithm: Killick R, Fearnhead P, Eckley IA (2012) Optimal detection of changepoints with a linear computational cost, \emph{JASA} \bold{107(500)}, 1590--1598 98 | 99 | MBIC: Zhang, N. R. and Siegmund, D. O. (2007) A Modified Bayes Information Criterion with Applications to the Analysis of Comparative Genomic Hybridization Data. \emph{Biometrics} \bold{63}, 22-32. 100 | } 101 | \author{ 102 | Rebecca Killick, Simon Taylor 103 | } 104 | 105 | 106 | \seealso{ 107 | \code{\link[changepoint]{cpt.mean}}, 108 | \code{\link[changepoint]{penalty_decision}}, 109 | \code{\link[changepoint]{plot-methods}}, 110 | \code{\linkS4class{cpt}}, 111 | \code{\link[stats]{lm.fit}} 112 | } 113 | \examples{ 114 | ## Trend change 115 | set.seed(1) 116 | x <- 1:200 117 | beta0 <- rep(c(0,100,50,0),each=50) 118 | beta1 <- rep(c(1,-1,0,0.25),each=50) 119 | y <- beta0 + beta1*x + rnorm(200) 120 | data <- cbind(y,1,x) 121 | 122 | out <- cpt.reg(data, method="PELT", minseglen=5, penalty="MBIC", test.stat="Normal") 123 | cpts(out) ##changepoints 124 | param.est(out) ##parameter estimates (rows: beta estimates per segment) 125 | plot(out) ##plot of fit 126 | 127 | 128 | 129 | ## Seasonal change, period 12 130 | n=100 131 | indicator=rep(1,n) 132 | trend=1:n 133 | seasonal=cos(2*pi*(1:n -6)/12) # yearly, peak in summer 134 | cpt.s = c(rep(0,floor(n/4)), rep(2, floor(n/4)), rep(1, floor(n/4)),rep(0,n-3*floor(n/4))) 135 | ##3 Alternating Cpts 136 | y=0.1*cpt.s*1:n+cos(2*pi*(1:n -6)/12)+rnorm(n) 137 | data=cbind(y,indicator,trend,seasonal) 138 | out=cpt.reg(data, minseglen=12) 139 | plot(out,cpt.width=3) 140 | cpts(out) 141 | param.est(out) ## column order of estimates matches the column order of inputs 142 | } 143 | 144 | \keyword{changepoint} 145 | \keyword{regression} 146 | \keyword{models} 147 | \keyword{ts} 148 | -------------------------------------------------------------------------------- /man/cpt-class.Rd: -------------------------------------------------------------------------------- 1 | \name{cpt-class} 2 | \Rdversion{1.1} 3 | \docType{class} 4 | \alias{cpt-class} 5 | \alias{cpt-method,cpts} 6 | \alias{cpt-method,cpttype} 7 | \alias{cpt-method,data.set} 8 | \alias{cpt-method,data.set.ts} 9 | \alias{cpt-method,test.stat} 10 | \alias{cpt-method,ncpts.max} 11 | \alias{cpt-method,method} 12 | \alias{cpt-method,minseglen} 13 | \alias{cpt-method,param.est} 14 | \alias{cpt-method,pen.type} 15 | \alias{cpt-method,pen.value} 16 | \alias{cpt-method,cpts<-} 17 | \alias{cpt-method,cpttype<-} 18 | \alias{cpt-method,data.set<-} 19 | \alias{cpt-method,test.stat<-} 20 | \alias{cpt-method,ncpts.max<-} 21 | \alias{cpt-method,method<-} 22 | \alias{cpt-method,minseglen<-} 23 | \alias{cpt-method,param.est<-} 24 | \alias{cpt-method,pen.type<-} 25 | \alias{cpt-method,pen.value<-} 26 | \alias{cpt-method,print} 27 | \alias{cpt-method,summary} 28 | \alias{cpt-method,plot} 29 | \alias{cpt-method,param} 30 | \alias{cpt-method,logLik} 31 | 32 | \title{Class "cpt"} 33 | \description{ 34 | A class for changepoint objects. 35 | } 36 | \section{Objects from the Class}{ 37 | Objects can be created by calls of the form \code{new("cpt", ...)}. 38 | \describe{ 39 | \item{\code{new("cpt", ...)}:}{creates a new object with class cpt } 40 | } 41 | } 42 | \section{Slots}{ 43 | \describe{ 44 | \item{\code{data.set}:}{Object of class \code{"ts"}, a coerced time series of the original data. } 45 | \item{\code{cpttype}:}{Object of class \code{"character"}, the type of changepoint that was identified. } 46 | \item{\code{method}:}{Object of class \code{"character"}, the method that was used to search for changepoints. } 47 | \item{\code{test.stat}:}{Object of class \code{"character"}, the test statistic for the analysis of the data. } 48 | \item{\code{pen.type}:}{Object of class \code{"character"}, the penalty type specified in the analysis.} 49 | \item{\code{pen.value}:}{Object of class \code{"numeric"}, the value of the penalty used in the analysis.} 50 | \item{\code{minseglen}:}{Object of class \code{"numeric"}, the minimum segment length (no. of observations between changepoints) used in the analysis.} 51 | \item{\code{cpts}:}{Object of class \code{"numeric"}, vector of changepoints identified. } 52 | \item{\code{ncpts.max}:}{Object of class \code{"numeric"}, maximum number of changepoint that can be identified. } 53 | \item{\code{param.est}:}{Object of class \code{"list"}, list where each element is a vector of parameter estimates, if requested. } 54 | \item{\code{date}:}{Object of class \code{"character"}, date and time the changepoint analysis was run. } 55 | \item{\code{version}:}{Object of class \code{"character"}, version number of the package used when the analysis was run.} 56 | 57 | } 58 | } 59 | \section{Methods}{ 60 | \describe{ 61 | \item{cpts}{\code{signature(object = "cpt")}: retrieves cpts slot } 62 | \item{cpttype}{\code{signature(object = "cpt")}: retrieves cpttype slot } 63 | \item{data.set}{\code{signature(object = "cpt")}: retrieves vector version of data.set slot } 64 | \item{data.set.ts}{\code{signature(object = "cpt")}: retrieves time series version of data.set slot } 65 | \item{test.stat}{\code{signature(object = "cpt")}: retrieves test.stat slot } 66 | \item{ncpts.max}{\code{signature(object = "cpt")}: retrieves ncpts.max slot } 67 | \item{method}{\code{signature(object = "cpt")}: retrieves method slot } 68 | \item{minseglen}{\code{signature(object = "cpt")}: retrieves minseglen slot } 69 | \item{param.est}{\code{signature(object = "cpt")}: retrieves param.est slot } 70 | \item{pen.type}{\code{signature(object = "cpt")}: retrieves pen.type slot } 71 | \item{pen.value}{\code{signature(object = "cpt")}: retrieves pen.value slot } 72 | \item{cpts<-}{\code{signature(object = "cpt")}: replaces cpts slot } 73 | \item{cpttype<-}{\code{signature(object = "cpt")}: replaces cpttype slot } 74 | \item{data.set<-}{\code{signature(object = "cpt")}: replaces data.set slot } 75 | \item{test.stat<-}{\code{signature(object = "cpt")}: replaces test.stat slot } 76 | \item{ncpts.max<-}{\code{signature(object = "cpt")}: replaces ncpts.max slot } 77 | \item{method<-}{\code{signature(object = "cpt")}: replaces method slot } 78 | \item{minseglen<-}{\code{signature(object = "cpt")}: replaces minseglen slot } 79 | \item{param.est<-}{\code{signature(object = "cpt")}: replaces param.est slot } 80 | \item{pen.type<-}{\code{signature(object = "cpt")}: replaces pen.type slot } 81 | \item{pen.value<-}{\code{signature(object = "cpt")}: replaces pen.value slot } 82 | \item{print}{\code{signature(object = "cpt")}: prints details of the cpt object including summary} 83 | \item{summary}{\code{signature(object = "cpt")}: prints a summary of the cpt object } 84 | \item{plot}{\code{signature(object = "cpt")}: plots the cpt object with changepoints highlighted} 85 | \item{param}{\code{signature(object = "cpt")}: calculates the parameter estimates for the cpt object} 86 | \item{logLik}{\code{signature(object = "cpt")}: returns the overall log-likelihood of the cpt object} 87 | } 88 | } 89 | 90 | \author{ 91 | Rebecca Killick 92 | } 93 | 94 | \seealso{ 95 | \code{\link{data.set-methods}},\code{\link{cpts-methods}},\code{\linkS4class{cpt.reg}},\code{\link{cpt.mean}},\code{\link{cpt.var}},\code{\link{cpt.meanvar}} 96 | } 97 | \examples{ 98 | showClass("cpt") # shows the structure of the cpt class 99 | 100 | x=new("cpt") # creates a new object with the cpt class defaults 101 | cpts(x) # retrieves the cpts slot from x 102 | cpts(x)<-c(10,50,100) # replaces the cpts slot from x with c(10,50,100) 103 | 104 | # Example of a change in variance at 100 in simulated normal data 105 | set.seed(1) 106 | x=c(rnorm(100,0,1),rnorm(100,0,10)) 107 | ans=cpt.var(x) 108 | print(ans) # prints details of the analysis including a summary 109 | summary(ans) 110 | plot(ans) # plots the data with change (vertical line) at 100 111 | logLik(ans) # raw likelihood of the data with changepoints, second value is likelihood + penalty 112 | } 113 | \keyword{classes} 114 | \keyword{cpt} 115 | \keyword{internal} -------------------------------------------------------------------------------- /R/single.nonparametric.R: -------------------------------------------------------------------------------- 1 | single.var.css.calc <- 2 | function(data,extrainf=TRUE, minseglen){ 3 | singledim=function(data,extrainf=TRUE,minseglen){ 4 | n=length(data) 5 | y2=c(0,cumsum(data^2)) 6 | taustar=minseglen:(n-minseglen+1) 7 | tmp=(y2[taustar+1]/y2[n+1])-taustar/n 8 | 9 | D=max(abs(tmp),na.rm=T) 10 | tau=which.max(abs(tmp)) 11 | if(extrainf==TRUE){ 12 | out=c(tau,sqrt(n/2)*D) 13 | names(out)=c('cpt','test statistic') 14 | return(out) 15 | } 16 | else{ 17 | return(tau) 18 | } 19 | } 20 | 21 | 22 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 23 | # single data set 24 | cpt=singledim(data,extrainf,minseglen) 25 | return(cpt) 26 | } 27 | else{ 28 | rep=nrow(data) 29 | n=ncol(data) 30 | cpt=NULL 31 | if(extrainf==FALSE){ 32 | for(i in 1:rep){ 33 | cpt[i]=singledim(data[i,],extrainf,minseglen) 34 | } 35 | } 36 | else{ 37 | cpt=matrix(0,ncol=2,nrow=rep) 38 | for(i in 1:rep){ 39 | cpt[i,]=singledim(data[i,],extrainf,minseglen) 40 | } 41 | colnames(cpt)=c('cpt','test statistic') 42 | } 43 | return(cpt) 44 | } 45 | } 46 | 47 | 48 | single.var.css<-function(data,penalty="MBIC",pen.value=0,class=TRUE,param.estimates=TRUE,minseglen){ 49 | if(length(pen.value)>1){stop('Only one dimensional penalties can be used for CSS')} 50 | if(penalty=="MBIC"){stop("MBIC penalty is not valid for nonparametric test statistics.")} 51 | diffparam=1 52 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 53 | # single dataset 54 | n=length(data) 55 | } 56 | else{ 57 | n=ncol(data) 58 | } 59 | if(n<4){stop('Data must have atleast 4 observations to fit a changepoint model.')} 60 | if(n<(2*minseglen)){stop('Minimum segment legnth is too large to include a change in this data')} 61 | 62 | pen.value = penalty_decision(penalty, pen.value, n, diffparam, asymcheck = "var.css", method="AMOC") 63 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 64 | tmp=single.var.css.calc(coredata(data),extrainf=TRUE,minseglen) 65 | ans=decision(tau=tmp[1],null=tmp[2],penalty="Manual",n=n,diffparam=1,pen.value=pen.value) 66 | if(class==TRUE){ 67 | out=new("cpt") 68 | data.set(out)=data; cpttype(out)="variance"; method(out)="AMOC"; test.stat(out)="CSS"; pen.type(out)=penalty; pen.value(out)=ans$pen;ncpts.max(out)=1 69 | if(ans$cpt != n){cpts(out)=c(ans$cpt,n)} 70 | else{cpts(out)=ans$cpt} 71 | if(param.estimates==TRUE){ 72 | out=param(out) 73 | } 74 | return(out) 75 | } 76 | else{ return(ans$cpt)} 77 | } 78 | else{ 79 | tmp=single.var.css.calc(data,extrainf=TRUE,minseglen) 80 | ans=decision(tau=tmp[,1],null=tmp[,2],penalty="Manual",n=n,diffparam=1,pen.value=pen.value) 81 | if(class==TRUE){ 82 | rep=nrow(data) 83 | out=list() 84 | for(i in 1:rep){ 85 | out[[i]]=new("cpt") 86 | data.set(out[[i]])=ts(data[i,]); cpttype(out[[i]])="variance"; method(out[[i]])="AMOC"; test.stat(out[[i]])="CSS"; pen.type(out[[i]])=penalty;pen.value(out[[i]])=ans$pen;ncpts.max(out[[i]])=1 87 | if(ans$cpt[i] != n){cpts(out[[i]])=c(ans$cpt[i],n)} 88 | else{cpts(out[[i]])=ans$cpt[i]} 89 | if(param.estimates==TRUE){ 90 | out[[i]]=param(out[[i]]) 91 | } 92 | } 93 | return(out) 94 | } 95 | else{ return(ans$cpt)} 96 | } 97 | } 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | single.mean.cusum.calc <- 109 | function(data,extrainf=TRUE,minseglen){ 110 | singledim=function(data,extrainf=TRUE,minseglen){ 111 | n=length(data) 112 | ybar=mean(data) 113 | y=c(0,cumsum(data-ybar)) 114 | y=y/n 115 | 116 | M=max(abs(y[minseglen:(n-minseglen+1)]),na.rm=T) 117 | tau=which.max(abs(y[minseglen:(n-minseglen+1)]))+minseglen-1 118 | if(extrainf==TRUE){ 119 | out=c(tau,M) 120 | names(out)=c('cpt','test statistic') 121 | return(out) 122 | } 123 | else{ 124 | return(tau) 125 | } 126 | } 127 | 128 | 129 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 130 | # single data set 131 | cpt=singledim(data,extrainf,minseglen) 132 | return(cpt) 133 | } 134 | else{ 135 | rep=nrow(data) 136 | n=ncol(data) 137 | cpt=NULL 138 | if(extrainf==FALSE){ 139 | for(i in 1:rep){ 140 | cpt[i]=singledim(data[i,],extrainf,minseglen) 141 | } 142 | } 143 | else{ 144 | cpt=matrix(0,ncol=2,nrow=rep) 145 | for(i in 1:rep){ 146 | cpt[i,]=singledim(data[i,],extrainf,minseglen) 147 | } 148 | colnames(cpt)=c('cpt','test statistic') 149 | } 150 | return(cpt) 151 | } 152 | } 153 | 154 | 155 | single.mean.cusum<-function(data,penalty="Asymptotic",pen.value=0.05,class=TRUE,param.estimates=TRUE,minseglen){ 156 | if(length(pen.value)>1){stop('Only one dimensional penalties can be used for CUSUM')} 157 | if(penalty=="MBIC"){stop("MBIC penalty is not valid for nonparametric test statistics.")} 158 | 159 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 160 | # single dataset 161 | n=length(data) 162 | } 163 | else{ 164 | n=ncol(data) 165 | } 166 | if(n<2){stop('Data must have atleast 2 observations to fit a changepoint model.')} 167 | if(n<(2*minseglen)){stop('Minimum segment legnth is too large to include a change in this data')} 168 | 169 | pen.value = penalty_decision(penalty, pen.value, n, diffparam=1, asymcheck="mean.cusum", method="AMOC") 170 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 171 | tmp=single.mean.cusum.calc(coredata(data),extrainf=TRUE,minseglen) 172 | ans=decision(tau=tmp[1],null=tmp[2],penalty=penalty,n=n,diffparam=1,pen.value=pen.value) 173 | if(class==TRUE){ 174 | out=new("cpt") 175 | data.set(out)=data; cpttype(out)="mean"; method(out)="AMOC"; test.stat(out)="CUSUM"; pen.type(out)=penalty; pen.value(out)=ans$pen;ncpts.max(out)=1 176 | if(ans$cpt != n){cpts(out)=c(ans$cpt,n)} 177 | else{cpts(out)=ans$cpt} 178 | if(param.estimates==TRUE){ 179 | out=param(out) 180 | } 181 | return(out) 182 | } 183 | else{ return(ans$cpt)} 184 | } 185 | else{ 186 | tmp=single.mean.cusum.calc(data,extrainf=TRUE,minseglen) 187 | ans=decision(tau=tmp[,1],null=tmp[,2],penalty=penalty,n=n,diffparam=1,pen.value=pen.value) 188 | if(class==TRUE){ 189 | rep=nrow(data) 190 | out=list() 191 | for(i in 1:rep){ 192 | out[[i]]=new("cpt") 193 | data.set(out[[i]])=ts(data[i,]); cpttype(out[[i]])="mean"; method(out[[i]])="AMOC"; test.stat(out[[i]])="CUSUM"; pen.type(out[[i]])=penalty;pen.value(out[[i]])=ans$pen;ncpts.max(out[[i]])=1 194 | if(ans$cpt[i] != n){cpts(out[[i]])=c(ans$cpt[i],n)} 195 | else{cpts(out[[i]])=ans$cpt[i]} 196 | if(param.estimates==TRUE){ 197 | out[[i]]=param(out[[i]]) 198 | } 199 | } 200 | return(out) 201 | } 202 | else{ return(ans$cpt)} 203 | } 204 | } 205 | 206 | -------------------------------------------------------------------------------- /src/PELT_one_func_minseglen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // RK addition 4 | #include // RK addition 5 | #include // RK addition 6 | #include // RK addition 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | //#include "cost_general_functions.c" 13 | #define SWAP(a,b) { int t; t=a; a=b; b=t; } // Macro for swapping 14 | 15 | //static int *lastchangecpts; 16 | //static double *lastchangelike; 17 | static int *checklist; 18 | static double *tmplike; 19 | static int *tmpt; 20 | 21 | void FreePELT(int* error) 22 | { 23 | if(*error==0){ 24 | // free((void *)lastchangecpts); 25 | // free((void *)lastchangelike); 26 | free((void *)checklist); 27 | free((void *)tmplike); 28 | free((void *)tmpt); 29 | } 30 | } 31 | 32 | void PELTC(char** cost_func, 33 | double* sumstat, // Summary statistic for the time serie 34 | int* n, // Length of the time series 35 | double* pen, // Penalty used to decide if a changepoint is significant 36 | int* cptsout, // Vector of identified changepoint locations 37 | int* error, // 0 by default, nonzero indicates error in code 38 | double* shape, // only used when cost_func is the gamma likelihood 39 | int* minseglen, //minimum segment length 40 | double* lastchangelike, // stores likelihood up to that time using 41 | //optimal changepoint locations up to that time 42 | int* lastchangecpts, // stores last changepoint locations 43 | int* numchangecpts //stores the current number of changepoints 44 | ) 45 | { 46 | // R code does know.mean and fills mu if necessary 47 | 48 | double (*costfunction)(double, double, double, int, double); 49 | double mll_var(double, double, double, int, double); 50 | double mll_mean(double, double, double, int, double); 51 | double mll_meanvar(double, double, double, int, double); 52 | double mll_meanvar_exp(double, double, double, int, double); 53 | double mll_meanvar_gamma(double, double, double, int, double); 54 | double mll_meanvar_poisson(double, double, double, int, double); 55 | double mbic_var(double, double, double, int, double); 56 | double mbic_mean(double, double, double, int, double); 57 | double mbic_meanvar(double, double, double, int, double); 58 | double mbic_meanvar_exp(double, double, double, int, double); 59 | double mbic_meanvar_gamma(double, double, double, int, double); 60 | double mbic_meanvar_poisson(double, double, double, int, double); 61 | 62 | if (strcmp(*cost_func,"var.norm")==0){ 63 | costfunction = &mll_var; 64 | } 65 | else if (strcmp(*cost_func,"mean.norm")==0){ 66 | costfunction = &mll_mean; 67 | } 68 | else if (strcmp(*cost_func,"meanvar.norm")==0){ 69 | costfunction = &mll_meanvar; 70 | } 71 | else if (strcmp(*cost_func,"meanvar.exp")==0){ 72 | costfunction = &mll_meanvar_exp; 73 | } 74 | else if (strcmp(*cost_func,"meanvar.gamma")==0){ 75 | costfunction = &mll_meanvar_gamma; 76 | } 77 | else if (strcmp(*cost_func,"meanvar.poisson")==0){ 78 | costfunction = &mll_meanvar_poisson; 79 | } 80 | else if (strcmp(*cost_func,"mean.norm.mbic")==0){ 81 | costfunction = &mbic_mean; 82 | } 83 | else if (strcmp(*cost_func,"var.norm.mbic")==0){ 84 | costfunction = &mbic_var; 85 | } 86 | else if (strcmp(*cost_func,"meanvar.norm.mbic")==0){ 87 | costfunction = &mbic_meanvar; 88 | } 89 | else if (strcmp(*cost_func,"meanvar.exp.mbic")==0){ 90 | costfunction = &mbic_meanvar_exp; 91 | } 92 | else if (strcmp(*cost_func,"meanvar.gamma.mbic")==0){ 93 | costfunction = &mbic_meanvar_gamma; 94 | } 95 | else if (strcmp(*cost_func,"meanvar.poisson.mbic")==0){ 96 | costfunction = &mbic_meanvar_poisson; 97 | } 98 | 99 | 100 | // int *lastchangecpts; 101 | // lastchangecpts = (int *)calloc(*n+1,sizeof(int)); 102 | // if (lastchangecpts==NULL) { 103 | // *error = 1; 104 | // goto err1; 105 | // } 106 | 107 | //double lastchangelike[*n]; /* stores likelihood up to that time using optimal changepoint locations up to that time */ 108 | // double *lastchangelike; 109 | // lastchangelike = (double *)calloc(*n+1,sizeof(double)); 110 | // if (lastchangelike==NULL) { 111 | // *error = 2; 112 | // goto err2; 113 | // } 114 | 115 | //int checklist[*n]; 116 | int *checklist; 117 | checklist = (int *)calloc(*n+1,sizeof(int)); 118 | if (checklist==NULL) { 119 | *error = 1; 120 | goto err1; 121 | } 122 | 123 | 124 | 125 | int nchecklist; 126 | double minout; 127 | 128 | //double tmplike[*n]; 129 | double *tmplike; 130 | tmplike = (double *)calloc(*n+1,sizeof(double)); 131 | if (tmplike==NULL) { 132 | *error = 2; 133 | goto err2; 134 | } 135 | 136 | 137 | //int tmpt[*n]; 138 | int *tmpt; 139 | tmpt = (int *)calloc(*n+1,sizeof(int)); 140 | if (tmpt==NULL) { 141 | *error = 3; 142 | goto err3; 143 | } 144 | 145 | int tstar,i,whichout,nchecktmp; 146 | 147 | 148 | void min_which(double*, int, double*, int*); 149 | 150 | lastchangelike[0]= -*pen; 151 | lastchangecpts[0]=0; 152 | numchangecpts[0]=0; 153 | // lastchangelike[1]=costfunction(*(sumstat+1),*(sumstat + *n + 1 + 1),*(sumstat + *n + *n + 2 + 1),1, *shape); 154 | // lastchangecpts[1]=0; lastchangecpts[*n+1]=1; 155 | 156 | int j; 157 | 158 | for(j=*minseglen;j<(2*(*minseglen));j++){ 159 | lastchangelike[j] = costfunction(*(sumstat+j),*(sumstat + *n + 1 + j),*(sumstat + *n + *n + 2 + j),j, *shape); 160 | // lastchangelike[j] = mll_mean(n, sumstat, j, 0, j, *shape); 161 | } 162 | 163 | 164 | for(j=*minseglen;j<(2*(*minseglen));j++){ 165 | lastchangecpts[j] = 0; 166 | } 167 | 168 | for(j=*minseglen;j<(2*(*minseglen));j++){ 169 | numchangecpts[j] =1; 170 | } 171 | 172 | 173 | nchecklist=2; 174 | checklist[0]=0; 175 | checklist[1]=*minseglen; 176 | 177 | for(tstar=2*(*minseglen);tstar<(*n+1);tstar++){ 178 | R_CheckUserInterrupt(); /* checks if user has interrupted the R session and quits if true */ 179 | 180 | if ((lastchangelike[tstar]) == 0){ 181 | for(i=0;i<(nchecklist);i++){ 182 | tmplike[i]=lastchangelike[checklist[i]] + costfunction(*(sumstat+tstar)-*(sumstat+checklist[i]),*(sumstat + *n + 1 +tstar)-*(sumstat + *n + 1 +checklist[i]),*(sumstat + *n + *n + 2 +tstar)-*(sumstat + *n + *n + 2 +checklist[i]), tstar-checklist[i], *shape)+*pen; 183 | } 184 | min_which(tmplike,nchecklist,&minout,&whichout); /*updates minout and whichout with min and which element */ 185 | lastchangelike[tstar]=minout; 186 | lastchangecpts[tstar]=checklist[whichout]; 187 | numchangecpts[tstar]=numchangecpts[lastchangecpts[tstar]]+1; 188 | /* Update checklist for next iteration, first element is next tau */ 189 | nchecktmp=0; 190 | for(i=0;i0)){stop('Poisson test statistic requires positive data')} 58 | if(sum(as.integer(data)==data)!=length(data)){stop('Poisson test statistic requires integer data')} 59 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 60 | # single dataset 61 | n=length(data) 62 | } 63 | else{ 64 | n=ncol(data) 65 | } 66 | if(n<4){stop('Data must have atleast 4 observations to fit a changepoint model.')} 67 | if(n<(2*minseglen)){stop('Minimum segment legnth is too large to include a change in this data')} 68 | 69 | pen.value = penalty_decision(penalty, pen.value, n, diffparam=1, asymcheck="meanvar.poisson", method="AMOC") 70 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 71 | tmp=single.meanvar.poisson.calc(coredata(data),extrainf=TRUE,minseglen) 72 | if(penalty=="MBIC"){ 73 | tmp[3]=tmp[3]+log(tmp[1])+log(n-tmp[1]+1) 74 | } 75 | ans=decision(tmp[1],tmp[2],tmp[3],penalty,n,diffparam=1,pen.value) 76 | if(class==TRUE){ 77 | return(class_input(data, cpttype="mean and variance", method="AMOC", test.stat="Poisson", penalty=penalty, pen.value=ans$pen, minseglen=minseglen, param.estimates=param.estimates, out=c(0,ans$cpt))) 78 | } 79 | else{ return(ans$cpt)} 80 | } 81 | else{ 82 | tmp=single.meanvar.poisson.calc(data,extrainf=TRUE,minseglen) 83 | if(penalty=="MBIC"){ 84 | tmp[,3]=tmp[,3]+log(tmp[,1])+log(n-tmp[,1]+1) 85 | } 86 | ans=decision(tmp[,1],tmp[,2],tmp[,3],penalty,n,diffparam=1,pen.value) 87 | if(class==TRUE){ 88 | rep=nrow(data) 89 | out=list() 90 | for(i in 1:rep){ 91 | out[[i]]=class_input(data[i,], cpttype="mean and variance", method="AMOC", test.stat="Poisson", penalty=penalty, pen.value=ans$pen, minseglen=minseglen, param.estimates=param.estimates, out=c(0,ans$cpt[i])) 92 | } 93 | return(out) 94 | } 95 | else{ return(ans$cpt)} 96 | } 97 | } 98 | 99 | 100 | segneigh.meanvar.poisson=function(data,Q=5,pen=0){ 101 | if((sum(data<0)>0)){stop('Poisson test statistic requires positive data')} 102 | if(sum(as.integer(data)==data)!=length(data)){stop('Poisson test statistic requires integer data')} 103 | n=length(data) 104 | if(n<4){stop('Data must have atleast 4 observations to fit a changepoint model.')} 105 | if(Q>((n/2)+1)){stop(paste('Q is larger than the maximum number of segments',(n/2)+1))} 106 | all.seg=matrix(0,ncol=n,nrow=n) 107 | for(i in 1:n){ 108 | sumx=0 109 | for(j in i:n){ 110 | len=j-i+1 111 | sumx=sumx+data[j] 112 | if(sumx==0){ 113 | all.seg[i,j]=-Inf 114 | } 115 | else{ 116 | all.seg[i,j]=sumx*log(sumx)-sumx*log(len) 117 | } 118 | } 119 | } 120 | like.Q=matrix(0,ncol=n,nrow=Q) 121 | like.Q[1,]=all.seg[1,] 122 | cp=matrix(NA,ncol=n,nrow=Q) 123 | for(q in 2:Q){ 124 | for(j in q:n){ 125 | like=NULL 126 | if((j-2-q)<0){v=q} 127 | else{v=(q):(j-2)} 128 | like=like.Q[q-1,v]+all.seg[v+1,j] 129 | 130 | like.Q[q,j]= max(like,na.rm=TRUE) 131 | cp[q,j]=which(like==max(like,na.rm=TRUE))[1]+(q-1) 132 | } 133 | 134 | } 135 | cps.Q=matrix(NA,ncol=Q,nrow=Q) 136 | for(q in 2:Q){ 137 | cps.Q[q,1]=cp[q,n] 138 | for(i in 1:(q-1)){ 139 | cps.Q[q,(i+1)]=cp[(q-i),cps.Q[q,i]] 140 | } 141 | } 142 | 143 | k=0:(Q-1) 144 | 145 | criterion=-2*like.Q[,n]+k*pen 146 | op.cps=which(criterion==min(criterion,na.rm=T))[1]-1 147 | 148 | if(op.cps==(Q-1)){warning('The number of segments identified is Q, it is advised to increase Q to make sure changepoints have not been missed.')} 149 | if(op.cps==0){cpts=n} 150 | else{cpts=c(sort(cps.Q[op.cps+1,][cps.Q[op.cps+1,]>0]),n)} 151 | 152 | return(list(cps=t(apply(cps.Q,1,sort,na.last=TRUE)),cpts=cpts,op.cpts=op.cps,pen=pen,like=criterion[op.cps+1],like.Q=like.Q[,n])) 153 | } 154 | 155 | 156 | multiple.meanvar.poisson=function(data,mul.method="PELT",penalty="MBIC",pen.value=0,Q=5,class=TRUE,param.estimates=TRUE,minseglen){ 157 | if((sum(data<0)>0)){stop('Poisson test statistic requires positive data')} 158 | if(sum(as.integer(data)==data)!=length(data)){stop('Poisson test statistic requires integer data')} 159 | if(!((mul.method=="PELT")||(mul.method=="BinSeg")||(mul.method=="SegNeigh"))){ 160 | stop("Multiple Method is not recognised") 161 | } 162 | costfunc = "meanvar.poisson" 163 | if(penalty=="MBIC"){ 164 | if(mul.method=="SegNeigh"){ 165 | stop('MBIC penalty not implemented for SegNeigh method, please choose an alternative penalty') 166 | } 167 | costfunc = "meanvar.poisson.mbic" 168 | } 169 | 170 | diffparam=1 171 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 172 | # single dataset 173 | n=length(data) 174 | } 175 | else{ 176 | n=ncol(data) 177 | } 178 | if(n<(2*minseglen)){stop('Minimum segment legnth is too large to include a change in this data')} 179 | 180 | pen.value = penalty_decision(penalty, pen.value, n, diffparam=1, asymcheck=costfunc, method=mul.method) 181 | if(is.null(dim(data))==TRUE || length(dim(data)) == 1){ 182 | # single dataset 183 | out = data_input(data=data,method=mul.method,pen.value=pen.value,costfunc=costfunc,minseglen=minseglen,Q=Q) 184 | 185 | if(class==TRUE){ 186 | return(class_input(data, cpttype="mean and variance", method=mul.method, test.stat="Poisson", penalty=penalty, pen.value=pen.value, minseglen=minseglen, param.estimates=param.estimates, out=out, Q=Q)) 187 | } 188 | else{ return(out[[2]])} 189 | } 190 | else{ 191 | rep=nrow(data) 192 | out=list() 193 | for(i in 1:rep){ 194 | out[[i]]=data_input(data[i,],method=mul.method,pen.value=pen.value,costfunc=costfunc,minseglen=minseglen,Q=Q) 195 | } 196 | 197 | cpts=lapply(out, '[[', 2) 198 | 199 | if(class==TRUE){ 200 | ans=list() 201 | for(i in 1:rep){ 202 | ans[[i]]=class_input(data[i,], cpttype="mean and variance", method=mul.method, test.stat="Poisson", penalty=penalty, pen.value=pen.value, minseglen=minseglen, param.estimates=param.estimates, out=out[[i]], Q=Q) 203 | } 204 | return(ans) 205 | } 206 | else{return(cpts)} 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /tests/testthat/_snaps/plot/diagnostic-plot-default.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 0 38 | 50 39 | 100 40 | 150 41 | 200 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 100 51 | 200 52 | 300 53 | 400 54 | 500 55 | 56 | Number of Changepoints 57 | Penalty Value 58 | 59 | 60 | -------------------------------------------------------------------------------- /tests/figs/plot-diagnostic-true-tests/diagnostic-plot-default.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 0 38 | 50 39 | 100 40 | 150 41 | 200 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 100 51 | 200 52 | 300 53 | 400 54 | 500 55 | 56 | Number of Changepoints 57 | Penalty Value 58 | 59 | 60 | -------------------------------------------------------------------------------- /R/CptReg.R: -------------------------------------------------------------------------------- 1 | cpt.reg <- function(data, penalty="MBIC", pen.value=0, method="PELT", test.stat="Normal", 2 | class=TRUE, param.estimates=TRUE, shape = 0, minseglen=3, tol = 1e-07){ 3 | checkData(data) 4 | MBIC=0 5 | ##Check arguments are valid 6 | if(!is.array(data) || !is.numeric(data)) ##Further checks applied later 7 | stop("Argument 'data' must be a numerical matrix/array.") 8 | if(!is.character(penalty) || length(penalty)>1) 9 | stop("Argument 'penalty' is invalid.") 10 | #value of 'penalty' & 'pen.value' checked within changepoint::penalty_decision 11 | if(!is.character(method) || length(method)>1) 12 | stop("Argument 'method' is invalid.") 13 | if(method!="AMOC" && method != "PELT") ##RESTRICTION IN USE 14 | stop("Invalid method, must be AMOC or PELT.") 15 | if(!is.character(test.stat) || length(test.stat)>1) 16 | stop("Argument 'test.stat' is invalid.") 17 | if(test.stat != "Normal"){ ##RESTRICTION IN USE 18 | warning(paste0("test.stat = ",test.stat," is not supported. Converted to test.stat='Normal'")) 19 | test.stat <- "Normal" 20 | } 21 | if(!is.logical(class) || length(class)>1) 22 | stop("Argument 'class' is invalid.") 23 | if(!is.logical(param.estimates) || length(param.estimates)>1) 24 | stop("Argument 'param.estimates' is invalid.") 25 | if(!is.numeric(minseglen) || length(minseglen)>1) 26 | stop("Argument 'minseglen' is invalid.") 27 | if(minseglen <= 0 || minseglen%%1 != 0) ##Further checks applied later 28 | stop("Argument 'minseglen' must be positive integer.") 29 | if(!is.numeric(tol) || length(tol)!=1) 30 | stop("Argument 'tol' is invalid.") 31 | if(tol<0) stop("Argument 'tol' must be positive.") 32 | ##Argument shape is assessed by the command where it is to be used. 33 | if(penalty=="CROPS"){ 34 | stop("CROPS has not yet been implemented for cpt.reg") 35 | } 36 | 37 | #Single data set? convert to multiple data set array. 38 | if(length(dim(data)) == 2) data <- array(data,dim=c(1,dim(data))) 39 | 40 | ans <- vector("list",dim(data)[1]) 41 | for(i in 1:dim(data)[1]){ ##To each data set. 42 | #Check format for ith data set 43 | datai <- check_data(data[i,,]) 44 | 45 | #Evaluate penalty value 46 | pen.value <- penalty_decision(penalty=penalty, 47 | pen.value=pen.value, n=nrow(datai), diffparam=ncol(datai), 48 | asymcheck="reg.norm", method=method) 49 | if(penalty=="MBIC"){MBIC=1} 50 | 51 | #Check value of minseglen 52 | if(minseglen < (ncol(datai)-1)){ 53 | warning(paste("minseglen is too small, set to:",ncol(datai))) 54 | minsegleni <- ncol(datai) 55 | }else if(nrow(datai) < (2*minseglen)){ 56 | stop("Minimum segment length is too large to include a change in this data.") 57 | }else{ 58 | minsegleni <- minseglen 59 | } 60 | 61 | CPTS <- ChangepointRegression(data=datai, penalty=penalty, 62 | penalty.value=pen.value, method=method, dist=test.stat, minseglen=minseglen, 63 | shape = shape, tol=tol, cpts.only=class, MBIC=MBIC) 64 | 65 | if(class){ 66 | #Convert to cpt.reg object 67 | ansi <- new("cpt.reg") 68 | data.set(ansi) <- datai 69 | cpttype(ansi) <- "regression" 70 | method(ansi) <- method 71 | distribution(ansi) <- test.stat 72 | pen.type(ansi) <- penalty 73 | pen.value(ansi) <- pen.value 74 | cpts(ansi) <- CPTS 75 | if(method=="PELT") ncpts.max(ansi) <- Inf 76 | if(param.estimates) ansi = param(ansi) 77 | ans[[i]] <- ansi 78 | }else{ 79 | #store what is returned from calculation 80 | ans[[i]] <- CPTS 81 | } 82 | } 83 | 84 | ##Return result, only first case if there is only a singe data set 85 | if(dim(data)[1]==1){ return(ans[[1]]) }else{ return(ans) } 86 | } 87 | 88 | #################### 89 | 90 | check_data <- function(data, minseglen=3){ 91 | if(!is.array(data) || !is.numeric(data)) 92 | stop("Argument 'data' must be a numerical matrix.") 93 | if(length(dim(data))!=2) 94 | stop("Argument 'data' must be a numerical matrix.") 95 | if(!is.numeric(minseglen) || length(minseglen)>1) 96 | stop("Argument 'minseglen' is invalid.") 97 | 98 | n <- nrow(data) 99 | p <- ncol(data)-1 100 | if(p==0) stop("Dimension of data is 1, no regressors found.") 101 | if(n1){ 109 | i <- which(intercept)[-1]+1 110 | warning("Multiple intercepts found. Keeping only first instance.") 111 | data <- data[,-i] 112 | } 113 | return(data) 114 | } 115 | 116 | 117 | ChangepointRegression <- function(data, penalty="MBIC", penalty.value=0, 118 | method="AMOC", dist="Normal", minseglen=3, cpts.only=TRUE, shape=0, MBIC=0, tol=1e-07){ 119 | ##Assume all input arguments are entered correctly 120 | if(!is.logical(cpts.only) && length(cpts.only)>1) 121 | stop("Argument 'cpts.only' is invalid.") 122 | if(method=="AMOC" && dist=="Normal"){ 123 | out <- CptReg_AMOC_Normal(data, penalty, penalty.value, minseglen, shape, MBIC, tol) 124 | }else if(method=="PELT" && dist=="Normal"){ 125 | out <- CptReg_PELT_Normal(data, penalty.value, minseglen, shape,MBIC, tol) 126 | }else{ 127 | stop("Changepoint in regression method not recognised.") 128 | } 129 | if(cpts.only){ 130 | return(sort(out$cpts)) 131 | }else{ 132 | return(out) 133 | } 134 | } 135 | 136 | CptReg_AMOC_Normal <- function(data, penalty="MBIC", penalty.value=0, minseglen=3, 137 | shape=0, MBIC=0, tol=1e-07){ 138 | n <- as.integer(nrow(data)) 139 | p <- as.integer(ncol(data)-1) 140 | if(p<1 || n0=-2logLik with this fixed variance 143 | if(!is.numeric(shape) || length(shape)!=1) 144 | stop("Argument 'shape' is invalid.") 145 | 146 | ##Clean-up on exit 147 | answer=list() 148 | answer[[5]]=1 149 | on.exit(.C("Free_CptReg_Normal_AMOC",answer[[6]],PACKAGE="changepoint")) 150 | 151 | answer <- .C("CptReg_Normal_AMOC", data=as.double(data), n=as.integer(n), 152 | m=as.integer(p+1), pen=as.double(penalty.value), err=0L, 153 | shape=as.double(shape), minseglen=as.integer(minseglen), tol=as.double(tol), 154 | tau=0L, nulllike=vector("double",1), taulike=vector("double",1), 155 | tmplike=vector("double",n), MBIC=as.integer(MBIC),PACKAGE="changepoint") 156 | 157 | #Check if error has occured 158 | if(answer$err!=0) stop("C code error:",answer$err,call.=F) 159 | tmp <- c(answer$tau,answer$nulllike,answer$taulike) 160 | #Following line in cpt.mean (RSS) but not in old cpt.reg (-2Loglik) 161 | #if(penalty=="MBIC") tmp[3] = tmp[3] + log(tmp[1]) + log(n-tmp[1]+1) 162 | out <- decision(tau = tmp[1], null = tmp[2], alt = tmp[3], 163 | penalty = penalty, n=n, diffparam=p, pen.value = penalty.value) 164 | names(out) <- c("cpts","pen.value") 165 | return(out) #return list of cpts & pen.value 166 | } 167 | 168 | CptReg_PELT_Normal <- function(data, penalty.value=0, minseglen=3, shape=0, 169 | MBIC=0, tol=1e-07){ 170 | n <- as.integer(nrow(data)) 171 | p <- as.integer(ncol(data)-1) 172 | #tol <- 1e-07 #Rank tolerance (see lm.fit) 173 | #shape <- -1 #-1=RSS,0=-2logLik,>0=-2logLik with this fixed variance 174 | if(!is.numeric(shape) || length(shape)!=1) 175 | stop("Argument 'shape' is invalid.") 176 | 177 | #Check if error has occured 178 | answer=list() 179 | answer[[6]]=1 180 | on.exit(.C("Free_CptReg_Normal_PELT",answer[[6]],PACKAGE="changepoint")) 181 | 182 | answer <- .C("CptReg_Normal_PELT", data=as.double(data), n=n, 183 | m=as.integer(p+1), pen=as.double(penalty.value), cpt=vector("integer",n), 184 | err=0L, shape=as.double(shape), minseglen=as.integer(minseglen), 185 | tol=as.double(tol), lastchangelike=vector("double",n+1), 186 | lastchangecpts=vector("integer",n+1), numchangecpts=vector("integer",n+1), 187 | MBIC=as.integer(MBIC), PACKAGE="changepoint") 188 | 189 | if(answer$err!=0){ 190 | stop("C code error:",answer$err,call.=F) 191 | } 192 | return(list(lastchangecpts=answer$lastchangecpts, 193 | cpts=sort(answer$cpt[answer$cpt>0]), 194 | lastchangelike=answer$lastchangelike, 195 | ncpts=answer$numchangecpts)) 196 | } 197 | 198 | 199 | 200 | ######################################################################## 201 | 202 | 203 | 204 | -------------------------------------------------------------------------------- /man/cpt.mean.Rd: -------------------------------------------------------------------------------- 1 | \name{cpt.mean} 2 | \alias{cpt.mean} 3 | \title{ 4 | Identifying Changes in Mean 5 | } 6 | \description{ 7 | Calculates the optimal positioning and (potentially) number of changepoints for data using the user specified method. 8 | } 9 | \usage{ 10 | cpt.mean(data,penalty="MBIC",pen.value=0,method="PELT",Q=5,test.stat="Normal",class=TRUE, 11 | param.estimates=TRUE,minseglen=1) 12 | } 13 | \arguments{ 14 | \item{data}{ 15 | A vector, ts object or matrix containing the data within which you wish to find a changepoint. If data is a matrix, each row is considered a separate dataset. 16 | } 17 | \item{penalty}{ 18 | Choice of "None", "SIC", "BIC", "MBIC", AIC", "Hannan-Quinn", "Asymptotic", "Manual" and "CROPS" penalties. If Manual is specified, the manual penalty is contained in the pen.value parameter. If Asymptotic is specified, the theoretical type I error is contained in the pen.value parameter. If CROPS is specified, the penalty range is contained in the pen.value parameter; note this is a vector of length 2 which contains the minimum and maximum penalty value. Note CROPS can only be used if the method is "PELT". The predefined penalties listed DO count the changepoint as a parameter, postfix a 0 e.g."SIC0" to NOT count the changepoint as a parameter. 19 | } 20 | \item{pen.value}{ 21 | The theoretical type I error e.g.0.05 when using the Asymptotic penalty. A vector of length 2 (min,max) if using the CROPS penalty. The value of the penalty when using the Manual penalty option - this can be a numeric value or text giving the formula to use. Available variables are, n=length of original data, null=null likelihood, alt=alternative likelihood, tau=proposed changepoint, diffparam=difference in number of alternatve and null parameters. 22 | } 23 | \item{method}{ 24 | Choice of "AMOC", "PELT", "SegNeigh" or "BinSeg". Default is "PELT" (from 2.3). 25 | } 26 | \item{Q}{ 27 | The maximum number of changepoints to search for using the "BinSeg" method. The maximum number of segments (number of changepoints + 1) to search for using the "SegNeigh" method. 28 | } 29 | \item{test.stat}{ 30 | The assumed test statistic / distribution of the data. Currently only "Normal" and "CUSUM" supported. 31 | } 32 | \item{class}{ 33 | Logical. If TRUE then an object of class \code{cpt} is returned. 34 | } 35 | \item{param.estimates}{ 36 | Logical. If TRUE and class=TRUE then parameter estimates are returned. If FALSE or class=FALSE no parameter estimates are returned. 37 | } 38 | \item{minseglen}{ 39 | Positive integer giving the minimum segment length (no. of observations between changes), default is the minimum allowed by theory. 40 | } 41 | } 42 | \details{ 43 | This function is used to find changes in mean for data using the test statistic specfified in the test.stat parameter. The changes are found using the method supplied which can be single changepoint (AMOC) or multiple changepoints using exact (PELT or SegNeigh) or approximate (BinSeg) methods. A changepoint is denoted as the last observation of the segment / regime. 44 | } 45 | \value{ 46 | If \code{class=TRUE} then an object of S4 class "cpt" is returned. The slot \code{cpts} contains the changepoints that are returned. For \code{class=FALSE} the structure is as follows. 47 | 48 | If data is a vector (single dataset) then a vector/list is returned depending on the value of method. If data is a matrix (multiple datasets) then a list is returned where each element in the list is either a vector or list depending on the value of method. 49 | 50 | If method is AMOC then a vector (one dataset) or matrix (multiple datasets) is returned, the columns are: 51 | \item{cpt}{The most probable location of a changepoint if a change was identified or NA if no changepoint.} 52 | \item{p value}{The p-value of the identified changepoint.} 53 | If method is PELT then a vector is returned containing the changepoint locations for the penalty supplied. This always ends with n. 54 | If the penalty is CROPS then a list is returned with elements: 55 | \item{cpt.out}{A data frame containing the value of the penalty value where the number of segmentations changes, the number of segmentations and the value of the cost at that penalty value.} 56 | \item{changepoints}{The optimal changepoint for the different penalty values starting with the lowest penalty value} 57 | If method is SegNeigh then a list is returned with elements: 58 | \item{cps}{Matrix containing the changepoint positions for 1,...,Q changepoints.} 59 | \item{op.cpts}{The optimal changepoint locations for the penalty supplied.} 60 | \item{pen}{Penalty used to find the optimal number of changepoints.} 61 | \item{like}{Value of the -2*log(likelihood ratio) + penalty for the optimal number of changepoints selected.} 62 | If method is BinSeg then a list is returned with elements: 63 | \item{cps}{2xQ Matrix containing the changepoint positions on the first row and the test statistic on the second row.} 64 | \item{op.cpts}{The optimal changepoint locations for the penalty supplied.} 65 | \item{pen}{Penalty used to find the optimal number of changepoints.} 66 | } 67 | \references{ 68 | Change in Normal mean: Hinkley, D. V. (1970) Inference About the Change-Point in a Sequence of Random Variables, \emph{Biometrika} \bold{57}, 1--17 69 | 70 | CUSUM Test: M. Csorgo, L. Horvath (1997) Limit Theorems in Change-Point Analysis, \emph{Wiley} 71 | 72 | PELT Algorithm: Killick R, Fearnhead P, Eckley IA (2012) Optimal detection of changepoints with a linear computational cost, \emph{JASA} \bold{107(500)}, 1590--1598 73 | 74 | CROPS: Haynes K, Eckley IA, Fearnhead P (2014) Efficient penalty search for multiple changepoint problems (in submission), arXiv:1412.3617 75 | 76 | Binary Segmentation: Scott, A. J. and Knott, M. (1974) A Cluster Analysis Method for Grouping Means in the Analysis of Variance, \emph{Biometrics} \bold{30(3)}, 507--512 77 | 78 | Segment Neighbourhoods: Auger, I. E. And Lawrence, C. E. (1989) Algorithms for the Optimal Identification of Segment Neighborhoods, \emph{Bulletin of Mathematical Biology} \bold{51(1)}, 39--54 79 | 80 | MBIC: Zhang, N. R. and Siegmund, D. O. (2007) A Modified Bayes Information Criterion with Applications to the Analysis of Comparative Genomic Hybridization Data. \emph{Biometrics} \bold{63}, 22-32. 81 | } 82 | \author{ 83 | Rebecca Killick 84 | } 85 | 86 | 87 | \seealso{ 88 | \code{\link{cpt.var}},\code{\link{cpt.meanvar}},\code{\link{plot-methods}},\code{\linkS4class{cpt}} 89 | } 90 | \examples{ 91 | # Example of a change in mean at 100 in simulated normal data 92 | set.seed(1) 93 | x=c(rnorm(100,0,1),rnorm(100,10,1)) 94 | cpt.mean(x,penalty="SIC",method="AMOC",class=FALSE) # returns 100 to show that the null hypothesis 95 | #was rejected and the change in mean is at 100 and the confidence level is 1. 96 | ans=cpt.mean(x,penalty="Asymptotic",pen.value=0.01,method="AMOC") 97 | cpts(ans)# returns 100 to show that the null hypothesis was rejected, the change in mean is at 100 98 | #and we are 99% confident of this result 99 | cpt.mean(x,penalty="Manual",pen.value=0.8,method="AMOC",test.stat="CUSUM") 100 | # returns 101 as the changepoint location 101 | 102 | 103 | # Example of multiple changes in mean at 50,100,150 in simulated normal data 104 | set.seed(1) 105 | x=c(rnorm(50,0,1),rnorm(50,5,1),rnorm(50,10,1),rnorm(50,3,1)) 106 | cpt.mean(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5,class=FALSE) 107 | # returns optimal number of changepoints is 3, locations are 50,100,150. 108 | 109 | # Example of using the CROPS penalty in data set above 110 | set.seed(1) 111 | x=c(rnorm(50,0,1),rnorm(50,5,1),rnorm(50,10,1),rnorm(50,3,1)) 112 | out=cpt.mean(x, pen.value = c(4,1500),penalty = "CROPS",method = "PELT") 113 | cpts.full(out) # returns 7 segmentations for penalty values between 4 and 1500. 114 | # We find segmentations with 7, 5, 4, 3, 2, 1 and 0 changepoints. 115 | # Note that the empty final row indicates no changepoints. 116 | pen.value.full(out) # gives associated penalty transition points 117 | # CROPS does not give an optimal set of changepoints thus we may wish to explore further 118 | plot(out,diagnostic=TRUE) 119 | # looks like the segmentation with 3 changepoints, 50,100,150 is the most appropriate 120 | plot(out,ncpts=3) 121 | 122 | 123 | # Example multiple datasets where the first row has multiple changes in mean and the second row has 124 | #no change in mean 125 | set.seed(1) 126 | x=c(rnorm(50,0,1),rnorm(50,5,1),rnorm(50,10,1),rnorm(50,3,1)) 127 | y=rnorm(200,0,1) 128 | z=rbind(x,y) 129 | cpt.mean(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE) # returns list 130 | #that has two elements, the first has 3 changes in mean and variance at 50,100,150 and the second 131 | #has no changes in variance 132 | ans=cpt.mean(z,penalty="Asymptotic",pen.value=0.01,method="PELT") 133 | cpts(ans[[1]]) # same results as for the SegNeigh method. 134 | cpts(ans[[2]]) # same results as for the SegNeigh method. 135 | } 136 | 137 | \keyword{methods} 138 | \keyword{univar} 139 | \keyword{models} 140 | \keyword{ts} 141 | -------------------------------------------------------------------------------- /man/cpt.meanvar.Rd: -------------------------------------------------------------------------------- 1 | \name{cpt.meanvar} 2 | \alias{cpt.meanvar} 3 | \title{ 4 | Identifying Changes in Mean and Variance 5 | } 6 | \description{ 7 | Calculates the optimal positioning and (potentially) number of changepoints for data using the user specified method. 8 | } 9 | \usage{ 10 | cpt.meanvar(data,penalty="MBIC",pen.value=0,method="PELT",Q=5,test.stat="Normal", 11 | class=TRUE,param.estimates=TRUE,shape=1,minseglen=2) 12 | } 13 | \arguments{ 14 | \item{data}{ 15 | A vector, ts object or matrix containing the data within which you wish to find a changepoint. If data is a matrix, each row is considered a separate dataset. 16 | } 17 | \item{penalty}{ 18 | Choice of "None", "SIC", "BIC", "MBIC", AIC", "Hannan-Quinn", "Asymptotic", "Manual" and "CROPS" penalties. If Manual is specified, the manual penalty is contained in the pen.value parameter. If Asymptotic is specified, the theoretical type I error is contained in the pen.value parameter. If CROPS is specified, the penalty range is contained in the pen.value parameter; note this is a vector of length 2 which contains the minimum and maximum penalty value. Note CROPS can only be used if the method is "PELT". The predefined penalties listed DO count the changepoint as a parameter, postfix a 0 e.g."SIC0" to NOT count the changepoint as a parameter. 19 | } 20 | \item{pen.value}{ 21 | The theoretical type I error e.g.0.05 when using the Asymptotic penalty. A vector of length 2 (min,max) if using the CROPS penalty. The value of the penalty when using the Manual penalty option - this can be a numeric value or text giving the formula to use. Available variables are, n=length of original data, null=null likelihood, alt=alternative likelihood, tau=proposed changepoint, diffparam=difference in number of alternatve and null parameters. 22 | } 23 | \item{method}{ 24 | Choice of "AMOC", "PELT", "SegNeigh" or "BinSeg". Default is "PELT" (from 2.3). 25 | } 26 | \item{Q}{ 27 | The maximum number of changepoints to search for using the "BinSeg" method. The maximum number of segments (number of changepoints + 1) to search for using the "SegNeigh" method. 28 | } 29 | \item{test.stat}{ 30 | The assumed test statistic / distribution of the data. Currently only "Normal", "Gamma", "Exponential" and "Poisson" are supported. 31 | } 32 | \item{class}{ 33 | Logical. If TRUE then an object of class \code{cpt} is returned. 34 | } 35 | \item{param.estimates}{ 36 | Logical. If TRUE and class=TRUE then parameter estimates are returned. If FALSE or class=FALSE no parameter estimates are returned. 37 | } 38 | \item{shape}{ 39 | Value of the assumed known shape parameter required when test.stat="Gamma". 40 | } 41 | \item{minseglen}{ 42 | Positive integer giving the minimum segment length (no. of observations between changes), default is the minimum allowed by theory. 43 | } 44 | } 45 | \details{ 46 | This function is used to find changes in mean and variance for data using the test statistic specified in the test.stat parameter. The changes are found using the method supplied which can be single changepoint (AMOC) or multiple changepoints using exact (PELT or SegNeigh) or approximate (BinSeg) methods. A changepoint is denoted as the last observation of the segment / regime. 47 | } 48 | \value{ 49 | If \code{class=TRUE} then an object of S4 class "cpt" is returned. The slot \code{cpts} contains the changepoints that are returned. For \code{class=FALSE} the structure is as follows. 50 | 51 | If data is a vector (single dataset) then a vector/list is returned depending on the value of method. If data is a matrix (multiple datasets) then a list is returned where each element in the list is either a vector or list depending on the value of method. 52 | 53 | If method is AMOC then a vector (one dataset) or matrix (multiple datasets) is returned, the columns are: 54 | \item{cpt}{The most probable location of a changepoint if a change was identified or NA if no changepoint.} 55 | \item{p value}{The p-value of the identified changepoint.} 56 | If method is PELT then a vector is returned containing the changepoint locations for the penalty supplied. This always ends with n. 57 | If the penalty is CROPS then a list is returned with elements: 58 | \item{cpt.out}{A data frame containing the value of the penalty value where the number of segmentations changes, the number of segmentations and the value of the cost at that penalty value.} 59 | \item{changepoints}{The optimal changepoints for the different penalty values starting with the lowest penalty value} 60 | If method is SegNeigh then a list is returned with elements: 61 | \item{cps}{Matrix containing the changepoint positions for 1,...,Q changepoints.} 62 | \item{op.cpts}{The optimal changepoint locations for the penalty supplied.} 63 | \item{pen}{Penalty used to find the optimal number of changepoints.} 64 | \item{like}{Value of the -2*log(likelihood ratio) + penalty for the optimal number of changepoints selected.} 65 | If method is BinSeg then a list is returned with elements: 66 | \item{cps}{2xQ Matrix containing the changepoint positions on the first row and the test statistic on the second row.} 67 | \item{op.cpts}{The optimal changepoint locations for the penalty supplied.} 68 | \item{pen}{Penalty used to find the optimal number of changepoints.} 69 | } 70 | \references{ 71 | Change in Normal mean and variance: Chen, J. and Gupta, A. K. (2000) \emph{Parametric statistical change point analysis}, Birkhauser 72 | 73 | Change in Gamma shape parameter: Chen, J. and Gupta, A. K. (2000) \emph{Parametric statistical change point analysis}, Birkhauser 74 | 75 | Change in Exponential model: Chen, J. and Gupta, A. K. (2000) \emph{Parametric statistical change point analysis}, Birkhauser 76 | 77 | Change in Poisson model: Chen, J. and Gupta, A. K. (2000) \emph{Parametric statistical change point analysis}, Birkhauser 78 | 79 | PELT Algorithm: Killick R, Fearnhead P, Eckley IA (2012) Optimal detection of changepoints with a linear computational cost, \emph{JASA} \bold{107(500)}, 1590--1598 80 | 81 | CROPS: Haynes K, Eckley IA, Fearnhead P (2014) Efficient penalty search for multiple changepoint problems (in submission), arXiv:1412.3617 82 | 83 | Binary Segmentation: Scott, A. J. and Knott, M. (1974) A Cluster Analysis Method for Grouping Means in the Analysis of Variance, \emph{Biometrics} \bold{30(3)}, 507--512 84 | 85 | Segment Neighbourhoods: Auger, I. E. And Lawrence, C. E. (1989) Algorithms for the Optimal Identification of Segment Neighborhoods, \emph{Bulletin of Mathematical Biology} \bold{51(1)}, 39--54 86 | 87 | MBIC: Zhang, N. R. and Siegmund, D. O. (2007) A Modified Bayes Information Criterion with Applications to the Analysis of Comparative Genomic Hybridization Data. \emph{Biometrics} \bold{63}, 22-32. 88 | } 89 | \author{ 90 | Rebecca Killick 91 | } 92 | 93 | 94 | \seealso{ 95 | \code{\link{cpt.var}},\code{\link{cpt.mean}},\code{\link{plot-methods}},\code{\linkS4class{cpt}} 96 | } 97 | \examples{ 98 | # Example of a change in scale parameter (mean and variance) at 100 in simulated gamma data 99 | set.seed(1) 100 | x=c(rgamma(100,shape=1,rate=1),rgamma(100,shape=1,rate=5)) 101 | cpt.meanvar(x,penalty="SIC",method="AMOC",test.stat="Gamma",class=FALSE,shape=1) # returns 97 to 102 | #show that the null hypothesis was rejected and the change in scale parameter is at 97 103 | ans=cpt.meanvar(x,penalty="AIC",method="AMOC",test.stat="Gamma",shape=1) 104 | cpts(ans) 105 | # returns 97 to show that the null hypothesis was rejected, the change in scale parameter is at 97 106 | 107 | 108 | # Example of multiple changes in mean and variance at 50,100,150 in simulated normal data 109 | set.seed(1) 110 | x=c(rnorm(50,0,1),rnorm(50,5,3),rnorm(50,10,1),rnorm(50,3,10)) 111 | cpt.meanvar(x,penalty="Manual",pen.value="4*log(n)",method="BinSeg",Q=5,class=FALSE) 112 | # returns optimal number of changepoints is 4, locations are 50,100,150,152. 113 | 114 | # Example of using the CROPS penalty in the above example 115 | set.seed(1) 116 | x=c(rnorm(50,0,1),rnorm(50,5,3),rnorm(50,10,1),rnorm(50,3,10)) 117 | out=cpt.meanvar(x,pen.value=c(2*log(length(x)),100*log(length(x))),penalty="CROPS",method="PELT") 118 | cpts.full(out) 119 | # returns 6 segmentations for penalty values between 2log(n) and 100log(n). 120 | # We find segmentations with 9, 7, 4, 3, 1 and 0 changepoints. 121 | # Note that the empty final row indicates no changepoints. 122 | pen.value.full(out) # gives associated penalty transition points 123 | # CROPS does not give an optimal set of changepoints thus we may wish to explore further 124 | plot(out,diagnostic=TRUE) 125 | # looks like the segmentation with 4 changepoints, 50,100,150,200 is the most appropriate 126 | plot(out,ncpts=3) 127 | 128 | 129 | # Example multiple datasets where the first row has multiple changes in mean and variance and the 130 | #second row has no change in mean or variance 131 | set.seed(1) 132 | x=c(rnorm(50,0,1),rnorm(50,5,3),rnorm(50,10,1),rnorm(50,3,10)) 133 | y=rnorm(200,0,1) 134 | z=rbind(x,y) 135 | cpt.meanvar(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE) # returns list 136 | #that has two elements, the first has 3 changes in mean and variance at 50,100,150 and the second 137 | #has no changes in mean or variance 138 | ans=cpt.meanvar(z,penalty="Asymptotic",pen.value=0.01,method="PELT") 139 | cpts(ans[[1]]) # same results as for the SegNeigh method. 140 | cpts(ans[[2]]) # same results as for the SegNeigh method. 141 | } 142 | 143 | \keyword{methods} 144 | \keyword{univar} 145 | \keyword{models} 146 | \keyword{ts} 147 | --------------------------------------------------------------------------------