diff --git a/apple-touch-icon-120x120.png b/apple-touch-icon-120x120.png index 460dd3ee..a3349be6 100644 Binary files a/apple-touch-icon-120x120.png and b/apple-touch-icon-120x120.png differ diff --git a/apple-touch-icon-152x152.png b/apple-touch-icon-152x152.png index ece55df0..e3ca24af 100644 Binary files a/apple-touch-icon-152x152.png and b/apple-touch-icon-152x152.png differ diff --git a/apple-touch-icon-180x180.png b/apple-touch-icon-180x180.png index 3b15cbb6..20de1933 100644 Binary files a/apple-touch-icon-180x180.png and b/apple-touch-icon-180x180.png differ diff --git a/apple-touch-icon-60x60.png b/apple-touch-icon-60x60.png index bdca138e..df0082d8 100644 Binary files a/apple-touch-icon-60x60.png and b/apple-touch-icon-60x60.png differ diff --git a/apple-touch-icon-76x76.png b/apple-touch-icon-76x76.png index 116bde7c..fd9f7095 100644 Binary files a/apple-touch-icon-76x76.png and b/apple-touch-icon-76x76.png differ diff --git a/apple-touch-icon.png b/apple-touch-icon.png index 2d0b1459..738e5b93 100644 Binary files a/apple-touch-icon.png and b/apple-touch-icon.png differ diff --git a/favicon-16x16.png b/favicon-16x16.png index 28672868..1aced930 100644 Binary files a/favicon-16x16.png and b/favicon-16x16.png differ diff --git a/favicon-32x32.png b/favicon-32x32.png index a6bb9c74..4151a5ba 100644 Binary files a/favicon-32x32.png and b/favicon-32x32.png differ diff --git a/pkgdown.yml b/pkgdown.yml index f051f793..dacaf80c 100644 --- a/pkgdown.yml +++ b/pkgdown.yml @@ -6,7 +6,7 @@ articles: comparison-pelt: comparison-pelt.html examples-advanced: examples-advanced.html examples-custom-model: examples-custom-model.html -last_built: 2024-05-28T03:41Z +last_built: 2024-05-29T01:38Z urls: reference: https://fastcpd.xingchi.li/reference article: https://fastcpd.xingchi.li/articles diff --git a/reference/Rplot002.png b/reference/Rplot002.png index d9fec6e9..b7874356 100644 Binary files a/reference/Rplot002.png and b/reference/Rplot002.png differ diff --git a/reference/fastcpd_mean.html b/reference/fastcpd_mean.html index fd9a76af..abc4499a 100644 --- a/reference/fastcpd_mean.html +++ b/reference/fastcpd_mean.html @@ -129,7 +129,7 @@

Examples result <- fastcpd.mean(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed -#> 0.114 0.098 0.074 +#> 0.102 0.114 0.075 result@cp_set #> [1] 10007 # \donttest{ @@ -139,7 +139,7 @@

Examples result <- fastcpd.mean(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed -#> 0.563 0.279 0.523 +#> 0.569 0.285 0.528 result@cp_set #> [1] 10007 19998 # } @@ -153,7 +153,7 @@

Examples ) )) #> user system elapsed -#> 0.238 0.284 0.192 +#> 0.241 0.268 0.189 result@cp_set #> [1] 10034 20082 # } diff --git a/reference/fastcpd_meanvariance.html b/reference/fastcpd_meanvariance.html index 3d2131f5..c448682e 100644 --- a/reference/fastcpd_meanvariance.html +++ b/reference/fastcpd_meanvariance.html @@ -179,7 +179,7 @@

Examples result <- fastcpd.variance(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed -#> 1.278 0.000 1.277 +#> 1.405 0.000 1.405 result@cp_set #> [1] 1985 4000 # } @@ -193,7 +193,7 @@

Examples ) )) #> user system elapsed -#> 1.053 0.000 1.053 +#> 1.143 0.001 1.144 result@cp_set #> [1] 1985 4000 # } diff --git a/reference/fastcpd_variance.html b/reference/fastcpd_variance.html index c70c31cc..8dd5f45c 100644 --- a/reference/fastcpd_variance.html +++ b/reference/fastcpd_variance.html @@ -153,7 +153,7 @@

Examples result <- fastcpd.variance(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed -#> 1.911 0.000 1.911 +#> 2.09 0.00 2.09 result@cp_set #> [1] 2997 5989 # } @@ -167,7 +167,7 @@

Examples ) )) #> user system elapsed -#> 1.883 0.000 1.883 +#> 2.033 0.000 2.033 result@cp_set #> [1] 2997 5989 # } diff --git a/reference/plot-2.png b/reference/plot-2.png index 0e6708db..db3ee47e 100644 Binary files a/reference/plot-2.png and b/reference/plot-2.png differ diff --git a/reference/uk_seatbelts-1.png b/reference/uk_seatbelts-1.png index fb8727ce..d0590c8b 100644 Binary files a/reference/uk_seatbelts-1.png and b/reference/uk_seatbelts-1.png differ diff --git a/reference/uk_seatbelts-2.png b/reference/uk_seatbelts-2.png index f80ee50f..304b1077 100644 Binary files a/reference/uk_seatbelts-2.png and b/reference/uk_seatbelts-2.png differ diff --git a/reference/well_log-2.png b/reference/well_log-2.png index 6ffb813f..8a338a1c 100644 Binary files a/reference/well_log-2.png and b/reference/well_log-2.png differ diff --git a/reference/well_log.html b/reference/well_log.html index c5aba910..e18687f3 100644 --- a/reference/well_log.html +++ b/reference/well_log.html @@ -119,7 +119,6 @@

Examples } result@residuals <- matrix(residual) - result@family <- "mean" result@data <- data.frame(x = c(well_log)) plot(result) } @@ -130,6 +129,7 @@

Examples#> #> Change points: #> 19 577 1034 1070 1216 1361 1428 1526 1685 1866 2047 2409 2469 2531 2591 2775 3744 3855 3945 3963 +#> Built-in plot should only work for built-in families. # } diff --git a/search.json b/search.json index 9489ae55..03b9140d 100644 --- a/search.json +++ b/search.json @@ -1 +1 @@ -[{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":null,"dir":"","previous_headings":"","what":"GNU General Public License","title":"GNU General Public License","text":"Version 3, 29 June 2007 Copyright © 2007 Free Software Foundation, Inc.  Everyone permitted copy distribute verbatim copies license document, changing allowed.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"preamble","dir":"","previous_headings":"","what":"Preamble","title":"GNU General Public License","text":"GNU General Public License free, copyleft license software kinds works. licenses software practical works designed take away freedom share change works. contrast, GNU General Public License intended guarantee freedom share change versions program–make sure remains free software users. , Free Software Foundation, use GNU General Public License software; applies also work released way authors. can apply programs, . speak free software, referring freedom, price. General Public Licenses designed make sure freedom distribute copies free software (charge wish), receive source code can get want , can change software use pieces new free programs, know can things. protect rights, need prevent others denying rights asking surrender rights. Therefore, certain responsibilities distribute copies software, modify : responsibilities respect freedom others. example, distribute copies program, whether gratis fee, must pass recipients freedoms received. must make sure , , receive can get source code. must show terms know rights. Developers use GNU GPL protect rights two steps: (1) assert copyright software, (2) offer License giving legal permission copy, distribute /modify . developers’ authors’ protection, GPL clearly explains warranty free software. users’ authors’ sake, GPL requires modified versions marked changed, problems attributed erroneously authors previous versions. devices designed deny users access install run modified versions software inside , although manufacturer can . fundamentally incompatible aim protecting users’ freedom change software. systematic pattern abuse occurs area products individuals use, precisely unacceptable. Therefore, designed version GPL prohibit practice products. problems arise substantially domains, stand ready extend provision domains future versions GPL, needed protect freedom users. Finally, every program threatened constantly software patents. States allow patents restrict development use software general-purpose computers, , wish avoid special danger patents applied free program make effectively proprietary. prevent , GPL assures patents used render program non-free. precise terms conditions copying, distribution modification follow.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_0-definitions","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"0. Definitions","title":"GNU General Public License","text":"“License” refers version 3 GNU General Public License. “Copyright” also means copyright-like laws apply kinds works, semiconductor masks. “Program” refers copyrightable work licensed License. licensee addressed “”. “Licensees” “recipients” may individuals organizations. “modify” work means copy adapt part work fashion requiring copyright permission, making exact copy. resulting work called “modified version” earlier work work “based ” earlier work. “covered work” means either unmodified Program work based Program. “propagate” work means anything , without permission, make directly secondarily liable infringement applicable copyright law, except executing computer modifying private copy. Propagation includes copying, distribution (without modification), making available public, countries activities well. “convey” work means kind propagation enables parties make receive copies. Mere interaction user computer network, transfer copy, conveying. interactive user interface displays “Appropriate Legal Notices” extent includes convenient prominently visible feature (1) displays appropriate copyright notice, (2) tells user warranty work (except extent warranties provided), licensees may convey work License, view copy License. interface presents list user commands options, menu, prominent item list meets criterion.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_1-source-code","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"1. Source Code","title":"GNU General Public License","text":"“source code” work means preferred form work making modifications . “Object code” means non-source form work. “Standard Interface” means interface either official standard defined recognized standards body, , case interfaces specified particular programming language, one widely used among developers working language. “System Libraries” executable work include anything, work whole, () included normal form packaging Major Component, part Major Component, (b) serves enable use work Major Component, implement Standard Interface implementation available public source code form. “Major Component”, context, means major essential component (kernel, window system, ) specific operating system () executable work runs, compiler used produce work, object code interpreter used run . “Corresponding Source” work object code form means source code needed generate, install, (executable work) run object code modify work, including scripts control activities. However, include work’s System Libraries, general-purpose tools generally available free programs used unmodified performing activities part work. example, Corresponding Source includes interface definition files associated source files work, source code shared libraries dynamically linked subprograms work specifically designed require, intimate data communication control flow subprograms parts work. Corresponding Source need include anything users can regenerate automatically parts Corresponding Source. Corresponding Source work source code form work.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_2-basic-permissions","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"2. Basic Permissions","title":"GNU General Public License","text":"rights granted License granted term copyright Program, irrevocable provided stated conditions met. License explicitly affirms unlimited permission run unmodified Program. output running covered work covered License output, given content, constitutes covered work. License acknowledges rights fair use equivalent, provided copyright law. may make, run propagate covered works convey, without conditions long license otherwise remains force. may convey covered works others sole purpose make modifications exclusively , provide facilities running works, provided comply terms License conveying material control copyright. thus making running covered works must exclusively behalf, direction control, terms prohibit making copies copyrighted material outside relationship . Conveying circumstances permitted solely conditions stated . Sublicensing allowed; section 10 makes unnecessary.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_3-protecting-users-legal-rights-from-anti-circumvention-law","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"3. Protecting Users’ Legal Rights From Anti-Circumvention Law","title":"GNU General Public License","text":"covered work shall deemed part effective technological measure applicable law fulfilling obligations article 11 WIPO copyright treaty adopted 20 December 1996, similar laws prohibiting restricting circumvention measures. convey covered work, waive legal power forbid circumvention technological measures extent circumvention effected exercising rights License respect covered work, disclaim intention limit operation modification work means enforcing, work’s users, third parties’ legal rights forbid circumvention technological measures.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_4-conveying-verbatim-copies","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"4. Conveying Verbatim Copies","title":"GNU General Public License","text":"may convey verbatim copies Program’s source code receive , medium, provided conspicuously appropriately publish copy appropriate copyright notice; keep intact notices stating License non-permissive terms added accord section 7 apply code; keep intact notices absence warranty; give recipients copy License along Program. may charge price price copy convey, may offer support warranty protection fee.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_5-conveying-modified-source-versions","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"5. Conveying Modified Source Versions","title":"GNU General Public License","text":"may convey work based Program, modifications produce Program, form source code terms section 4, provided also meet conditions: ) work must carry prominent notices stating modified , giving relevant date. b) work must carry prominent notices stating released License conditions added section 7. requirement modifies requirement section 4 “keep intact notices”. c) must license entire work, whole, License anyone comes possession copy. License therefore apply, along applicable section 7 additional terms, whole work, parts, regardless packaged. License gives permission license work way, invalidate permission separately received . d) work interactive user interfaces, must display Appropriate Legal Notices; however, Program interactive interfaces display Appropriate Legal Notices, work need make . compilation covered work separate independent works, nature extensions covered work, combined form larger program, volume storage distribution medium, called “aggregate” compilation resulting copyright used limit access legal rights compilation’s users beyond individual works permit. Inclusion covered work aggregate cause License apply parts aggregate.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_6-conveying-non-source-forms","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"6. Conveying Non-Source Forms","title":"GNU General Public License","text":"may convey covered work object code form terms sections 4 5, provided also convey machine-readable Corresponding Source terms License, one ways: ) Convey object code , embodied , physical product (including physical distribution medium), accompanied Corresponding Source fixed durable physical medium customarily used software interchange. b) Convey object code , embodied , physical product (including physical distribution medium), accompanied written offer, valid least three years valid long offer spare parts customer support product model, give anyone possesses object code either (1) copy Corresponding Source software product covered License, durable physical medium customarily used software interchange, price reasonable cost physically performing conveying source, (2) access copy Corresponding Source network server charge. c) Convey individual copies object code copy written offer provide Corresponding Source. alternative allowed occasionally noncommercially, received object code offer, accord subsection 6b. d) Convey object code offering access designated place (gratis charge), offer equivalent access Corresponding Source way place charge. need require recipients copy Corresponding Source along object code. place copy object code network server, Corresponding Source may different server (operated third party) supports equivalent copying facilities, provided maintain clear directions next object code saying find Corresponding Source. Regardless server hosts Corresponding Source, remain obligated ensure available long needed satisfy requirements. e) Convey object code using peer--peer transmission, provided inform peers object code Corresponding Source work offered general public charge subsection 6d. separable portion object code, whose source code excluded Corresponding Source System Library, need included conveying object code work. “User Product” either (1) “consumer product”, means tangible personal property normally used personal, family, household purposes, (2) anything designed sold incorporation dwelling. determining whether product consumer product, doubtful cases shall resolved favor coverage. particular product received particular user, “normally used” refers typical common use class product, regardless status particular user way particular user actually uses, expects expected use, product. product consumer product regardless whether product substantial commercial, industrial non-consumer uses, unless uses represent significant mode use product. “Installation Information” User Product means methods, procedures, authorization keys, information required install execute modified versions covered work User Product modified version Corresponding Source. information must suffice ensure continued functioning modified object code case prevented interfered solely modification made. convey object code work section , , specifically use , User Product, conveying occurs part transaction right possession use User Product transferred recipient perpetuity fixed term (regardless transaction characterized), Corresponding Source conveyed section must accompanied Installation Information. requirement apply neither third party retains ability install modified object code User Product (example, work installed ROM). requirement provide Installation Information include requirement continue provide support service, warranty, updates work modified installed recipient, User Product modified installed. Access network may denied modification materially adversely affects operation network violates rules protocols communication across network. Corresponding Source conveyed, Installation Information provided, accord section must format publicly documented (implementation available public source code form), must require special password key unpacking, reading copying.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_7-additional-terms","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"7. Additional Terms","title":"GNU General Public License","text":"“Additional permissions” terms supplement terms License making exceptions one conditions. Additional permissions applicable entire Program shall treated though included License, extent valid applicable law. additional permissions apply part Program, part may used separately permissions, entire Program remains governed License without regard additional permissions. convey copy covered work, may option remove additional permissions copy, part . (Additional permissions may written require removal certain cases modify work.) may place additional permissions material, added covered work, can give appropriate copyright permission. Notwithstanding provision License, material add covered work, may (authorized copyright holders material) supplement terms License terms: ) Disclaiming warranty limiting liability differently terms sections 15 16 License; b) Requiring preservation specified reasonable legal notices author attributions material Appropriate Legal Notices displayed works containing ; c) Prohibiting misrepresentation origin material, requiring modified versions material marked reasonable ways different original version; d) Limiting use publicity purposes names licensors authors material; e) Declining grant rights trademark law use trade names, trademarks, service marks; f) Requiring indemnification licensors authors material anyone conveys material (modified versions ) contractual assumptions liability recipient, liability contractual assumptions directly impose licensors authors. non-permissive additional terms considered “restrictions” within meaning section 10. Program received , part , contains notice stating governed License along term restriction, may remove term. license document contains restriction permits relicensing conveying License, may add covered work material governed terms license document, provided restriction survive relicensing conveying. add terms covered work accord section, must place, relevant source files, statement additional terms apply files, notice indicating find applicable terms. Additional terms, permissive non-permissive, may stated form separately written license, stated exceptions; requirements apply either way.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_8-termination","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"8. Termination","title":"GNU General Public License","text":"may propagate modify covered work except expressly provided License. attempt otherwise propagate modify void, automatically terminate rights License (including patent licenses granted third paragraph section 11). However, cease violation License, license particular copyright holder reinstated () provisionally, unless copyright holder explicitly finally terminates license, (b) permanently, copyright holder fails notify violation reasonable means prior 60 days cessation. Moreover, license particular copyright holder reinstated permanently copyright holder notifies violation reasonable means, first time received notice violation License (work) copyright holder, cure violation prior 30 days receipt notice. Termination rights section terminate licenses parties received copies rights License. rights terminated permanently reinstated, qualify receive new licenses material section 10.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_9-acceptance-not-required-for-having-copies","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"9. Acceptance Not Required for Having Copies","title":"GNU General Public License","text":"required accept License order receive run copy Program. Ancillary propagation covered work occurring solely consequence using peer--peer transmission receive copy likewise require acceptance. However, nothing License grants permission propagate modify covered work. actions infringe copyright accept License. Therefore, modifying propagating covered work, indicate acceptance License .","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_10-automatic-licensing-of-downstream-recipients","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"10. Automatic Licensing of Downstream Recipients","title":"GNU General Public License","text":"time convey covered work, recipient automatically receives license original licensors, run, modify propagate work, subject License. responsible enforcing compliance third parties License. “entity transaction” transaction transferring control organization, substantially assets one, subdividing organization, merging organizations. propagation covered work results entity transaction, party transaction receives copy work also receives whatever licenses work party’s predecessor interest give previous paragraph, plus right possession Corresponding Source work predecessor interest, predecessor can get reasonable efforts. may impose restrictions exercise rights granted affirmed License. example, may impose license fee, royalty, charge exercise rights granted License, may initiate litigation (including cross-claim counterclaim lawsuit) alleging patent claim infringed making, using, selling, offering sale, importing Program portion .","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_11-patents","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"11. Patents","title":"GNU General Public License","text":"“contributor” copyright holder authorizes use License Program work Program based. work thus licensed called contributor’s “contributor version”. contributor’s “essential patent claims” patent claims owned controlled contributor, whether already acquired hereafter acquired, infringed manner, permitted License, making, using, selling contributor version, include claims infringed consequence modification contributor version. purposes definition, “control” includes right grant patent sublicenses manner consistent requirements License. contributor grants non-exclusive, worldwide, royalty-free patent license contributor’s essential patent claims, make, use, sell, offer sale, import otherwise run, modify propagate contents contributor version. following three paragraphs, “patent license” express agreement commitment, however denominated, enforce patent (express permission practice patent covenant sue patent infringement). “grant” patent license party means make agreement commitment enforce patent party. convey covered work, knowingly relying patent license, Corresponding Source work available anyone copy, free charge terms License, publicly available network server readily accessible means, must either (1) cause Corresponding Source available, (2) arrange deprive benefit patent license particular work, (3) arrange, manner consistent requirements License, extend patent license downstream recipients. “Knowingly relying” means actual knowledge , patent license, conveying covered work country, recipient’s use covered work country, infringe one identifiable patents country reason believe valid. , pursuant connection single transaction arrangement, convey, propagate procuring conveyance , covered work, grant patent license parties receiving covered work authorizing use, propagate, modify convey specific copy covered work, patent license grant automatically extended recipients covered work works based . patent license “discriminatory” include within scope coverage, prohibits exercise , conditioned non-exercise one rights specifically granted License. may convey covered work party arrangement third party business distributing software, make payment third party based extent activity conveying work, third party grants, parties receive covered work , discriminatory patent license () connection copies covered work conveyed (copies made copies), (b) primarily connection specific products compilations contain covered work, unless entered arrangement, patent license granted, prior 28 March 2007. Nothing License shall construed excluding limiting implied license defenses infringement may otherwise available applicable patent law.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_12-no-surrender-of-others-freedom","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"12. No Surrender of Others’ Freedom","title":"GNU General Public License","text":"conditions imposed (whether court order, agreement otherwise) contradict conditions License, excuse conditions License. convey covered work satisfy simultaneously obligations License pertinent obligations, consequence may convey . example, agree terms obligate collect royalty conveying convey Program, way satisfy terms License refrain entirely conveying Program.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_13-use-with-the-gnu-affero-general-public-license","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"13. Use with the GNU Affero General Public License","title":"GNU General Public License","text":"Notwithstanding provision License, permission link combine covered work work licensed version 3 GNU Affero General Public License single combined work, convey resulting work. terms License continue apply part covered work, special requirements GNU Affero General Public License, section 13, concerning interaction network apply combination .","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_14-revised-versions-of-this-license","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"14. Revised Versions of this License","title":"GNU General Public License","text":"Free Software Foundation may publish revised /new versions GNU General Public License time time. new versions similar spirit present version, may differ detail address new problems concerns. version given distinguishing version number. Program specifies certain numbered version GNU General Public License “later version” applies , option following terms conditions either numbered version later version published Free Software Foundation. Program specify version number GNU General Public License, may choose version ever published Free Software Foundation. Program specifies proxy can decide future versions GNU General Public License can used, proxy’s public statement acceptance version permanently authorizes choose version Program. Later license versions may give additional different permissions. However, additional obligations imposed author copyright holder result choosing follow later version.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_15-disclaimer-of-warranty","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"15. Disclaimer of Warranty","title":"GNU General Public License","text":"WARRANTY PROGRAM, EXTENT PERMITTED APPLICABLE LAW. EXCEPT OTHERWISE STATED WRITING COPYRIGHT HOLDERS /PARTIES PROVIDE PROGRAM “” WITHOUT WARRANTY KIND, EITHER EXPRESSED IMPLIED, INCLUDING, LIMITED , IMPLIED WARRANTIES MERCHANTABILITY FITNESS PARTICULAR PURPOSE. ENTIRE RISK QUALITY PERFORMANCE PROGRAM . PROGRAM PROVE DEFECTIVE, ASSUME COST NECESSARY SERVICING, REPAIR CORRECTION.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_16-limitation-of-liability","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"16. Limitation of Liability","title":"GNU General Public License","text":"EVENT UNLESS REQUIRED APPLICABLE LAW AGREED WRITING COPYRIGHT HOLDER, PARTY MODIFIES /CONVEYS PROGRAM PERMITTED , LIABLE DAMAGES, INCLUDING GENERAL, SPECIAL, INCIDENTAL CONSEQUENTIAL DAMAGES ARISING USE INABILITY USE PROGRAM (INCLUDING LIMITED LOSS DATA DATA RENDERED INACCURATE LOSSES SUSTAINED THIRD PARTIES FAILURE PROGRAM OPERATE PROGRAMS), EVEN HOLDER PARTY ADVISED POSSIBILITY DAMAGES.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_17-interpretation-of-sections-15-and-16","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"17. Interpretation of Sections 15 and 16","title":"GNU General Public License","text":"disclaimer warranty limitation liability provided given local legal effect according terms, reviewing courts shall apply local law closely approximates absolute waiver civil liability connection Program, unless warranty assumption liability accompanies copy Program return fee. END TERMS CONDITIONS","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"how-to-apply-these-terms-to-your-new-programs","dir":"","previous_headings":"","what":"How to Apply These Terms to Your New Programs","title":"GNU General Public License","text":"develop new program, want greatest possible use public, best way achieve make free software everyone can redistribute change terms. , attach following notices program. safest attach start source file effectively state exclusion warranty; file least “copyright” line pointer full notice found. Also add information contact electronic paper mail. program terminal interaction, make output short notice like starts interactive mode: hypothetical commands show w show c show appropriate parts General Public License. course, program’s commands might different; GUI interface, use “box”. also get employer (work programmer) school, , sign “copyright disclaimer” program, necessary. information , apply follow GNU GPL, see . GNU General Public License permit incorporating program proprietary programs. program subroutine library, may consider useful permit linking proprietary applications library. want , use GNU Lesser General Public License instead License. first, please read .","code":" Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. This is free software, and you are welcome to redistribute it under certain conditions; type 'show c' for details."},{"path":[]},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-change","dir":"Articles","previous_headings":"Data setup","what":"Univariate mean change","title":"Comparison with other R packages","text":"","code":"# Univariate mean change set.seed(1) p <- 1 mean_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_1)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-andor-variance-change","dir":"Articles","previous_headings":"Data setup","what":"Univariate mean and/or variance change","title":"Comparison with other R packages","text":"","code":"# Univariate mean and/or variance change set.seed(1) p <- 1 mv_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_1)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-change","dir":"Articles","previous_headings":"Data setup","what":"Multivariate mean change","title":"Comparison with other R packages","text":"","code":"# Multivariate mean change set.seed(1) p <- 3 mean_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_3)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-andor-variance-change","dir":"Articles","previous_headings":"Data setup","what":"Multivariate mean and/or variance change","title":"Comparison with other R packages","text":"","code":"# Multivariate mean and/or variance change set.seed(1) p <- 4 mv_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_3)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"linear-regression","dir":"Articles","previous_headings":"Data setup","what":"Linear regression","title":"Comparison with other R packages","text":"","code":"# Linear regression set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta_0[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta_0[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta_0[3, ] + rnorm(100, 0, 3) ) lm_data <- data.frame(y = y, x = x) plot.ts(lm_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"logistic-regression","dir":"Articles","previous_headings":"Data setup","what":"Logistic regression","title":"Comparison with other R packages","text":"","code":"# Logistic regression set.seed(1) n <- 500 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(300, 1, 1 / (1 + exp(-x[1:300, ] %*% theta[1, ]))), rbinom(200, 1, 1 / (1 + exp(-x[301:n, ] %*% theta[2, ]))) ) binomial_data <- data.frame(y = y, x = x) plot.ts(binomial_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"poisson-regression","dir":"Articles","previous_headings":"Data setup","what":"Poisson regression","title":"Comparison with other R packages","text":"","code":"# Poisson regression set.seed(1) n <- 1100 p <- 3 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) delta <- rnorm(p) theta_0 <- c(1, 0.3, -1) y <- c( rpois(500, exp(x[1:500, ] %*% theta_0)), rpois(300, exp(x[501:800, ] %*% (theta_0 + delta))), rpois(200, exp(x[801:1000, ] %*% theta_0)), rpois(100, exp(x[1001:1100, ] %*% (theta_0 - delta))) ) poisson_data <- data.frame(y = y, x = x) plot.ts(log(poisson_data$y)) plot.ts(poisson_data[, -1])"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"lasso","dir":"Articles","previous_headings":"Data setup","what":"Lasso","title":"Comparison with other R packages","text":"","code":"# Lasso set.seed(1) n <- 480 p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) lasso_data <- data.frame(y = y, x = x) plot.ts(lasso_data[, seq_len(p_true + 1)])"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"ar3","dir":"Articles","previous_headings":"Data setup","what":"AR(3)","title":"Comparison with other R packages","text":"","code":"# AR(3) set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } ar_data <- x[-seq_len(3)] plot.ts(ar_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"garch1-1","dir":"Articles","previous_headings":"Data setup","what":"GARCH(1, 1)","title":"Comparison with other R packages","text":"","code":"# GARCH(1, 1) set.seed(1) n <- 400 sigma_2 <- rep(1, n + 1) x <- rep(0, n + 1) for (i in seq_len(200)) { sigma_2[i + 1] <- 20 + 0.5 * x[i]^2 + 0.1 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } for (i in 201:400) { sigma_2[i + 1] <- 1 + 0.1 * x[i]^2 + 0.5 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } garch_data <- x[-1] plot.ts(garch_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"var2","dir":"Articles","previous_headings":"Data setup","what":"VAR(2)","title":"Comparison with other R packages","text":"","code":"# VAR(2) set.seed(1) n <- 800 p <- 2 theta_1 <- matrix(c(-0.3, 0.6, -0.5, 0.4, 0.2, 0.2, 0.2, -0.2), nrow = p) theta_2 <- matrix(c(0.3, -0.4, 0.1, -0.5, -0.5, -0.2, -0.5, 0.2), nrow = p) x <- matrix(0, n + 2, p) for (i in 1:500) { x[i + 2, ] <- theta_1 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } for (i in 501:n) { x[i + 2, ] <- theta_2 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } var_data <- x[-seq_len(2), ] plot.ts(var_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-change-1","dir":"Articles","previous_headings":"","what":"Univariate mean change","title":"Comparison with other R packages","text":"true change points 300 700. methods plotted due un-retrievable change points.","code":"results[[\"mean_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_1, r.progress = FALSE)@cp_set results[[\"mean_data_1\"]][[\"fastcpd\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_1, G = floor(length(mean_data_1) / 6))$cpts results[[\"mean_data_1\"]][[\"CptNonPar\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ 1, data = data.frame(y = mean_data_1))$breakpoints results[[\"mean_data_1\"]][[\"strucchange\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_1)$estimates results[[\"mean_data_1\"]][[\"ecp\"]] #> [1] 1 301 701 1001 results[[\"mean_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.mean(c(mean_data_1))@cpts results[[\"mean_data_1\"]][[\"changepoint\"]] #> [1] 300 1000 results[[\"mean_data_1\"]][[\"breakfast\"]] <- breakfast::breakfast(mean_data_1)$cptmodel.list[[6]]$cpts results[[\"mean_data_1\"]][[\"breakfast\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"wbs\"]] <- wbs::wbs(mean_data_1)$cpt$cpt.ic$mbic.penalty results[[\"mean_data_1\"]][[\"wbs\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"mosum\"]] <- mosum::mosum(c(mean_data_1), G = 40)$cpts.info$cpts results[[\"mean_data_1\"]][[\"mosum\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"fpop\"]] <- fpop::Fpop(mean_data_1, nrow(mean_data_1))$t.est results[[\"mean_data_1\"]][[\"fpop\"]] #> [1] 300 700 1000 results[[\"mean_data_1\"]][[\"gfpop\"]] <- gfpop::gfpop( data = mean_data_1, mygraph = gfpop::graph( penalty = 2 * log(nrow(mean_data_1)) * gfpop::sdDiff(mean_data_1) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints results[[\"mean_data_1\"]][[\"gfpop\"]] #> [1] 300 700 1000 results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_1), ncol(mean_data_1) ) )$changepoints[, \"location\"] results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_1, K = 2)$bestBkp results[[\"mean_data_1\"]][[\"jointseg\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mean_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mean_data_1\"]][[\"Rbeast\"]] #> [1] 701 301 NaN NaN NaN NaN NaN NaN NaN NaN results[[\"mean_data_1\"]][[\"stepR\"]] <- stepR::stepFit(mean_data_1, alpha = 0.5)$rightEnd results[[\"mean_data_1\"]][[\"stepR\"]] #> [1] 300 700 1000 results[[\"mean_data_1\"]][[\"cpm\"]] <- cpm::processStream(mean_data_1, cpmType = \"Student\")$changePoints results[[\"mean_data_1\"]][[\"cpm\"]] #> [1] 299 699 results[[\"mean_data_1\"]][[\"segmented\"]] <- segmented::stepmented( as.numeric(mean_data_1), npsi = 2 )$psi[, \"Est.\"] results[[\"mean_data_1\"]][[\"segmented\"]] #> psi1.index psi2.index #> 298.1981 699.1524 results[[\"mean_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1), data = data.frame(y = mean_data_1, x = seq_len(nrow(mean_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"mcp\"]]) } results[[\"mean_data_1\"]][[\"not\"]] <- not::not(mean_data_1, contrast = \"pcwsConstMean\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"not\"]]) } results[[\"mean_data_1\"]][[\"bcp\"]] <- bcp::bcp(mean_data_1) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"bcp\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-andor-variance-change-1","dir":"Articles","previous_headings":"","what":"Univariate mean and/or variance change","title":"Comparison with other R packages","text":"true change points 300, 700, 1000, 1300 1700. methods plotted due un-retrievable change points.","code":"results[[\"mv_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_1, r.progress = FALSE)@cp_set results[[\"mv_data_1\"]][[\"fastcpd\"]] #> [1] 300 700 1001 1300 1700 results[[\"mv_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_1)$estimates results[[\"mv_data_1\"]][[\"ecp\"]] #> [1] 1 301 701 1001 1301 1701 2001 results[[\"mv_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.meanvar(c(mv_data_1))@cpts results[[\"mv_data_1\"]][[\"changepoint\"]] #> [1] 300 2000 results[[\"mv_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mv_data_1, G = floor(length(mv_data_1) / 6))$cpts results[[\"mv_data_1\"]][[\"CptNonPar\"]] #> [1] 333 700 1300 results[[\"mv_data_1\"]][[\"cpm\"]] <- cpm::processStream(mv_data_1, cpmType = \"GLR\")$changePoints results[[\"mv_data_1\"]][[\"cpm\"]] #> [1] 293 300 403 408 618 621 696 1000 1021 1024 1293 1300 1417 1693 1700 #> [16] 1981 results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_1), ncol(mv_data_1) ) )$changepoints[, \"location\"] results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] #> [1] 300 700 701 702 704 707 708 712 715 716 717 718 721 722 723 #> [16] 726 727 729 731 732 734 736 740 742 744 746 748 750 753 755 #> [31] 756 757 759 760 762 764 765 766 768 769 771 772 774 776 777 #> [46] 784 785 786 789 791 792 794 797 798 799 801 802 803 807 809 #> [61] 810 813 815 817 819 826 827 828 829 831 833 835 836 837 838 #> [76] 840 841 842 843 845 848 849 852 854 860 862 864 866 868 870 #> [91] 872 875 879 881 884 886 887 888 889 896 897 898 899 901 903 #> [106] 904 905 906 909 910 912 913 915 917 919 921 922 923 925 927 #> [121] 928 932 934 936 937 940 944 945 947 948 949 951 956 958 959 #> [136] 961 962 963 964 966 967 968 972 974 976 978 979 986 988 990 #> [151] 992 995 998 1000 1300 1700 1702 1703 1704 1705 1708 1710 1712 1714 1716 #> [166] 1717 1718 1720 1721 1723 1725 1726 1727 1729 1731 1733 1735 1736 1737 1739 #> [181] 1742 1745 1747 1748 1752 1754 1756 1758 1759 1760 1766 1768 1770 1771 1773 #> [196] 1775 1778 1782 1784 1785 1790 1792 1793 1795 1796 1797 1799 1800 1802 1803 #> [211] 1804 1805 1806 1807 1808 1809 1813 1815 1816 1818 1821 1824 1825 1827 1828 #> [226] 1829 1833 1835 1837 1840 1841 1842 1848 1849 1851 1852 1854 1855 1857 1859 #> [241] 1860 1862 1863 1865 1867 1868 1876 1878 1879 1880 1882 1883 1884 1886 1887 #> [256] 1889 1894 1898 1899 1905 1906 1907 1908 1909 1912 1919 1920 1921 1923 1924 #> [271] 1926 1927 1928 1930 1933 1934 1935 1936 1938 1940 1941 1944 1947 1950 1952 #> [286] 1954 1955 1956 1960 1962 1963 1965 1966 1967 1969 1970 1974 1976 1977 1978 #> [301] 1980 1985 1987 1988 1990 1996 1997 1998 results[[\"mv_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mv_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mv_data_1\"]][[\"Rbeast\"]] #> [1] 1855 1794 1301 1986 301 703 1981 1769 1860 709 results[[\"mv_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1, ~ 1, ~ 1, ~ 1), data = data.frame(y = mv_data_1, x = seq_len(nrow(mv_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"mcp\"]]) } results[[\"mv_data_1\"]][[\"not\"]] <- not::not(mv_data_1, contrast = \"pcwsConstMeanVar\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"not\"]]) } #> Press [enter] to continue"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-change-1","dir":"Articles","previous_headings":"","what":"Multivariate mean change","title":"Comparison with other R packages","text":"true change points 300 700. methods plotted due un-retrievable change points.","code":"results[[\"mean_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_3, r.progress = FALSE)@cp_set results[[\"mean_data_3\"]][[\"fastcpd\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_3, G = floor(nrow(mean_data_3) / 6))$cpts results[[\"mean_data_3\"]][[\"CptNonPar\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_3), ncol(mean_data_3) ) )$changepoints[, \"location\"] results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_3, K = 2)$bestBkp results[[\"mean_data_3\"]][[\"jointseg\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mean_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mean_data_3\"]][[\"Rbeast\"]] #> [,1] [,2] [,3] #> [1,] 301 701 301 #> [2,] 701 301 701 #> [3,] 142 117 926 #> [4,] 305 NaN NaN #> [5,] 705 NaN NaN #> [6,] 694 NaN NaN #> [7,] NaN NaN NaN #> [8,] NaN NaN NaN #> [9,] NaN NaN NaN #> [10,] NaN NaN NaN results[[\"mean_data_3\"]][[\"strucchange\"]] <- strucchange::breakpoints( cbind(y.1, y.2, y.3) ~ 1, data = data.frame(y = mean_data_3) )$breakpoints results[[\"mean_data_3\"]][[\"strucchange\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_3)$estimates results[[\"mean_data_3\"]][[\"ecp\"]] #> [1] 1 301 701 1001 results[[\"mean_data_3\"]][[\"bcp\"]] <- bcp::bcp(mean_data_3) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_3\"]][[\"bcp\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-andor-variance-change-1","dir":"Articles","previous_headings":"","what":"Multivariate mean and/or variance change","title":"Comparison with other R packages","text":"true change points 300, 700, 1000, 1300 1700. methods plotted due un-retrievable change points.","code":"results[[\"mv_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_3, r.progress = FALSE)@cp_set results[[\"mv_data_3\"]][[\"fastcpd\"]] #> [1] 300 700 1000 1300 1700 results[[\"mv_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_3)$estimates results[[\"mv_data_3\"]][[\"ecp\"]] #> [1] 1 301 701 1001 1301 1701 2001 results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_3), ncol(mv_data_3) ) )$changepoints[, \"location\"] results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] #> [1] 300 700 701 703 705 707 708 709 711 712 714 715 717 718 720 #> [16] 721 723 724 726 727 729 731 733 734 736 737 739 740 742 743 #> [31] 744 746 747 749 750 752 753 754 755 756 758 760 762 763 765 #> [46] 766 767 769 770 772 773 774 775 777 779 780 782 784 786 788 #> [61] 790 791 793 795 797 799 801 803 804 806 809 810 811 813 814 #> [76] 816 817 818 820 821 823 825 827 828 830 831 833 835 836 837 #> [91] 838 840 842 843 845 846 848 849 850 852 853 854 855 856 858 #> [106] 859 860 862 863 865 866 868 869 871 872 874 876 877 878 879 #> [121] 881 883 885 887 888 889 891 893 894 895 897 898 900 901 903 #> [136] 904 906 908 909 911 913 914 916 917 918 920 921 923 924 925 #> [151] 927 928 929 931 932 934 936 937 938 939 941 942 943 945 946 #> [166] 947 949 950 952 954 955 956 957 958 959 961 962 964 965 967 #> [181] 968 970 972 973 974 975 977 979 981 982 984 985 986 987 988 #> [196] 990 991 992 994 995 997 999 1000 1300 1700 1702 1703 1704 1705 1706 #> [211] 1708 1709 1710 1712 1713 1714 1715 1717 1719 1721 1722 1723 1725 1727 1729 #> [226] 1730 1732 1734 1735 1737 1738 1739 1741 1742 1744 1746 1748 1750 1752 1753 #> [241] 1754 1755 1757 1758 1759 1761 1762 1763 1764 1766 1767 1769 1770 1771 1773 #> [256] 1774 1775 1777 1779 1781 1782 1783 1785 1786 1788 1789 1791 1793 1794 1796 #> [271] 1798 1800 1803 1804 1805 1806 1808 1809 1811 1812 1814 1815 1817 1818 1819 #> [286] 1821 1822 1824 1825 1827 1828 1829 1831 1833 1835 1836 1838 1839 1841 1843 #> [301] 1844 1846 1847 1848 1850 1851 1853 1854 1856 1857 1858 1859 1860 1862 1863 #> [316] 1864 1865 1867 1869 1870 1872 1873 1874 1876 1878 1879 1881 1882 1884 1885 #> [331] 1887 1889 1891 1893 1894 1896 1898 1899 1900 1901 1902 1904 1906 1907 1909 #> [346] 1911 1913 1914 1916 1917 1918 1919 1921 1923 1924 1925 1927 1928 1930 1932 #> [361] 1933 1935 1936 1938 1939 1941 1942 1944 1946 1948 1950 1951 1952 1954 1956 #> [376] 1957 1959 1961 1963 1965 1967 1968 1970 1972 1973 1974 1976 1977 1979 1981 #> [391] 1982 1984 1985 1987 1989 1990 1992 1993 1995 1996 1998 results[[\"mv_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mv_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mv_data_3\"]][[\"Rbeast\"]] #> [,1] [,2] [,3] [,4] #> [1,] 701 1301 301 1301 #> [2,] 1301 301 1301 710 #> [3,] 301 701 1829 301 #> [4,] 1968 1993 702 886 #> [5,] 1994 884 1822 1975 #> [6,] 814 755 810 1915 #> [7,] 1962 781 845 778 #> [8,] 1978 767 1738 1985 #> [9,] 1870 747 1754 792 #> [10,] 1843 722 771 953"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"linear-regression-1","dir":"Articles","previous_headings":"","what":"Linear regression","title":"Comparison with other R packages","text":"true change points 100 200.","code":"results[[\"lm_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lm(lm_data, r.progress = FALSE)@cp_set results[[\"lm_data\"]][[\"fastcpd\"]] #> [1] 97 201 results[[\"lm_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lm_data)$breakpoints results[[\"lm_data\"]][[\"strucchange\"]] #> [1] 100 201 results[[\"lm_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ . - 1, data.frame(y = lm_data$y, x = lm_data[, -1], index = seq_len(nrow(lm_data))) ), seg.Z = ~ index )$psi[, \"Est.\"] results[[\"lm_data\"]][[\"segmented\"]] #> [1] 233"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"logistic-regression-1","dir":"Articles","previous_headings":"","what":"Logistic regression","title":"Comparison with other R packages","text":"true change point 300.","code":"results[[\"binomial_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.binomial(binomial_data, r.progress = FALSE)@cp_set results[[\"binomial_data\"]][[\"fastcpd\"]] #> [1] 302 results[[\"binomial_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = binomial_data)$breakpoints results[[\"binomial_data\"]][[\"strucchange\"]] #> [1] 297"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"poisson-regression-1","dir":"Articles","previous_headings":"","what":"Poisson regression","title":"Comparison with other R packages","text":"true change points 500, 800 1000.","code":"results[[\"poisson_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.poisson(poisson_data, r.progress = FALSE)@cp_set results[[\"poisson_data\"]][[\"fastcpd\"]] #> [1] 498 805 1003 results[[\"poisson_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = poisson_data)$breakpoints results[[\"poisson_data\"]][[\"strucchange\"]] #> [1] 935"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"lasso-1","dir":"Articles","previous_headings":"","what":"Lasso","title":"Comparison with other R packages","text":"true change points 80, 200 320.","code":"results[[\"lasso_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lasso(lasso_data, r.progress = FALSE)@cp_set results[[\"lasso_data\"]][[\"fastcpd\"]] #> [1] 79 199 321 results[[\"lasso_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lasso_data)$breakpoints results[[\"lasso_data\"]][[\"strucchange\"]] #> [1] 80 200 321"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"ar3-1","dir":"Articles","previous_headings":"","what":"AR(3)","title":"Comparison with other R packages","text":"true change point 600. methods plotted due un-retrievable change points.","code":"results[[\"ar_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.ar(ar_data, 3, r.progress = FALSE)@cp_set results[[\"ar_data\"]][[\"fastcpd\"]] #> [1] 614 results[[\"ar_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(ar_data, G = floor(length(ar_data) / 6))$cpts results[[\"ar_data\"]][[\"CptNonPar\"]] #> numeric(0) results[[\"ar_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ x + 1, data.frame(y = ar_data, x = seq_along(ar_data)) ), seg.Z = ~ x )$psi[, \"Est.\"] results[[\"ar_data\"]][[\"segmented\"]] #> [1] 690 results[[\"ar_data\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1 + ar(3), ~ 0 + ar(3)), data = data.frame(y = ar_data, x = seq_along(ar_data)), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"ar_data\"]][[\"mcp\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"garch1-1-1","dir":"Articles","previous_headings":"","what":"GARCH(1, 1)","title":"Comparison with other R packages","text":"true change point 200.","code":"results[[\"garch_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.garch(garch_data, c(1, 1), r.progress = FALSE)@cp_set results[[\"garch_data\"]][[\"fastcpd\"]] #> [1] 205 results[[\"garch_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(garch_data, G = floor(length(garch_data) / 6))$cpts results[[\"garch_data\"]][[\"CptNonPar\"]] #> [1] 206 results[[\"garch_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(x ~ 1, data = data.frame(x = garch_data))$breakpoints results[[\"garch_data\"]][[\"strucchange\"]] #> [1] NA"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"var2-1","dir":"Articles","previous_headings":"","what":"VAR(2)","title":"Comparison with other R packages","text":"true change points 500.","code":"results[[\"var_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.var(var_data, 2, r.progress = FALSE)@cp_set results[[\"var_data\"]][[\"fastcpd\"]] #> [1] 500 results[[\"var_data\"]][[\"VARDetect\"]] <- VARDetect::tbss(var_data)$cp results[[\"var_data\"]][[\"VARDetect\"]] #> [1] 501"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"detection-comparison-using-well_log","dir":"Articles","previous_headings":"","what":"Detection comparison using well_log","title":"Comparison with other R packages","text":"","code":"well_log <- fastcpd::well_log well_log <- well_log[well_log > 1e5] results[[\"well_log\"]] <- list( fastcpd = fastcpd.mean(well_log, trim = 0.003)@cp_set, changepoint = changepoint::cpt.mean(well_log)@cpts, CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6))$cpts, strucchange = strucchange::breakpoints( y ~ 1, data = data.frame(y = well_log) )$breakpoints, ecp = ecp::e.divisive(matrix(well_log))$estimates, breakfast = breakfast::breakfast(well_log)$cptmodel.list[[6]]$cpts, wbs = wbs::wbs(well_log)$cpt$cpt.ic$mbic.penalty, mosum = mosum::mosum(c(well_log), G = 40)$cpts.info$cpts, # fpop = fpop::Fpop(well_log, length(well_log))$t.est, # meaningless gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints, InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) )$changepoints[, \"location\"], jointseg = jointseg::jointSeg(well_log, K = 12)$bestBkp, Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp, stepR = stepR::stepFit(well_log, alpha = 0.5)$rightEnd ) results[[\"well_log\"]] #> $fastcpd #> [1] 12 572 704 776 1021 1057 1198 1347 1406 1502 1660 1842 2023 2202 2384 #> [16] 2445 2507 2567 2749 2926 3094 3107 3509 3622 3709 3820 3976 #> #> $changepoint #> [1] 2738 3989 #> #> $CptNonPar #> [1] 1021 1681 2022 2738 #> #> $strucchange #> [1] 1057 1660 2568 3283 #> #> $ecp #> [1] 1 33 315 435 567 705 803 1026 1058 1348 1503 1662 1843 2024 2203 #> [16] 2386 2446 2508 2569 2745 2780 2922 3073 3136 3252 3465 3500 3554 3623 3710 #> [31] 3821 3868 3990 #> #> $breakfast #> [1] 33 310 434 572 704 779 1021 1057 1347 1502 1659 1842 2021 2032 2202 #> [16] 2384 2446 2507 2567 2747 2779 2926 3094 3106 3125 3283 3464 3499 3622 3709 #> [31] 3806 3835 3848 3877 3896 3976 #> #> $wbs #> [1] 2568 1057 1661 1842 2385 2023 2445 1502 2744 6 2507 1021 3709 3820 1402 #> [16] 434 1408 3131 3976 3509 3622 776 1197 3094 704 3104 1347 314 2921 3251 #> [31] 3464 3848 3906 2779 1663 3636 60 3904 2202 566 12 3639 1200 7 1671 #> [46] 706 #> #> $mosum #> [1] 6 434 704 1017 1057 1325 1502 1661 1842 2023 2385 2445 2507 2567 2744 #> [16] 3060 3438 3509 3610 3697 3820 3867 3976 #> #> $gfpop #> [1] 6 7 8 12 314 434 556 560 704 776 1021 1057 1197 1200 1347 #> [16] 1364 1405 1407 1491 1502 1661 1842 2023 2385 2445 2507 2567 2664 2747 2752 #> [31] 2921 3094 3104 3125 3251 3464 3499 3622 3709 3820 3976 3989 #> #> $InspectChangepoint #> [1] 6 8 15 71 314 434 521 704 706 776 830 1021 1057 1059 1347 #> [16] 1402 1405 1408 1412 1502 1520 1659 1661 1663 1694 1842 1848 2022 2202 2384 #> [31] 2387 2445 2507 2567 2568 2673 2738 2746 2752 2779 2921 3131 3251 3464 3509 #> [46] 3609 3658 3709 3806 3976 #> #> $jointseg #> [1] 6 1021 1057 1502 1661 1842 2022 2384 2445 2507 2568 2738 #> #> $Rbeast #> [1] 1058 1662 7 1022 2447 2386 2023 1503 2745 3710 #> #> $stepR #> [1] 7 14 314 434 566 704 776 1021 1057 1197 1200 1347 1405 1407 1502 #> [16] 1661 1694 1842 2023 2202 2385 2445 2507 2567 2747 2752 2921 3094 3104 3125 #> [31] 3251 3464 3499 3609 3658 3709 3820 3867 3905 3976 3989 package_list <- sort(names(results[[\"well_log\"]]), decreasing = TRUE) comparison_table <- NULL for (package_index in seq_along(package_list)) { package <- package_list[[package_index]] comparison_table <- rbind( comparison_table, data.frame( change_point = results[[\"well_log\"]][[package]], package = package, y_offset = (package_index - 1) * 1000 ) ) } most_selected <- sort(table(comparison_table$change_point), decreasing = TRUE) most_selected <- sort(as.numeric(names(most_selected[most_selected >= 4]))) for (i in seq_len(length(most_selected) - 1)) { if (most_selected[i + 1] - most_selected[i] < 2) { most_selected[i] <- NA most_selected[i + 1] <- most_selected[i + 1] - 0.5 } } (most_selected <- most_selected[!is.na(most_selected)]) #> [1] 6.5 314.0 434.0 704.0 776.0 1021.0 1057.0 1347.0 1502.0 1661.0 #> [11] 1842.0 2023.0 2202.0 2384.5 2445.0 2507.0 2567.5 2738.0 2921.0 3094.0 #> [21] 3251.0 3464.0 3509.0 3622.0 3709.0 3820.0 3976.0 if (requireNamespace(\"ggplot2\", quietly = TRUE)) { ggplot2::ggplot() + ggplot2::geom_point( data = data.frame(x = seq_along(well_log), y = c(well_log)), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_vline( xintercept = most_selected, color = \"black\", linetype = \"dashed\", alpha = 0.2 ) + ggplot2::geom_point( data = comparison_table, ggplot2::aes(x = change_point, y = 50000 + y_offset, color = package), shape = 17, size = 1.9 ) + ggplot2::geom_hline( data = comparison_table, ggplot2::aes(yintercept = 50000 + y_offset, color = package), linetype = \"dashed\", alpha = 0.1 ) + ggplot2::coord_cartesian( ylim = c(50000 - 500, max(well_log) + 1000), xlim = c(-200, length(well_log) + 200), expand = FALSE ) + ggplot2::theme( panel.background = ggplot2::element_blank(), panel.border = ggplot2::element_rect(colour = \"black\", fill = NA), panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank() ) + ggplot2::xlab(NULL) + ggplot2::ylab(NULL) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"time-comparison-using-well_log","dir":"Articles","previous_headings":"","what":"Time comparison using well_log","title":"Comparison with other R packages","text":"","code":"results[[\"microbenchmark\"]] <- microbenchmark::microbenchmark( fastcpd = fastcpd::fastcpd.mean(well_log, trim = 0.003, r.progress = FALSE), changepoint = changepoint::cpt.mean(well_log, method = \"PELT\"), CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6)), strucchange = strucchange::breakpoints(y ~ 1, data = data.frame(y = well_log)), ecp = ecp::e.divisive(matrix(well_log)), breakfast = breakfast::breakfast(well_log), wbs = wbs::wbs(well_log), mosum = mosum::mosum(c(well_log), G = 40), fpop = fpop::Fpop(well_log, nrow(well_log)), gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" ), InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) ), jointseg = jointseg::jointSeg(well_log, K = 12), Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE ), stepR = stepR::stepFit(well_log, alpha = 0.5), not = not::not(well_log, contrast = \"pcwsConstMean\"), times = 10 ) results[[\"microbenchmark\"]] #> Unit: milliseconds #> expr min lq mean median #> fastcpd 6.257120e+01 6.696175e+01 7.183964e+01 7.168669e+01 #> changepoint 3.205076e+01 3.305076e+01 4.025595e+01 3.774400e+01 #> CptNonPar 1.875995e+04 2.014073e+04 2.244910e+04 2.224506e+04 #> strucchange 6.359889e+04 6.409690e+04 6.651444e+04 6.565439e+04 #> ecp 7.505232e+05 7.895889e+05 8.168647e+05 8.170952e+05 #> breakfast 9.606171e+03 9.819381e+03 1.052611e+04 1.048197e+04 #> wbs 1.163237e+02 1.180537e+02 1.263443e+02 1.254133e+02 #> mosum 1.059973e+00 1.147898e+00 2.175931e+00 1.409396e+00 #> fpop 2.604002e+00 3.676689e+00 4.859312e+00 4.308841e+00 #> gfpop 6.057383e+01 6.595090e+01 7.277643e+01 7.212125e+01 #> InspectChangepoint 1.584727e+02 2.171007e+02 2.414921e+02 2.424665e+02 #> jointseg 2.104403e+01 2.284920e+01 3.577861e+01 2.708083e+01 #> Rbeast 6.545716e+02 6.913542e+02 7.993305e+02 7.504324e+02 #> stepR 2.740003e+01 3.020269e+01 3.149624e+01 3.188329e+01 #> not 9.715867e+01 1.000287e+02 1.101258e+02 1.074475e+02 #> uq max neval #> 7.689381e+01 8.332375e+01 10 #> 4.669485e+01 5.904173e+01 10 #> 2.380236e+04 2.705807e+04 10 #> 6.597650e+04 7.757488e+04 10 #> 8.259550e+05 8.834288e+05 10 #> 1.103289e+04 1.197235e+04 10 #> 1.276892e+02 1.455261e+02 10 #> 1.957312e+00 8.705062e+00 10 #> 5.107175e+00 1.085365e+01 10 #> 7.742432e+01 8.794816e+01 10 #> 3.060519e+02 3.145695e+02 10 #> 4.362760e+01 9.516090e+01 10 #> 8.539695e+02 1.163607e+03 10 #> 3.294534e+01 3.530981e+01 10 #> 1.147504e+02 1.331989e+02 10 if (requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"microbenchmark\", quietly = TRUE)) { ggplot2::autoplot(results[[\"microbenchmark\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Comparison with other R packages","text":"document generated following code:","code":"R -e 'knitr::knit(\"vignettes/comparison-packages.Rmd.original\", output = \"vignettes/comparison-packages.Rmd\")' && rm -rf vignettes/figure && mv -f figure vignettes"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"related-issues","dir":"Articles","previous_headings":"","what":"Related issues","title":"Comparison with other R packages","text":"mosum: [#4]. mcp: link 1 link 2. bcp: [#5]. gfpop: [#10].","code":""},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"acknowledgements","dir":"Articles","previous_headings":"","what":"Acknowledgements","title":"Comparison with other R packages","text":"Dr. Vito Muggeo, author segmented package tips piece-wise constant function.","code":""},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Comparison with other R packages","text":"","code":"knitr::opts_chunk$set( collapse = TRUE, comment = \"#>\", eval = TRUE, cache = FALSE, warning = FALSE, fig.width = 8, fig.height = 5 ) if (requireNamespace(\"microbenchmark\", quietly = TRUE)) { library(microbenchmark) } if (file.exists(\"comparison-packages-results.RData\")) { # Available at https://pcloud.xingchi.li/comparison-packages-results.RData load(\"comparison-packages-results.RData\") } else { results <- list() } # Univariate mean change set.seed(1) p <- 1 mean_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_1) # Univariate mean and/or variance change set.seed(1) p <- 1 mv_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_1) # Multivariate mean change set.seed(1) p <- 3 mean_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_3) # Multivariate mean and/or variance change set.seed(1) p <- 4 mv_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_3) # Linear regression set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta_0[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta_0[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta_0[3, ] + rnorm(100, 0, 3) ) lm_data <- data.frame(y = y, x = x) plot.ts(lm_data) # Logistic regression set.seed(1) n <- 500 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(300, 1, 1 / (1 + exp(-x[1:300, ] %*% theta[1, ]))), rbinom(200, 1, 1 / (1 + exp(-x[301:n, ] %*% theta[2, ]))) ) binomial_data <- data.frame(y = y, x = x) plot.ts(binomial_data) # Poisson regression set.seed(1) n <- 1100 p <- 3 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) delta <- rnorm(p) theta_0 <- c(1, 0.3, -1) y <- c( rpois(500, exp(x[1:500, ] %*% theta_0)), rpois(300, exp(x[501:800, ] %*% (theta_0 + delta))), rpois(200, exp(x[801:1000, ] %*% theta_0)), rpois(100, exp(x[1001:1100, ] %*% (theta_0 - delta))) ) poisson_data <- data.frame(y = y, x = x) plot.ts(log(poisson_data$y)) plot.ts(poisson_data[, -1]) # Lasso set.seed(1) n <- 480 p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) lasso_data <- data.frame(y = y, x = x) plot.ts(lasso_data[, seq_len(p_true + 1)]) # AR(3) set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } ar_data <- x[-seq_len(3)] plot.ts(ar_data) # GARCH(1, 1) set.seed(1) n <- 400 sigma_2 <- rep(1, n + 1) x <- rep(0, n + 1) for (i in seq_len(200)) { sigma_2[i + 1] <- 20 + 0.5 * x[i]^2 + 0.1 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } for (i in 201:400) { sigma_2[i + 1] <- 1 + 0.1 * x[i]^2 + 0.5 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } garch_data <- x[-1] plot.ts(garch_data) # VAR(2) set.seed(1) n <- 800 p <- 2 theta_1 <- matrix(c(-0.3, 0.6, -0.5, 0.4, 0.2, 0.2, 0.2, -0.2), nrow = p) theta_2 <- matrix(c(0.3, -0.4, 0.1, -0.5, -0.5, -0.2, -0.5, 0.2), nrow = p) x <- matrix(0, n + 2, p) for (i in 1:500) { x[i + 2, ] <- theta_1 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } for (i in 501:n) { x[i + 2, ] <- theta_2 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } var_data <- x[-seq_len(2), ] plot.ts(var_data) results[[\"mean_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_1, r.progress = FALSE)@cp_set results[[\"mean_data_1\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"fastcpd\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_1, G = floor(length(mean_data_1) / 6))$cpts results[[\"mean_data_1\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"CptNonPar\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ 1, data = data.frame(y = mean_data_1))$breakpoints results[[\"mean_data_1\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"strucchange\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_1)$estimates results[[\"mean_data_1\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"ecp\"]], c(1, 301, 701, 1001), tolerance = 0.2) results[[\"mean_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.mean(c(mean_data_1))@cpts results[[\"mean_data_1\"]][[\"changepoint\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"changepoint\"]], c(300, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"breakfast\"]] <- breakfast::breakfast(mean_data_1)$cptmodel.list[[6]]$cpts results[[\"mean_data_1\"]][[\"breakfast\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"breakfast\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"wbs\"]] <- wbs::wbs(mean_data_1)$cpt$cpt.ic$mbic.penalty results[[\"mean_data_1\"]][[\"wbs\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"wbs\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"mosum\"]] <- mosum::mosum(c(mean_data_1), G = 40)$cpts.info$cpts results[[\"mean_data_1\"]][[\"mosum\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"mosum\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"fpop\"]] <- fpop::Fpop(mean_data_1, nrow(mean_data_1))$t.est results[[\"mean_data_1\"]][[\"fpop\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"fpop\"]], c(300, 700, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"gfpop\"]] <- gfpop::gfpop( data = mean_data_1, mygraph = gfpop::graph( penalty = 2 * log(nrow(mean_data_1)) * gfpop::sdDiff(mean_data_1) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints results[[\"mean_data_1\"]][[\"gfpop\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"gfpop\"]], c(300, 700, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_1), ncol(mean_data_1) ) )$changepoints[, \"location\"] results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"InspectChangepoint\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_1, K = 2)$bestBkp results[[\"mean_data_1\"]][[\"jointseg\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"jointseg\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mean_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mean_data_1\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"Rbeast\"]], c(701, 301, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN), tolerance = 0.2) results[[\"mean_data_1\"]][[\"stepR\"]] <- stepR::stepFit(mean_data_1, alpha = 0.5)$rightEnd results[[\"mean_data_1\"]][[\"stepR\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"stepR\"]], c(300, 700, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"cpm\"]] <- cpm::processStream(mean_data_1, cpmType = \"Student\")$changePoints results[[\"mean_data_1\"]][[\"cpm\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"cpm\"]], c(299, 699), tolerance = 0.2) results[[\"mean_data_1\"]][[\"segmented\"]] <- segmented::stepmented( as.numeric(mean_data_1), npsi = 2 )$psi[, \"Est.\"] results[[\"mean_data_1\"]][[\"segmented\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"segmented\"]], c(298, 699), ignore_attr = TRUE, tolerance = 0.2) results[[\"mean_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1), data = data.frame(y = mean_data_1, x = seq_len(nrow(mean_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"mcp\"]]) } results[[\"mean_data_1\"]][[\"not\"]] <- not::not(mean_data_1, contrast = \"pcwsConstMean\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"not\"]]) } results[[\"mean_data_1\"]][[\"bcp\"]] <- bcp::bcp(mean_data_1) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"bcp\"]]) } results[[\"mv_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_1, r.progress = FALSE)@cp_set results[[\"mv_data_1\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"fastcpd\"]], c(300, 700, 1001, 1300, 1700), tolerance = 0.2) results[[\"mv_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_1)$estimates results[[\"mv_data_1\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"ecp\"]], c(1, 301, 701, 1001, 1301, 1701, 2001), tolerance = 0.2) results[[\"mv_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.meanvar(c(mv_data_1))@cpts results[[\"mv_data_1\"]][[\"changepoint\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"changepoint\"]], c(300, 2000), tolerance = 0.2) results[[\"mv_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mv_data_1, G = floor(length(mv_data_1) / 6))$cpts results[[\"mv_data_1\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"CptNonPar\"]], c(333, 700, 1300), tolerance = 0.2) results[[\"mv_data_1\"]][[\"cpm\"]] <- cpm::processStream(mv_data_1, cpmType = \"GLR\")$changePoints results[[\"mv_data_1\"]][[\"cpm\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"cpm\"]], c(293, 300, 403, 408, 618, 621, 696, 1000, 1021, 1024, 1293, 1300, 1417, 1693, 1700, 1981), tolerance = 0.2) results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_1), ncol(mv_data_1) ) )$changepoints[, \"location\"] results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"InspectChangepoint\"]], c( 300, 700, 701, 702, 704, 707, 708, 712, 715, 716, 717, 718, 721, 722, 723, 726, 727, 729, 731, 732, 734, 736, 740, 742, 744, 746, 748, 750, 753, 755, 756, 757, 759, 760, 762, 764, 765, 766, 768, 769, 771, 772, 774, 776, 777, 784, 785, 786, 789, 791, 792, 794, 797, 798, 799, 801, 802, 803, 807, 809, 810, 813, 815, 817, 819, 826, 827, 828, 829, 831, 833, 835, 836, 837, 838, 840, 841, 842, 843, 845, 848, 849, 852, 854, 860, 862, 864, 866, 868, 870, 872, 875, 879, 881, 884, 886, 887, 888, 889, 896, 897, 898, 899, 901, 903, 904, 905, 906, 909, 910, 912, 913, 915, 917, 919, 921, 922, 923, 925, 927, 928, 932, 934, 936, 937, 940, 944, 945, 947, 948, 949, 951, 956, 958, 959, 961, 962, 963, 964, 966, 967, 968, 972, 974, 976, 978, 979, 986, 988, 990, 992, 995, 998, 1000, 1300, 1700, 1702, 1703, 1704, 1705, 1708, 1710, 1712, 1714, 1716, 1717, 1718, 1720, 1721, 1723, 1725, 1726, 1727, 1729, 1731, 1733, 1735, 1736, 1737, 1739, 1742, 1745, 1747, 1748, 1752, 1754, 1756, 1758, 1759, 1760, 1766, 1768, 1770, 1771, 1773, 1775, 1778, 1782, 1784, 1785, 1790, 1792, 1793, 1795, 1796, 1797, 1799, 1800, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1813, 1815, 1816, 1818, 1821, 1824, 1825, 1827, 1828, 1829, 1833, 1835, 1837, 1840, 1841, 1842, 1848, 1849, 1851, 1852, 1854, 1855, 1857, 1859, 1860, 1862, 1863, 1865, 1867, 1868, 1876, 1878, 1879, 1880, 1882, 1883, 1884, 1886, 1887, 1889, 1894, 1898, 1899, 1905, 1906, 1907, 1908, 1909, 1912, 1919, 1920, 1921, 1923, 1924, 1926, 1927, 1928, 1930, 1933, 1934, 1935, 1936, 1938, 1940, 1941, 1944, 1947, 1950, 1952, 1954, 1955, 1956, 1960, 1962, 1963, 1965, 1966, 1967, 1969, 1970, 1974, 1976, 1977, 1978, 1980, 1985, 1987, 1988, 1990, 1996, 1997, 1998 ), tolerance = 0.2) results[[\"mv_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mv_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mv_data_1\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"Rbeast\"]], c(1855, 1794, 1301, 1986, 301, 703, 1981, 1769, 1860, 709), tolerance = 0.2) results[[\"mv_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1, ~ 1, ~ 1, ~ 1), data = data.frame(y = mv_data_1, x = seq_len(nrow(mv_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"mcp\"]]) } results[[\"mv_data_1\"]][[\"not\"]] <- not::not(mv_data_1, contrast = \"pcwsConstMeanVar\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"not\"]]) } results[[\"mean_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_3, r.progress = FALSE)@cp_set results[[\"mean_data_3\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"fastcpd\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_3, G = floor(nrow(mean_data_3) / 6))$cpts results[[\"mean_data_3\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"CptNonPar\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_3), ncol(mean_data_3) ) )$changepoints[, \"location\"] results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"InspectChangepoint\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_3, K = 2)$bestBkp results[[\"mean_data_3\"]][[\"jointseg\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"jointseg\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mean_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mean_data_3\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"Rbeast\"]], matrix(c( 301, 701, 301, 701, 301, 701, 142, 117, 926, 305, NaN, NaN, 705, NaN, NaN, 694, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ), nrow = 10, ncol = 3, byrow = TRUE), tolerance = 0.2) results[[\"mean_data_3\"]][[\"strucchange\"]] <- strucchange::breakpoints( cbind(y.1, y.2, y.3) ~ 1, data = data.frame(y = mean_data_3) )$breakpoints results[[\"mean_data_3\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"strucchange\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_3)$estimates results[[\"mean_data_3\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"ecp\"]], c(1, 301, 701, 1001), tolerance = 0.2) results[[\"mean_data_3\"]][[\"bcp\"]] <- bcp::bcp(mean_data_3) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_3\"]][[\"bcp\"]]) } results[[\"mv_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_3, r.progress = FALSE)@cp_set results[[\"mv_data_3\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"fastcpd\"]], c(300, 700, 1000, 1300, 1700), tolerance = 0.2) results[[\"mv_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_3)$estimates results[[\"mv_data_3\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"ecp\"]], c(1, 301, 701, 1001, 1301, 1701, 2001), tolerance = 0.2) results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_3), ncol(mv_data_3) ) )$changepoints[, \"location\"] results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"InspectChangepoint\"]], c( 300, 700, 701, 703, 705, 707, 708, 709, 711, 712, 714, 715, 717, 718, 720, 721, 723, 724, 726, 727, 729, 731, 733, 734, 736, 737, 739, 740, 742, 743, 744, 746, 747, 749, 750, 752, 753, 754, 755, 756, 758, 760, 762, 763, 765, 766, 767, 769, 770, 772, 773, 774, 775, 777, 779, 780, 782, 784, 786, 788, 790, 791, 793, 795, 797, 799, 801, 803, 804, 806, 809, 810, 811, 813, 814, 816, 817, 818, 820, 821, 823, 825, 827, 828, 830, 831, 833, 835, 836, 837, 838, 840, 842, 843, 845, 846, 848, 849, 850, 852, 853, 854, 855, 856, 858, 859, 860, 862, 863, 865, 866, 868, 869, 871, 872, 874, 876, 877, 878, 879, 881, 883, 885, 887, 888, 889, 891, 893, 894, 895, 897, 898, 900, 901, 903, 904, 906, 908, 909, 911, 913, 914, 916, 917, 918, 920, 921, 923, 924, 925, 927, 928, 929, 931, 932, 934, 936, 937, 938, 939, 941, 942, 943, 945, 946, 947, 949, 950, 952, 954, 955, 956, 957, 958, 959, 961, 962, 964, 965, 967, 968, 970, 972, 973, 974, 975, 977, 979, 981, 982, 984, 985, 986, 987, 988, 990, 991, 992, 994, 995, 997, 999, 1000, 1300, 1700, 1702, 1703, 1704, 1705, 1706, 1708, 1709, 1710, 1712, 1713, 1714, 1715, 1717, 1719, 1721, 1722, 1723, 1725, 1727, 1729, 1730, 1732, 1734, 1735, 1737, 1738, 1739, 1741, 1742, 1744, 1746, 1748, 1750, 1752, 1753, 1754, 1755, 1757, 1758, 1759, 1761, 1762, 1763, 1764, 1766, 1767, 1769, 1770, 1771, 1773, 1774, 1775, 1777, 1779, 1781, 1782, 1783, 1785, 1786, 1788, 1789, 1791, 1793, 1794, 1796, 1798, 1800, 1803, 1804, 1805, 1806, 1808, 1809, 1811, 1812, 1814, 1815, 1817, 1818, 1819, 1821, 1822, 1824, 1825, 1827, 1828, 1829, 1831, 1833, 1835, 1836, 1838, 1839, 1841, 1843, 1844, 1846, 1847, 1848, 1850, 1851, 1853, 1854, 1856, 1857, 1858, 1859, 1860, 1862, 1863, 1864, 1865, 1867, 1869, 1870, 1872, 1873, 1874, 1876, 1878, 1879, 1881, 1882, 1884, 1885, 1887, 1889, 1891, 1893, 1894, 1896, 1898, 1899, 1900, 1901, 1902, 1904, 1906, 1907, 1909, 1911, 1913, 1914, 1916, 1917, 1918, 1919, 1921, 1923, 1924, 1925, 1927, 1928, 1930, 1932, 1933, 1935, 1936, 1938, 1939, 1941, 1942, 1944, 1946, 1948, 1950, 1951, 1952, 1954, 1956, 1957, 1959, 1961, 1963, 1965, 1967, 1968, 1970, 1972, 1973, 1974, 1976, 1977, 1979, 1981, 1982, 1984, 1985, 1987, 1989, 1990, 1992, 1993, 1995, 1996, 1998 ), tolerance = 0.2) results[[\"mv_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mv_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mv_data_3\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"Rbeast\"]], matrix(c( 701, 1301, 301, 1301, 1301, 301, 1301, 710, 301, 701, 1829, 301, 1968, 1993, 702, 886, 1994, 884, 1822, 1975, 814, 755, 810, 1915, 1962, 781, 845, 778, 1978, 767, 1738, 1985, 1870, 747, 1754, 792, 1843, 722, 771, 953 ), nrow = 10, ncol = 4, byrow = TRUE), tolerance = 0.2) results[[\"lm_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lm(lm_data, r.progress = FALSE)@cp_set results[[\"lm_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"lm_data\"]][[\"fastcpd\"]], c(97, 201), tolerance = 0.2) results[[\"lm_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lm_data)$breakpoints results[[\"lm_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"lm_data\"]][[\"strucchange\"]], c(100, 201), tolerance = 0.2) results[[\"lm_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ . - 1, data.frame(y = lm_data$y, x = lm_data[, -1], index = seq_len(nrow(lm_data))) ), seg.Z = ~ index )$psi[, \"Est.\"] results[[\"lm_data\"]][[\"segmented\"]] testthat::expect_equal(results[[\"lm_data\"]][[\"segmented\"]], c(233), ignore_attr = TRUE, tolerance = 0.2) results[[\"binomial_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.binomial(binomial_data, r.progress = FALSE)@cp_set results[[\"binomial_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"binomial_data\"]][[\"fastcpd\"]], 302, tolerance = 0.2) results[[\"binomial_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = binomial_data)$breakpoints results[[\"binomial_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"binomial_data\"]][[\"strucchange\"]], 297, tolerance = 0.2) results[[\"poisson_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.poisson(poisson_data, r.progress = FALSE)@cp_set results[[\"poisson_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"poisson_data\"]][[\"fastcpd\"]], c(498, 805, 1003), tolerance = 0.2) results[[\"poisson_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = poisson_data)$breakpoints results[[\"poisson_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"poisson_data\"]][[\"strucchange\"]], 935, tolerance = 0.2) results[[\"lasso_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lasso(lasso_data, r.progress = FALSE)@cp_set results[[\"lasso_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"lasso_data\"]][[\"fastcpd\"]], c(79, 199, 320), tolerance = 0.2) results[[\"lasso_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lasso_data)$breakpoints results[[\"lasso_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"lasso_data\"]][[\"strucchange\"]], c(80, 200, 321), tolerance = 0.2) results[[\"ar_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.ar(ar_data, 3, r.progress = FALSE)@cp_set results[[\"ar_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"ar_data\"]][[\"fastcpd\"]], c(614), tolerance = 0.2) results[[\"ar_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(ar_data, G = floor(length(ar_data) / 6))$cpts results[[\"ar_data\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"ar_data\"]][[\"CptNonPar\"]], numeric(0), tolerance = 0.2) results[[\"ar_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ x + 1, data.frame(y = ar_data, x = seq_along(ar_data)) ), seg.Z = ~ x )$psi[, \"Est.\"] results[[\"ar_data\"]][[\"segmented\"]] testthat::expect_equal(results[[\"ar_data\"]][[\"segmented\"]], c(690), ignore_attr = TRUE, tolerance = 0.2) results[[\"ar_data\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1 + ar(3), ~ 0 + ar(3)), data = data.frame(y = ar_data, x = seq_along(ar_data)), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"ar_data\"]][[\"mcp\"]]) } results[[\"garch_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.garch(garch_data, c(1, 1), r.progress = FALSE)@cp_set results[[\"garch_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"garch_data\"]][[\"fastcpd\"]], c(205), tolerance = 0.2) results[[\"garch_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(garch_data, G = floor(length(garch_data) / 6))$cpts results[[\"garch_data\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"garch_data\"]][[\"CptNonPar\"]], c(206), tolerance = 0.2) results[[\"garch_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(x ~ 1, data = data.frame(x = garch_data))$breakpoints results[[\"garch_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"garch_data\"]][[\"strucchange\"]], NA, tolerance = 0.2) results[[\"var_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.var(var_data, 2, r.progress = FALSE)@cp_set results[[\"var_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"var_data\"]][[\"fastcpd\"]], c(500), tolerance = 0.2) results[[\"var_data\"]][[\"VARDetect\"]] <- VARDetect::tbss(var_data)$cp results[[\"var_data\"]][[\"VARDetect\"]] testthat::expect_equal(results[[\"var_data\"]][[\"VARDetect\"]], c(501), tolerance = 0.2) well_log <- fastcpd::well_log well_log <- well_log[well_log > 1e5] results[[\"well_log\"]] <- list( fastcpd = fastcpd.mean(well_log, trim = 0.003)@cp_set, changepoint = changepoint::cpt.mean(well_log)@cpts, CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6))$cpts, strucchange = strucchange::breakpoints( y ~ 1, data = data.frame(y = well_log) )$breakpoints, ecp = ecp::e.divisive(matrix(well_log))$estimates, breakfast = breakfast::breakfast(well_log)$cptmodel.list[[6]]$cpts, wbs = wbs::wbs(well_log)$cpt$cpt.ic$mbic.penalty, mosum = mosum::mosum(c(well_log), G = 40)$cpts.info$cpts, # fpop = fpop::Fpop(well_log, length(well_log))$t.est, # meaningless gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints, InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) )$changepoints[, \"location\"], jointseg = jointseg::jointSeg(well_log, K = 12)$bestBkp, Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp, stepR = stepR::stepFit(well_log, alpha = 0.5)$rightEnd ) results[[\"well_log\"]] package_list <- sort(names(results[[\"well_log\"]]), decreasing = TRUE) comparison_table <- NULL for (package_index in seq_along(package_list)) { package <- package_list[[package_index]] comparison_table <- rbind( comparison_table, data.frame( change_point = results[[\"well_log\"]][[package]], package = package, y_offset = (package_index - 1) * 1000 ) ) } most_selected <- sort(table(comparison_table$change_point), decreasing = TRUE) most_selected <- sort(as.numeric(names(most_selected[most_selected >= 4]))) for (i in seq_len(length(most_selected) - 1)) { if (most_selected[i + 1] - most_selected[i] < 2) { most_selected[i] <- NA most_selected[i + 1] <- most_selected[i + 1] - 0.5 } } (most_selected <- most_selected[!is.na(most_selected)]) if (requireNamespace(\"ggplot2\", quietly = TRUE)) { ggplot2::ggplot() + ggplot2::geom_point( data = data.frame(x = seq_along(well_log), y = c(well_log)), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_vline( xintercept = most_selected, color = \"black\", linetype = \"dashed\", alpha = 0.2 ) + ggplot2::geom_point( data = comparison_table, ggplot2::aes(x = change_point, y = 50000 + y_offset, color = package), shape = 17, size = 1.9 ) + ggplot2::geom_hline( data = comparison_table, ggplot2::aes(yintercept = 50000 + y_offset, color = package), linetype = \"dashed\", alpha = 0.1 ) + ggplot2::coord_cartesian( ylim = c(50000 - 500, max(well_log) + 1000), xlim = c(-200, length(well_log) + 200), expand = FALSE ) + ggplot2::theme( panel.background = ggplot2::element_blank(), panel.border = ggplot2::element_rect(colour = \"black\", fill = NA), panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank() ) + ggplot2::xlab(NULL) + ggplot2::ylab(NULL) } results[[\"microbenchmark\"]] <- microbenchmark::microbenchmark( fastcpd = fastcpd::fastcpd.mean(well_log, trim = 0.003, r.progress = FALSE), changepoint = changepoint::cpt.mean(well_log, method = \"PELT\"), CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6)), strucchange = strucchange::breakpoints(y ~ 1, data = data.frame(y = well_log)), ecp = ecp::e.divisive(matrix(well_log)), breakfast = breakfast::breakfast(well_log), wbs = wbs::wbs(well_log), mosum = mosum::mosum(c(well_log), G = 40), fpop = fpop::Fpop(well_log, nrow(well_log)), gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" ), InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) ), jointseg = jointseg::jointSeg(well_log, K = 12), Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE ), stepR = stepR::stepFit(well_log, alpha = 0.5), not = not::not(well_log, contrast = \"pcwsConstMean\"), times = 10 ) results[[\"microbenchmark\"]] if (requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"microbenchmark\", quietly = TRUE)) { ggplot2::autoplot(results[[\"microbenchmark\"]]) } if (!file.exists(\"comparison-packages-results.RData\")) { save(results, file = \"comparison-packages-results.RData\") }"},{"path":[]},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"logistic-regression","dir":"Articles","previous_headings":"Setup","what":"Logistic regression","title":"Comparison with vanilla PELT","text":"","code":"#' Cost function for Logistic regression, i.e. binomial family in GLM. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_binomial <- function(data, family = \"binomial\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm( as.matrix(data[, 1:p]), data[, p + 1], family = family ) return(out$deviance / 2) } #' Implementation of vanilla PELT for logistic regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_binomial <- function(data, beta, cost = cost_glm_binomial) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) } cp <- cp_set[[n + 1]] nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_logistic_update <- function( data_new, coef, cum_coef, cmatrix, epsilon = 1e-10) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- 1 / (1 + exp(-eta)) cmatrix <- cmatrix + (X_new %o% X_new) * as.numeric((1 - mu) * mu) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_binomial <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + log(1 + exp(u)) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_binomial <- function(data, beta, B = 10, trim = 0.025) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm( as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = \"binomial\" ) coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) e_eta <- exp(coef %*% X1) const <- e_eta / (1 + e_eta)^2 cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_logistic_update(data[t, ], coef_c, cum_coef_c, cmatrix_c) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) { cval[i] <- neg_log_lik_binomial(data[k:t, ], cum_coef[, i] / (t - k + 1)) } } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t / (1 + e_eta_t)^2 cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (seq_len(length(cp)))[(cp < trim * n) | (cp > (1 - trim) * n)] cp <- cp[-ind3] } nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"poisson-regression","dir":"Articles","previous_headings":"Setup","what":"Poisson regression","title":"Comparison with vanilla PELT","text":"","code":"#' Cost function for Poisson regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_poisson <- function(data, family = \"poisson\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm(as.matrix(data[, 1:p]), data[, p + 1], family = family) return(out$deviance / 2) } #' Implementation of vanilla PELT for poisson regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_poisson <- function(data, beta, cost = cost_glm_poisson) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) # if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_poisson_update <- function(data_new, coef, cum_coef, cmatrix, epsilon = 0.001, G = 10^10, L = -20, H = 20) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- exp(eta) cmatrix <- cmatrix + (X_new %o% X_new) * min(as.numeric(mu), G) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) coef <- pmin(pmax(coef, L), H) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_poisson <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + exp(u) + lfactorial(Y) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_poisson <- function(data, beta, B = 10, trim = 0.03, epsilon = 0.001, G = 10^10, L = -20, H = 20) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm(x = as.matrix(data[index == i, 1:p]), y = data[index == i, p + 1], family = \"poisson\") coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- pmin(pmax(matrix(coef.int[1, ], p, 1), L), H) e_eta <- exp(coef %*% X1) const <- e_eta cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_poisson_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, epsilon = epsilon, G = G, L = L, H = H) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cum_coef_win <- pmin(pmax(cum_coef[, i] / (t - k + 1), L), H) if (t - k >= p - 1) cval[i] <- neg_log_lik_poisson(data[k:t, ], cum_coef_win) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- pmin(pmax(coef.int[index[t], ], L), H) e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"Poisson\") # nLL <- out$deviance/2 + nLL # } # output <- list(cp, nLL) # names(output) <- c(\"cp\", \"nLL\") output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from poisson regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_poisson <- function(n, d, true.coef, true.cp.loc, Sigma) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { mu <- exp(x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE]) group <- rpois(length(mu), mu) y <- c(y, group) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"penalized-linear-regression","dir":"Articles","previous_headings":"Setup","what":"Penalized linear regression","title":"Comparison with vanilla PELT","text":"","code":"#' Cost function for penalized linear regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param lambda Penalty coefficient. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_lasso <- function(data, lambda, family = \"gaussian\") { data <- as.matrix(data) n <- dim(data)[1] p <- dim(data)[2] - 1 out <- glmnet::glmnet(as.matrix(data[, 1:p]), data[, p + 1], family = family, lambda = lambda) return(deviance(out) / 2) } #' Implementation of vanilla PELT for penalized linear regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_lasso <- function(data, beta, B = 10, cost = cost_lasso, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 index <- rep(1:B, rep(n / B, B)) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= 1) cval[i] <- suppressWarnings(cost(data[k:t, ], lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1)))) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- glmnet(as.matrix(data_seg[, 1:p]), data_seg[, p+1], lambda=lambda, family=family) # nLL <- deviance(out)/2 + nLL # } # output <- list(cp, nLL) output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' @param a Coefficient to be updated. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Updated coefficient. soft_threshold <- function(a, lambda) { sign(a) * pmax(abs(a) - lambda, 0) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_lasso_update <- function(data_new, coef, cum_coef, cmatrix, lambda) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] mu <- X_new %*% coef cmatrix <- cmatrix + X_new %o% X_new # B <- as.vector(cmatrix_inv%*%X_new) # cmatrix_inv <- cmatrix_inv - B%o%B/(1+sum(X_new*B)) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix, lik_dev) nc <- norm(cmatrix, type = \"F\") # the choice of norm affects the speed. Spectral norm is more accurate but slower than F norm. coef <- soft_threshold(coef, lambda / nc) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_lasso <- function(data, b, lambda) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] resi <- Y - X %*% b L <- sum(resi^2) / 2 + lambda * sum(abs(b)) return(L) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_lasso <- function(data, beta, B = 10, trim = 0.025, epsilon = 1e-5, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef.int[i, ] <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef.int[i, ]) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef.int[i, ]) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) eta <- coef %*% X1 # c_int <- diag(1/epsilon,p) - X1%o%X1/epsilon^2/(1+sum(X1^2)/epsilon) # cmatrix_inv <- array(c_int, c(p,p,1)) cmatrix <- array(X1 %o% X1 + epsilon * diag(1, p), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] # cmatrix_inv_c <- cmatrix_inv[,,i] cmatrix_c <- cmatrix[, , i] k <- set[i] + 1 out <- cost_lasso_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] # cmatrix_inv[,,i] <- out[[3]] cmatrix[, , i] <- out[[3]] if (t - k >= 2) cval[i] <- neg_log_lik_lasso(data[k:t, ], cum_coef[, i] / (t - k + 1), lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] # cmatrix_inv_add <- diag(1/epsilon,p) - Xt%o%Xt/epsilon^2/(1+sum(Xt^2)/epsilon) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) # cmatrix_inv <- abind::abind(cmatrix_inv, cmatrix_inv_add, along=3) cmatrix <- abind::abind(cmatrix, Xt %o% Xt + epsilon * diag(1, p), along = 3) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] # cmatrix_inv <- cmatrix_inv[,,ind2,drop=FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries and merge change-points cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"binomial\") # nLL <- out$deviance/2 + nLL # } output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from penalized linear regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @param evar Error variance. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_lasso <- function(n, d, true.coef, true.cp.loc, Sigma, evar) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { Xb <- x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE] add <- Xb + rnorm(length(Xb), sd = sqrt(evar)) y <- c(y, add) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"logistic-regression-1","dir":"Articles","previous_headings":"","what":"Logistic regression","title":"Comparison with vanilla PELT","text":"","code":"set.seed(1) p <- 5 x <- matrix(rnorm(300 * p, 0, 1), ncol = p) # Randomly generate coefficients with different means. theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) # Randomly generate response variables based on the segmented data and # corresponding coefficients y <- c( rbinom(125, 1, 1 / (1 + exp(-x[1:125, ] %*% theta[1, ]))), rbinom(300 - 125, 1, 1 / (1 + exp(-x[(125 + 1):300, ] %*% theta[2, ]))) ) segd_binomial(cbind(x, y), (p + 1) * log(300) / 2, B = 5)$cp #> [1] 125 fastcpd.binomial( cbind(y, x), segment_count = 5, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125 pelt_vanilla_binomial(cbind(x, y), (p + 1) * log(300) / 2)$cp #> [1] 0 125 fastcpd.binomial( cbind(y, x), segment_count = 5, vanilla_percentage = 1, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"poisson-regression-1","dir":"Articles","previous_headings":"","what":"Poisson regression","title":"Comparison with vanilla PELT","text":"","code":"set.seed(1) n <- 1500 d <- 5 rho <- 0.9 Sigma <- array(0, c(d, d)) for (i in 1:d) { Sigma[i, ] <- rho^(abs(i - (1:d))) } delta <- c(5, 7, 9, 11, 13) a.sq <- 1 delta.new <- delta * sqrt(a.sq) / sqrt(as.numeric(t(delta) %*% Sigma %*% delta)) true.cp.loc <- c(375, 750, 1125) # regression coefficients true.coef <- matrix(0, nrow = d, ncol = length(true.cp.loc) + 1) true.coef[, 1] <- c(1, 1.2, -1, 0.5, -2) true.coef[, 2] <- true.coef[, 1] + delta.new true.coef[, 3] <- true.coef[, 1] true.coef[, 4] <- true.coef[, 3] - delta.new out <- data_gen_poisson(n, d, true.coef, true.cp.loc, Sigma) data <- out[[1]] g_tr <- out[[2]] beta <- log(n) * (d + 1) / 2 segd_poisson( data, beta, trim = 0.03, B = 10, epsilon = 0.001, G = 10^10, L = -20, H = 20 )$cp #> [1] 380 751 1136 1251 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), beta = beta, cost_adjustment = \"BIC\", epsilon = 0.001, segment_count = 10, r.progress = FALSE )@cp_set #> [1] 380 751 1136 1251 pelt_vanilla_poisson(data, beta)$cp #> [1] 0 374 752 1133 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), segment_count = 10, vanilla_percentage = 1, beta = beta, cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 374 752 1133"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"penalized-linear-regression-1","dir":"Articles","previous_headings":"","what":"Penalized linear regression","title":"Comparison with vanilla PELT","text":"","code":"set.seed(1) n <- 1000 s <- 3 d <- 50 evar <- 0.5 Sigma <- diag(1, d) true.cp.loc <- c(100, 300, 500, 800, 900) seg <- length(true.cp.loc) + 1 true.coef <- matrix(rnorm(seg * s), s, seg) true.coef <- rbind(true.coef, matrix(0, d - s, seg)) out <- data_gen_lasso(n, d, true.coef, true.cp.loc, Sigma, evar) data <- out[[1]] beta <- log(n) / 2 # beta here has different meaning segd_lasso(data, beta, B = 10, trim = 0.025)$cp #> [1] 100 300 520 800 901 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 100 300 520 800 901 pelt_vanilla_lasso(data, beta, cost = cost_lasso)$cp #> [1] 100 #> [1] 200 #> [1] 300 #> [1] 400 #> [1] 500 #> [1] 600 #> [1] 700 #> [1] 800 #> [1] 900 #> [1] 1000 #> [1] 0 103 299 510 800 900 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), vanilla_percentage = 1, epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 103 299 510 800 900"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Comparison with vanilla PELT","text":"evaluation vignette set FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Comparison with vanilla PELT","text":"","code":"knitr::opts_chunk$set( collapse = TRUE, comment = \"#>\", eval = FALSE, cache = FALSE, warning = FALSE, fig.width = 8, fig.height = 5 ) library(fastcpd) #' Cost function for Logistic regression, i.e. binomial family in GLM. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_binomial <- function(data, family = \"binomial\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm( as.matrix(data[, 1:p]), data[, p + 1], family = family ) return(out$deviance / 2) } #' Implementation of vanilla PELT for logistic regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_binomial <- function(data, beta, cost = cost_glm_binomial) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) } cp <- cp_set[[n + 1]] nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_logistic_update <- function( data_new, coef, cum_coef, cmatrix, epsilon = 1e-10) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- 1 / (1 + exp(-eta)) cmatrix <- cmatrix + (X_new %o% X_new) * as.numeric((1 - mu) * mu) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_binomial <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + log(1 + exp(u)) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_binomial <- function(data, beta, B = 10, trim = 0.025) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm( as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = \"binomial\" ) coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) e_eta <- exp(coef %*% X1) const <- e_eta / (1 + e_eta)^2 cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_logistic_update(data[t, ], coef_c, cum_coef_c, cmatrix_c) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) { cval[i] <- neg_log_lik_binomial(data[k:t, ], cum_coef[, i] / (t - k + 1)) } } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t / (1 + e_eta_t)^2 cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (seq_len(length(cp)))[(cp < trim * n) | (cp > (1 - trim) * n)] cp <- cp[-ind3] } nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) } #' Cost function for Poisson regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_poisson <- function(data, family = \"poisson\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm(as.matrix(data[, 1:p]), data[, p + 1], family = family) return(out$deviance / 2) } #' Implementation of vanilla PELT for poisson regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_poisson <- function(data, beta, cost = cost_glm_poisson) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) # if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_poisson_update <- function(data_new, coef, cum_coef, cmatrix, epsilon = 0.001, G = 10^10, L = -20, H = 20) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- exp(eta) cmatrix <- cmatrix + (X_new %o% X_new) * min(as.numeric(mu), G) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) coef <- pmin(pmax(coef, L), H) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_poisson <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + exp(u) + lfactorial(Y) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_poisson <- function(data, beta, B = 10, trim = 0.03, epsilon = 0.001, G = 10^10, L = -20, H = 20) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm(x = as.matrix(data[index == i, 1:p]), y = data[index == i, p + 1], family = \"poisson\") coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- pmin(pmax(matrix(coef.int[1, ], p, 1), L), H) e_eta <- exp(coef %*% X1) const <- e_eta cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_poisson_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, epsilon = epsilon, G = G, L = L, H = H) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cum_coef_win <- pmin(pmax(cum_coef[, i] / (t - k + 1), L), H) if (t - k >= p - 1) cval[i] <- neg_log_lik_poisson(data[k:t, ], cum_coef_win) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- pmin(pmax(coef.int[index[t], ], L), H) e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"Poisson\") # nLL <- out$deviance/2 + nLL # } # output <- list(cp, nLL) # names(output) <- c(\"cp\", \"nLL\") output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from poisson regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_poisson <- function(n, d, true.coef, true.cp.loc, Sigma) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { mu <- exp(x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE]) group <- rpois(length(mu), mu) y <- c(y, group) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) } #' Cost function for penalized linear regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param lambda Penalty coefficient. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_lasso <- function(data, lambda, family = \"gaussian\") { data <- as.matrix(data) n <- dim(data)[1] p <- dim(data)[2] - 1 out <- glmnet::glmnet(as.matrix(data[, 1:p]), data[, p + 1], family = family, lambda = lambda) return(deviance(out) / 2) } #' Implementation of vanilla PELT for penalized linear regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_lasso <- function(data, beta, B = 10, cost = cost_lasso, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 index <- rep(1:B, rep(n / B, B)) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= 1) cval[i] <- suppressWarnings(cost(data[k:t, ], lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1)))) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- glmnet(as.matrix(data_seg[, 1:p]), data_seg[, p+1], lambda=lambda, family=family) # nLL <- deviance(out)/2 + nLL # } # output <- list(cp, nLL) output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' @param a Coefficient to be updated. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Updated coefficient. soft_threshold <- function(a, lambda) { sign(a) * pmax(abs(a) - lambda, 0) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_lasso_update <- function(data_new, coef, cum_coef, cmatrix, lambda) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] mu <- X_new %*% coef cmatrix <- cmatrix + X_new %o% X_new # B <- as.vector(cmatrix_inv%*%X_new) # cmatrix_inv <- cmatrix_inv - B%o%B/(1+sum(X_new*B)) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix, lik_dev) nc <- norm(cmatrix, type = \"F\") # the choice of norm affects the speed. Spectral norm is more accurate but slower than F norm. coef <- soft_threshold(coef, lambda / nc) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_lasso <- function(data, b, lambda) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] resi <- Y - X %*% b L <- sum(resi^2) / 2 + lambda * sum(abs(b)) return(L) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_lasso <- function(data, beta, B = 10, trim = 0.025, epsilon = 1e-5, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef.int[i, ] <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef.int[i, ]) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef.int[i, ]) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) eta <- coef %*% X1 # c_int <- diag(1/epsilon,p) - X1%o%X1/epsilon^2/(1+sum(X1^2)/epsilon) # cmatrix_inv <- array(c_int, c(p,p,1)) cmatrix <- array(X1 %o% X1 + epsilon * diag(1, p), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] # cmatrix_inv_c <- cmatrix_inv[,,i] cmatrix_c <- cmatrix[, , i] k <- set[i] + 1 out <- cost_lasso_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] # cmatrix_inv[,,i] <- out[[3]] cmatrix[, , i] <- out[[3]] if (t - k >= 2) cval[i] <- neg_log_lik_lasso(data[k:t, ], cum_coef[, i] / (t - k + 1), lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] # cmatrix_inv_add <- diag(1/epsilon,p) - Xt%o%Xt/epsilon^2/(1+sum(Xt^2)/epsilon) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) # cmatrix_inv <- abind::abind(cmatrix_inv, cmatrix_inv_add, along=3) cmatrix <- abind::abind(cmatrix, Xt %o% Xt + epsilon * diag(1, p), along = 3) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] # cmatrix_inv <- cmatrix_inv[,,ind2,drop=FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries and merge change-points cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"binomial\") # nLL <- out$deviance/2 + nLL # } output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from penalized linear regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @param evar Error variance. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_lasso <- function(n, d, true.coef, true.cp.loc, Sigma, evar) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { Xb <- x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE] add <- Xb + rnorm(length(Xb), sd = sqrt(evar)) y <- c(y, add) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) } set.seed(1) p <- 5 x <- matrix(rnorm(300 * p, 0, 1), ncol = p) # Randomly generate coefficients with different means. theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) # Randomly generate response variables based on the segmented data and # corresponding coefficients y <- c( rbinom(125, 1, 1 / (1 + exp(-x[1:125, ] %*% theta[1, ]))), rbinom(300 - 125, 1, 1 / (1 + exp(-x[(125 + 1):300, ] %*% theta[2, ]))) ) segd_binomial(cbind(x, y), (p + 1) * log(300) / 2, B = 5)$cp #> [1] 125 fastcpd.binomial( cbind(y, x), segment_count = 5, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125 pelt_vanilla_binomial(cbind(x, y), (p + 1) * log(300) / 2)$cp #> [1] 0 125 fastcpd.binomial( cbind(y, x), segment_count = 5, vanilla_percentage = 1, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125 set.seed(1) n <- 1500 d <- 5 rho <- 0.9 Sigma <- array(0, c(d, d)) for (i in 1:d) { Sigma[i, ] <- rho^(abs(i - (1:d))) } delta <- c(5, 7, 9, 11, 13) a.sq <- 1 delta.new <- delta * sqrt(a.sq) / sqrt(as.numeric(t(delta) %*% Sigma %*% delta)) true.cp.loc <- c(375, 750, 1125) # regression coefficients true.coef <- matrix(0, nrow = d, ncol = length(true.cp.loc) + 1) true.coef[, 1] <- c(1, 1.2, -1, 0.5, -2) true.coef[, 2] <- true.coef[, 1] + delta.new true.coef[, 3] <- true.coef[, 1] true.coef[, 4] <- true.coef[, 3] - delta.new out <- data_gen_poisson(n, d, true.coef, true.cp.loc, Sigma) data <- out[[1]] g_tr <- out[[2]] beta <- log(n) * (d + 1) / 2 segd_poisson( data, beta, trim = 0.03, B = 10, epsilon = 0.001, G = 10^10, L = -20, H = 20 )$cp #> [1] 380 751 1136 1251 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), beta = beta, cost_adjustment = \"BIC\", epsilon = 0.001, segment_count = 10, r.progress = FALSE )@cp_set #> [1] 380 751 1136 1251 pelt_vanilla_poisson(data, beta)$cp #> [1] 0 374 752 1133 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), segment_count = 10, vanilla_percentage = 1, beta = beta, cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 374 752 1133 set.seed(1) n <- 1000 s <- 3 d <- 50 evar <- 0.5 Sigma <- diag(1, d) true.cp.loc <- c(100, 300, 500, 800, 900) seg <- length(true.cp.loc) + 1 true.coef <- matrix(rnorm(seg * s), s, seg) true.coef <- rbind(true.coef, matrix(0, d - s, seg)) out <- data_gen_lasso(n, d, true.coef, true.cp.loc, Sigma, evar) data <- out[[1]] beta <- log(n) / 2 # beta here has different meaning segd_lasso(data, beta, B = 10, trim = 0.025)$cp #> [1] 100 300 520 800 901 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 100 300 520 800 901 pelt_vanilla_lasso(data, beta, cost = cost_lasso)$cp #> [1] 100 #> [1] 200 #> [1] 300 #> [1] 400 #> [1] 500 #> [1] 600 #> [1] 700 #> [1] 800 #> [1] 900 #> [1] 1000 #> [1] 0 103 299 510 800 900 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), vanilla_percentage = 1, epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 103 299 510 800 900"},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"multiple-epochs","dir":"Articles","previous_headings":"","what":"Multiple epochs","title":"Advanced examples","text":"","code":"result_multiple_epochs <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < 20) 1 else 0, r.progress = FALSE ) summary(result_multiple_epochs) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < #> 20) 1 else 0, r.progress = FALSE) #> #> Change points: #> 79 199 320 #> #> Cost values: #> 125.1501 155.4486 164.1284 177.9478 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.898713954 -3.006202485 #> [2,] -2.668688731 -2.473539196 4.209460633 3.096081968 #> [3,] -4.354248176 0.983488542 5.011651466 3.718141515 #> [4,] -4.662566697 0.279505605 2.623742216 -3.130632564 #> [5,] -4.820139326 0.496565230 4.616444609 3.175562645 #> [6,] -3.860308911 -2.576467859 3.336301862 4.342890940 #> [7,] 0.117234534 -0.219145186 -0.039861426 -0.016395368 #> [8,] 0.117552281 0.035701649 -0.075921029 -0.022022294 #> [9,] -0.188756652 0.133653604 0.052907904 0.070683538 #> [10,] -0.074530965 0.010173561 0.003352400 0.012791767 #> [11,] -0.061721013 -0.176769170 -0.044814526 -0.166042077 #> [12,] 0.021874954 -0.133777712 0.103526247 0.077820866 #> [13,] 0.067777695 0.129187849 -0.072098209 0.077436979 #> [14,] -0.122270920 0.110865328 0.095306898 0.114046507 #> [15,] -0.014192027 0.086100909 0.006037145 -0.015966855 #> [16,] -0.135970691 -0.043211895 0.026749332 -0.095751983 #> [17,] 0.007608049 -0.079445537 -0.003130812 -0.010689927 #> [18,] -0.010343806 0.132392790 -0.156866781 -0.138057246 #> [19,] 0.252797503 0.253113165 -0.167436237 0.158967697 #> [20,] -0.044505498 -0.049962311 0.177560915 0.030169316 #> [21,] 0.084292730 0.366833682 -0.224849869 -0.181503446 #> [22,] -0.373913824 -0.060767726 0.003860114 0.045649997 #> [23,] 0.316816197 -0.191114879 -0.118941359 -0.025305755 #> [24,] 0.008174405 0.158066938 -0.135680402 -0.137396624 #> [25,] -0.388590727 0.098542973 -0.064383316 0.022336212 #> [26,] -0.109916673 0.042028223 0.017815134 -0.059782090 #> [27,] -0.040845604 0.023057708 0.053296378 0.030431412 #> [28,] 0.080696347 -0.097192796 0.122665492 -0.080706668 #> [29,] -0.117530646 0.002589360 0.062791240 -0.051542901 #> [30,] -0.037935138 -0.091390269 0.122976438 0.048892128 #> [31,] 0.109674339 0.158620750 -0.181785506 0.075434045 #> [32,] 0.069971569 -0.425139230 0.030472706 0.033375101 #> [33,] 0.050674415 0.050518682 0.034305534 0.070983107 #> [34,] 0.065490413 0.142178491 0.164899160 0.032575571 #> [35,] -0.045676268 -0.057128729 0.022794426 -0.134534869 #> [36,] 0.368217440 0.096573493 0.114099285 -0.020673038 #> [37,] 0.037286542 -0.056101952 -0.023029273 0.102043216 #> [38,] 0.094379823 -0.263914331 0.003288922 -0.052763017 #> [39,] 0.153890231 0.071449851 -0.097509620 -0.120642848 #> [40,] -0.146742186 -0.117294226 -0.208865036 -0.045775725 #> [41,] -0.274840831 0.009051742 0.127312770 0.123910218 #> [42,] -0.008508998 0.110465493 0.118453225 -0.005505113 #> [43,] 0.014213371 -0.041499271 0.113699538 -0.056131976 #> [44,] -0.080890432 0.094198355 0.051912519 0.013524984 #> [45,] 0.051925330 -0.007981636 0.085966205 -0.033628617 #> [46,] 0.213031014 0.168676929 0.035738422 -0.029655833 #> [47,] 0.175716867 0.045500673 0.175504506 0.119507574 #> [48,] -0.040155598 -0.120432098 -0.139162874 -0.054349982 #> [49,] 0.164103981 0.024976184 0.029374949 -0.056092814 #> [50,] -0.103164763 -0.135032022 0.047475507 -0.032491294"},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"vanilla-percentage","dir":"Articles","previous_headings":"","what":"Vanilla percentage","title":"Advanced examples","text":"","code":"result_vanilla_percentage <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE ) summary(result_vanilla_percentage) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE) #> #> Change points: #> 79 199 317 #> #> Cost values: #> 125.1501 155.4486 160.4363 274.2228 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.900559900 -2.749985232 #> [2,] -2.668688731 -2.473539196 4.198921150 3.057387294 #> [3,] -4.354248176 0.983488542 5.054325016 3.735347545 #> [4,] -4.662566697 0.279505605 2.628354481 -3.216027015 #> [5,] -4.820139326 0.496565230 4.611603593 3.231304715 #> [6,] -3.860308911 -2.576467859 3.304903523 4.577405578 #> [7,] 0.117234534 -0.219145186 -0.015091394 -0.086048806 #> [8,] 0.117552281 0.035701649 -0.127986984 -0.138807479 #> [9,] -0.188756652 0.133653604 0.038250526 -0.037807789 #> [10,] -0.074530965 0.010173561 0.021979969 -0.047707753 #> [11,] -0.061721013 -0.176769170 -0.059312332 -0.135633390 #> [12,] 0.021874954 -0.133777712 0.028889403 -0.130808437 #> [13,] 0.067777695 0.129187849 -0.026100120 0.002593292 #> [14,] -0.122270920 0.110865328 0.084858637 0.233903944 #> [15,] -0.014192027 0.086100909 0.030268607 0.243927135 #> [16,] -0.135970691 -0.043211895 -0.032229963 -0.134840827 #> [17,] 0.007608049 -0.079445537 -0.013675459 0.102010778 #> [18,] -0.010343806 0.132392790 -0.151237533 -0.123955960 #> [19,] 0.252797503 0.253113165 -0.197620792 0.099319824 #> [20,] -0.044505498 -0.049962311 0.233024977 0.176211754 #> [21,] 0.084292730 0.366833682 -0.220176991 -0.035984666 #> [22,] -0.373913824 -0.060767726 0.036430343 -0.105248408 #> [23,] 0.316816197 -0.191114879 -0.127549606 -0.112775343 #> [24,] 0.008174405 0.158066938 -0.121000263 0.232613924 #> [25,] -0.388590727 0.098542973 -0.045207238 0.003838229 #> [26,] -0.109916673 0.042028223 0.031122592 -0.173521656 #> [27,] -0.040845604 0.023057708 0.067839093 0.084198705 #> [28,] 0.080696347 -0.097192796 0.099373268 -0.166716428 #> [29,] -0.117530646 0.002589360 0.051425084 0.098244052 #> [30,] -0.037935138 -0.091390269 0.154099931 0.100237663 #> [31,] 0.109674339 0.158620750 -0.181802621 -0.112714822 #> [32,] 0.069971569 -0.425139230 -0.020032195 -0.193571920 #> [33,] 0.050674415 0.050518682 -0.010950361 -0.068936556 #> [34,] 0.065490413 0.142178491 0.174034864 0.072663828 #> [35,] -0.045676268 -0.057128729 0.050522160 -0.165689753 #> [36,] 0.368217440 0.096573493 0.120128008 0.020252183 #> [37,] 0.037286542 -0.056101952 -0.018563948 0.263830281 #> [38,] 0.094379823 -0.263914331 -0.002747609 -0.010850423 #> [39,] 0.153890231 0.071449851 -0.075881371 -0.060456457 #> [40,] -0.146742186 -0.117294226 -0.255229801 -0.124537184 #> [41,] -0.274840831 0.009051742 0.124862149 0.074433697 #> [42,] -0.008508998 0.110465493 0.215727137 0.269240087 #> [43,] 0.014213371 -0.041499271 0.097848087 0.105722611 #> [44,] -0.080890432 0.094198355 0.004240823 -0.187634605 #> [45,] 0.051925330 -0.007981636 0.044460236 -0.066537564 #> [46,] 0.213031014 0.168676929 0.097190703 0.046563300 #> [47,] 0.175716867 0.045500673 0.175902385 -0.030590597 #> [48,] -0.040155598 -0.120432098 -0.137764051 -0.088812037 #> [49,] 0.164103981 0.024976184 0.015955563 -0.232780151 #> [50,] -0.103164763 -0.135032022 0.029553283 0.267389634"},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Advanced examples","text":"evaluation vignette set FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Advanced examples","text":"","code":"knitr::opts_chunk$set(collapse = TRUE, comment = \"#>\", eval = FALSE) library(fastcpd) set.seed(1) p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(480, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:480, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", r.progress = FALSE ) summary(result) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", r.progress = FALSE) #> #> Change points: #> 79 199 317 #> #> Cost values: #> 125.1501 155.4486 160.4363 274.2228 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.900559900 -2.749985232 #> [2,] -2.668688731 -2.473539196 4.198921150 3.057387294 #> [3,] -4.354248176 0.983488542 5.054325016 3.735347545 #> [4,] -4.662566697 0.279505605 2.628354481 -3.216027015 #> [5,] -4.820139326 0.496565230 4.611603593 3.231304715 #> [6,] -3.860308911 -2.576467859 3.304903523 4.577405578 #> [7,] 0.117234534 -0.219145186 -0.015091394 -0.086048806 #> [8,] 0.117552281 0.035701649 -0.127986984 -0.138807479 #> [9,] -0.188756652 0.133653604 0.038250526 -0.037807789 #> [10,] -0.074530965 0.010173561 0.021979969 -0.047707753 #> [11,] -0.061721013 -0.176769170 -0.059312332 -0.135633390 #> [12,] 0.021874954 -0.133777712 0.028889403 -0.130808437 #> [13,] 0.067777695 0.129187849 -0.026100120 0.002593292 #> [14,] -0.122270920 0.110865328 0.084858637 0.233903944 #> [15,] -0.014192027 0.086100909 0.030268607 0.243927135 #> [16,] -0.135970691 -0.043211895 -0.032229963 -0.134840827 #> [17,] 0.007608049 -0.079445537 -0.013675459 0.102010778 #> [18,] -0.010343806 0.132392790 -0.151237533 -0.123955960 #> [19,] 0.252797503 0.253113165 -0.197620792 0.099319824 #> [20,] -0.044505498 -0.049962311 0.233024977 0.176211754 #> [21,] 0.084292730 0.366833682 -0.220176991 -0.035984666 #> [22,] -0.373913824 -0.060767726 0.036430343 -0.105248408 #> [23,] 0.316816197 -0.191114879 -0.127549606 -0.112775343 #> [24,] 0.008174405 0.158066938 -0.121000263 0.232613924 #> [25,] -0.388590727 0.098542973 -0.045207238 0.003838229 #> [26,] -0.109916673 0.042028223 0.031122592 -0.173521656 #> [27,] -0.040845604 0.023057708 0.067839093 0.084198705 #> [28,] 0.080696347 -0.097192796 0.099373268 -0.166716428 #> [29,] -0.117530646 0.002589360 0.051425084 0.098244052 #> [30,] -0.037935138 -0.091390269 0.154099931 0.100237663 #> [31,] 0.109674339 0.158620750 -0.181802621 -0.112714822 #> [32,] 0.069971569 -0.425139230 -0.020032195 -0.193571920 #> [33,] 0.050674415 0.050518682 -0.010950361 -0.068936556 #> [34,] 0.065490413 0.142178491 0.174034864 0.072663828 #> [35,] -0.045676268 -0.057128729 0.050522160 -0.165689753 #> [36,] 0.368217440 0.096573493 0.120128008 0.020252183 #> [37,] 0.037286542 -0.056101952 -0.018563948 0.263830281 #> [38,] 0.094379823 -0.263914331 -0.002747609 -0.010850423 #> [39,] 0.153890231 0.071449851 -0.075881371 -0.060456457 #> [40,] -0.146742186 -0.117294226 -0.255229801 -0.124537184 #> [41,] -0.274840831 0.009051742 0.124862149 0.074433697 #> [42,] -0.008508998 0.110465493 0.215727137 0.269240087 #> [43,] 0.014213371 -0.041499271 0.097848087 0.105722611 #> [44,] -0.080890432 0.094198355 0.004240823 -0.187634605 #> [45,] 0.051925330 -0.007981636 0.044460236 -0.066537564 #> [46,] 0.213031014 0.168676929 0.097190703 0.046563300 #> [47,] 0.175716867 0.045500673 0.175902385 -0.030590597 #> [48,] -0.040155598 -0.120432098 -0.137764051 -0.088812037 #> [49,] 0.164103981 0.024976184 0.015955563 -0.232780151 #> [50,] -0.103164763 -0.135032022 0.029553283 0.267389634 set.seed(1) p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(480, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:480, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result_multiple_epochs <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < 20) 1 else 0, r.progress = FALSE ) summary(result_multiple_epochs) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < #> 20) 1 else 0, r.progress = FALSE) #> #> Change points: #> 79 199 320 #> #> Cost values: #> 125.1501 155.4486 164.1284 177.9478 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.898713954 -3.006202485 #> [2,] -2.668688731 -2.473539196 4.209460633 3.096081968 #> [3,] -4.354248176 0.983488542 5.011651466 3.718141515 #> [4,] -4.662566697 0.279505605 2.623742216 -3.130632564 #> [5,] -4.820139326 0.496565230 4.616444609 3.175562645 #> [6,] -3.860308911 -2.576467859 3.336301862 4.342890940 #> [7,] 0.117234534 -0.219145186 -0.039861426 -0.016395368 #> [8,] 0.117552281 0.035701649 -0.075921029 -0.022022294 #> [9,] -0.188756652 0.133653604 0.052907904 0.070683538 #> [10,] -0.074530965 0.010173561 0.003352400 0.012791767 #> [11,] -0.061721013 -0.176769170 -0.044814526 -0.166042077 #> [12,] 0.021874954 -0.133777712 0.103526247 0.077820866 #> [13,] 0.067777695 0.129187849 -0.072098209 0.077436979 #> [14,] -0.122270920 0.110865328 0.095306898 0.114046507 #> [15,] -0.014192027 0.086100909 0.006037145 -0.015966855 #> [16,] -0.135970691 -0.043211895 0.026749332 -0.095751983 #> [17,] 0.007608049 -0.079445537 -0.003130812 -0.010689927 #> [18,] -0.010343806 0.132392790 -0.156866781 -0.138057246 #> [19,] 0.252797503 0.253113165 -0.167436237 0.158967697 #> [20,] -0.044505498 -0.049962311 0.177560915 0.030169316 #> [21,] 0.084292730 0.366833682 -0.224849869 -0.181503446 #> [22,] -0.373913824 -0.060767726 0.003860114 0.045649997 #> [23,] 0.316816197 -0.191114879 -0.118941359 -0.025305755 #> [24,] 0.008174405 0.158066938 -0.135680402 -0.137396624 #> [25,] -0.388590727 0.098542973 -0.064383316 0.022336212 #> [26,] -0.109916673 0.042028223 0.017815134 -0.059782090 #> [27,] -0.040845604 0.023057708 0.053296378 0.030431412 #> [28,] 0.080696347 -0.097192796 0.122665492 -0.080706668 #> [29,] -0.117530646 0.002589360 0.062791240 -0.051542901 #> [30,] -0.037935138 -0.091390269 0.122976438 0.048892128 #> [31,] 0.109674339 0.158620750 -0.181785506 0.075434045 #> [32,] 0.069971569 -0.425139230 0.030472706 0.033375101 #> [33,] 0.050674415 0.050518682 0.034305534 0.070983107 #> [34,] 0.065490413 0.142178491 0.164899160 0.032575571 #> [35,] -0.045676268 -0.057128729 0.022794426 -0.134534869 #> [36,] 0.368217440 0.096573493 0.114099285 -0.020673038 #> [37,] 0.037286542 -0.056101952 -0.023029273 0.102043216 #> [38,] 0.094379823 -0.263914331 0.003288922 -0.052763017 #> [39,] 0.153890231 0.071449851 -0.097509620 -0.120642848 #> [40,] -0.146742186 -0.117294226 -0.208865036 -0.045775725 #> [41,] -0.274840831 0.009051742 0.127312770 0.123910218 #> [42,] -0.008508998 0.110465493 0.118453225 -0.005505113 #> [43,] 0.014213371 -0.041499271 0.113699538 -0.056131976 #> [44,] -0.080890432 0.094198355 0.051912519 0.013524984 #> [45,] 0.051925330 -0.007981636 0.085966205 -0.033628617 #> [46,] 0.213031014 0.168676929 0.035738422 -0.029655833 #> [47,] 0.175716867 0.045500673 0.175504506 0.119507574 #> [48,] -0.040155598 -0.120432098 -0.139162874 -0.054349982 #> [49,] 0.164103981 0.024976184 0.029374949 -0.056092814 #> [50,] -0.103164763 -0.135032022 0.047475507 -0.032491294 set.seed(1) p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(480, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:480, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result_vanilla_percentage <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE ) summary(result_vanilla_percentage) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE) #> #> Change points: #> 79 199 317 #> #> Cost values: #> 125.1501 155.4486 160.4363 274.2228 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.900559900 -2.749985232 #> [2,] -2.668688731 -2.473539196 4.198921150 3.057387294 #> [3,] -4.354248176 0.983488542 5.054325016 3.735347545 #> [4,] -4.662566697 0.279505605 2.628354481 -3.216027015 #> [5,] -4.820139326 0.496565230 4.611603593 3.231304715 #> [6,] -3.860308911 -2.576467859 3.304903523 4.577405578 #> [7,] 0.117234534 -0.219145186 -0.015091394 -0.086048806 #> [8,] 0.117552281 0.035701649 -0.127986984 -0.138807479 #> [9,] -0.188756652 0.133653604 0.038250526 -0.037807789 #> [10,] -0.074530965 0.010173561 0.021979969 -0.047707753 #> [11,] -0.061721013 -0.176769170 -0.059312332 -0.135633390 #> [12,] 0.021874954 -0.133777712 0.028889403 -0.130808437 #> [13,] 0.067777695 0.129187849 -0.026100120 0.002593292 #> [14,] -0.122270920 0.110865328 0.084858637 0.233903944 #> [15,] -0.014192027 0.086100909 0.030268607 0.243927135 #> [16,] -0.135970691 -0.043211895 -0.032229963 -0.134840827 #> [17,] 0.007608049 -0.079445537 -0.013675459 0.102010778 #> [18,] -0.010343806 0.132392790 -0.151237533 -0.123955960 #> [19,] 0.252797503 0.253113165 -0.197620792 0.099319824 #> [20,] -0.044505498 -0.049962311 0.233024977 0.176211754 #> [21,] 0.084292730 0.366833682 -0.220176991 -0.035984666 #> [22,] -0.373913824 -0.060767726 0.036430343 -0.105248408 #> [23,] 0.316816197 -0.191114879 -0.127549606 -0.112775343 #> [24,] 0.008174405 0.158066938 -0.121000263 0.232613924 #> [25,] -0.388590727 0.098542973 -0.045207238 0.003838229 #> [26,] -0.109916673 0.042028223 0.031122592 -0.173521656 #> [27,] -0.040845604 0.023057708 0.067839093 0.084198705 #> [28,] 0.080696347 -0.097192796 0.099373268 -0.166716428 #> [29,] -0.117530646 0.002589360 0.051425084 0.098244052 #> [30,] -0.037935138 -0.091390269 0.154099931 0.100237663 #> [31,] 0.109674339 0.158620750 -0.181802621 -0.112714822 #> [32,] 0.069971569 -0.425139230 -0.020032195 -0.193571920 #> [33,] 0.050674415 0.050518682 -0.010950361 -0.068936556 #> [34,] 0.065490413 0.142178491 0.174034864 0.072663828 #> [35,] -0.045676268 -0.057128729 0.050522160 -0.165689753 #> [36,] 0.368217440 0.096573493 0.120128008 0.020252183 #> [37,] 0.037286542 -0.056101952 -0.018563948 0.263830281 #> [38,] 0.094379823 -0.263914331 -0.002747609 -0.010850423 #> [39,] 0.153890231 0.071449851 -0.075881371 -0.060456457 #> [40,] -0.146742186 -0.117294226 -0.255229801 -0.124537184 #> [41,] -0.274840831 0.009051742 0.124862149 0.074433697 #> [42,] -0.008508998 0.110465493 0.215727137 0.269240087 #> [43,] 0.014213371 -0.041499271 0.097848087 0.105722611 #> [44,] -0.080890432 0.094198355 0.004240823 -0.187634605 #> [45,] 0.051925330 -0.007981636 0.044460236 -0.066537564 #> [46,] 0.213031014 0.168676929 0.097190703 0.046563300 #> [47,] 0.175716867 0.045500673 0.175902385 -0.030590597 #> [48,] -0.040155598 -0.120432098 -0.137764051 -0.088812037 #> [49,] 0.164103981 0.024976184 0.015955563 -0.232780151 #> [50,] -0.103164763 -0.135032022 0.029553283 0.267389634"},{"path":"https://fastcpd.xingchi.li/articles/examples-custom-model.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Custom logistic regression model","text":"evaluation vignette set FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/articles/examples-custom-model.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Custom logistic regression model","text":"","code":"knitr::opts_chunk$set(collapse = TRUE, comment = \"#>\", eval = FALSE) library(fastcpd) set.seed(1) x <- matrix(rnorm(1500, 0, 1), ncol = 5) theta <- rbind(rnorm(5, 0, 1), rnorm(5, 2, 1)) y <- c( rbinom(125, 1, 1 / (1 + exp(-x[1:125, ] %*% theta[1, ]))), rbinom(175, 1, 1 / (1 + exp(-x[126:300, ] %*% theta[2, ]))) ) binomial_data <- data.frame(y = y, x = x) result <- fastcpd.binomial(cbind(y, x), r.progress = FALSE, cost_adjustment = NULL) #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred summary(result) #> #> Call: #> fastcpd.binomial(data = cbind(y, x), r.progress = FALSE, cost_adjustment = NULL) #> #> Change points: #> 126 #> #> Cost values: #> 56.90525 30.76875 #> #> Parameters: #> segment 1 segment 2 #> 1 0.7259293 1.878525 #> 2 -1.0294802 2.704376 #> 3 1.0576503 3.702310 #> 4 -0.8812767 2.258796 #> 5 0.2419351 2.524173 logistic_loss <- function(data, theta) { x <- data[, -1] y <- data[, 1] u <- x %*% theta nll <- -y * u + log(1 + exp(u)) nll[u > 10] <- -y[u > 10] * u[u > 10] + u[u > 10] sum(nll) } logistic_gradient <- function(data, theta) { x <- data[nrow(data), -1] y <- data[nrow(data), 1] c(-(y - 1 / (1 + exp(-x %*% theta)))) * x } logistic_hessian <- function(data, theta) { x <- data[nrow(data), -1] prob <- 1 / (1 + exp(-x %*% theta)) (x %o% x) * c((1 - prob) * prob) } result <- fastcpd( y ~ . - 1, binomial_data, epsilon = 1e-5, cost = logistic_loss, cost_gradient = logistic_gradient, cost_hessian = logistic_hessian, r.progress = FALSE ) summary(result) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = binomial_data, cost = logistic_loss, #> cost_gradient = logistic_gradient, cost_hessian = logistic_hessian, #> epsilon = 1e-05, r.progress = FALSE) #> #> Change points: #> 22 125 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 -59.20045 0.8170446 1.902379 #> 2 -34.56676 -0.9600438 2.751578 #> 3 216.53373 0.9353306 3.734179 #> 4 -80.96420 -0.7393653 2.247423 #> 5 51.25224 0.1390591 2.535372"},{"path":"https://fastcpd.xingchi.li/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Xingchi Li. Author, maintainer, copyright holder. Xianyang Zhang. Author, copyright holder.","code":""},{"path":"https://fastcpd.xingchi.li/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"Li X, Zhang X (2024). “fastcpd: Fast Change Point Detection R.” doi:10.48550/arXiv.2404.05933. Zhang X, Dawn T (2023). “Sequential Gradient Descent Quasi-Newton's Method Change-Point Analysis.” Ruiz, Francisco, Dy, Jennifer, van de Meent, Jan-Willem (eds.), Proceedings 26th International Conference Artificial Intelligence Statistics, volume 206 series Proceedings Machine Learning Research, 1129–1143. https://proceedings.mlr.press/v206/zhang23b.html.","code":"@Misc{, title = {fastcpd: Fast Change Point Detection in R}, author = {Xingchi Li and Xianyang Zhang}, year = {2024}, doi = {10.48550/arXiv.2404.05933}, publisher = {arXiv}, } @InProceedings{, title = {Sequential Gradient Descent and Quasi-Newton's Method for Change-Point Analysis}, author = {Xianyang Zhang and Trisha Dawn}, year = {2023}, booktitle = {Proceedings of The 26th International Conference on Artificial Intelligence and Statistics}, volume = {206}, pages = {1129--1143}, editor = {{Ruiz} and {Francisco} and {Dy} and {Jennifer} and {van de Meent} and {Jan-Willem}}, series = {Proceedings of Machine Learning Research}, month = {25--27 Apr}, publisher = {PMLR}, pdf = {https://proceedings.mlr.press/v206/zhang23b/zhang23b.pdf}, url = {https://proceedings.mlr.press/v206/zhang23b.html}, abstract = {One common approach to detecting change-points is minimizing a cost function over possible numbers and locations of change-points. The framework includes several well-established procedures, such as the penalized likelihood and minimum description length. Such an approach requires finding the cost value repeatedly over different segments of the data set, which can be time-consuming when (i) the data sequence is long and (ii) obtaining the cost value involves solving a non-trivial optimization problem. This paper introduces a new sequential updating method (SE) to find the cost value effectively. The core idea is to update the cost value using the information from previous steps without re-optimizing the objective function. The new method is applied to change-point detection in generalized linear models and penalized regression. Numerical studies show that the new approach can be orders of magnitude faster than the Pruned Exact Linear Time (PELT) method without sacrificing estimation accuracy.}, }"},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"overview","dir":"","previous_headings":"","what":"Overview","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd (fast change point detection) fast implmentation change point detection methods R. fastcpd package designed find change points fast manner. easy install extensible kinds change point problems user specified cost function apart built-cost functions. learn behind algorithms: fastcpd: Fast Change Point Detection R Sequential Gradient Descent Quasi-Newton’s Method Change-Point Analysis","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"","code":"# Install from CRAN install.packages(\"fastcpd\") # Development version from r-universe with CRAN version as a fallback install.packages( \"fastcpd\", repos = c(\"https://doccstat.r-universe.dev\", \"https://cloud.r-project.org\") ) ## install.packages(\"pak\") pak::pak(\"doccstat/fastcpd\") ## install.packages(\"devtools\") devtools::install_github(\"doccstat/fastcpd\") # conda-forge is a fork from CRAN and may not be up-to-date # Use mamba mamba install r-fastcpd # Use conda conda install -c conda-forge r-fastcpd"},{"path":"https://fastcpd.xingchi.li/index.html","id":"faq","dir":"","previous_headings":"Installation","what":"FAQ","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd depends following packages: Rcpp, C++ source code compilation. RcppArmadillo, fast linear algebra. fastglm, fast generalized linear models. glmnet, penalized regression. ggplot2, data visualization. ’re compiling source, can run following command see complete set system packages needed machine. suggested packages required main functionality package. required vignettes. want learn package comparison vignettes, either check vignettes CRAN pkgdown generated documentation. package able install Mac Linux distribution without problems dependencies installed. However, encountered problems related gfortran, might RcppArmadillo installed previously. Try Mac OSX stackoverflow solution Linux stackover solution trouble installing RcppArmadillo.","code":"pak::pkg_sysreqs(\"doccstat/fastcpd\") #> ── Install scripts ───────────────────────────────────────────── Ubuntu 20.04 #> apt-get -y update #> apt-get -y install libcurl4-openssl-dev libssl-dev zlib1g-dev make #> #> ── Packages and their system dependencies ─────────────────────────────────── #> curl – libcurl4-openssl-dev, libssl-dev #> data.table – zlib1g-dev #> fs – make #> openssl – libssl-dev"},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"r-shiny-app","dir":"","previous_headings":"Cheatsheet","what":"R Shiny App","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Available soon: rshiny.fastcpd.xingchi.li","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"usage","dir":"","previous_headings":"","what":"Usage","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"hard demonstrate features fastcpd single example due flexibility package. examples, please refer function reference. r.progress = FALSE used suppress progress bar. Users expected see progress bar running code default.","code":"set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } result <- fastcpd::fastcpd.ar(x[3 + seq_len(n)], 3, r.progress = FALSE) summary(result) #> #> Call: #> fastcpd::fastcpd.ar(data = x[3 + seq_len(n)], order = 3, r.progress = FALSE) #> #> Change points: #> 614 #> #> Cost values: #> 2743.759 2028.588 #> #> Parameters: #> segment 1 segment 2 #> 1 0.57120256 0.2371809 #> 2 -0.20985108 0.4031244 #> 3 0.08221978 0.2290323 plot(result)"},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"main-function","dir":"","previous_headings":"Examples","what":"Main function","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"time-series","dir":"","previous_headings":"Examples > Wrapper functions","what":"Time series","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"AR(p): fastcpd_ar ARIMA(p, d, q): fastcpd_arima ARMA(p, q): fastcpd_arma GARCH(p, q): fastcpd_garch VAR(p): fastcpd_var General time series: fastcpd_ts","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"unlabeled-data","dir":"","previous_headings":"Examples > Wrapper functions","what":"Unlabeled data","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Mean change: fastcpd_mean Variance change: fastcpd_variance Mean /variance change: fastcpd_meanvariance","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"regression-data","dir":"","previous_headings":"Examples > Wrapper functions","what":"Regression data","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Logistic regression: fastcpd_binomial Penalized linear regression: fastcpd_lasso Linear regression: fastcpd_lm Poisson regression: fastcpd_poisson","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"variance-estimation","dir":"","previous_headings":"Examples > Utility functions","what":"Variance estimation","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Variance estimation ARMA models: variance_arma Variance estimation linear models: variance_lm Variance estimation mean change models: variance_mean Variance estimation median change models: variance_median","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"class-methods","dir":"","previous_headings":"Examples > Utility functions","what":"Class methods","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Plot: plot Print: print Show: show Summary: summary","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"data","dir":"","previous_headings":"Examples","what":"Data","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Bitcoin Market Price (USD): bitcoin Occupancy Detection Data Set: occupancy Transcription Profiling 57 Human Bladder Carcinoma Samples: transcriptome UK Seatbelts Data: uk_seatbelts Well-log Dataset Numerical Bayesian Methods Applied Signal Processing: well_log","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"main-class","dir":"","previous_headings":"Examples","what":"Main class","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"make-contributions","dir":"","previous_headings":"","what":"Make contributions","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Fork repo. Create new branch main branch. Make changes commit . Please follow Google’s R style guide naming variables functions. adding new family models new cost functions corresponding gradient Hessian, please add src/fastcpd_class_cost.cc proper example tests vignettes/gallery.Rmd tests/testthat/test-gallery.R. Add family name src/fastcpd_constants.h. [Recommended] Add new wrapper function R/fastcpd_wrappers.R new family models move examples new wrapper function roxygen examples. Add new wrapper function corresponding section _pkgdown.yml. Push changes fork. Create pull request. Make sure pull request create new warnings errors devtools::check().","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"contact-us","dir":"","previous_headings":"","what":"Contact us","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"File ticket GitHub Issues. Contact authors specified DESCRIPTION.","code":""},{"path":[]},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":null,"dir":"Reference","previous_headings":"","what":"Bitcoin Market Price (USD) — bitcoin","title":"Bitcoin Market Price (USD) — bitcoin","text":"average USD market price across major bitcoin exchanges.","code":""},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Bitcoin Market Price (USD) — bitcoin","text":"","code":"bitcoin"},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Bitcoin Market Price (USD) — bitcoin","text":"data frame 1354 rows 2 variables: date POSIXct,POSIXt (TZ: \"UTC\") 2019-01-02 2023-10-28 price average USD market price across major bitcoin exchanges","code":""},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Bitcoin Market Price (USD) — bitcoin","text":"","code":""},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Bitcoin Market Price (USD) — bitcoin","text":"","code":"# \\donttest{ if (requireNamespace(\"ggplot2\", quietly = TRUE)) { p <- ggplot2::ggplot(bitcoin, ggplot2::aes(x = date, y = price)) + ggplot2::geom_line() print(p) result <- suppressWarnings(fastcpd.garch( diff(log(bitcoin$price[600:900])), c(1, 1), beta = \"BIC\", cost_adjustment = \"BIC\" )) summary(result) bitcoin$date[result@cp_set + 600] plot(result) cp_dates <- bitcoin[600 + result@cp_set + 1, \"date\"] ggplot2::ggplot( data = data.frame( x = bitcoin$date[600:900], y = bitcoin$price[600:900] ), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_line(color = \"steelblue\") + ggplot2::geom_vline( xintercept = cp_dates, color = \"red\", linetype = \"dotted\", linewidth = 0.5, alpha = 0.7 ) + ggplot2::labs( x = \"Year\", y = \"Bitcoin price in USD\" ) + ggplot2::annotate( \"text\", x = cp_dates, y = 2000, label = as.character(cp_dates), color = \"steelblue\" ) + ggplot2::theme_bw() } #> Registered S3 method overwritten by 'quantmod': #> method from #> as.zoo.data.frame zoo #> #> Call: #> fastcpd.garch(data = diff(log(bitcoin$price[600:900])), order = c(1, #> 1), beta = \"BIC\", cost_adjustment = \"BIC\") #> #> Change points: #> 128 #> #> Cost values: #> -328.3151 -324.7008 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd-class.html","id":null,"dir":"Reference","previous_headings":"","what":"An S4 class to store the output created with fastcpd() — fastcpd-class","title":"An S4 class to store the output created with fastcpd() — fastcpd-class","text":"S4 class stores output fastcpd() fastcpd.family. fastcpd object consist several slots including call fastcpd(), data used, family model, change points, cost values, residuals, estimated parameters boolean indicating whether model fitted change points change points parameters, can select using @.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd-class.html","id":"slots","dir":"Reference","previous_headings":"","what":"Slots","title":"An S4 class to store the output created with fastcpd() — fastcpd-class","text":"call call function. data data passed function. order order time series model. family family model. cp_set set change points. cost_values cost function values segment. residuals residuals model change points. Used built-families. thetas estimated parameters segment. Used built-families. cp_only boolean indicating whether fastcpd() run return change points change points estimated parameters cost values segment.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently — fastcpd","title":"Find change points efficiently — fastcpd","text":"fastcpd() takes formulas, data, families extra parameters returns fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently — fastcpd","text":"","code":"fastcpd( formula = y ~ . - 1, data, beta = \"MBIC\", cost_adjustment = \"MBIC\", family = NULL, cost = NULL, cost_gradient = NULL, cost_hessian = NULL, line_search = c(1), lower = rep(-Inf, p), upper = rep(Inf, p), pruning_coef = 0, segment_count = 10, trim = 0.02, momentum_coef = 0, multiple_epochs = function(x) 0, epsilon = 1e-10, order = c(0, 0, 0), p = ncol(data) - 1, cp_only = FALSE, vanilla_percentage = 0, warm_start = FALSE, ... )"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently — fastcpd","text":"formula formula object specifying model fitted. (optional) response variable LHS formula, covariates RHS. naming variables used formula consistent column names data frame provided data. intercept term removed formula. response variable needed mean/variance change models time series models. default, intercept column added data, similar lm() function. Thus, suggested users remove intercept term appending - 1 formula. Note fastcpd.family functions require formula input. data data frame dimension \\(T \\times d\\) containing data segmented (row denotes data point \\(z_t \\\\mathbb{R}^d\\) \\(t = 1, \\ldots, T\\)) required main function, matrix vector input also accepted fastcpd.family functions. beta Penalty criterion number change points. parameter takes string value \"BIC\", \"MBIC\", \"MDL\" numeric value. numeric value provided, value used penalty. default, mBIC criterion used, \\(\\beta = (p + 2) \\log(T) / 2\\). parameter usage paired cost_adjustment described . Discussions penalty criterion can found references. cost_adjustment Cost adjustment criterion. can \"BIC\", \"MBIC\", \"MDL\" NULL. default, cost adjustment criterion set \"MBIC\". \"MBIC\" \"MDL\" criteria modify cost function adding negative adjustment term cost function. \"BIC\" NULL modify cost function. Details can found references. family Family class change point model. can \"mean\" mean change, \"variance\" variance change, \"meanvariance\" mean /variance change, \"lm\" linear regression, \"binomial\" logistic regression, \"poisson\" Poisson regression, \"lasso\" penalized linear regression, \"ar\" AR(\\(p\\)) models, \"arma\" ARMA(\\(p\\), \\(q\\)) models, \"arima\" ARIMA(\\(p\\), \\(d\\), \\(q\\)) models, \"garch\" GARCH(\\(p\\), \\(q\\)) models, \"var\" VAR(\\(p\\)) models \"custom\" user-specified custom models. Omitting parameter specifying parameter \"custom\" NULL, case, users must specify custom cost function. cost Cost function used. cost, cost_gradient, cost_hessian specified time family built-families cost functions implemented C++ provide better performance. specified, default negative log-likelihood corresponding family. Custom cost functions can provided following two formats: cost = function(data) {...} cost = function(data, theta) {...} Users can specify loss function using second format used calculate cost value. formats, input data subset original data frame form matrix (matrix single column case univariate data set). first format, specified cost function directly calculates cost value. fastcpd() performs vanilla PELT algorithm, cost_gradient cost_hessian provided since parameter updating necessary vanilla PELT. second format, loss function \\(\\sum_{= s}^t l(z_i, \\theta)\\) provided, optimized parameter \\(\\theta\\) obtain cost value. detailed discussion custom cost function usage can found references. cost_gradient Gradient custom cost function. Example usage: gradient function takes two inputs, first matrix representing segment data, similar format used cost function, second parameter needs optimized. gradient function returns value gradient loss function, .e., \\(\\sum_{= s}^t \\nabla l(z_i, \\theta)\\). cost_hessian Hessian custom loss function. Hessian function takes two inputs, first matrix representing segment data, similar format used cost function, second parameter needs optimized. gradient function returns Hessian loss function, .e., \\(\\sum_{= s}^t \\nabla^2 l(z_i, \\theta)\\). line_search vector numeric values provided, line search performed find optimal step size update. Detailed usage line_search can found references. lower Lower bound parameters. Used specify domain parameters gradient descent step. specified, lower bound set -Inf parameters. lower especially useful estimated parameters take positive values, noise variance. upper Upper bound parameters. Used specify domain parameters gradient descent step. specified, upper bound set Inf parameters. pruning_coef Pruning coefficient $c_0$ used pruning step PELT algorithm default value 0. cost_adjustment specified \"MBIC\", adjustment term \\(p\\log(2)\\) added pruning coefficient. cost_adjustment specified \"MDL\", adjustment term \\(p\\log_2(2)\\) added pruning coefficient. Detailed discussion pruning coefficient can found references. segment_count initial guess number segments. specified, initial guess number segments 10. initial guess affects initial estimates parameters SeGD. trim Trimming boundary change points change point close boundary counted change point. parameter also specifies minimum distance two change points. several change points mutual distances smaller trim * nrow(data), change points merged one single change point. value parameter 0 1. momentum_coef Momentum coefficient applied update. parameter used loss function bad-shaped maintaining momentum previous update desired. Default value 0, meaning algorithm maintain momentum default. multiple_epochs function can specified adaptive number multiple epochs can utilized improve algorithm's performance. multiple_epochs function length data segment. function returns integer indicating many epochs performed apart default update. default, function returns zero, meaning multiple epochs used update parameters. Example usage: function let SeGD perform parameter updates additional epoch segment length less 100 additional epoch segments lengths greater equal 100. epsilon Epsilon avoid numerical issues. used Hessian computation Logistic Regression Poisson Regression. order Order AR(\\(p\\)), VAR(\\(p\\)) ARIMA(\\(p\\), \\(d\\), \\(q\\)) model. p Number covariates model. specified, number covariates inferred data, .e., p = ncol(data) - 1. parameter superseded order case time series models: \"ar\", \"var\", \"arima\". cp_only TRUE, change points returned. Otherwise, cost function values together estimated parameters segment also returned. default value set FALSE plot can used visualize results built-model. cp_only performance impact algorithm, since cost values estimated parameters segment need calculated stored. users interested change points, setting cp_only TRUE help computational cost. vanilla_percentage parameter \\(v\\) zero one. segment, length \\(vT\\), cost value computed performing exact minimization loss function parameter. length greater \\(vT\\), cost value approximated SeGD. Therefore, parameter induces algorithm can interpreted interpolation dynamic programming SeGD (\\(v = 0\\)) vanilla PELT (\\(v = 1\\)). readers referred references details. warm_start TRUE, algorithm use estimated parameters previous segment initial value current segment. parameter used \"glm\" families. ... parameters specific models. include.mean used determine mean/intercept term included ARIMA(\\(p\\), \\(d\\), \\(q\\)) GARCH(\\(p\\), \\(q\\)) models. r.clock used create RcppClock object record time spent C++ code. Default empty string. set non-empty string, object specified name created. Usage: library(RcppClock); plot(VARIABLE_NAME). r.progress used control progress bar. default progress bar shown. disable , set r.progress = FALSE. p.response used specify number response variables. parameter especially useful linear models multivariate responses.","code":"cost_gradient = function(data, theta) { ... return(gradient) } multiple_epochs = function(segment_length) { if (segment_length < 100) 1 else 0 }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently — fastcpd","text":"fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"gallery","dir":"Reference","previous_headings":"","what":"Gallery","title":"Find change points efficiently — fastcpd","text":"https://github.com/doccstat/fastcpd/tree/main/tests/testthat/examples","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"references","dir":"Reference","previous_headings":"","what":"References","title":"Find change points efficiently — fastcpd","text":"Xingchi Li, Xianyang Zhang (2024). ``fastcpd: Fast Change Point Detection R.'' arXiv:2404.05933, https://arxiv.org/abs/2404.05933. Xianyang Zhang, Trisha Dawn (2023). ``Sequential Gradient Descent Quasi-Newton's Method Change-Point Analysis.'' Ruiz, Francisco, Dy, Jennifer, van de Meent, Jan-Willem (eds.), Proceedings 26th International Conference Artificial Intelligence Statistics, volume 206 series Proceedings Machine Learning Research, 1129-1143.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently — fastcpd","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 200 p <- 4 d <- 2 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_1 <- matrix(runif(8, -3, -1), nrow = p) theta_2 <- matrix(runif(8, -1, 3), nrow = p) y <- rbind( x[1:125, ] %*% theta_1 + mvtnorm::rmvnorm(125, rep(0, d), 3 * diag(d)), x[126:n, ] %*% theta_2 + mvtnorm::rmvnorm(75, rep(0, d), 3 * diag(d)) ) result_mlm <- fastcpd( cbind(y.1, y.2) ~ . - 1, cbind.data.frame(y = y, x = x), family = \"lm\" ) summary(result_mlm) } #> #> Call: #> fastcpd(formula = cbind(y.1, y.2) ~ . - 1, data = cbind.data.frame(y = y, #> x = x), family = \"lm\") #> #> Change points: #> 125 #> #> Cost values: #> 501.0668 296.4026 if ( requireNamespace(\"mvtnorm\", quietly = TRUE) && requireNamespace(\"stats\", quietly = TRUE) ) { set.seed(1) n <- 400 + 300 + 500 p <- 5 x <- mvtnorm::rmvnorm(n, mean = rep(0, p), sigma = diag(p)) theta <- rbind( mvtnorm::rmvnorm(1, mean = rep(0, p - 3), sigma = diag(p - 3)), mvtnorm::rmvnorm(1, mean = rep(5, p - 3), sigma = diag(p - 3)), mvtnorm::rmvnorm(1, mean = rep(9, p - 3), sigma = diag(p - 3)) ) theta <- cbind(theta, matrix(0, 3, 3)) theta <- theta[rep(seq_len(3), c(400, 300, 500)), ] y_true <- rowSums(x * theta) factor <- c( 2 * stats::rbinom(400, size = 1, prob = 0.95) - 1, 2 * stats::rbinom(300, size = 1, prob = 0.95) - 1, 2 * stats::rbinom(500, size = 1, prob = 0.95) - 1 ) y <- factor * y_true + stats::rnorm(n) data <- cbind.data.frame(y, x) huber_threshold <- 1 huber_loss <- function(data, theta) { residual <- data[, 1] - data[, -1, drop = FALSE] %*% theta indicator <- abs(residual) <= huber_threshold sum( residual^2 / 2 * indicator + huber_threshold * ( abs(residual) - huber_threshold / 2 ) * (1 - indicator) ) } huber_loss_gradient <- function(data, theta) { residual <- c(data[nrow(data), 1] - data[nrow(data), -1] %*% theta) if (abs(residual) <= huber_threshold) { -residual * data[nrow(data), -1] } else { -huber_threshold * sign(residual) * data[nrow(data), -1] } } huber_loss_hessian <- function(data, theta) { residual <- c(data[nrow(data), 1] - data[nrow(data), -1] %*% theta) if (abs(residual) <= huber_threshold) { outer(data[nrow(data), -1], data[nrow(data), -1]) } else { 0.01 * diag(length(theta)) } } huber_regression_result <- fastcpd( formula = y ~ . - 1, data = data, beta = (p + 1) * log(n) / 2, cost = huber_loss, cost_gradient = huber_loss_gradient, cost_hessian = huber_loss_hessian ) summary(huber_regression_result) } #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data, beta = (p + 1) * log(n)/2, #> cost = huber_loss, cost_gradient = huber_loss_gradient, cost_hessian = huber_loss_hessian) #> #> Change points: #> 418 726 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 -0.464532607 2.760546489 8.744706508 #> 2 -0.995964940 5.059690163 9.506534878 #> 3 -0.093877079 0.023321737 -0.008908851 #> 4 -0.002000477 -0.090091843 -0.047909865 #> 5 0.009364910 -0.009543696 0.025171681 # \\donttest{ set.seed(1) p <- 5 x <- matrix(rnorm(375 * p, 0, 1), ncol = p) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(200, 1, 1 / (1 + exp(-x[1:200, ] %*% theta[1, ]))), rbinom(175, 1, 1 / (1 + exp(-x[201:375, ] %*% theta[2, ]))) ) data <- data.frame(y = y, x = x) result_builtin <- suppressWarnings(fastcpd.binomial(data)) logistic_loss <- function(data, theta) { x <- data[, -1] y <- data[, 1] u <- x %*% theta nll <- -y * u + log(1 + exp(u)) nll[u > 10] <- -y[u > 10] * u[u > 10] + u[u > 10] sum(nll) } logistic_loss_gradient <- function(data, theta) { x <- data[nrow(data), -1] y <- data[nrow(data), 1] c(-(y - 1 / (1 + exp(-x %*% theta)))) * x } logistic_loss_hessian <- function(data, theta) { x <- data[nrow(data), -1] prob <- 1 / (1 + exp(-x %*% theta)) (x %o% x) * c((1 - prob) * prob) } result_custom <- fastcpd( formula = y ~ . - 1, data = data, epsilon = 1e-5, cost = logistic_loss, cost_gradient = logistic_loss_gradient, cost_hessian = logistic_loss_hessian ) cat( \"Change points detected by built-in logistic regression model: \", result_builtin@cp_set, \"\\n\", \"Change points detected by custom logistic regression model: \", result_custom@cp_set, \"\\n\", sep = \"\" ) #> Change points detected by built-in logistic regression model: 202 #> Change points detected by custom logistic regression model: 202 result_custom_two_epochs <- fastcpd( formula = y ~ . - 1, data = data, multiple_epochs = function(segment_length) 1, epsilon = 1e-5, cost = logistic_loss, cost_gradient = logistic_loss_gradient, cost_hessian = logistic_loss_hessian ) summary(result_custom_two_epochs) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data, cost = logistic_loss, #> cost_gradient = logistic_loss_gradient, cost_hessian = logistic_loss_hessian, #> multiple_epochs = function(segment_length) 1, epsilon = 1e-05) #> #> Change points: #> 202 #> #> Parameters: #> segment 1 segment 2 #> 1 -0.4884741 1.9829469 #> 2 -1.5637585 1.6145734 #> 3 -1.6874162 4.6191745 #> 4 -0.4192355 -0.1568114 #> 5 2.0315690 2.3956803 # } # \\donttest{ if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 480 p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) small_lasso_data <- cbind.data.frame(y, x) result_no_vp <- fastcpd.lasso( small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, pruning_coef = 0 ) summary(result_no_vp) result_20_vp <- fastcpd.lasso( small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, vanilla_percentage = 0.2, pruning_coef = 0 ) summary(result_20_vp) } #> #> Call: #> fastcpd.lasso(data = small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, #> pruning_coef = 0) #> #> Change points: #> 79 202 325 #> #> Cost values: #> 13.72919 225.1447 85.69365 47.51894 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.2528808428 4.848522259 -2.995657982 #> [2,] -2.668688731 -2.2387392752 4.183821811 3.100656318 #> [3,] -4.354248176 1.1867292005 4.878538526 3.711902167 #> [4,] -4.662566697 0.5043975083 2.724613946 -3.128550136 #> [5,] -4.820139326 0.3907766479 4.658932261 3.187247302 #> [6,] -3.860308911 -2.4708867317 3.364992430 4.322270139 #> [7,] 0.117234534 -0.2337451460 0.037468301 -0.005247904 #> [8,] 0.117552281 -0.0774905233 -0.171677542 -0.029653585 #> [9,] -0.188756652 0.4850028633 -0.149864384 0.068109708 #> [10,] -0.074530965 -0.2693267166 -0.099057468 0.007239663 #> [11,] -0.061721013 -0.3929366442 0.057377332 -0.168703949 #> [12,] 0.021874954 -0.4294667473 0.112828002 0.080008740 #> [13,] 0.067777695 0.3501129775 0.065290363 0.081191140 #> [14,] -0.122270920 0.4348764010 0.124026153 0.093730519 #> [15,] -0.014192027 -0.1009201441 0.055676406 -0.022302179 #> [16,] -0.135970691 -0.3228209051 -0.322266525 -0.093584779 #> [17,] 0.007608049 -0.0697777666 -0.173314905 0.003534974 #> [18,] -0.010343806 0.1576855458 0.036483478 -0.169347885 #> [19,] 0.252797503 0.5568680926 0.079891379 0.146785657 #> [20,] -0.044505498 -0.5831313510 -0.136705472 0.008129199 #> [21,] 0.084292730 0.5462807777 -0.201700276 -0.168701992 #> [22,] -0.373913824 0.3760916945 0.061397643 0.032865121 #> [23,] 0.316816197 -0.2806604425 -0.162557542 -0.007149145 #> [24,] 0.008174405 0.2296689003 -0.036906468 -0.133226098 #> [25,] -0.388590727 0.6583142640 -0.162051056 0.017998768 #> [26,] -0.109916673 0.0687814570 0.068395416 -0.060802578 #> [27,] -0.040845604 -0.0868845012 -0.208878583 0.028223323 #> [28,] 0.080696347 -0.1975246890 -0.131401498 -0.070869199 #> [29,] -0.117530646 0.0006050774 -0.034054322 -0.066685344 #> [30,] -0.037935138 0.2909132628 0.054106263 0.030597693 #> [31,] 0.109674339 0.2194249070 -0.124483059 0.076983144 #> [32,] 0.069971569 -0.7939801174 0.065441362 0.040494938 #> [33,] 0.050674415 -0.2109295276 0.002639198 0.069521670 #> [34,] 0.065490413 0.6682523717 0.207420084 0.045711660 #> [35,] -0.045676268 -0.5333420170 0.171445009 -0.109004764 #> [36,] 0.368217440 -0.0662157788 -0.032932140 -0.018136377 #> [37,] 0.037286542 -0.2511805574 -0.363798390 0.117605382 #> [38,] 0.094379823 -0.0542487631 0.164432960 -0.049479163 #> [39,] 0.153890231 -0.0926699875 -0.025621414 -0.121494687 #> [40,] -0.146742186 -0.1892763351 -0.102276477 -0.048861175 #> [41,] -0.274840831 -0.0377048905 0.059701562 0.110453514 #> [42,] -0.008508998 -0.0555288478 -0.052656118 0.006037477 #> [43,] 0.014213371 -0.4104375842 -0.017832766 -0.058840493 #> [44,] -0.080890432 0.0356020878 -0.065307053 -0.003562026 #> [45,] 0.051925330 0.0175766032 -0.016616109 -0.016646624 #> [46,] 0.213031014 -0.0719719280 0.025295461 -0.024318200 #> [47,] 0.175716867 0.1820716574 0.253325834 0.118459579 #> [48,] -0.040155598 -0.1721349330 -0.276607899 -0.058926739 #> [49,] 0.164103981 0.0096690123 -0.109759177 -0.055922804 #> [50,] -0.103164763 0.0179753021 0.228753665 -0.031882016 #> #> Call: #> fastcpd.lasso(data = small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, #> vanilla_percentage = 0.2, pruning_coef = 0) #> #> Change points: #> 80 202 320 #> #> Cost values: #> 15.30678 221.5585 30.87735 48.53091 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.615455985 -2.309283420 4.8750817914 -3.006202485 #> [2,] -2.678202261 -2.259223507 4.2707257840 3.096081968 #> [3,] -4.388362187 1.150652841 5.1265068595 3.718141515 #> [4,] -4.586413963 0.532197076 2.6470978568 -3.130632564 #> [5,] -4.852159806 0.429244510 4.6494743503 3.175562645 #> [6,] -3.823465522 -2.436663308 3.3618026781 4.342890940 #> [7,] 0.116154596 -0.264242115 -0.0727840814 -0.016395368 #> [8,] 0.160209372 -0.109364822 -0.0084325962 -0.022022294 #> [9,] -0.254798925 0.467114039 0.1063025022 0.070683538 #> [10,] -0.012955289 -0.319108692 0.0158283310 0.012791767 #> [11,] 0.018525857 -0.400042642 0.0460933309 -0.166042077 #> [12,] 0.015730602 -0.472837555 0.1629334863 0.077820866 #> [13,] 0.020134823 0.345191970 -0.0685630320 0.077436979 #> [14,] -0.019808550 0.469540855 0.0999989714 0.114046507 #> [15,] -0.040890576 -0.151353540 0.0281131032 -0.015966855 #> [16,] -0.213173204 -0.410553236 -0.0786859735 -0.095751983 #> [17,] 0.002688288 -0.026211969 -0.0836390903 -0.010689927 #> [18,] -0.013245030 0.110930058 -0.0648479795 -0.138057246 #> [19,] 0.153187151 0.519156799 -0.1634987155 0.158967697 #> [20,] -0.029584251 -0.562329939 0.0092242673 0.030169316 #> [21,] 0.100615839 0.571051467 -0.1762195594 -0.181503446 #> [22,] -0.313718826 0.415755188 0.1355664556 0.045649997 #> [23,] 0.246017007 -0.316648014 -0.1498815469 -0.025305755 #> [24,] 0.008216104 0.225545561 -0.0316699448 -0.137396624 #> [25,] -0.325075840 0.679118982 -0.0740016267 0.022336212 #> [26,] -0.057726812 0.117150669 0.0487654853 -0.059782090 #> [27,] 0.001998315 -0.033072437 0.0703865789 0.030431412 #> [28,] -0.019721901 -0.226018232 0.0632399484 -0.080706668 #> [29,] -0.100102752 0.034648703 0.0026192106 -0.051542901 #> [30,] -0.002193672 0.273435850 0.1065809370 0.048892128 #> [31,] 0.096046658 0.202400480 -0.1630891704 0.075434045 #> [32,] 0.151150818 -0.756337115 0.0007249262 0.033375101 #> [33,] 0.041988689 -0.224816396 0.0347939295 0.070983107 #> [34,] 0.053090539 0.656857298 0.1766941914 0.032575571 #> [35,] 0.048963552 -0.523861501 0.0843866955 -0.134534869 #> [36,] 0.426376926 -0.057992972 0.0393842237 -0.020673038 #> [37,] 0.090812237 -0.282803290 -0.0408241929 0.102043216 #> [38,] 0.179330109 -0.048614561 0.0909078160 -0.052763017 #> [39,] 0.138776838 -0.045196412 -0.0771155793 -0.120642848 #> [40,] -0.240934510 -0.179273912 -0.2239376855 -0.045775725 #> [41,] -0.343869848 -0.035394612 0.0549633763 0.123910218 #> [42,] 0.010690576 -0.111879602 0.0832100573 -0.005505113 #> [43,] 0.081490861 -0.376798426 0.0415210753 -0.056131976 #> [44,] -0.171008829 -0.007286424 0.0731242211 0.013524984 #> [45,] 0.047604778 -0.015973066 0.0405922084 -0.033628617 #> [46,] 0.127081043 -0.074390064 -0.0201907706 -0.029655833 #> [47,] 0.138821573 0.144223868 0.1718247772 0.119507574 #> [48,] -0.044021065 -0.180536724 -0.2437603101 -0.054349982 #> [49,] 0.198647494 0.008399368 0.0231802136 -0.056092814 #> [50,] -0.092465571 0.015580790 0.0812578961 -0.032491294 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"fastcpd_ar() fastcpd.ar() wrapper functions fastcpd() find change points AR(\\(p\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"","code":"fastcpd_ar(data, order = 0, ...) fastcpd.ar(data, order = 0, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"data numeric vector, matrix, data frame time series object. order positive integer specifying order AR model. ... arguments passed fastcpd(), example, segment_count. One special argument can passed include.mean, logical value indicating whether mean included model. default value TRUE.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"","code":"set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } result <- fastcpd.ar(x[3 + seq_len(n)], 3) summary(result) #> #> Call: #> fastcpd.ar(data = x[3 + seq_len(n)], order = 3) #> #> Change points: #> 614 #> #> Cost values: #> 2743.759 2028.588 #> #> Parameters: #> segment 1 segment 2 #> 1 0.57120256 0.2371809 #> 2 -0.20985108 0.4031244 #> 3 0.08221978 0.2290323 plot(result)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"fastcpd_arima() fastcpd.arima() wrapper functions fastcpd() find change points ARIMA(\\(p\\), \\(d\\), \\(q\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"","code":"fastcpd_arima(data, order = 0, ...) fastcpd.arima(data, order = 0, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"data numeric vector, matrix, data frame time series object. order vector length three specifying order ARIMA model. ... arguments passed fastcpd(), example, segment_count. One special argument can passed include.mean, logical value indicating whether mean included model. default value TRUE.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"","code":"# \\donttest{ set.seed(1) n <- 271 w <- rnorm(n + 1, 0, 3) dx <- rep(0, n + 1) x <- rep(0, n + 1) for (i in 1:180) { dx[i + 1] <- 0.8 * dx[i] + w[i + 1] - 0.5 * w[i] x[i + 1] <- x[i] + dx[i + 1] } for (i in 181:n) { dx[i + 1] <- -0.6 * dx[i] + w[i + 1] + 0.3 * w[i] x[i + 1] <- x[i] + dx[i + 1] } result <- fastcpd.arima( diff(x[1 + seq_len(n)]), c(1, 0, 1), segment_count = 3, include.mean = FALSE ) summary(result) #> #> Call: #> fastcpd.arima(data = diff(x[1 + seq_len(n)]), order = c(1, 0, #> 1), segment_count = 3, include.mean = FALSE) #> #> Change points: #> 178 #> #> Cost values: #> 435.4238 227.0583 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"fastcpd_arma() fastcpd.arma() wrapper functions fastcpd() find change points ARMA(\\(p\\), \\(q\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"","code":"fastcpd_arma(data, order = c(0, 0), ...) fastcpd.arma(data, order = c(0, 0), ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"data numeric vector, matrix, data frame time series object. order vector length two specifying order ARMA model. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"","code":"# \\donttest{ set.seed(1) n <- 300 w <- rnorm(n + 3, 0, 3) x <- rep(0, n + 3) for (i in 1:200) { x[i + 3] <- 0.1 * x[i + 2] - 0.3 * x[i + 1] + 0.1 * x[i] + 0.1 * w[i + 2] + 0.5 * w[i + 1] + w[i + 3] } for (i in 201:n) { x[i + 3] <- 0.3 * x[i + 2] + 0.1 * x[i + 1] - 0.3 * x[i] - 0.6 * w[i + 2] - 0.1 * w[i + 1] + w[i + 3] } result <- suppressWarnings( fastcpd.arma( data = x[3 + seq_len(n)], order = c(3, 2), segment_count = 3, lower = c(rep(-1, 3 + 2), 1e-10), upper = c(rep(1, 3 + 2), Inf), line_search = c(1, 0.1, 1e-2), beta = \"BIC\", cost_adjustment = \"BIC\" ) ) summary(result) #> #> Call: #> fastcpd.arma(data = x[3 + seq_len(n)], order = c(3, 2), segment_count = 3, #> lower = c(rep(-1, 3 + 2), 1e-10), upper = c(rep(1, 3 + 2), #> Inf), line_search = c(1, 0.1, 0.01), beta = \"BIC\", cost_adjustment = \"BIC\") #> #> Change points: #> 200 #> #> Cost values: #> 491.6904 251.4816 #> #> Parameters: #> segment 1 segment 2 #> 1 0.001350478 0.74342384 #> 2 -0.828668713 -0.07333667 #> 3 0.200084507 -0.36950210 #> 4 0.208486751 -1.27312849 #> 5 0.999994041 0.45204533 #> 6 7.831992812 8.83494421 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in logistic regression models — fastcpd_binomial","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"fastcpd_binomial() fastcpd.binomial() wrapper functions fastcpd() find change points logistic regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"","code":"fastcpd_binomial(data, ...) fastcpd.binomial(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 500 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(300, 1, 1 / (1 + exp(-x[1:300, ] %*% theta[1, ]))), rbinom(200, 1, 1 / (1 + exp(-x[301:n, ] %*% theta[2, ]))) ) result <- suppressWarnings(fastcpd.binomial(cbind(y, x))) summary(result) plot(result) } #> #> Call: #> fastcpd.binomial(data = cbind(y, x)) #> #> Change points: #> 302 #> #> Cost values: #> 124.4554 54.26381 #> #> Parameters: #> segment 1 segment 2 #> 1 -0.9260182 2.1294962 #> 2 -1.6033835 2.7583247 #> 3 1.0343338 2.3818010 #> 4 0.3653870 0.7261152"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_family.html","id":null,"dir":"Reference","previous_headings":"","what":"Wrapper functions for fastcpd — fastcpd_family","title":"Wrapper functions for fastcpd — fastcpd_family","text":"Wrapper functions fastcpd find change points various models.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"fastcpd_garch() fastcpd.garch() wrapper functions fastcpd() find change points GARCH(\\(p\\), \\(q\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"","code":"fastcpd_garch(data, order = c(0, 0), ...) fastcpd.garch(data, order = c(0, 0), ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"data numeric vector, matrix, data frame time series object. order positive integer vector length two specifying order GARCH model. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"","code":"# \\donttest{ set.seed(1) n <- 400 sigma_2 <- rep(1, n + 1) x <- rep(0, n + 1) for (i in seq_len(200)) { sigma_2[i + 1] <- 20 + 0.5 * x[i]^2 + 0.1 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } for (i in 201:400) { sigma_2[i + 1] <- 1 + 0.1 * x[i]^2 + 0.5 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } result <- suppressWarnings( fastcpd.garch(x[-1], c(1, 1), include.mean = FALSE) ) summary(result) #> #> Call: #> fastcpd.garch(data = x[-1], order = c(1, 1), include.mean = FALSE) #> #> Change points: #> 205 #> #> Cost values: #> 449.6614 178.5686 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"fastcpd_lasso() fastcpd.lasso() wrapper functions fastcpd() find change points penalized linear regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"","code":"fastcpd_lasso(data, ...) fastcpd.lasso(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"","code":"# \\donttest{ if ( requireNamespace(\"dplyr\", quietly = TRUE) && requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"mvtnorm\", quietly = TRUE) && requireNamespace(\"reshape2\", quietly = TRUE) ) { set.seed(1) n <- 480 p_true <- 5 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result <- fastcpd.lasso( cbind(y, x), multiple_epochs = function(segment_length) if (segment_length < 30) 1 else 0 ) summary(result) plot(result) thetas <- result@thetas thetas <- cbind.data.frame(thetas, t(theta_0)) names(thetas) <- c( \"segment 1\", \"segment 2\", \"segment 3\", \"segment 4\", \"segment 1 truth\", \"segment 2 truth\", \"segment 3 truth\", \"segment 4 truth\" ) thetas$coordinate <- c(seq_len(p_true), rep(\"rest\", p - p_true)) molten <- reshape2::melt(thetas, id.vars = \"coordinate\") molten <- dplyr::mutate( molten, segment = gsub(\"segment \", \"\", variable), segment = gsub(\" truth\", \"\", segment), height = as.numeric(gsub(\"segment.*\", \"\", segment)) + 0.2 * as.numeric(grepl(\"truth\", variable)), parameter = ifelse(grepl(\"truth\", variable), \"truth\", \"estimated\") ) ggplot2::ggplot() + ggplot2::geom_point( data = molten, ggplot2::aes( x = value, y = height, shape = coordinate, color = parameter ), size = 4 ) + ggplot2::ylim(0.8, 4.4) + ggplot2::ylab(\"segment\") + ggplot2::theme_bw() } #> #> Call: #> fastcpd.lasso(data = cbind(y, x), multiple_epochs = function(segment_length) if (segment_length < #> 30) 1 else 0) #> #> Change points: #> 80 200 320 #> #> Cost values: #> -33.53772 -2.964751 -4.538601 19.48282 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.687893678 -1.347364754 3.65464618 -2.68320467 #> [2,] -2.641775791 -2.722142529 2.25369487 3.91484879 #> [3,] -4.297655293 -2.279064483 4.73193630 -1.25039274 #> [4,] -4.356637349 1.117644847 4.19753119 -3.02828978 #> [5,] -4.563162167 -0.040732304 5.12149272 2.94566719 #> [6,] -0.037544892 0.093563258 0.06929427 0.19337556 #> [7,] -0.032180502 0.183459849 0.07694356 0.08343640 #> [8,] -0.121340500 0.143508371 -0.14206767 0.20150137 #> [9,] 0.228114500 0.087096913 -0.12489127 -0.05441234 #> [10,] 0.249594205 0.127790123 0.01855028 0.06403369 #> [11,] 0.121185946 -0.082768796 -0.04036376 -0.06970833 #> [12,] 0.086436979 -0.074336995 -0.25546800 -0.14816071 #> [13,] 0.033878715 0.164515279 0.08546067 0.07043841 #> [14,] -0.116324402 -0.147577009 0.02165927 -0.03678181 #> [15,] 0.088055326 0.040483693 0.18363877 0.07635422 #> [16,] -0.006839643 0.145071723 -0.01270428 -0.03708197 #> [17,] 0.042459016 0.004187653 0.02101507 -0.02227376 #> [18,] -0.044886170 -0.121886946 -0.06794188 0.02104911 #> [19,] 0.064254844 0.025662919 0.06775764 -0.06292499 #> [20,] 0.264382648 -0.030473566 0.09266098 0.05610440 #> [21,] -0.103292295 -0.024421785 0.02619333 -0.13185732 #> [22,] 0.224819510 0.117787479 -0.26153170 -0.09608036 #> [23,] -0.127726743 -0.113070877 0.09848171 -0.04150170 #> [24,] 0.185177947 0.007586467 -0.13529912 0.07395925 #> [25,] 0.132166684 -0.033427156 0.15292622 -0.10491541 #> [26,] -0.204139455 -0.284791058 0.12597351 -0.01359206 #> [27,] -0.203091983 -0.079616474 -0.12028823 -0.07730226 #> [28,] 0.163081293 0.012938143 0.01038811 0.19519095 #> [29,] 0.215170449 -0.135372096 0.03345574 -0.19036979 #> [30,] 0.199697345 -0.024836934 -0.10536087 0.01297619 #> [31,] 0.093075706 0.008479655 0.10480234 0.12841112 #> [32,] -0.231128671 0.056259142 0.07898864 0.00344448 #> [33,] 0.321769381 -0.143982228 -0.10975100 0.06738358 #> [34,] 0.089703182 -0.116702312 0.09320772 -0.10698341 #> [35,] 0.139247995 0.063062479 -0.10850835 0.15595021 #> [36,] 0.129089775 0.077552759 0.13091543 -0.15788233 #> [37,] -0.167999629 -0.040832358 -0.06478776 0.16724727 #> [38,] -0.126037849 0.110736887 -0.15924920 -0.04970495 #> [39,] -0.211132385 -0.229198706 -0.09567662 0.06684922 #> [40,] 0.321557058 -0.256192844 0.07924873 -0.03288319 #> [41,] 0.429749205 0.026979297 -0.07309375 -0.05840023 #> [42,] -0.011319985 -0.011363023 0.06767687 -0.04725431 #> [43,] 0.234802656 -0.019449223 0.13028901 -0.03556068 #> [44,] 0.184529802 0.020282379 -0.20988569 -0.03096108 #> [45,] -0.384355502 0.172850905 0.08402851 -0.02255405 #> [46,] 0.223274950 0.132941542 0.12073814 0.10020210 #> [47,] -0.313123582 -0.202596128 0.05480104 -0.07697244 #> [48,] -0.376893471 0.007438720 0.09792671 0.02638471 #> [49,] 0.089255534 -0.011341307 -0.08469776 -0.02137857 #> [50,] -0.041427938 0.181537606 -0.17195726 -0.10186665 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in linear regression models — fastcpd_lm","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"fastcpd_lm() fastcpd.lm() wrapper functions fastcpd() find change points linear regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"","code":"fastcpd_lm(data, ...) fastcpd.lm(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta_0[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta_0[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta_0[3, ] + rnorm(100, 0, 3) ) result_lm <- fastcpd.lm(cbind(y, x)) summary(result_lm) plot(result_lm) set.seed(1) n <- 600 p <- 4 d <- 2 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_1 <- matrix(runif(8, -3, -1), nrow = p) theta_2 <- matrix(runif(8, -1, 3), nrow = p) y <- rbind( x[1:350, ] %*% theta_1 + mvtnorm::rmvnorm(350, rep(0, d), 3 * diag(d)), x[351:n, ] %*% theta_2 + mvtnorm::rmvnorm(250, rep(0, d), 3 * diag(d)) ) result_mlm <- fastcpd.lm(cbind.data.frame(y = y, x = x), p.response = 2) summary(result_mlm) } #> #> Call: #> fastcpd.lm(data = cbind(y, x)) #> #> Change points: #> 97 201 #> #> Cost values: #> 517.5695 412.9626 459.8569 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 0.74291290 -0.6153049 0.8733473 #> 2 3.69465275 -0.5034948 0.3222868 #> 3 -1.24746871 2.2522352 1.0188455 #> 4 0.09579985 -1.9875126 2.2761340 #> #> Call: #> fastcpd.lm(data = cbind.data.frame(y = y, x = x), p.response = 2) #> #> Change points: #> 350 #> #> Cost values: #> 1405.82 993.8667"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in mean change models — fastcpd_mean","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"fastcpd_mean() fastcpd.mean() wrapper functions fastcpd() find mean change. function similar fastcpd() except data default matrix data frame vector row / element observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"","code":"fastcpd_mean(data, ...) fastcpd.mean(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"data matrix, data frame vector. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 3 data <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) result <- fastcpd.mean(data) summary(result) } #> #> Call: #> fastcpd.mean(data = data) #> #> Change points: #> 300 700 #> #> Cost values: #> 2558.102 3417.845 2551.517 set.seed(1) data <- c(rnorm(10000), rnorm(1000) + 1) (result_time <- system.time( result <- fastcpd.mean(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 0.114 0.098 0.074 result@cp_set #> [1] 10007 # \\donttest{ set.seed(1) data <- c(rnorm(10000), rnorm(10000, 1), rnorm(10000)) (result_time <- system.time( result <- fastcpd.mean(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 0.563 0.279 0.523 result@cp_set #> [1] 10007 19998 # } # \\donttest{ set.seed(1) data <- c(rnorm(10000), rnorm(10000, 1), rnorm(10000)) (result_time <- system.time( result <- fastcpd.mean( data, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE, cp_only = TRUE ) )) #> user system elapsed #> 0.238 0.284 0.192 result@cp_set #> [1] 10034 20082 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"fastcpd_meanvariance(), fastcpd.meanvariance(), fastcpd_mv(), fastcpd.mv() wrapper functions fastcpd() find meanvariance change. function similar fastcpd() except data default matrix data frame vector row / element observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"","code":"fastcpd_meanvariance(data, ...) fastcpd.meanvariance(data, ...) fastcpd_mv(data, ...) fastcpd.mv(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"data matrix, data frame vector. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 1 result <- fastcpd.mv( rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) ) summary(result) plot(result) } #> #> Call: #> fastcpd.mv(data = rbind(mvtnorm::rmvnorm(300, mean = rep(0, p), #> sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, #> p), sigma = diag(100, p)))) #> #> Change points: #> 300 700 1001 1300 1700 #> #> Cost values: #> 412.1898 586.8914 1139.531 433.9691 562.2071 1142.934 if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 4 result <- fastcpd.mv( rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) ) summary(result) } #> #> Call: #> fastcpd.mv(data = rbind(mvtnorm::rmvnorm(300, mean = rep(0, p), #> sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, #> p), sigma = diag(100, p)))) #> #> Change points: #> 300 700 1000 1300 1700 #> #> Cost values: #> 1714.968 2299.119 4500.027 1654.917 2259.023 4444.359 # \\donttest{ set.seed(1) data <- c(rnorm(2000, 0, 1), rnorm(2000, 1, 1), rnorm(2000, 1, 2)) (result_time <- system.time( result <- fastcpd.variance(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 1.278 0.000 1.277 result@cp_set #> [1] 1985 4000 # } # \\donttest{ set.seed(1) data <- c(rnorm(2000, 0, 1), rnorm(2000, 1, 1), rnorm(2000, 1, 2)) (result_time <- system.time( result <- fastcpd.variance( data, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = TRUE, cp_only = TRUE ) )) #> user system elapsed #> 1.053 0.000 1.053 result@cp_set #> [1] 1985 4000 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in Poisson regression models — fastcpd_poisson","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"fastcpd_poisson() fastcpd.poisson() wrapper functions fastcpd() find change points Poisson regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"","code":"fastcpd_poisson(data, ...) fastcpd.poisson(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"","code":"# \\donttest{ if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 1100 p <- 3 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) delta <- rnorm(p) theta_0 <- c(1, 0.3, -1) y <- c( rpois(500, exp(x[1:500, ] %*% theta_0)), rpois(300, exp(x[501:800, ] %*% (theta_0 + delta))), rpois(200, exp(x[801:1000, ] %*% theta_0)), rpois(100, exp(x[1001:1100, ] %*% (theta_0 - delta))) ) result <- fastcpd.poisson(cbind(y, x)) summary(result) plot(result) } #> #> Call: #> fastcpd.poisson(data = cbind(y, x)) #> #> Change points: #> 498 805 1003 #> #> Cost values: #> 230.0866 190.1381 82.77324 38.45199 #> #> Parameters: #> segment 1 segment 2 segment 3 segment 4 #> 1 1.020002 0.6391880 1.0424108 1.4451928 #> 2 0.275458 -0.2585056 0.2620085 0.9910079 #> 3 -1.048875 -0.5768481 -0.9632918 -1.4354638 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in time series data — fastcpd_ts","title":"Find change points efficiently in time series data — fastcpd_ts","text":"fastcpd_ts() fastcpd.ts() wrapper functions fastcpd() find change points time series data. function similar fastcpd() except data time series family one \"ar\", \"var\", \"arma\", \"arima\" \"garch\".","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in time series data — fastcpd_ts","text":"","code":"fastcpd_ts(data, family = NULL, order = c(0, 0, 0), ...) fastcpd.ts(data, family = NULL, order = c(0, 0, 0), ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in time series data — fastcpd_ts","text":"data numeric vector, matrix, data frame time series object. family character string specifying family time series. value one \"ar\", \"var\", \"arima\" \"garch\". order positive integer vector length less four specifying order time series. Possible combinations family : \"ar\", NUMERIC(1): AR(\\(p\\)) model using linear regression. \"var\", NUMERIC(1): VAR(\\(p\\)) model using linear regression. \"arima\", NUMERIC(3): ARIMA(\\(p\\), \\(d\\), \\(q\\)) model using forecast::Arima(). \"garch\", NUMERIC(2): GARCH(\\(p\\), \\(q\\)) model using tseries::garch(). ... arguments passed fastcpd(), example, segment_count. One special argument can passed include.mean, logical value indicating whether mean included model. default value TRUE.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in time series data — fastcpd_ts","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in time series data — fastcpd_ts","text":"","code":"# \\donttest{ set.seed(1) n <- 400 w <- rnorm(n + 4, 0, 0.1) x <- rep(NA, n) for (i in 1:200) { x[i] <- w[i + 4] - 5 / 3 * w[i + 3] + 11 / 12 * w[i + 2] - 5 / 12 * w[i + 1] + 1 / 6 * w[i] } for (i in 201:n) { x[i] <- w[i + 4] - 4 / 3 * w[i + 3] + 7 / 9 * w[i + 2] - 16 / 27 * w[i + 1] + 4 / 27 * w[i] } result <- fastcpd.ts( x, \"arma\", c(0, 4), lower = c(-2, -2, -2, -2, 1e-10), upper = c(2, 2, 2, 2, Inf), line_search = c(1, 0.1, 1e-2), trim = 0.05 ) summary(result) #> #> Call: #> fastcpd.ts(data = x, family = \"arma\", order = c(0, 4), lower = c(-2, #> -2, -2, -2, 1e-10), upper = c(2, 2, 2, 2, Inf), line_search = c(1, #> 0.1, 0.01), trim = 0.05) #> #> Change points: #> 209 #> #> Cost values: #> -188.7341 -168.4595 #> #> Parameters: #> segment 1 segment 2 #> 1 -1.626949633 -1.366413227 #> 2 0.925842329 0.850647424 #> 3 -0.472212640 -0.677337192 #> 4 0.181290578 0.193108171 #> 5 0.009523233 0.009914231 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"fastcpd_var() fastcpd.var() wrapper functions fastcpd_ts() find change points VAR(\\(p\\)) models. function similar fastcpd_ts() except data default matrix row observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"","code":"fastcpd_var(data, order = 0, ...) fastcpd.var(data, order = 0, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"data matrix, data frame time series object. order positive integer specifying order VAR model. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"","code":"set.seed(1) n <- 300 p <- 2 theta_1 <- matrix(c(-0.3, 0.6, -0.5, 0.4, 0.2, 0.2, 0.2, -0.2), nrow = p) theta_2 <- matrix(c(0.3, -0.4, 0.1, -0.5, -0.5, -0.2, -0.5, 0.2), nrow = p) x <- matrix(0, n + 2, p) for (i in 1:200) { x[i + 2, ] <- theta_1 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } for (i in 201:n) { x[i + 2, ] <- theta_2 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } result <- fastcpd.var(x, 2) summary(result) #> #> Call: #> fastcpd.var(data = x, order = 2) #> #> Change points: #> 204 #> #> Cost values: #> 561.1008 283.7941"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in variance change models — fastcpd_variance","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"fastcpd_variance() fastcpd.variance() wrapper functions fastcpd() find variance change. function similar fastcpd() except data default matrix data frame vector row / element observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"","code":"fastcpd_variance(data, ...) fastcpd.variance(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"data matrix, data frame vector. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"","code":"set.seed(1) data <- c(rnorm(300, 0, 1), rnorm(400, 0, 100), rnorm(300, 0, 1)) result <- fastcpd.variance(data) summary(result) #> #> Call: #> fastcpd.variance(data = data) #> #> Change points: #> 300 700 #> #> Cost values: #> 703.3755 2430.33 705.6112 if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 3 result <- fastcpd.variance( rbind( mvtnorm::rmvnorm( 300, rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p)) ), mvtnorm::rmvnorm( 400, rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p)) ), mvtnorm::rmvnorm( 300, rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p)) ) ) ) summary(result) } #> #> Call: #> fastcpd.variance(data = rbind(mvtnorm::rmvnorm(300, rep(0, p), #> crossprod(matrix(runif(p^2) * 2 - 1, p))), mvtnorm::rmvnorm(400, #> rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p))), mvtnorm::rmvnorm(300, #> rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p))))) #> #> Change points: #> 300 700 #> #> Cost values: #> 753.6359 1706.144 1280.607 # \\donttest{ set.seed(1) data <- c(rnorm(3000, 0, 1), rnorm(3000, 0, 2), rnorm(3000, 0, 1)) (result_time <- system.time( result <- fastcpd.variance(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 1.911 0.000 1.911 result@cp_set #> [1] 2997 5989 # } # \\donttest{ set.seed(1) data <- c(rnorm(3000, 0, 1), rnorm(3000, 0, 2), rnorm(3000, 0, 1)) (result_time <- system.time( result <- fastcpd.variance( data, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE, cp_only = TRUE ) )) #> user system elapsed #> 1.883 0.000 1.883 result@cp_set #> [1] 2997 5989 # }"},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":null,"dir":"Reference","previous_headings":"","what":"Occupancy Detection Data Set — occupancy","title":"Occupancy Detection Data Set — occupancy","text":"Data set binary classification room occupancy temperature, humidity, light CO2 measurements. Ground-truth occupancy obtained time stamped pictures taken every minute.","code":""},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Occupancy Detection Data Set — occupancy","text":"","code":"occupancy"},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Occupancy Detection Data Set — occupancy","text":"data frame 9752 rows 7 variables: date Character format \"YYYY-MM-DD hh:mm:ss\" 2015-02-11 14:48:00 2015-02-18 09:19:00 Temperature Temperature Celsius Humidity Humidity Light Light CO2 CO2 HumidityRatio Humidity Ratio Occupancy Binary variable values 0 (unoccupied) 1","code":""},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Occupancy Detection Data Set — occupancy","text":"","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":null,"dir":"Reference","previous_headings":"","what":"Plot the data and the change points for a fastcpd object — plot.fastcpd","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"Plot data change points fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"","code":"# S3 method for fastcpd plot( x, color_max_count = Inf, data_point_alpha = 0.8, data_point_linewidth = 0.5, data_point_size = 1, legend_position = \"none\", panel_background = ggplot2::element_blank(), panel_border = ggplot2::element_rect(fill = NA, colour = \"grey20\"), panel_grid_major = ggplot2::element_line(colour = \"grey98\"), panel_grid_minor = ggplot2::element_line(colour = \"grey98\"), segment_separator_alpha = 0.8, segment_separator_color = \"grey\", segment_separator_linetype = \"dashed\", strip_background = ggplot2::element_rect(fill = \"grey85\", colour = \"grey20\"), xlab = NULL, ylab = NULL, ... ) # S4 method for fastcpd,missing plot( x, color_max_count = Inf, data_point_alpha = 0.8, data_point_linewidth = 0.5, data_point_size = 1, legend_position = \"none\", panel_background = ggplot2::element_blank(), panel_border = ggplot2::element_rect(fill = NA, colour = \"grey20\"), panel_grid_major = ggplot2::element_line(colour = \"grey98\"), panel_grid_minor = ggplot2::element_line(colour = \"grey98\"), segment_separator_alpha = 0.8, segment_separator_color = \"grey\", segment_separator_linetype = \"dashed\", strip_background = ggplot2::element_rect(fill = \"grey85\", colour = \"grey20\"), xlab = NULL, ylab = NULL, ... )"},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"x fastcpd object. color_max_count Maximum number colors use plotting segments. data_point_alpha Alpha data points. data_point_linewidth Linewidth data points. data_point_size Size data points. legend_position Position legend. panel_background Background panel. panel_border Border panel. panel_grid_major Major grid lines panel. panel_grid_minor Minor grid lines panel. segment_separator_alpha Alpha segment separator lines. segment_separator_color Color segment separator lines. segment_separator_linetype Linetype segment separator lines. strip_background Background strip. xlab Label x-axis. ylab Label y-axis. ... Ignored.","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"return value, called plotting.","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 1 x <- mvtnorm::rmvnorm(300, rep(0, p), diag(p)) theta_0 <- matrix(c(1, -1, 0.5)) y <- c( x[1:100, ] * theta_0[1, ] + rnorm(100, 0, 1), x[101:200, ] * theta_0[2, ] + rnorm(100, 0, 1), x[201:300, ] * theta_0[3, ] + rnorm(100, 0, 1) ) result <- fastcpd.lm(cbind(y, x), r.clock = \"fastcpd_profiler\") summary(result) plot(result) if (requireNamespace(\"RcppClock\", quietly = TRUE)) { library(RcppClock) plot(fastcpd_profiler) } } #> #> Call: #> fastcpd.lm(data = cbind(y, x), r.clock = \"fastcpd_profiler\") #> #> Change points: #> 100 201 #> #> Cost values: #> 48.16996 66.1816 45.66268 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 0.9520606 -0.8307605 0.4593161"},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":null,"dir":"Reference","previous_headings":"","what":"Print the call and the change points for a fastcpd object — print.fastcpd","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"Print call change points fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"","code":"# S3 method for fastcpd print(x, ...) # S4 method for fastcpd print(x, ...)"},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"x fastcpd object. ... Ignored.","code":""},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"Return (temporarily) invisible copy fastcpd object. Called primarily printing change points model.","code":""},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":null,"dir":"Reference","previous_headings":"","what":"Show the available methods for a fastcpd object — show.fastcpd","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"Show available methods fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"","code":"# S3 method for fastcpd show(object) # S4 method for fastcpd show(object)"},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"object fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"return value, called showing list available methods fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":null,"dir":"Reference","previous_headings":"","what":"Show the summary of a fastcpd object — summary.fastcpd","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"Show summary fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"","code":"# S3 method for fastcpd summary(object, ...) # S4 method for fastcpd summary(object, ...)"},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"object fastcpd object. ... Ignored.","code":""},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"Return (temporarily) invisible copy fastcpd object. Called primarily printing summary model including call, change points, cost values estimated parameters.","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":null,"dir":"Reference","previous_headings":"","what":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"Transcriptome analysis 57 bladder carcinomas Affymetrix HG-U95A HG-U95Av2 microarrays","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"","code":"transcriptome"},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"data frame 2215 rows 43 variables: 3 Individual 3 4 Individual 4 5 Individual 5 6 Individual 6 7 Individual 7 8 Individual 8 9 Individual 9 10 Individual 10 14 Individual 14 15 Individual 15 16 Individual 16 17 Individual 17 18 Individual 18 19 Individual 19 21 Individual 21 22 Individual 22 24 Individual 24 26 Individual 26 28 Individual 28 30 Individual 30 31 Individual 31 33 Individual 33 34 Individual 34 35 Individual 35 36 Individual 36 37 Individual 37 38 Individual 38 39 Individual 39 40 Individual 40 41 Individual 41 42 Individual 42 43 Individual 43 44 Individual 44 45 Individual 45 46 Individual 46 47 Individual 47 48 Individual 48 49 Individual 49 50 Individual 50 51 Individual 51 53 Individual 53 54 Individual 54 57 Individual 57","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":" ","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"","code":"# \\donttest{ if (requireNamespace(\"ggplot2\", quietly = TRUE)) { result <- fastcpd.mean(transcriptome$\"10\", trim = 0.005) summary(result) plot(result) result_all <- fastcpd.mean( transcriptome, beta = (ncol(transcriptome) + 1) * log(nrow(transcriptome)) / 2 * 5, trim = 0 ) plots <- lapply( seq_len(ncol(transcriptome)), function(i) { ggplot2::ggplot( data = data.frame( x = seq_along(transcriptome[, i]), y = transcriptome[, i] ), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_line(color = \"steelblue\") + ggplot2::geom_vline( xintercept = result_all@cp_set, color = \"red\", linetype = \"dotted\", linewidth = 0.5, alpha = 0.7 ) + ggplot2::theme_void() } ) if (requireNamespace(\"gridExtra\", quietly = TRUE)) { gridExtra::grid.arrange(grobs = plots, ncol = 1, nrow = ncol(transcriptome)) } } #> #> Call: #> fastcpd.mean(data = transcriptome$\"10\", trim = 0.005) #> #> Change points: #> 178 264 401 534 601 656 788 811 869 934 971 1055 1142 1286 1319 1386 1657 1724 1906 1972 1996 2041 #> #> Cost values: #> -312.3778 -152.8685 -223.128 -194.7351 28.05096 -110.2493 -234.4602 -48.13672 -104.2513 -95.29414 -34.57256 -118.7807 -135.2328 -228.7489 -66.44221 -93.27936 -388.8495 141.1922 -326.8845 -94.17653 -10.70687 -28.44282 -267.2412 # }"},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":null,"dir":"Reference","previous_headings":"","what":"UK Seatbelts Data — uk_seatbelts","title":"UK Seatbelts Data — uk_seatbelts","text":"Road Casualties Great Britain 1969–84.","code":""},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"UK Seatbelts Data — uk_seatbelts","text":"","code":"uk_seatbelts"},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"UK Seatbelts Data — uk_seatbelts","text":"uk_seatbelts multiple time series, columns DriversKilled car drivers killed. front front-seat passengers killed seriously injured. rear rear-seat passengers killed seriously injured. kms distance driven. PetrolPrice petrol price. VanKilled number van (‘light goods vehicle’) drivers. law 0/1: law effect month?","code":""},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"UK Seatbelts Data — uk_seatbelts","text":"R package datasets","code":""},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"UK Seatbelts Data — uk_seatbelts","text":"","code":"if ( requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"lubridate\", quietly = TRUE) && requireNamespace(\"zoo\", quietly = TRUE) ) { result_ar <- fastcpd.ar(diff(uk_seatbelts[, \"drivers\"], 12), 1, beta = \"BIC\") summary(result_ar) plot(result_ar) result_lm <- suppressMessages(fastcpd.lm( diff(uk_seatbelts[, c(\"drivers\", \"kms\", \"PetrolPrice\", \"law\")], lag = 12) )) cp_dates <- as.Date(\"1969-01-01\", format = \"%Y-%m-%d\") cp_dates <- cp_dates + lubridate::period(month = 1 + result_lm@cp_set + 12) cp_dates <- zoo::as.yearmon(cp_dates) dates <- zoo::as.yearmon(time(uk_seatbelts)) uk_seatbelts_df <- data.frame( dates = dates, drivers = c(uk_seatbelts[, \"drivers\"]), color = as.factor((dates < cp_dates[1]) + (dates < cp_dates[2])) ) ggplot2::ggplot() + ggplot2::geom_line( data = uk_seatbelts_df, mapping = ggplot2::aes(x = dates, y = drivers, color = color) ) + ggplot2::geom_vline( xintercept = cp_dates, linetype = \"dashed\", color = \"red\" ) + zoo::scale_x_yearmon() + ggplot2::annotate( \"text\", x = cp_dates, y = 1025, label = as.character(cp_dates), color = \"blue\" ) + ggplot2::theme_bw() + ggplot2::theme(legend.position = \"none\") } #> #> Call: #> fastcpd.ar(data = diff(uk_seatbelts[, \"drivers\"], 12), order = 1, #> beta = \"BIC\") #> #> Change points: #> 71 158 #> #> Cost values: #> 1294597 914674.5 310957 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 0.5543967 -0.07501079 0.7825427"},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for ARMA model with change points — variance_arma","title":"Variance estimation for ARMA model with change points — variance_arma","text":"Estimate variance block take average.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for ARMA model with change points — variance_arma","text":"","code":"variance_arma(data, p, q, max_order = p * q) variance.arma(data, p, q, max_order = p * q)"},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for ARMA model with change points — variance_arma","text":"data one-column matrix vector. p order autoregressive part. q order moving average part. max_order maximum order AR model consider.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for ARMA model with change points — variance_arma","text":"numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for ARMA model with change points — variance_arma","text":"","code":"set.seed(1) n <- 300 w <- rnorm(n + 3, 0, 10) x <- rep(0, n + 3) for (i in 1:200) { x[i + 3] <- 0.1 * x[i + 2] - 0.3 * x[i + 1] + 0.1 * x[i] + 0.1 * w[i + 2] + 0.5 * w[i + 1] + w[i + 3] } for (i in 201:n) { x[i + 3] <- 0.3 * x[i + 2] + 0.1 * x[i + 1] - 0.3 * x[i] - 0.6 * w[i + 2] - 0.1 * w[i + 1] + w[i + 3] } (result <- variance.arma(x[-seq_len(3)], p = 3, q = 2)) #> $table #> sigma2 AIC BIC #> AR(1) 104.8647 4.659337 4.671683 #> AR(2) 100.2594 4.621094 4.645786 #> AR(3) 115.3102 4.767626 4.804664 #> AR(4) 105.4397 4.684806 4.734190 #> AR(5) 115.4258 4.781962 4.843691 #> AR(6) 117.0668 4.802745 4.876821 #> #> $sigma2_aic #> [1] 100.2594 #> #> $sigma2_bic #> [1] 100.2594 #>"},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for linear models with change points — variance_lm","title":"Variance estimation for linear models with change points — variance_lm","text":"Estimate variance block take average.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for linear models with change points — variance_lm","text":"","code":"variance_lm(data, d = 1, block_size = ncol(data) - d + 1, outlier_iqr = Inf) variance.lm(data, d = 1, block_size = ncol(data) - d + 1, outlier_iqr = Inf)"},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for linear models with change points — variance_lm","text":"data matrix data frame response variable first column. d dimension response variable. block_size size blocks use variance estimation. outlier_iqr number interquartile ranges use threshold outlier detection.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for linear models with change points — variance_lm","text":"numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for linear models with change points — variance_lm","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta[3, ] + rnorm(100, 0, 3) ) (sigma2 <- variance.lm(cbind(y, x))) set.seed(1) n <- 300 p <- 4 d <- 2 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- cbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) theta <- cbind(theta, theta) y <- rbind( x[1:100, ] %*% theta[, 1:2] + mvtnorm::rmvnorm(100, rep(0, d), diag(3, d)), x[101:200, ] %*% theta[, 3:4] + mvtnorm::rmvnorm(100, rep(0, d), diag(3, d)), x[201:n, ] %*% theta[, 5:6] + mvtnorm::rmvnorm(100, rep(0, d), diag(3, d)) ) (sigma <- variance.lm(cbind(y, x), d = d)) } #> [,1] [,2] #> [1,] 3.71946553 0.01962422 #> [2,] 0.03459023 3.59825273"},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for mean change models — variance_mean","title":"Variance estimation for mean change models — variance_mean","text":"Implement Rice estimator variance mean change models.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for mean change models — variance_mean","text":"","code":"variance_mean(data) variance.mean(data)"},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for mean change models — variance_mean","text":"data matrix data frame data points row.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for mean change models — variance_mean","text":"matrix representing variance-covariance matrix numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for mean change models — variance_mean","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 3 data <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) (sigma <- variance.mean(data)) } #> [,1] [,2] [,3] #> [1,] 113.5442077 -0.1433207 -2.973472 #> [2,] -0.1433207 106.1627730 8.430343 #> [3,] -2.9734719 8.4303433 112.602989"},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for median change models — variance_median","title":"Variance estimation for median change models — variance_median","text":"Implement Rice estimator.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for median change models — variance_median","text":"","code":"variance_median(data) variance.median(data)"},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for median change models — variance_median","text":"data vector data points.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for median change models — variance_median","text":"numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for median change models — variance_median","text":"","code":"(sigma2 <- variance.median(well_log)) #> [1] 5803645"},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":null,"dir":"Reference","previous_headings":"","what":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"well-known well-log dataset used many changepoint papers obtained Alan Turing Institute GitHub repository licensed MIT license.","code":""},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"","code":"well_log"},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"Time-Series length 4050.","code":""},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"","code":""},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"","code":"result <- fastcpd.mean(well_log, trim = 0.001) summary(result) #> #> Call: #> fastcpd.mean(data = well_log, trim = 0.001) #> #> Change points: #> 7 19 65 356 445 717 792 1034 1070 1215 1368 1428 1526 1684 1866 2047 2409 2469 2531 2591 2775 3166 3314 3490 3533 3673 3744 3855 3886 3945 3963 4035 #> #> Cost values: #> 70.90268 131.7555 383.0472 2445.132 758.3834 2296.059 637.0301 1996.247 302.8254 1840.756 2267.745 605.6831 1054.037 1310.089 1530.8 1508.578 2990.751 491.6914 513.248 487.8511 1872.188 3796.243 1220.895 1461.687 396.427 1172.664 602.3776 918.5697 277.6224 593.5602 197.5627 611.3688 133.8662 plot(result) # \\donttest{ if (requireNamespace(\"matrixStats\", quietly = TRUE)) { sigma2 <- variance.median(well_log) median_loss <- function(data) { sum(abs(data - matrixStats::colMedians(data))) / sqrt(sigma2) / 2 } result <- fastcpd( formula = ~ x - 1, data = cbind.data.frame(x = well_log), cost = median_loss, trim = 0.002 ) summary(result) segment_starts <- c(1, result@cp_set) segment_ends <- c(result@cp_set - 1, length(well_log)) residual <- NULL for (segment_index in seq_along(segment_starts)) { segment <- well_log[segment_starts[segment_index]:segment_ends[segment_index]] residual <- c(residual, segment - median(segment)) } result@residuals <- matrix(residual) result@family <- \"mean\" result@data <- data.frame(x = c(well_log)) plot(result) } #> #> Call: #> fastcpd(formula = ~x - 1, data = cbind.data.frame(x = well_log), #> cost = median_loss, trim = 0.002) #> #> Change points: #> 19 577 1034 1070 1216 1361 1428 1526 1685 1866 2047 2409 2469 2531 2591 2775 3744 3855 3945 3963 # }"},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0145","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.5","title":"fastcpd 0.14.5","text":"Add pre-compiled vignettes.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0144","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.4","title":"fastcpd 0.14.4","text":"Simplify code. Expose fastcpd_impl API use packages.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0143","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.3","title":"fastcpd 0.14.3","text":"CRAN release: 2024-04-26 Skip certain tests CRAN.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0142","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.2","title":"fastcpd 0.14.2","text":"Fix CRAN noSuggests error.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0141","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.1","title":"fastcpd 0.14.1","text":"CRAN release: 2024-04-18 Fix possible numeric issues CRAN breaking updates packages.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0140","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.0","title":"fastcpd 0.14.0","text":"CRAN release: 2024-04-16 Correct mBIC. Add citation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0132","dir":"Changelog","previous_headings":"","what":"fastcpd 0.13.2","title":"fastcpd 0.13.2","text":"Remove package check examples. Use proper pruning coefficients MBIC MDL. Make residuals matrices. Use preprocess faster mean change detection. Update examples demonstrating faster mean change. Replace comparison packages vignettes strange dependencies results eval = FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0131","dir":"Changelog","previous_headings":"","what":"fastcpd 0.13.1","title":"fastcpd 0.13.1","text":"CRAN release: 2024-04-01 Default pruning lasso. Comment gfpop due https://github.com/doccstat/fastcpd/issues/10.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0130","dir":"Changelog","previous_headings":"","what":"fastcpd 0.13.0","title":"fastcpd 0.13.0","text":"Update documentations.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0124","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.4","title":"fastcpd 0.12.4","text":"Customizable pretty plots.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0123","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.3","title":"fastcpd 0.12.3","text":"Remove pruning parameter replace convexity_coef = -Inf.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0122","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.2","title":"fastcpd 0.12.2","text":"CRAN release: 2024-03-19 Update vignettes.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0121","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.1","title":"fastcpd 0.12.1","text":"CRAN release: 2024-03-12 Remove useless C++ codes. Add debug points C++. Add examples data well_log. Add detection comparison well_log data. Add variance estimator median change. Deprecate winsorize_minval winsorize_maxval.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0120","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.0","title":"fastcpd 0.12.0","text":"Add Rice estimation ARMA model variance estimation. Add time comparison using Well-log data vignettes.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0113","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.3","title":"fastcpd 0.11.3","text":"Add Rice estimator mean change variance estimation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0112","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.2","title":"fastcpd 0.11.2","text":"Export variance estimator function linear models.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0111","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.1","title":"fastcpd 0.11.1","text":"Add package comparison CptNonPar, gfpop, InspectChangepoint, jointseg, Rbeast VARDetect.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0110","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.0","title":"fastcpd 0.11.0","text":"Note: now , MBIC used default penalty selection beta parameter. Add penalty selection criteria using BIC: (p + 1) * log(nrow(data)) / 2 Modified BIC: (p + 2) * log(nrow(data)) / 2 adjusted cost function. MDL: (p + 2) * log(nrow(data)) / 2 adjusted cost function. mean time, numeric value can passed beta well explicitly specify penalty BIC.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0103","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.3","title":"fastcpd 0.10.3","text":"CRAN release: 2024-01-24 Add package check examples tests.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0102","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.2","title":"fastcpd 0.10.2","text":"Remove bcp according ","code":"Package ‘bcp’ was removed from the CRAN repository. Formerly available versions can be obtained from the archive. Archived on 2024-01-12 as email to the maintainer is undeliverable. A summary of the most recent check results can be obtained from the check results archive. Please use the canonical form https://CRAN.R-project.org/package=bcp to link to this page."},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0101","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.1","title":"fastcpd 0.10.1","text":"CRAN release: 2024-01-09 Increase test coverage. Use interactive() check current R session interactive.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0100","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.0","title":"fastcpd 0.10.0","text":"CRAN release: 2023-12-21 Add package comparison packages.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-099","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.9","title":"fastcpd 0.9.9","text":"CRAN release: 2023-12-11 Add small shiny app.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-098","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.8","title":"fastcpd 0.9.8","text":"Preliminary support ARMA(p, q) model parameter order = c(p, q) family \"arma\". Add fastcpd.arma / fastcpd_arma ARMA(p, q) model. Add adaptive increasing beta values.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-097","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.7","title":"fastcpd 0.9.7","text":"Add lower upper parameters denote lower upper bounds parameters. Add line search. Add hardcoded ARMA(3, 2).","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-096","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.6","title":"fastcpd 0.9.6","text":"Add bitcoin well_log data. Add residual calculation mean family. Add plots bitcoin data. Fix residual calculation time series data seegments small. Handle variance estimation errors.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-095","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.5","title":"fastcpd 0.9.5","text":"Add wrapper functions AR(p) family: fastcpd.ar / fastcpd_ar, ARIMA(p, d, q) family: fastcpd.arima / fastcpd_arima, GARCH(p, q) family: fastcpd.garch / fastcpd_garch, linear regression family: fastcpd.lm / fastcpd_lm, logistic regression family: fastcpd.binomial / fastcpd_binomial, poisson regression family: fastcpd.poisson / fastcpd_poisson, penalized linear regression family: fastcpd.lasso / fastcpd_lasso, MA(q) model: fastcpd.ma / fastcpd_ma, mean change: fastcpd.mean / fastcpd_mean, variance change: fastcpd.variance / fastcpd_variance, mean variance change: fastcpd.meanvariance / fastcpd_meanvariance / fastcpd.mv / fastcpd_mv. Replace \"gaussian\" family \"lm\". Add progress bar. Fix design matrix formula bug.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-094","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.4","title":"fastcpd 0.9.4","text":"Fix sanity check. Add small AR(1) data gallery. Fix VAR(p) model bug. Add VAR(2) example Gallery. Remove commented code.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-093","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.3","title":"fastcpd 0.9.3","text":"Deprecate “vanilla” family vanilla_percentage parameter. Add check utility functions. Add MA(4) example. Fix bug beta updated old beta still use. Remove tests estimating variance “gaussian” family dynamically. Merge beta updating get_segment_statistics.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-092","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.2","title":"fastcpd 0.9.2","text":"Add gallery vignettes. Remove cheatsheets pdf package. Use forecast package ARIMA model. Use fGarch package GARCH model.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-091","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.1","title":"fastcpd 0.9.1","text":"Wrap && around || parentheses. Add ma(4) example using custom cost function. Add full support AR(p), MA(q) ARIMA(p, d, q) models.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-090","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.0","title":"fastcpd 0.9.0","text":"CRAN release: 2023-10-19 Submit CRAN update.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-084","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.4","title":"fastcpd 0.8.4","text":"Add cheatsheets. Refactor code utilize cost_function_wrapper. Optimize warm start.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-083","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.3","title":"fastcpd 0.8.3","text":"Add fastcpd.ts / fastcpd_ts time series data. Fix pre segmengation bug lasso. Fix bug related vanilla_percentage parameter lasso. Add tests invalid family fastcpd.ts. Remove cp_only = TRUE default family “custom”. Improved plotting “ar” “var” families. Add test coverage cp_only = TRUE fastcpd_ts. Increase test coverage. Provide user selection ggplot2 installed.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-082","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.2","title":"fastcpd 0.8.2","text":"Add cheatsheets WIP. Add smaller examples test penalized linear regression.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-081","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.1","title":"fastcpd 0.8.1","text":"Add new “ar” family autoregressive models. Add new “var” family vector autoregressive models.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-080","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.0","title":"fastcpd 0.8.0","text":"Deal following: Separate use internal C++ cost functions user-defined R cost functions. Add Codecov Icicle plot README. Remove cost_optim cost_update RcppExports.R. Estimate variance “gaussian” family dynamically.","code":"Due to the excessive calls to `glmnet` between R and C++, it is better to use the R implementation of `fastcpd` for lasso."},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-076","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.6","title":"fastcpd 0.7.6","text":"Move default cost functions definition inside fastcpd definition. Define constant unordered set store family sets. Avoid using length(formals(cost)) check number arguments cost function. Introduce internal family “vanilla”.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-075","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.5","title":"fastcpd 0.7.5","text":"Add variance estimation example linear regression. Update reference page. Add validation family. Add user selection ggplot2 installed. Add AR(1) using forecast example tests.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-074","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.4","title":"fastcpd 0.7.4","text":"Update website UI. Update fastcpd documentation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-073","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.3","title":"fastcpd 0.7.3","text":"Allow multiple response variables formula. Add fastcpd logo README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-072","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.2","title":"fastcpd 0.7.2","text":"CRAN release: 2023-09-23 Add suggested package checking tests. Try solve amazing clang-ASAN error CRAN:","code":"Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/data/gannet/ripley/R/test-clang/mvtnorm/libs/mvtnorm.so': /data/gannet/ripley/R/test-clang/mvtnorm/libs/mvtnorm.so: undefined symbol: _ZNK7Fortran7runtime10Terminator5CrashEPKcz Calls: ... asNamespace -> loadNamespace -> library.dynam -> dyn.load"},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-071","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.1","title":"fastcpd 0.7.1","text":"Add package citation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-070","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.0","title":"fastcpd 0.7.0","text":"CRAN release: 2023-09-21 Remove C++ unit tests using catch commented code since new version development version Rcpp yet available CRAN. Related pull request: https://github.com/RcppCore/Rcpp/pull/1274. Add documentation fastcpd method.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-065","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.5","title":"fastcpd 0.6.5","text":"Add experiments.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-064","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.4","title":"fastcpd 0.6.4","text":"Check warning messages tests. encapsulation FastcpdParameters members.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-063","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.3","title":"fastcpd 0.6.3","text":"Add CRAN release badge.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-062","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.2","title":"fastcpd 0.6.2","text":"CRAN release: 2023-09-14 Address CRAN comments. Add experiments.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-061","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.1","title":"fastcpd 0.6.1","text":"Address CRAN comments.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-060","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.0","title":"fastcpd 0.6.0","text":"Submit CRAN release.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-057","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.7","title":"fastcpd 0.5.7","text":"Fix loss function custom mean variance change.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-056","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.6","title":"fastcpd 0.5.6","text":"Add stargazers README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-055","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.5","title":"fastcpd 0.5.5","text":"Add example test multivariate mean shift. Add example test multivariate variance change. Add example test multivariate mean variance change. Add test linear regression multi-dimensional responses.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-054","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.4","title":"fastcpd 0.5.4","text":"Fix bug change point detected.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-053","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.3","title":"fastcpd 0.5.3","text":"Add experiments commented sake test time without affecting test coverage. Add examples README. Add CRAN manual using R CMD Rd2pdf . --output=man/figures/manual.pdf --force ---preview stackoverflow. Add example multiple epochs using custom cost functions. Add table contents README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-052","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.2","title":"fastcpd 0.5.2","text":"Add one-dimensional linear regression example plot.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-051","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.1","title":"fastcpd 0.5.1","text":"Prepare CRAN release.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-050","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.0","title":"fastcpd 0.5.0","text":"Rewrite whole package C++ except LASSO due excessive calls R C++ glmnet.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-040","dir":"Changelog","previous_headings":"","what":"fastcpd 0.4.0","title":"fastcpd 0.4.0","text":"Add transition vanilla PELT SeN using vanilla_percentage parameter.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-033","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.3","title":"fastcpd 0.3.3","text":"Merge implementation vanilla PELT SeN. Encapsulate implementation binding new coefficients previous coefficients. Rewrite fastcpd parameters updating C++.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-032","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.2","title":"fastcpd 0.3.2","text":"Integrate initialization update theta_hat, theta_sum hessian. Combine theta estimation single function. Add parameter vanilla_percentage denote method switching vanilla PETL SeN. Add documentation cp_only parameter. Add preparation merging vanilla PELT SeN.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-031","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.1","title":"fastcpd 0.3.1","text":"Add examples tests fastcpd. Rearrange C++ functions. Add precondition check.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-030","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.0","title":"fastcpd 0.3.0","text":"Bump test coverage class methods fastcpd.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-029","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.9","title":"fastcpd 0.2.9","text":"Fix Poisson regression bug related lfactorial.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-028","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.8","title":"fastcpd 0.2.8","text":"Make penalized linear regression estimated coefficients output sparse.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-027","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.7","title":"fastcpd 0.2.7","text":"Fix mean change example bug. Update documentation redirect README pkgdown generated webpage. Add contact methods ways file ticket.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-026","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.6","title":"fastcpd 0.2.6","text":"Add C++ sanity check Logistic regression data, .e. binomial family. Add examples tests fastcpd. Rename C++ source files follow Unix convention. Update documentation link README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-025","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.5","title":"fastcpd 0.2.5","text":"Hide internal functions documentation. Export fastcpd class.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-024","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.4","title":"fastcpd 0.2.4","text":"Add column name thetas slot fastcpd class. Fix plot residuals responses appear plot. Default cp_only FALSE. Remove residuals summary method.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-023","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.3","title":"fastcpd 0.2.3","text":"Add missing examples linear regression LASSO.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-022","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.2","title":"fastcpd 0.2.2","text":"Add examples illustrate use fastcpd function. Indicating internal functions users use .","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-021","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.1","title":"fastcpd 0.2.1","text":"Add examples README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-020","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.0","title":"fastcpd 0.2.0","text":"Added NEWS.md file track changes package.","code":""}] +[{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":null,"dir":"","previous_headings":"","what":"GNU General Public License","title":"GNU General Public License","text":"Version 3, 29 June 2007 Copyright © 2007 Free Software Foundation, Inc.  Everyone permitted copy distribute verbatim copies license document, changing allowed.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"preamble","dir":"","previous_headings":"","what":"Preamble","title":"GNU General Public License","text":"GNU General Public License free, copyleft license software kinds works. licenses software practical works designed take away freedom share change works. contrast, GNU General Public License intended guarantee freedom share change versions program–make sure remains free software users. , Free Software Foundation, use GNU General Public License software; applies also work released way authors. can apply programs, . speak free software, referring freedom, price. General Public Licenses designed make sure freedom distribute copies free software (charge wish), receive source code can get want , can change software use pieces new free programs, know can things. protect rights, need prevent others denying rights asking surrender rights. Therefore, certain responsibilities distribute copies software, modify : responsibilities respect freedom others. example, distribute copies program, whether gratis fee, must pass recipients freedoms received. must make sure , , receive can get source code. must show terms know rights. Developers use GNU GPL protect rights two steps: (1) assert copyright software, (2) offer License giving legal permission copy, distribute /modify . developers’ authors’ protection, GPL clearly explains warranty free software. users’ authors’ sake, GPL requires modified versions marked changed, problems attributed erroneously authors previous versions. devices designed deny users access install run modified versions software inside , although manufacturer can . fundamentally incompatible aim protecting users’ freedom change software. systematic pattern abuse occurs area products individuals use, precisely unacceptable. Therefore, designed version GPL prohibit practice products. problems arise substantially domains, stand ready extend provision domains future versions GPL, needed protect freedom users. Finally, every program threatened constantly software patents. States allow patents restrict development use software general-purpose computers, , wish avoid special danger patents applied free program make effectively proprietary. prevent , GPL assures patents used render program non-free. precise terms conditions copying, distribution modification follow.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_0-definitions","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"0. Definitions","title":"GNU General Public License","text":"“License” refers version 3 GNU General Public License. “Copyright” also means copyright-like laws apply kinds works, semiconductor masks. “Program” refers copyrightable work licensed License. licensee addressed “”. “Licensees” “recipients” may individuals organizations. “modify” work means copy adapt part work fashion requiring copyright permission, making exact copy. resulting work called “modified version” earlier work work “based ” earlier work. “covered work” means either unmodified Program work based Program. “propagate” work means anything , without permission, make directly secondarily liable infringement applicable copyright law, except executing computer modifying private copy. Propagation includes copying, distribution (without modification), making available public, countries activities well. “convey” work means kind propagation enables parties make receive copies. Mere interaction user computer network, transfer copy, conveying. interactive user interface displays “Appropriate Legal Notices” extent includes convenient prominently visible feature (1) displays appropriate copyright notice, (2) tells user warranty work (except extent warranties provided), licensees may convey work License, view copy License. interface presents list user commands options, menu, prominent item list meets criterion.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_1-source-code","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"1. Source Code","title":"GNU General Public License","text":"“source code” work means preferred form work making modifications . “Object code” means non-source form work. “Standard Interface” means interface either official standard defined recognized standards body, , case interfaces specified particular programming language, one widely used among developers working language. “System Libraries” executable work include anything, work whole, () included normal form packaging Major Component, part Major Component, (b) serves enable use work Major Component, implement Standard Interface implementation available public source code form. “Major Component”, context, means major essential component (kernel, window system, ) specific operating system () executable work runs, compiler used produce work, object code interpreter used run . “Corresponding Source” work object code form means source code needed generate, install, (executable work) run object code modify work, including scripts control activities. However, include work’s System Libraries, general-purpose tools generally available free programs used unmodified performing activities part work. example, Corresponding Source includes interface definition files associated source files work, source code shared libraries dynamically linked subprograms work specifically designed require, intimate data communication control flow subprograms parts work. Corresponding Source need include anything users can regenerate automatically parts Corresponding Source. Corresponding Source work source code form work.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_2-basic-permissions","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"2. Basic Permissions","title":"GNU General Public License","text":"rights granted License granted term copyright Program, irrevocable provided stated conditions met. License explicitly affirms unlimited permission run unmodified Program. output running covered work covered License output, given content, constitutes covered work. License acknowledges rights fair use equivalent, provided copyright law. may make, run propagate covered works convey, without conditions long license otherwise remains force. may convey covered works others sole purpose make modifications exclusively , provide facilities running works, provided comply terms License conveying material control copyright. thus making running covered works must exclusively behalf, direction control, terms prohibit making copies copyrighted material outside relationship . Conveying circumstances permitted solely conditions stated . Sublicensing allowed; section 10 makes unnecessary.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_3-protecting-users-legal-rights-from-anti-circumvention-law","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"3. Protecting Users’ Legal Rights From Anti-Circumvention Law","title":"GNU General Public License","text":"covered work shall deemed part effective technological measure applicable law fulfilling obligations article 11 WIPO copyright treaty adopted 20 December 1996, similar laws prohibiting restricting circumvention measures. convey covered work, waive legal power forbid circumvention technological measures extent circumvention effected exercising rights License respect covered work, disclaim intention limit operation modification work means enforcing, work’s users, third parties’ legal rights forbid circumvention technological measures.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_4-conveying-verbatim-copies","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"4. Conveying Verbatim Copies","title":"GNU General Public License","text":"may convey verbatim copies Program’s source code receive , medium, provided conspicuously appropriately publish copy appropriate copyright notice; keep intact notices stating License non-permissive terms added accord section 7 apply code; keep intact notices absence warranty; give recipients copy License along Program. may charge price price copy convey, may offer support warranty protection fee.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_5-conveying-modified-source-versions","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"5. Conveying Modified Source Versions","title":"GNU General Public License","text":"may convey work based Program, modifications produce Program, form source code terms section 4, provided also meet conditions: ) work must carry prominent notices stating modified , giving relevant date. b) work must carry prominent notices stating released License conditions added section 7. requirement modifies requirement section 4 “keep intact notices”. c) must license entire work, whole, License anyone comes possession copy. License therefore apply, along applicable section 7 additional terms, whole work, parts, regardless packaged. License gives permission license work way, invalidate permission separately received . d) work interactive user interfaces, must display Appropriate Legal Notices; however, Program interactive interfaces display Appropriate Legal Notices, work need make . compilation covered work separate independent works, nature extensions covered work, combined form larger program, volume storage distribution medium, called “aggregate” compilation resulting copyright used limit access legal rights compilation’s users beyond individual works permit. Inclusion covered work aggregate cause License apply parts aggregate.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_6-conveying-non-source-forms","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"6. Conveying Non-Source Forms","title":"GNU General Public License","text":"may convey covered work object code form terms sections 4 5, provided also convey machine-readable Corresponding Source terms License, one ways: ) Convey object code , embodied , physical product (including physical distribution medium), accompanied Corresponding Source fixed durable physical medium customarily used software interchange. b) Convey object code , embodied , physical product (including physical distribution medium), accompanied written offer, valid least three years valid long offer spare parts customer support product model, give anyone possesses object code either (1) copy Corresponding Source software product covered License, durable physical medium customarily used software interchange, price reasonable cost physically performing conveying source, (2) access copy Corresponding Source network server charge. c) Convey individual copies object code copy written offer provide Corresponding Source. alternative allowed occasionally noncommercially, received object code offer, accord subsection 6b. d) Convey object code offering access designated place (gratis charge), offer equivalent access Corresponding Source way place charge. need require recipients copy Corresponding Source along object code. place copy object code network server, Corresponding Source may different server (operated third party) supports equivalent copying facilities, provided maintain clear directions next object code saying find Corresponding Source. Regardless server hosts Corresponding Source, remain obligated ensure available long needed satisfy requirements. e) Convey object code using peer--peer transmission, provided inform peers object code Corresponding Source work offered general public charge subsection 6d. separable portion object code, whose source code excluded Corresponding Source System Library, need included conveying object code work. “User Product” either (1) “consumer product”, means tangible personal property normally used personal, family, household purposes, (2) anything designed sold incorporation dwelling. determining whether product consumer product, doubtful cases shall resolved favor coverage. particular product received particular user, “normally used” refers typical common use class product, regardless status particular user way particular user actually uses, expects expected use, product. product consumer product regardless whether product substantial commercial, industrial non-consumer uses, unless uses represent significant mode use product. “Installation Information” User Product means methods, procedures, authorization keys, information required install execute modified versions covered work User Product modified version Corresponding Source. information must suffice ensure continued functioning modified object code case prevented interfered solely modification made. convey object code work section , , specifically use , User Product, conveying occurs part transaction right possession use User Product transferred recipient perpetuity fixed term (regardless transaction characterized), Corresponding Source conveyed section must accompanied Installation Information. requirement apply neither third party retains ability install modified object code User Product (example, work installed ROM). requirement provide Installation Information include requirement continue provide support service, warranty, updates work modified installed recipient, User Product modified installed. Access network may denied modification materially adversely affects operation network violates rules protocols communication across network. Corresponding Source conveyed, Installation Information provided, accord section must format publicly documented (implementation available public source code form), must require special password key unpacking, reading copying.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_7-additional-terms","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"7. Additional Terms","title":"GNU General Public License","text":"“Additional permissions” terms supplement terms License making exceptions one conditions. Additional permissions applicable entire Program shall treated though included License, extent valid applicable law. additional permissions apply part Program, part may used separately permissions, entire Program remains governed License without regard additional permissions. convey copy covered work, may option remove additional permissions copy, part . (Additional permissions may written require removal certain cases modify work.) may place additional permissions material, added covered work, can give appropriate copyright permission. Notwithstanding provision License, material add covered work, may (authorized copyright holders material) supplement terms License terms: ) Disclaiming warranty limiting liability differently terms sections 15 16 License; b) Requiring preservation specified reasonable legal notices author attributions material Appropriate Legal Notices displayed works containing ; c) Prohibiting misrepresentation origin material, requiring modified versions material marked reasonable ways different original version; d) Limiting use publicity purposes names licensors authors material; e) Declining grant rights trademark law use trade names, trademarks, service marks; f) Requiring indemnification licensors authors material anyone conveys material (modified versions ) contractual assumptions liability recipient, liability contractual assumptions directly impose licensors authors. non-permissive additional terms considered “restrictions” within meaning section 10. Program received , part , contains notice stating governed License along term restriction, may remove term. license document contains restriction permits relicensing conveying License, may add covered work material governed terms license document, provided restriction survive relicensing conveying. add terms covered work accord section, must place, relevant source files, statement additional terms apply files, notice indicating find applicable terms. Additional terms, permissive non-permissive, may stated form separately written license, stated exceptions; requirements apply either way.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_8-termination","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"8. Termination","title":"GNU General Public License","text":"may propagate modify covered work except expressly provided License. attempt otherwise propagate modify void, automatically terminate rights License (including patent licenses granted third paragraph section 11). However, cease violation License, license particular copyright holder reinstated () provisionally, unless copyright holder explicitly finally terminates license, (b) permanently, copyright holder fails notify violation reasonable means prior 60 days cessation. Moreover, license particular copyright holder reinstated permanently copyright holder notifies violation reasonable means, first time received notice violation License (work) copyright holder, cure violation prior 30 days receipt notice. Termination rights section terminate licenses parties received copies rights License. rights terminated permanently reinstated, qualify receive new licenses material section 10.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_9-acceptance-not-required-for-having-copies","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"9. Acceptance Not Required for Having Copies","title":"GNU General Public License","text":"required accept License order receive run copy Program. Ancillary propagation covered work occurring solely consequence using peer--peer transmission receive copy likewise require acceptance. However, nothing License grants permission propagate modify covered work. actions infringe copyright accept License. Therefore, modifying propagating covered work, indicate acceptance License .","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_10-automatic-licensing-of-downstream-recipients","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"10. Automatic Licensing of Downstream Recipients","title":"GNU General Public License","text":"time convey covered work, recipient automatically receives license original licensors, run, modify propagate work, subject License. responsible enforcing compliance third parties License. “entity transaction” transaction transferring control organization, substantially assets one, subdividing organization, merging organizations. propagation covered work results entity transaction, party transaction receives copy work also receives whatever licenses work party’s predecessor interest give previous paragraph, plus right possession Corresponding Source work predecessor interest, predecessor can get reasonable efforts. may impose restrictions exercise rights granted affirmed License. example, may impose license fee, royalty, charge exercise rights granted License, may initiate litigation (including cross-claim counterclaim lawsuit) alleging patent claim infringed making, using, selling, offering sale, importing Program portion .","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_11-patents","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"11. Patents","title":"GNU General Public License","text":"“contributor” copyright holder authorizes use License Program work Program based. work thus licensed called contributor’s “contributor version”. contributor’s “essential patent claims” patent claims owned controlled contributor, whether already acquired hereafter acquired, infringed manner, permitted License, making, using, selling contributor version, include claims infringed consequence modification contributor version. purposes definition, “control” includes right grant patent sublicenses manner consistent requirements License. contributor grants non-exclusive, worldwide, royalty-free patent license contributor’s essential patent claims, make, use, sell, offer sale, import otherwise run, modify propagate contents contributor version. following three paragraphs, “patent license” express agreement commitment, however denominated, enforce patent (express permission practice patent covenant sue patent infringement). “grant” patent license party means make agreement commitment enforce patent party. convey covered work, knowingly relying patent license, Corresponding Source work available anyone copy, free charge terms License, publicly available network server readily accessible means, must either (1) cause Corresponding Source available, (2) arrange deprive benefit patent license particular work, (3) arrange, manner consistent requirements License, extend patent license downstream recipients. “Knowingly relying” means actual knowledge , patent license, conveying covered work country, recipient’s use covered work country, infringe one identifiable patents country reason believe valid. , pursuant connection single transaction arrangement, convey, propagate procuring conveyance , covered work, grant patent license parties receiving covered work authorizing use, propagate, modify convey specific copy covered work, patent license grant automatically extended recipients covered work works based . patent license “discriminatory” include within scope coverage, prohibits exercise , conditioned non-exercise one rights specifically granted License. may convey covered work party arrangement third party business distributing software, make payment third party based extent activity conveying work, third party grants, parties receive covered work , discriminatory patent license () connection copies covered work conveyed (copies made copies), (b) primarily connection specific products compilations contain covered work, unless entered arrangement, patent license granted, prior 28 March 2007. Nothing License shall construed excluding limiting implied license defenses infringement may otherwise available applicable patent law.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_12-no-surrender-of-others-freedom","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"12. No Surrender of Others’ Freedom","title":"GNU General Public License","text":"conditions imposed (whether court order, agreement otherwise) contradict conditions License, excuse conditions License. convey covered work satisfy simultaneously obligations License pertinent obligations, consequence may convey . example, agree terms obligate collect royalty conveying convey Program, way satisfy terms License refrain entirely conveying Program.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_13-use-with-the-gnu-affero-general-public-license","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"13. Use with the GNU Affero General Public License","title":"GNU General Public License","text":"Notwithstanding provision License, permission link combine covered work work licensed version 3 GNU Affero General Public License single combined work, convey resulting work. terms License continue apply part covered work, special requirements GNU Affero General Public License, section 13, concerning interaction network apply combination .","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_14-revised-versions-of-this-license","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"14. Revised Versions of this License","title":"GNU General Public License","text":"Free Software Foundation may publish revised /new versions GNU General Public License time time. new versions similar spirit present version, may differ detail address new problems concerns. version given distinguishing version number. Program specifies certain numbered version GNU General Public License “later version” applies , option following terms conditions either numbered version later version published Free Software Foundation. Program specify version number GNU General Public License, may choose version ever published Free Software Foundation. Program specifies proxy can decide future versions GNU General Public License can used, proxy’s public statement acceptance version permanently authorizes choose version Program. Later license versions may give additional different permissions. However, additional obligations imposed author copyright holder result choosing follow later version.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_15-disclaimer-of-warranty","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"15. Disclaimer of Warranty","title":"GNU General Public License","text":"WARRANTY PROGRAM, EXTENT PERMITTED APPLICABLE LAW. EXCEPT OTHERWISE STATED WRITING COPYRIGHT HOLDERS /PARTIES PROVIDE PROGRAM “” WITHOUT WARRANTY KIND, EITHER EXPRESSED IMPLIED, INCLUDING, LIMITED , IMPLIED WARRANTIES MERCHANTABILITY FITNESS PARTICULAR PURPOSE. ENTIRE RISK QUALITY PERFORMANCE PROGRAM . PROGRAM PROVE DEFECTIVE, ASSUME COST NECESSARY SERVICING, REPAIR CORRECTION.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_16-limitation-of-liability","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"16. Limitation of Liability","title":"GNU General Public License","text":"EVENT UNLESS REQUIRED APPLICABLE LAW AGREED WRITING COPYRIGHT HOLDER, PARTY MODIFIES /CONVEYS PROGRAM PERMITTED , LIABLE DAMAGES, INCLUDING GENERAL, SPECIAL, INCIDENTAL CONSEQUENTIAL DAMAGES ARISING USE INABILITY USE PROGRAM (INCLUDING LIMITED LOSS DATA DATA RENDERED INACCURATE LOSSES SUSTAINED THIRD PARTIES FAILURE PROGRAM OPERATE PROGRAMS), EVEN HOLDER PARTY ADVISED POSSIBILITY DAMAGES.","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"id_17-interpretation-of-sections-15-and-16","dir":"","previous_headings":"TERMS AND CONDITIONS","what":"17. Interpretation of Sections 15 and 16","title":"GNU General Public License","text":"disclaimer warranty limitation liability provided given local legal effect according terms, reviewing courts shall apply local law closely approximates absolute waiver civil liability connection Program, unless warranty assumption liability accompanies copy Program return fee. END TERMS CONDITIONS","code":""},{"path":"https://fastcpd.xingchi.li/LICENSE.html","id":"how-to-apply-these-terms-to-your-new-programs","dir":"","previous_headings":"","what":"How to Apply These Terms to Your New Programs","title":"GNU General Public License","text":"develop new program, want greatest possible use public, best way achieve make free software everyone can redistribute change terms. , attach following notices program. safest attach start source file effectively state exclusion warranty; file least “copyright” line pointer full notice found. Also add information contact electronic paper mail. program terminal interaction, make output short notice like starts interactive mode: hypothetical commands show w show c show appropriate parts General Public License. course, program’s commands might different; GUI interface, use “box”. also get employer (work programmer) school, , sign “copyright disclaimer” program, necessary. information , apply follow GNU GPL, see . GNU General Public License permit incorporating program proprietary programs. program subroutine library, may consider useful permit linking proprietary applications library. want , use GNU Lesser General Public License instead License. first, please read .","code":" Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. This is free software, and you are welcome to redistribute it under certain conditions; type 'show c' for details."},{"path":[]},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-change","dir":"Articles","previous_headings":"Data setup","what":"Univariate mean change","title":"Comparison with other R packages","text":"","code":"# Univariate mean change set.seed(1) p <- 1 mean_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_1)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-andor-variance-change","dir":"Articles","previous_headings":"Data setup","what":"Univariate mean and/or variance change","title":"Comparison with other R packages","text":"","code":"# Univariate mean and/or variance change set.seed(1) p <- 1 mv_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_1)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-change","dir":"Articles","previous_headings":"Data setup","what":"Multivariate mean change","title":"Comparison with other R packages","text":"","code":"# Multivariate mean change set.seed(1) p <- 3 mean_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_3)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-andor-variance-change","dir":"Articles","previous_headings":"Data setup","what":"Multivariate mean and/or variance change","title":"Comparison with other R packages","text":"","code":"# Multivariate mean and/or variance change set.seed(1) p <- 4 mv_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_3)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"linear-regression","dir":"Articles","previous_headings":"Data setup","what":"Linear regression","title":"Comparison with other R packages","text":"","code":"# Linear regression set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta_0[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta_0[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta_0[3, ] + rnorm(100, 0, 3) ) lm_data <- data.frame(y = y, x = x) plot.ts(lm_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"logistic-regression","dir":"Articles","previous_headings":"Data setup","what":"Logistic regression","title":"Comparison with other R packages","text":"","code":"# Logistic regression set.seed(1) n <- 500 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(300, 1, 1 / (1 + exp(-x[1:300, ] %*% theta[1, ]))), rbinom(200, 1, 1 / (1 + exp(-x[301:n, ] %*% theta[2, ]))) ) binomial_data <- data.frame(y = y, x = x) plot.ts(binomial_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"poisson-regression","dir":"Articles","previous_headings":"Data setup","what":"Poisson regression","title":"Comparison with other R packages","text":"","code":"# Poisson regression set.seed(1) n <- 1100 p <- 3 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) delta <- rnorm(p) theta_0 <- c(1, 0.3, -1) y <- c( rpois(500, exp(x[1:500, ] %*% theta_0)), rpois(300, exp(x[501:800, ] %*% (theta_0 + delta))), rpois(200, exp(x[801:1000, ] %*% theta_0)), rpois(100, exp(x[1001:1100, ] %*% (theta_0 - delta))) ) poisson_data <- data.frame(y = y, x = x) plot.ts(log(poisson_data$y)) plot.ts(poisson_data[, -1])"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"lasso","dir":"Articles","previous_headings":"Data setup","what":"Lasso","title":"Comparison with other R packages","text":"","code":"# Lasso set.seed(1) n <- 480 p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) lasso_data <- data.frame(y = y, x = x) plot.ts(lasso_data[, seq_len(p_true + 1)])"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"ar3","dir":"Articles","previous_headings":"Data setup","what":"AR(3)","title":"Comparison with other R packages","text":"","code":"# AR(3) set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } ar_data <- x[-seq_len(3)] plot.ts(ar_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"garch1-1","dir":"Articles","previous_headings":"Data setup","what":"GARCH(1, 1)","title":"Comparison with other R packages","text":"","code":"# GARCH(1, 1) set.seed(1) n <- 400 sigma_2 <- rep(1, n + 1) x <- rep(0, n + 1) for (i in seq_len(200)) { sigma_2[i + 1] <- 20 + 0.5 * x[i]^2 + 0.1 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } for (i in 201:400) { sigma_2[i + 1] <- 1 + 0.1 * x[i]^2 + 0.5 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } garch_data <- x[-1] plot.ts(garch_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"var2","dir":"Articles","previous_headings":"Data setup","what":"VAR(2)","title":"Comparison with other R packages","text":"","code":"# VAR(2) set.seed(1) n <- 800 p <- 2 theta_1 <- matrix(c(-0.3, 0.6, -0.5, 0.4, 0.2, 0.2, 0.2, -0.2), nrow = p) theta_2 <- matrix(c(0.3, -0.4, 0.1, -0.5, -0.5, -0.2, -0.5, 0.2), nrow = p) x <- matrix(0, n + 2, p) for (i in 1:500) { x[i + 2, ] <- theta_1 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } for (i in 501:n) { x[i + 2, ] <- theta_2 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } var_data <- x[-seq_len(2), ] plot.ts(var_data)"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-change-1","dir":"Articles","previous_headings":"","what":"Univariate mean change","title":"Comparison with other R packages","text":"true change points 300 700. methods plotted due un-retrievable change points.","code":"results[[\"mean_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_1, r.progress = FALSE)@cp_set results[[\"mean_data_1\"]][[\"fastcpd\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_1, G = floor(length(mean_data_1) / 6))$cpts results[[\"mean_data_1\"]][[\"CptNonPar\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ 1, data = data.frame(y = mean_data_1))$breakpoints results[[\"mean_data_1\"]][[\"strucchange\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_1)$estimates results[[\"mean_data_1\"]][[\"ecp\"]] #> [1] 1 301 701 1001 results[[\"mean_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.mean(c(mean_data_1))@cpts results[[\"mean_data_1\"]][[\"changepoint\"]] #> [1] 300 1000 results[[\"mean_data_1\"]][[\"breakfast\"]] <- breakfast::breakfast(mean_data_1)$cptmodel.list[[6]]$cpts results[[\"mean_data_1\"]][[\"breakfast\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"wbs\"]] <- wbs::wbs(mean_data_1)$cpt$cpt.ic$mbic.penalty results[[\"mean_data_1\"]][[\"wbs\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"mosum\"]] <- mosum::mosum(c(mean_data_1), G = 40)$cpts.info$cpts results[[\"mean_data_1\"]][[\"mosum\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"fpop\"]] <- fpop::Fpop(mean_data_1, nrow(mean_data_1))$t.est results[[\"mean_data_1\"]][[\"fpop\"]] #> [1] 300 700 1000 results[[\"mean_data_1\"]][[\"gfpop\"]] <- gfpop::gfpop( data = mean_data_1, mygraph = gfpop::graph( penalty = 2 * log(nrow(mean_data_1)) * gfpop::sdDiff(mean_data_1) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints results[[\"mean_data_1\"]][[\"gfpop\"]] #> [1] 300 700 1000 results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_1), ncol(mean_data_1) ) )$changepoints[, \"location\"] results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_1, K = 2)$bestBkp results[[\"mean_data_1\"]][[\"jointseg\"]] #> [1] 300 700 results[[\"mean_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mean_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mean_data_1\"]][[\"Rbeast\"]] #> [1] 701 301 NaN NaN NaN NaN NaN NaN NaN NaN results[[\"mean_data_1\"]][[\"stepR\"]] <- stepR::stepFit(mean_data_1, alpha = 0.5)$rightEnd results[[\"mean_data_1\"]][[\"stepR\"]] #> [1] 300 700 1000 results[[\"mean_data_1\"]][[\"cpm\"]] <- cpm::processStream(mean_data_1, cpmType = \"Student\")$changePoints results[[\"mean_data_1\"]][[\"cpm\"]] #> [1] 299 699 results[[\"mean_data_1\"]][[\"segmented\"]] <- segmented::stepmented( as.numeric(mean_data_1), npsi = 2 )$psi[, \"Est.\"] results[[\"mean_data_1\"]][[\"segmented\"]] #> psi1.index psi2.index #> 298.1981 699.1524 results[[\"mean_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1), data = data.frame(y = mean_data_1, x = seq_len(nrow(mean_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"mcp\"]]) } results[[\"mean_data_1\"]][[\"not\"]] <- not::not(mean_data_1, contrast = \"pcwsConstMean\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"not\"]]) } results[[\"mean_data_1\"]][[\"bcp\"]] <- bcp::bcp(mean_data_1) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"bcp\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"univariate-mean-andor-variance-change-1","dir":"Articles","previous_headings":"","what":"Univariate mean and/or variance change","title":"Comparison with other R packages","text":"true change points 300, 700, 1000, 1300 1700. methods plotted due un-retrievable change points.","code":"results[[\"mv_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_1, r.progress = FALSE)@cp_set results[[\"mv_data_1\"]][[\"fastcpd\"]] #> [1] 300 700 1001 1300 1700 results[[\"mv_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_1)$estimates results[[\"mv_data_1\"]][[\"ecp\"]] #> [1] 1 301 701 1001 1301 1701 2001 results[[\"mv_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.meanvar(c(mv_data_1))@cpts results[[\"mv_data_1\"]][[\"changepoint\"]] #> [1] 300 2000 results[[\"mv_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mv_data_1, G = floor(length(mv_data_1) / 6))$cpts results[[\"mv_data_1\"]][[\"CptNonPar\"]] #> [1] 333 700 1300 results[[\"mv_data_1\"]][[\"cpm\"]] <- cpm::processStream(mv_data_1, cpmType = \"GLR\")$changePoints results[[\"mv_data_1\"]][[\"cpm\"]] #> [1] 293 300 403 408 618 621 696 1000 1021 1024 1293 1300 1417 1693 1700 #> [16] 1981 results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_1), ncol(mv_data_1) ) )$changepoints[, \"location\"] results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] #> [1] 300 700 701 702 704 707 708 712 715 716 717 718 721 722 723 #> [16] 726 727 729 731 732 734 736 740 742 744 746 748 750 753 755 #> [31] 756 757 759 760 762 764 765 766 768 769 771 772 774 776 777 #> [46] 784 785 786 789 791 792 794 797 798 799 801 802 803 807 809 #> [61] 810 813 815 817 819 826 827 828 829 831 833 835 836 837 838 #> [76] 840 841 842 843 845 848 849 852 854 860 862 864 866 868 870 #> [91] 872 875 879 881 884 886 887 888 889 896 897 898 899 901 903 #> [106] 904 905 906 909 910 912 913 915 917 919 921 922 923 925 927 #> [121] 928 932 934 936 937 940 944 945 947 948 949 951 956 958 959 #> [136] 961 962 963 964 966 967 968 972 974 976 978 979 986 988 990 #> [151] 992 995 998 1000 1300 1700 1702 1703 1704 1705 1708 1710 1712 1714 1716 #> [166] 1717 1718 1720 1721 1723 1725 1726 1727 1729 1731 1733 1735 1736 1737 1739 #> [181] 1742 1745 1747 1748 1752 1754 1756 1758 1759 1760 1766 1768 1770 1771 1773 #> [196] 1775 1778 1782 1784 1785 1790 1792 1793 1795 1796 1797 1799 1800 1802 1803 #> [211] 1804 1805 1806 1807 1808 1809 1813 1815 1816 1818 1821 1824 1825 1827 1828 #> [226] 1829 1833 1835 1837 1840 1841 1842 1848 1849 1851 1852 1854 1855 1857 1859 #> [241] 1860 1862 1863 1865 1867 1868 1876 1878 1879 1880 1882 1883 1884 1886 1887 #> [256] 1889 1894 1898 1899 1905 1906 1907 1908 1909 1912 1919 1920 1921 1923 1924 #> [271] 1926 1927 1928 1930 1933 1934 1935 1936 1938 1940 1941 1944 1947 1950 1952 #> [286] 1954 1955 1956 1960 1962 1963 1965 1966 1967 1969 1970 1974 1976 1977 1978 #> [301] 1980 1985 1987 1988 1990 1996 1997 1998 results[[\"mv_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mv_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mv_data_1\"]][[\"Rbeast\"]] #> [1] 1855 1794 1301 1986 301 703 1981 1769 1860 709 results[[\"mv_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1, ~ 1, ~ 1, ~ 1), data = data.frame(y = mv_data_1, x = seq_len(nrow(mv_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"mcp\"]]) } results[[\"mv_data_1\"]][[\"not\"]] <- not::not(mv_data_1, contrast = \"pcwsConstMeanVar\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"not\"]]) } #> Press [enter] to continue"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-change-1","dir":"Articles","previous_headings":"","what":"Multivariate mean change","title":"Comparison with other R packages","text":"true change points 300 700. methods plotted due un-retrievable change points.","code":"results[[\"mean_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_3, r.progress = FALSE)@cp_set results[[\"mean_data_3\"]][[\"fastcpd\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_3, G = floor(nrow(mean_data_3) / 6))$cpts results[[\"mean_data_3\"]][[\"CptNonPar\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_3), ncol(mean_data_3) ) )$changepoints[, \"location\"] results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_3, K = 2)$bestBkp results[[\"mean_data_3\"]][[\"jointseg\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mean_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mean_data_3\"]][[\"Rbeast\"]] #> [,1] [,2] [,3] #> [1,] 301 701 301 #> [2,] 701 301 701 #> [3,] 142 117 926 #> [4,] 305 NaN NaN #> [5,] 705 NaN NaN #> [6,] 694 NaN NaN #> [7,] NaN NaN NaN #> [8,] NaN NaN NaN #> [9,] NaN NaN NaN #> [10,] NaN NaN NaN results[[\"mean_data_3\"]][[\"strucchange\"]] <- strucchange::breakpoints( cbind(y.1, y.2, y.3) ~ 1, data = data.frame(y = mean_data_3) )$breakpoints results[[\"mean_data_3\"]][[\"strucchange\"]] #> [1] 300 700 results[[\"mean_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_3)$estimates results[[\"mean_data_3\"]][[\"ecp\"]] #> [1] 1 301 701 1001 results[[\"mean_data_3\"]][[\"bcp\"]] <- bcp::bcp(mean_data_3) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_3\"]][[\"bcp\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"multivariate-mean-andor-variance-change-1","dir":"Articles","previous_headings":"","what":"Multivariate mean and/or variance change","title":"Comparison with other R packages","text":"true change points 300, 700, 1000, 1300 1700. methods plotted due un-retrievable change points.","code":"results[[\"mv_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_3, r.progress = FALSE)@cp_set results[[\"mv_data_3\"]][[\"fastcpd\"]] #> [1] 300 700 1000 1300 1700 results[[\"mv_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_3)$estimates results[[\"mv_data_3\"]][[\"ecp\"]] #> [1] 1 301 701 1001 1301 1701 2001 results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_3), ncol(mv_data_3) ) )$changepoints[, \"location\"] results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] #> [1] 300 700 701 703 705 707 708 709 711 712 714 715 717 718 720 #> [16] 721 723 724 726 727 729 731 733 734 736 737 739 740 742 743 #> [31] 744 746 747 749 750 752 753 754 755 756 758 760 762 763 765 #> [46] 766 767 769 770 772 773 774 775 777 779 780 782 784 786 788 #> [61] 790 791 793 795 797 799 801 803 804 806 809 810 811 813 814 #> [76] 816 817 818 820 821 823 825 827 828 830 831 833 835 836 837 #> [91] 838 840 842 843 845 846 848 849 850 852 853 854 855 856 858 #> [106] 859 860 862 863 865 866 868 869 871 872 874 876 877 878 879 #> [121] 881 883 885 887 888 889 891 893 894 895 897 898 900 901 903 #> [136] 904 906 908 909 911 913 914 916 917 918 920 921 923 924 925 #> [151] 927 928 929 931 932 934 936 937 938 939 941 942 943 945 946 #> [166] 947 949 950 952 954 955 956 957 958 959 961 962 964 965 967 #> [181] 968 970 972 973 974 975 977 979 981 982 984 985 986 987 988 #> [196] 990 991 992 994 995 997 999 1000 1300 1700 1702 1703 1704 1705 1706 #> [211] 1708 1709 1710 1712 1713 1714 1715 1717 1719 1721 1722 1723 1725 1727 1729 #> [226] 1730 1732 1734 1735 1737 1738 1739 1741 1742 1744 1746 1748 1750 1752 1753 #> [241] 1754 1755 1757 1758 1759 1761 1762 1763 1764 1766 1767 1769 1770 1771 1773 #> [256] 1774 1775 1777 1779 1781 1782 1783 1785 1786 1788 1789 1791 1793 1794 1796 #> [271] 1798 1800 1803 1804 1805 1806 1808 1809 1811 1812 1814 1815 1817 1818 1819 #> [286] 1821 1822 1824 1825 1827 1828 1829 1831 1833 1835 1836 1838 1839 1841 1843 #> [301] 1844 1846 1847 1848 1850 1851 1853 1854 1856 1857 1858 1859 1860 1862 1863 #> [316] 1864 1865 1867 1869 1870 1872 1873 1874 1876 1878 1879 1881 1882 1884 1885 #> [331] 1887 1889 1891 1893 1894 1896 1898 1899 1900 1901 1902 1904 1906 1907 1909 #> [346] 1911 1913 1914 1916 1917 1918 1919 1921 1923 1924 1925 1927 1928 1930 1932 #> [361] 1933 1935 1936 1938 1939 1941 1942 1944 1946 1948 1950 1951 1952 1954 1956 #> [376] 1957 1959 1961 1963 1965 1967 1968 1970 1972 1973 1974 1976 1977 1979 1981 #> [391] 1982 1984 1985 1987 1989 1990 1992 1993 1995 1996 1998 results[[\"mv_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mv_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mv_data_3\"]][[\"Rbeast\"]] #> [,1] [,2] [,3] [,4] #> [1,] 701 1301 301 1301 #> [2,] 1301 301 1301 710 #> [3,] 301 701 1829 301 #> [4,] 1968 1993 702 886 #> [5,] 1994 884 1822 1975 #> [6,] 814 755 810 1915 #> [7,] 1962 781 845 778 #> [8,] 1978 767 1738 1985 #> [9,] 1870 747 1754 792 #> [10,] 1843 722 771 953"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"linear-regression-1","dir":"Articles","previous_headings":"","what":"Linear regression","title":"Comparison with other R packages","text":"true change points 100 200.","code":"results[[\"lm_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lm(lm_data, r.progress = FALSE)@cp_set results[[\"lm_data\"]][[\"fastcpd\"]] #> [1] 97 201 results[[\"lm_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lm_data)$breakpoints results[[\"lm_data\"]][[\"strucchange\"]] #> [1] 100 201 results[[\"lm_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ . - 1, data.frame(y = lm_data$y, x = lm_data[, -1], index = seq_len(nrow(lm_data))) ), seg.Z = ~ index )$psi[, \"Est.\"] results[[\"lm_data\"]][[\"segmented\"]] #> [1] 233"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"logistic-regression-1","dir":"Articles","previous_headings":"","what":"Logistic regression","title":"Comparison with other R packages","text":"true change point 300.","code":"results[[\"binomial_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.binomial(binomial_data, r.progress = FALSE)@cp_set results[[\"binomial_data\"]][[\"fastcpd\"]] #> [1] 302 results[[\"binomial_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = binomial_data)$breakpoints results[[\"binomial_data\"]][[\"strucchange\"]] #> [1] 297"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"poisson-regression-1","dir":"Articles","previous_headings":"","what":"Poisson regression","title":"Comparison with other R packages","text":"true change points 500, 800 1000.","code":"results[[\"poisson_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.poisson(poisson_data, r.progress = FALSE)@cp_set results[[\"poisson_data\"]][[\"fastcpd\"]] #> [1] 498 805 1003 results[[\"poisson_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = poisson_data)$breakpoints results[[\"poisson_data\"]][[\"strucchange\"]] #> [1] 935"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"lasso-1","dir":"Articles","previous_headings":"","what":"Lasso","title":"Comparison with other R packages","text":"true change points 80, 200 320.","code":"results[[\"lasso_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lasso(lasso_data, r.progress = FALSE)@cp_set results[[\"lasso_data\"]][[\"fastcpd\"]] #> [1] 79 199 321 results[[\"lasso_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lasso_data)$breakpoints results[[\"lasso_data\"]][[\"strucchange\"]] #> [1] 80 200 321"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"ar3-1","dir":"Articles","previous_headings":"","what":"AR(3)","title":"Comparison with other R packages","text":"true change point 600. methods plotted due un-retrievable change points.","code":"results[[\"ar_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.ar(ar_data, 3, r.progress = FALSE)@cp_set results[[\"ar_data\"]][[\"fastcpd\"]] #> [1] 614 results[[\"ar_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(ar_data, G = floor(length(ar_data) / 6))$cpts results[[\"ar_data\"]][[\"CptNonPar\"]] #> numeric(0) results[[\"ar_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ x + 1, data.frame(y = ar_data, x = seq_along(ar_data)) ), seg.Z = ~ x )$psi[, \"Est.\"] results[[\"ar_data\"]][[\"segmented\"]] #> [1] 690 results[[\"ar_data\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1 + ar(3), ~ 0 + ar(3)), data = data.frame(y = ar_data, x = seq_along(ar_data)), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"ar_data\"]][[\"mcp\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"garch1-1-1","dir":"Articles","previous_headings":"","what":"GARCH(1, 1)","title":"Comparison with other R packages","text":"true change point 200.","code":"results[[\"garch_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.garch(garch_data, c(1, 1), r.progress = FALSE)@cp_set results[[\"garch_data\"]][[\"fastcpd\"]] #> [1] 205 results[[\"garch_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(garch_data, G = floor(length(garch_data) / 6))$cpts results[[\"garch_data\"]][[\"CptNonPar\"]] #> [1] 206 results[[\"garch_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(x ~ 1, data = data.frame(x = garch_data))$breakpoints results[[\"garch_data\"]][[\"strucchange\"]] #> [1] NA"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"var2-1","dir":"Articles","previous_headings":"","what":"VAR(2)","title":"Comparison with other R packages","text":"true change points 500.","code":"results[[\"var_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.var(var_data, 2, r.progress = FALSE)@cp_set results[[\"var_data\"]][[\"fastcpd\"]] #> [1] 500 results[[\"var_data\"]][[\"VARDetect\"]] <- VARDetect::tbss(var_data)$cp results[[\"var_data\"]][[\"VARDetect\"]] #> [1] 501"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"detection-comparison-using-well_log","dir":"Articles","previous_headings":"","what":"Detection comparison using well_log","title":"Comparison with other R packages","text":"","code":"well_log <- fastcpd::well_log well_log <- well_log[well_log > 1e5] results[[\"well_log\"]] <- list( fastcpd = fastcpd.mean(well_log, trim = 0.003)@cp_set, changepoint = changepoint::cpt.mean(well_log)@cpts, CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6))$cpts, strucchange = strucchange::breakpoints( y ~ 1, data = data.frame(y = well_log) )$breakpoints, ecp = ecp::e.divisive(matrix(well_log))$estimates, breakfast = breakfast::breakfast(well_log)$cptmodel.list[[6]]$cpts, wbs = wbs::wbs(well_log)$cpt$cpt.ic$mbic.penalty, mosum = mosum::mosum(c(well_log), G = 40)$cpts.info$cpts, # fpop = fpop::Fpop(well_log, length(well_log))$t.est, # meaningless gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints, InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) )$changepoints[, \"location\"], jointseg = jointseg::jointSeg(well_log, K = 12)$bestBkp, Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp, stepR = stepR::stepFit(well_log, alpha = 0.5)$rightEnd ) results[[\"well_log\"]] #> $fastcpd #> [1] 12 572 704 776 1021 1057 1198 1347 1406 1502 1660 1842 2023 2202 2384 #> [16] 2445 2507 2567 2749 2926 3094 3107 3509 3622 3709 3820 3976 #> #> $changepoint #> [1] 2738 3989 #> #> $CptNonPar #> [1] 1021 1681 2022 2738 #> #> $strucchange #> [1] 1057 1660 2568 3283 #> #> $ecp #> [1] 1 33 315 435 567 705 803 1026 1058 1348 1503 1662 1843 2024 2203 #> [16] 2386 2446 2508 2569 2745 2780 2922 3073 3136 3252 3465 3500 3554 3623 3710 #> [31] 3821 3868 3990 #> #> $breakfast #> [1] 33 310 434 572 704 779 1021 1057 1347 1502 1659 1842 2021 2032 2202 #> [16] 2384 2446 2507 2567 2747 2779 2926 3094 3106 3125 3283 3464 3499 3622 3709 #> [31] 3806 3835 3848 3877 3896 3976 #> #> $wbs #> [1] 2568 1057 1661 1842 2385 2023 2445 1502 2744 6 2507 1021 3709 3820 1402 #> [16] 434 1408 3131 3976 3509 3622 776 1197 3094 704 3104 1347 314 2921 3251 #> [31] 3464 3848 3906 2779 1663 3636 60 3904 2202 566 12 3639 1200 7 1671 #> [46] 706 #> #> $mosum #> [1] 6 434 704 1017 1057 1325 1502 1661 1842 2023 2385 2445 2507 2567 2744 #> [16] 3060 3438 3509 3610 3697 3820 3867 3976 #> #> $gfpop #> [1] 6 7 8 12 314 434 556 560 704 776 1021 1057 1197 1200 1347 #> [16] 1364 1405 1407 1491 1502 1661 1842 2023 2385 2445 2507 2567 2664 2747 2752 #> [31] 2921 3094 3104 3125 3251 3464 3499 3622 3709 3820 3976 3989 #> #> $InspectChangepoint #> [1] 6 8 15 71 314 434 521 704 706 776 830 1021 1057 1059 1347 #> [16] 1402 1405 1408 1412 1502 1520 1659 1661 1663 1694 1842 1848 2022 2202 2384 #> [31] 2387 2445 2507 2567 2568 2673 2738 2746 2752 2779 2921 3131 3251 3464 3509 #> [46] 3609 3658 3709 3806 3976 #> #> $jointseg #> [1] 6 1021 1057 1502 1661 1842 2022 2384 2445 2507 2568 2738 #> #> $Rbeast #> [1] 1058 1662 7 1022 2447 2386 2023 1503 2745 3710 #> #> $stepR #> [1] 7 14 314 434 566 704 776 1021 1057 1197 1200 1347 1405 1407 1502 #> [16] 1661 1694 1842 2023 2202 2385 2445 2507 2567 2747 2752 2921 3094 3104 3125 #> [31] 3251 3464 3499 3609 3658 3709 3820 3867 3905 3976 3989 package_list <- sort(names(results[[\"well_log\"]]), decreasing = TRUE) comparison_table <- NULL for (package_index in seq_along(package_list)) { package <- package_list[[package_index]] comparison_table <- rbind( comparison_table, data.frame( change_point = results[[\"well_log\"]][[package]], package = package, y_offset = (package_index - 1) * 1000 ) ) } most_selected <- sort(table(comparison_table$change_point), decreasing = TRUE) most_selected <- sort(as.numeric(names(most_selected[most_selected >= 4]))) for (i in seq_len(length(most_selected) - 1)) { if (most_selected[i + 1] - most_selected[i] < 2) { most_selected[i] <- NA most_selected[i + 1] <- most_selected[i + 1] - 0.5 } } (most_selected <- most_selected[!is.na(most_selected)]) #> [1] 6.5 314.0 434.0 704.0 776.0 1021.0 1057.0 1347.0 1502.0 1661.0 #> [11] 1842.0 2023.0 2202.0 2384.5 2445.0 2507.0 2567.5 2738.0 2921.0 3094.0 #> [21] 3251.0 3464.0 3509.0 3622.0 3709.0 3820.0 3976.0 if (requireNamespace(\"ggplot2\", quietly = TRUE)) { ggplot2::ggplot() + ggplot2::geom_point( data = data.frame(x = seq_along(well_log), y = c(well_log)), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_vline( xintercept = most_selected, color = \"black\", linetype = \"dashed\", alpha = 0.2 ) + ggplot2::geom_point( data = comparison_table, ggplot2::aes(x = change_point, y = 50000 + y_offset, color = package), shape = 17, size = 1.9 ) + ggplot2::geom_hline( data = comparison_table, ggplot2::aes(yintercept = 50000 + y_offset, color = package), linetype = \"dashed\", alpha = 0.1 ) + ggplot2::coord_cartesian( ylim = c(50000 - 500, max(well_log) + 1000), xlim = c(-200, length(well_log) + 200), expand = FALSE ) + ggplot2::theme( panel.background = ggplot2::element_blank(), panel.border = ggplot2::element_rect(colour = \"black\", fill = NA), panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank() ) + ggplot2::xlab(NULL) + ggplot2::ylab(NULL) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"time-comparison-using-well_log","dir":"Articles","previous_headings":"","what":"Time comparison using well_log","title":"Comparison with other R packages","text":"","code":"results[[\"microbenchmark\"]] <- microbenchmark::microbenchmark( fastcpd = fastcpd::fastcpd.mean(well_log, trim = 0.003, r.progress = FALSE), changepoint = changepoint::cpt.mean(well_log, method = \"PELT\"), CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6)), strucchange = strucchange::breakpoints(y ~ 1, data = data.frame(y = well_log)), ecp = ecp::e.divisive(matrix(well_log)), breakfast = breakfast::breakfast(well_log), wbs = wbs::wbs(well_log), mosum = mosum::mosum(c(well_log), G = 40), fpop = fpop::Fpop(well_log, nrow(well_log)), gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" ), InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) ), jointseg = jointseg::jointSeg(well_log, K = 12), Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE ), stepR = stepR::stepFit(well_log, alpha = 0.5), not = not::not(well_log, contrast = \"pcwsConstMean\"), times = 10 ) results[[\"microbenchmark\"]] #> Unit: milliseconds #> expr min lq mean median #> fastcpd 6.257120e+01 6.696175e+01 7.183964e+01 7.168669e+01 #> changepoint 3.205076e+01 3.305076e+01 4.025595e+01 3.774400e+01 #> CptNonPar 1.875995e+04 2.014073e+04 2.244910e+04 2.224506e+04 #> strucchange 6.359889e+04 6.409690e+04 6.651444e+04 6.565439e+04 #> ecp 7.505232e+05 7.895889e+05 8.168647e+05 8.170952e+05 #> breakfast 9.606171e+03 9.819381e+03 1.052611e+04 1.048197e+04 #> wbs 1.163237e+02 1.180537e+02 1.263443e+02 1.254133e+02 #> mosum 1.059973e+00 1.147898e+00 2.175931e+00 1.409396e+00 #> fpop 2.604002e+00 3.676689e+00 4.859312e+00 4.308841e+00 #> gfpop 6.057383e+01 6.595090e+01 7.277643e+01 7.212125e+01 #> InspectChangepoint 1.584727e+02 2.171007e+02 2.414921e+02 2.424665e+02 #> jointseg 2.104403e+01 2.284920e+01 3.577861e+01 2.708083e+01 #> Rbeast 6.545716e+02 6.913542e+02 7.993305e+02 7.504324e+02 #> stepR 2.740003e+01 3.020269e+01 3.149624e+01 3.188329e+01 #> not 9.715867e+01 1.000287e+02 1.101258e+02 1.074475e+02 #> uq max neval #> 7.689381e+01 8.332375e+01 10 #> 4.669485e+01 5.904173e+01 10 #> 2.380236e+04 2.705807e+04 10 #> 6.597650e+04 7.757488e+04 10 #> 8.259550e+05 8.834288e+05 10 #> 1.103289e+04 1.197235e+04 10 #> 1.276892e+02 1.455261e+02 10 #> 1.957312e+00 8.705062e+00 10 #> 5.107175e+00 1.085365e+01 10 #> 7.742432e+01 8.794816e+01 10 #> 3.060519e+02 3.145695e+02 10 #> 4.362760e+01 9.516090e+01 10 #> 8.539695e+02 1.163607e+03 10 #> 3.294534e+01 3.530981e+01 10 #> 1.147504e+02 1.331989e+02 10 if (requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"microbenchmark\", quietly = TRUE)) { ggplot2::autoplot(results[[\"microbenchmark\"]]) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Comparison with other R packages","text":"document generated following code:","code":"R -e 'knitr::knit(\"vignettes/comparison-packages.Rmd.original\", output = \"vignettes/comparison-packages.Rmd\")' && rm -rf vignettes/figure && mv -f figure vignettes"},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"related-issues","dir":"Articles","previous_headings":"","what":"Related issues","title":"Comparison with other R packages","text":"mosum: [#4]. mcp: link 1 link 2. bcp: [#5]. gfpop: [#10].","code":""},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"acknowledgements","dir":"Articles","previous_headings":"","what":"Acknowledgements","title":"Comparison with other R packages","text":"Dr. Vito Muggeo, author segmented package tips piece-wise constant function.","code":""},{"path":"https://fastcpd.xingchi.li/articles/comparison-packages.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Comparison with other R packages","text":"","code":"knitr::opts_chunk$set( collapse = TRUE, comment = \"#>\", eval = TRUE, cache = FALSE, warning = FALSE, fig.width = 8, fig.height = 5 ) if (requireNamespace(\"microbenchmark\", quietly = TRUE)) { library(microbenchmark) } if (file.exists(\"comparison-packages-results.RData\")) { # Available at https://pcloud.xingchi.li/comparison-packages-results.RData load(\"comparison-packages-results.RData\") } else { results <- list() } # Univariate mean change set.seed(1) p <- 1 mean_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_1) # Univariate mean and/or variance change set.seed(1) p <- 1 mv_data_1 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_1) # Multivariate mean change set.seed(1) p <- 3 mean_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) plot.ts(mean_data_3) # Multivariate mean and/or variance change set.seed(1) p <- 4 mv_data_3 <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) plot.ts(mv_data_3) # Linear regression set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta_0[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta_0[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta_0[3, ] + rnorm(100, 0, 3) ) lm_data <- data.frame(y = y, x = x) plot.ts(lm_data) # Logistic regression set.seed(1) n <- 500 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(300, 1, 1 / (1 + exp(-x[1:300, ] %*% theta[1, ]))), rbinom(200, 1, 1 / (1 + exp(-x[301:n, ] %*% theta[2, ]))) ) binomial_data <- data.frame(y = y, x = x) plot.ts(binomial_data) # Poisson regression set.seed(1) n <- 1100 p <- 3 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) delta <- rnorm(p) theta_0 <- c(1, 0.3, -1) y <- c( rpois(500, exp(x[1:500, ] %*% theta_0)), rpois(300, exp(x[501:800, ] %*% (theta_0 + delta))), rpois(200, exp(x[801:1000, ] %*% theta_0)), rpois(100, exp(x[1001:1100, ] %*% (theta_0 - delta))) ) poisson_data <- data.frame(y = y, x = x) plot.ts(log(poisson_data$y)) plot.ts(poisson_data[, -1]) # Lasso set.seed(1) n <- 480 p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) lasso_data <- data.frame(y = y, x = x) plot.ts(lasso_data[, seq_len(p_true + 1)]) # AR(3) set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } ar_data <- x[-seq_len(3)] plot.ts(ar_data) # GARCH(1, 1) set.seed(1) n <- 400 sigma_2 <- rep(1, n + 1) x <- rep(0, n + 1) for (i in seq_len(200)) { sigma_2[i + 1] <- 20 + 0.5 * x[i]^2 + 0.1 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } for (i in 201:400) { sigma_2[i + 1] <- 1 + 0.1 * x[i]^2 + 0.5 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } garch_data <- x[-1] plot.ts(garch_data) # VAR(2) set.seed(1) n <- 800 p <- 2 theta_1 <- matrix(c(-0.3, 0.6, -0.5, 0.4, 0.2, 0.2, 0.2, -0.2), nrow = p) theta_2 <- matrix(c(0.3, -0.4, 0.1, -0.5, -0.5, -0.2, -0.5, 0.2), nrow = p) x <- matrix(0, n + 2, p) for (i in 1:500) { x[i + 2, ] <- theta_1 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } for (i in 501:n) { x[i + 2, ] <- theta_2 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } var_data <- x[-seq_len(2), ] plot.ts(var_data) results[[\"mean_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_1, r.progress = FALSE)@cp_set results[[\"mean_data_1\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"fastcpd\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_1, G = floor(length(mean_data_1) / 6))$cpts results[[\"mean_data_1\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"CptNonPar\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ 1, data = data.frame(y = mean_data_1))$breakpoints results[[\"mean_data_1\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"strucchange\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_1)$estimates results[[\"mean_data_1\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"ecp\"]], c(1, 301, 701, 1001), tolerance = 0.2) results[[\"mean_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.mean(c(mean_data_1))@cpts results[[\"mean_data_1\"]][[\"changepoint\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"changepoint\"]], c(300, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"breakfast\"]] <- breakfast::breakfast(mean_data_1)$cptmodel.list[[6]]$cpts results[[\"mean_data_1\"]][[\"breakfast\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"breakfast\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"wbs\"]] <- wbs::wbs(mean_data_1)$cpt$cpt.ic$mbic.penalty results[[\"mean_data_1\"]][[\"wbs\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"wbs\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"mosum\"]] <- mosum::mosum(c(mean_data_1), G = 40)$cpts.info$cpts results[[\"mean_data_1\"]][[\"mosum\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"mosum\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"fpop\"]] <- fpop::Fpop(mean_data_1, nrow(mean_data_1))$t.est results[[\"mean_data_1\"]][[\"fpop\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"fpop\"]], c(300, 700, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"gfpop\"]] <- gfpop::gfpop( data = mean_data_1, mygraph = gfpop::graph( penalty = 2 * log(nrow(mean_data_1)) * gfpop::sdDiff(mean_data_1) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints results[[\"mean_data_1\"]][[\"gfpop\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"gfpop\"]], c(300, 700, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_1), ncol(mean_data_1) ) )$changepoints[, \"location\"] results[[\"mean_data_1\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"InspectChangepoint\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_1, K = 2)$bestBkp results[[\"mean_data_1\"]][[\"jointseg\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"jointseg\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mean_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mean_data_1\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"Rbeast\"]], c(701, 301, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN), tolerance = 0.2) results[[\"mean_data_1\"]][[\"stepR\"]] <- stepR::stepFit(mean_data_1, alpha = 0.5)$rightEnd results[[\"mean_data_1\"]][[\"stepR\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"stepR\"]], c(300, 700, 1000), tolerance = 0.2) results[[\"mean_data_1\"]][[\"cpm\"]] <- cpm::processStream(mean_data_1, cpmType = \"Student\")$changePoints results[[\"mean_data_1\"]][[\"cpm\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"cpm\"]], c(299, 699), tolerance = 0.2) results[[\"mean_data_1\"]][[\"segmented\"]] <- segmented::stepmented( as.numeric(mean_data_1), npsi = 2 )$psi[, \"Est.\"] results[[\"mean_data_1\"]][[\"segmented\"]] testthat::expect_equal(results[[\"mean_data_1\"]][[\"segmented\"]], c(298, 699), ignore_attr = TRUE, tolerance = 0.2) results[[\"mean_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1), data = data.frame(y = mean_data_1, x = seq_len(nrow(mean_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"mcp\"]]) } results[[\"mean_data_1\"]][[\"not\"]] <- not::not(mean_data_1, contrast = \"pcwsConstMean\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"not\"]]) } results[[\"mean_data_1\"]][[\"bcp\"]] <- bcp::bcp(mean_data_1) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_1\"]][[\"bcp\"]]) } results[[\"mv_data_1\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_1, r.progress = FALSE)@cp_set results[[\"mv_data_1\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"fastcpd\"]], c(300, 700, 1001, 1300, 1700), tolerance = 0.2) results[[\"mv_data_1\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_1)$estimates results[[\"mv_data_1\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"ecp\"]], c(1, 301, 701, 1001, 1301, 1701, 2001), tolerance = 0.2) results[[\"mv_data_1\"]][[\"changepoint\"]] <- changepoint::cpt.meanvar(c(mv_data_1))@cpts results[[\"mv_data_1\"]][[\"changepoint\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"changepoint\"]], c(300, 2000), tolerance = 0.2) results[[\"mv_data_1\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mv_data_1, G = floor(length(mv_data_1) / 6))$cpts results[[\"mv_data_1\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"CptNonPar\"]], c(333, 700, 1300), tolerance = 0.2) results[[\"mv_data_1\"]][[\"cpm\"]] <- cpm::processStream(mv_data_1, cpmType = \"GLR\")$changePoints results[[\"mv_data_1\"]][[\"cpm\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"cpm\"]], c(293, 300, 403, 408, 618, 621, 696, 1000, 1021, 1024, 1293, 1300, 1417, 1693, 1700, 1981), tolerance = 0.2) results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_1), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_1), ncol(mv_data_1) ) )$changepoints[, \"location\"] results[[\"mv_data_1\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"InspectChangepoint\"]], c( 300, 700, 701, 702, 704, 707, 708, 712, 715, 716, 717, 718, 721, 722, 723, 726, 727, 729, 731, 732, 734, 736, 740, 742, 744, 746, 748, 750, 753, 755, 756, 757, 759, 760, 762, 764, 765, 766, 768, 769, 771, 772, 774, 776, 777, 784, 785, 786, 789, 791, 792, 794, 797, 798, 799, 801, 802, 803, 807, 809, 810, 813, 815, 817, 819, 826, 827, 828, 829, 831, 833, 835, 836, 837, 838, 840, 841, 842, 843, 845, 848, 849, 852, 854, 860, 862, 864, 866, 868, 870, 872, 875, 879, 881, 884, 886, 887, 888, 889, 896, 897, 898, 899, 901, 903, 904, 905, 906, 909, 910, 912, 913, 915, 917, 919, 921, 922, 923, 925, 927, 928, 932, 934, 936, 937, 940, 944, 945, 947, 948, 949, 951, 956, 958, 959, 961, 962, 963, 964, 966, 967, 968, 972, 974, 976, 978, 979, 986, 988, 990, 992, 995, 998, 1000, 1300, 1700, 1702, 1703, 1704, 1705, 1708, 1710, 1712, 1714, 1716, 1717, 1718, 1720, 1721, 1723, 1725, 1726, 1727, 1729, 1731, 1733, 1735, 1736, 1737, 1739, 1742, 1745, 1747, 1748, 1752, 1754, 1756, 1758, 1759, 1760, 1766, 1768, 1770, 1771, 1773, 1775, 1778, 1782, 1784, 1785, 1790, 1792, 1793, 1795, 1796, 1797, 1799, 1800, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1813, 1815, 1816, 1818, 1821, 1824, 1825, 1827, 1828, 1829, 1833, 1835, 1837, 1840, 1841, 1842, 1848, 1849, 1851, 1852, 1854, 1855, 1857, 1859, 1860, 1862, 1863, 1865, 1867, 1868, 1876, 1878, 1879, 1880, 1882, 1883, 1884, 1886, 1887, 1889, 1894, 1898, 1899, 1905, 1906, 1907, 1908, 1909, 1912, 1919, 1920, 1921, 1923, 1924, 1926, 1927, 1928, 1930, 1933, 1934, 1935, 1936, 1938, 1940, 1941, 1944, 1947, 1950, 1952, 1954, 1955, 1956, 1960, 1962, 1963, 1965, 1966, 1967, 1969, 1970, 1974, 1976, 1977, 1978, 1980, 1985, 1987, 1988, 1990, 1996, 1997, 1998 ), tolerance = 0.2) results[[\"mv_data_1\"]][[\"Rbeast\"]] <- Rbeast::beast( mv_data_1, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp results[[\"mv_data_1\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mv_data_1\"]][[\"Rbeast\"]], c(1855, 1794, 1301, 1986, 301, 703, 1981, 1769, 1860, 709), tolerance = 0.2) results[[\"mv_data_1\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1, ~ 1, ~ 1, ~ 1, ~ 1, ~ 1), data = data.frame(y = mv_data_1, x = seq_len(nrow(mv_data_1))), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"mcp\"]]) } results[[\"mv_data_1\"]][[\"not\"]] <- not::not(mv_data_1, contrast = \"pcwsConstMeanVar\") if (requireNamespace(\"not\", quietly = TRUE)) { plot(results[[\"mv_data_1\"]][[\"not\"]]) } results[[\"mean_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mean(mean_data_3, r.progress = FALSE)@cp_set results[[\"mean_data_3\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"fastcpd\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(mean_data_3, G = floor(nrow(mean_data_3) / 6))$cpts results[[\"mean_data_3\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"CptNonPar\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mean_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mean_data_3), ncol(mean_data_3) ) )$changepoints[, \"location\"] results[[\"mean_data_3\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"InspectChangepoint\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"jointseg\"]] <- jointseg::jointSeg(mean_data_3, K = 2)$bestBkp results[[\"mean_data_3\"]][[\"jointseg\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"jointseg\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mean_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mean_data_3\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"Rbeast\"]], matrix(c( 301, 701, 301, 701, 301, 701, 142, 117, 926, 305, NaN, NaN, 705, NaN, NaN, 694, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN ), nrow = 10, ncol = 3, byrow = TRUE), tolerance = 0.2) results[[\"mean_data_3\"]][[\"strucchange\"]] <- strucchange::breakpoints( cbind(y.1, y.2, y.3) ~ 1, data = data.frame(y = mean_data_3) )$breakpoints results[[\"mean_data_3\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"strucchange\"]], c(300, 700), tolerance = 0.2) results[[\"mean_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mean_data_3)$estimates results[[\"mean_data_3\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mean_data_3\"]][[\"ecp\"]], c(1, 301, 701, 1001), tolerance = 0.2) results[[\"mean_data_3\"]][[\"bcp\"]] <- bcp::bcp(mean_data_3) if (requireNamespace(\"bcp\", quietly = TRUE)) { plot(results[[\"mean_data_3\"]][[\"bcp\"]]) } results[[\"mv_data_3\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.mv(mv_data_3, r.progress = FALSE)@cp_set results[[\"mv_data_3\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"fastcpd\"]], c(300, 700, 1000, 1300, 1700), tolerance = 0.2) results[[\"mv_data_3\"]][[\"ecp\"]] <- ecp::e.divisive(mv_data_3)$estimates results[[\"mv_data_3\"]][[\"ecp\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"ecp\"]], c(1, 301, 701, 1001, 1301, 1701, 2001), tolerance = 0.2) results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] <- InspectChangepoint::inspect( t(mv_data_3), threshold = InspectChangepoint::compute.threshold( nrow(mv_data_3), ncol(mv_data_3) ) )$changepoints[, \"location\"] results[[\"mv_data_3\"]][[\"InspectChangepoint\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"InspectChangepoint\"]], c( 300, 700, 701, 703, 705, 707, 708, 709, 711, 712, 714, 715, 717, 718, 720, 721, 723, 724, 726, 727, 729, 731, 733, 734, 736, 737, 739, 740, 742, 743, 744, 746, 747, 749, 750, 752, 753, 754, 755, 756, 758, 760, 762, 763, 765, 766, 767, 769, 770, 772, 773, 774, 775, 777, 779, 780, 782, 784, 786, 788, 790, 791, 793, 795, 797, 799, 801, 803, 804, 806, 809, 810, 811, 813, 814, 816, 817, 818, 820, 821, 823, 825, 827, 828, 830, 831, 833, 835, 836, 837, 838, 840, 842, 843, 845, 846, 848, 849, 850, 852, 853, 854, 855, 856, 858, 859, 860, 862, 863, 865, 866, 868, 869, 871, 872, 874, 876, 877, 878, 879, 881, 883, 885, 887, 888, 889, 891, 893, 894, 895, 897, 898, 900, 901, 903, 904, 906, 908, 909, 911, 913, 914, 916, 917, 918, 920, 921, 923, 924, 925, 927, 928, 929, 931, 932, 934, 936, 937, 938, 939, 941, 942, 943, 945, 946, 947, 949, 950, 952, 954, 955, 956, 957, 958, 959, 961, 962, 964, 965, 967, 968, 970, 972, 973, 974, 975, 977, 979, 981, 982, 984, 985, 986, 987, 988, 990, 991, 992, 994, 995, 997, 999, 1000, 1300, 1700, 1702, 1703, 1704, 1705, 1706, 1708, 1709, 1710, 1712, 1713, 1714, 1715, 1717, 1719, 1721, 1722, 1723, 1725, 1727, 1729, 1730, 1732, 1734, 1735, 1737, 1738, 1739, 1741, 1742, 1744, 1746, 1748, 1750, 1752, 1753, 1754, 1755, 1757, 1758, 1759, 1761, 1762, 1763, 1764, 1766, 1767, 1769, 1770, 1771, 1773, 1774, 1775, 1777, 1779, 1781, 1782, 1783, 1785, 1786, 1788, 1789, 1791, 1793, 1794, 1796, 1798, 1800, 1803, 1804, 1805, 1806, 1808, 1809, 1811, 1812, 1814, 1815, 1817, 1818, 1819, 1821, 1822, 1824, 1825, 1827, 1828, 1829, 1831, 1833, 1835, 1836, 1838, 1839, 1841, 1843, 1844, 1846, 1847, 1848, 1850, 1851, 1853, 1854, 1856, 1857, 1858, 1859, 1860, 1862, 1863, 1864, 1865, 1867, 1869, 1870, 1872, 1873, 1874, 1876, 1878, 1879, 1881, 1882, 1884, 1885, 1887, 1889, 1891, 1893, 1894, 1896, 1898, 1899, 1900, 1901, 1902, 1904, 1906, 1907, 1909, 1911, 1913, 1914, 1916, 1917, 1918, 1919, 1921, 1923, 1924, 1925, 1927, 1928, 1930, 1932, 1933, 1935, 1936, 1938, 1939, 1941, 1942, 1944, 1946, 1948, 1950, 1951, 1952, 1954, 1956, 1957, 1959, 1961, 1963, 1965, 1967, 1968, 1970, 1972, 1973, 1974, 1976, 1977, 1979, 1981, 1982, 1984, 1985, 1987, 1989, 1990, 1992, 1993, 1995, 1996, 1998 ), tolerance = 0.2) results[[\"mv_data_3\"]][[\"Rbeast\"]] <- Rbeast::beast123( mv_data_3, metadata = list(whichDimIsTime = 1), season = \"none\" )$trend$cp results[[\"mv_data_3\"]][[\"Rbeast\"]] testthat::expect_equal(results[[\"mv_data_3\"]][[\"Rbeast\"]], matrix(c( 701, 1301, 301, 1301, 1301, 301, 1301, 710, 301, 701, 1829, 301, 1968, 1993, 702, 886, 1994, 884, 1822, 1975, 814, 755, 810, 1915, 1962, 781, 845, 778, 1978, 767, 1738, 1985, 1870, 747, 1754, 792, 1843, 722, 771, 953 ), nrow = 10, ncol = 4, byrow = TRUE), tolerance = 0.2) results[[\"lm_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lm(lm_data, r.progress = FALSE)@cp_set results[[\"lm_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"lm_data\"]][[\"fastcpd\"]], c(97, 201), tolerance = 0.2) results[[\"lm_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lm_data)$breakpoints results[[\"lm_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"lm_data\"]][[\"strucchange\"]], c(100, 201), tolerance = 0.2) results[[\"lm_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ . - 1, data.frame(y = lm_data$y, x = lm_data[, -1], index = seq_len(nrow(lm_data))) ), seg.Z = ~ index )$psi[, \"Est.\"] results[[\"lm_data\"]][[\"segmented\"]] testthat::expect_equal(results[[\"lm_data\"]][[\"segmented\"]], c(233), ignore_attr = TRUE, tolerance = 0.2) results[[\"binomial_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.binomial(binomial_data, r.progress = FALSE)@cp_set results[[\"binomial_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"binomial_data\"]][[\"fastcpd\"]], 302, tolerance = 0.2) results[[\"binomial_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = binomial_data)$breakpoints results[[\"binomial_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"binomial_data\"]][[\"strucchange\"]], 297, tolerance = 0.2) results[[\"poisson_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.poisson(poisson_data, r.progress = FALSE)@cp_set results[[\"poisson_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"poisson_data\"]][[\"fastcpd\"]], c(498, 805, 1003), tolerance = 0.2) results[[\"poisson_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = poisson_data)$breakpoints results[[\"poisson_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"poisson_data\"]][[\"strucchange\"]], 935, tolerance = 0.2) results[[\"lasso_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.lasso(lasso_data, r.progress = FALSE)@cp_set results[[\"lasso_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"lasso_data\"]][[\"fastcpd\"]], c(79, 199, 320), tolerance = 0.2) results[[\"lasso_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(y ~ . - 1, data = lasso_data)$breakpoints results[[\"lasso_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"lasso_data\"]][[\"strucchange\"]], c(80, 200, 321), tolerance = 0.2) results[[\"ar_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.ar(ar_data, 3, r.progress = FALSE)@cp_set results[[\"ar_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"ar_data\"]][[\"fastcpd\"]], c(614), tolerance = 0.2) results[[\"ar_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(ar_data, G = floor(length(ar_data) / 6))$cpts results[[\"ar_data\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"ar_data\"]][[\"CptNonPar\"]], numeric(0), tolerance = 0.2) results[[\"ar_data\"]][[\"segmented\"]] <- segmented::segmented( lm( y ~ x + 1, data.frame(y = ar_data, x = seq_along(ar_data)) ), seg.Z = ~ x )$psi[, \"Est.\"] results[[\"ar_data\"]][[\"segmented\"]] testthat::expect_equal(results[[\"ar_data\"]][[\"segmented\"]], c(690), ignore_attr = TRUE, tolerance = 0.2) results[[\"ar_data\"]][[\"mcp\"]] <- mcp::mcp( list(y ~ 1 + ar(3), ~ 0 + ar(3)), data = data.frame(y = ar_data, x = seq_along(ar_data)), par_x = \"x\" ) if (requireNamespace(\"mcp\", quietly = TRUE)) { plot(results[[\"ar_data\"]][[\"mcp\"]]) } results[[\"garch_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.garch(garch_data, c(1, 1), r.progress = FALSE)@cp_set results[[\"garch_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"garch_data\"]][[\"fastcpd\"]], c(205), tolerance = 0.2) results[[\"garch_data\"]][[\"CptNonPar\"]] <- CptNonPar::np.mojo(garch_data, G = floor(length(garch_data) / 6))$cpts results[[\"garch_data\"]][[\"CptNonPar\"]] testthat::expect_equal(results[[\"garch_data\"]][[\"CptNonPar\"]], c(206), tolerance = 0.2) results[[\"garch_data\"]][[\"strucchange\"]] <- strucchange::breakpoints(x ~ 1, data = data.frame(x = garch_data))$breakpoints results[[\"garch_data\"]][[\"strucchange\"]] testthat::expect_equal(results[[\"garch_data\"]][[\"strucchange\"]], NA, tolerance = 0.2) results[[\"var_data\"]][[\"fastcpd\"]] <- fastcpd::fastcpd.var(var_data, 2, r.progress = FALSE)@cp_set results[[\"var_data\"]][[\"fastcpd\"]] testthat::expect_equal(results[[\"var_data\"]][[\"fastcpd\"]], c(500), tolerance = 0.2) results[[\"var_data\"]][[\"VARDetect\"]] <- VARDetect::tbss(var_data)$cp results[[\"var_data\"]][[\"VARDetect\"]] testthat::expect_equal(results[[\"var_data\"]][[\"VARDetect\"]], c(501), tolerance = 0.2) well_log <- fastcpd::well_log well_log <- well_log[well_log > 1e5] results[[\"well_log\"]] <- list( fastcpd = fastcpd.mean(well_log, trim = 0.003)@cp_set, changepoint = changepoint::cpt.mean(well_log)@cpts, CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6))$cpts, strucchange = strucchange::breakpoints( y ~ 1, data = data.frame(y = well_log) )$breakpoints, ecp = ecp::e.divisive(matrix(well_log))$estimates, breakfast = breakfast::breakfast(well_log)$cptmodel.list[[6]]$cpts, wbs = wbs::wbs(well_log)$cpt$cpt.ic$mbic.penalty, mosum = mosum::mosum(c(well_log), G = 40)$cpts.info$cpts, # fpop = fpop::Fpop(well_log, length(well_log))$t.est, # meaningless gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" )$changepoints, InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) )$changepoints[, \"location\"], jointseg = jointseg::jointSeg(well_log, K = 12)$bestBkp, Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE )$trend$cp, stepR = stepR::stepFit(well_log, alpha = 0.5)$rightEnd ) results[[\"well_log\"]] package_list <- sort(names(results[[\"well_log\"]]), decreasing = TRUE) comparison_table <- NULL for (package_index in seq_along(package_list)) { package <- package_list[[package_index]] comparison_table <- rbind( comparison_table, data.frame( change_point = results[[\"well_log\"]][[package]], package = package, y_offset = (package_index - 1) * 1000 ) ) } most_selected <- sort(table(comparison_table$change_point), decreasing = TRUE) most_selected <- sort(as.numeric(names(most_selected[most_selected >= 4]))) for (i in seq_len(length(most_selected) - 1)) { if (most_selected[i + 1] - most_selected[i] < 2) { most_selected[i] <- NA most_selected[i + 1] <- most_selected[i + 1] - 0.5 } } (most_selected <- most_selected[!is.na(most_selected)]) if (requireNamespace(\"ggplot2\", quietly = TRUE)) { ggplot2::ggplot() + ggplot2::geom_point( data = data.frame(x = seq_along(well_log), y = c(well_log)), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_vline( xintercept = most_selected, color = \"black\", linetype = \"dashed\", alpha = 0.2 ) + ggplot2::geom_point( data = comparison_table, ggplot2::aes(x = change_point, y = 50000 + y_offset, color = package), shape = 17, size = 1.9 ) + ggplot2::geom_hline( data = comparison_table, ggplot2::aes(yintercept = 50000 + y_offset, color = package), linetype = \"dashed\", alpha = 0.1 ) + ggplot2::coord_cartesian( ylim = c(50000 - 500, max(well_log) + 1000), xlim = c(-200, length(well_log) + 200), expand = FALSE ) + ggplot2::theme( panel.background = ggplot2::element_blank(), panel.border = ggplot2::element_rect(colour = \"black\", fill = NA), panel.grid.major = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank() ) + ggplot2::xlab(NULL) + ggplot2::ylab(NULL) } results[[\"microbenchmark\"]] <- microbenchmark::microbenchmark( fastcpd = fastcpd::fastcpd.mean(well_log, trim = 0.003, r.progress = FALSE), changepoint = changepoint::cpt.mean(well_log, method = \"PELT\"), CptNonPar = CptNonPar::np.mojo(well_log, G = floor(length(well_log) / 6)), strucchange = strucchange::breakpoints(y ~ 1, data = data.frame(y = well_log)), ecp = ecp::e.divisive(matrix(well_log)), breakfast = breakfast::breakfast(well_log), wbs = wbs::wbs(well_log), mosum = mosum::mosum(c(well_log), G = 40), fpop = fpop::Fpop(well_log, nrow(well_log)), gfpop = gfpop::gfpop( data = well_log, mygraph = gfpop::graph( penalty = 2 * log(length(well_log)) * gfpop::sdDiff(well_log) ^ 2, type = \"updown\" ), type = \"mean\" ), InspectChangepoint = InspectChangepoint::inspect( well_log, threshold = InspectChangepoint::compute.threshold(length(well_log), 1) ), jointseg = jointseg::jointSeg(well_log, K = 12), Rbeast = Rbeast::beast( well_log, season = \"none\", print.progress = FALSE, quiet = TRUE ), stepR = stepR::stepFit(well_log, alpha = 0.5), not = not::not(well_log, contrast = \"pcwsConstMean\"), times = 10 ) results[[\"microbenchmark\"]] if (requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"microbenchmark\", quietly = TRUE)) { ggplot2::autoplot(results[[\"microbenchmark\"]]) } if (!file.exists(\"comparison-packages-results.RData\")) { save(results, file = \"comparison-packages-results.RData\") }"},{"path":[]},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"logistic-regression","dir":"Articles","previous_headings":"Setup","what":"Logistic regression","title":"Comparison with vanilla PELT","text":"","code":"#' Cost function for Logistic regression, i.e. binomial family in GLM. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_binomial <- function(data, family = \"binomial\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm( as.matrix(data[, 1:p]), data[, p + 1], family = family ) return(out$deviance / 2) } #' Implementation of vanilla PELT for logistic regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_binomial <- function(data, beta, cost = cost_glm_binomial) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) } cp <- cp_set[[n + 1]] nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_logistic_update <- function( data_new, coef, cum_coef, cmatrix, epsilon = 1e-10) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- 1 / (1 + exp(-eta)) cmatrix <- cmatrix + (X_new %o% X_new) * as.numeric((1 - mu) * mu) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_binomial <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + log(1 + exp(u)) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_binomial <- function(data, beta, B = 10, trim = 0.025) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm( as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = \"binomial\" ) coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) e_eta <- exp(coef %*% X1) const <- e_eta / (1 + e_eta)^2 cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_logistic_update(data[t, ], coef_c, cum_coef_c, cmatrix_c) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) { cval[i] <- neg_log_lik_binomial(data[k:t, ], cum_coef[, i] / (t - k + 1)) } } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t / (1 + e_eta_t)^2 cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (seq_len(length(cp)))[(cp < trim * n) | (cp > (1 - trim) * n)] cp <- cp[-ind3] } nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"poisson-regression","dir":"Articles","previous_headings":"Setup","what":"Poisson regression","title":"Comparison with vanilla PELT","text":"","code":"#' Cost function for Poisson regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_poisson <- function(data, family = \"poisson\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm(as.matrix(data[, 1:p]), data[, p + 1], family = family) return(out$deviance / 2) } #' Implementation of vanilla PELT for poisson regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_poisson <- function(data, beta, cost = cost_glm_poisson) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) # if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_poisson_update <- function(data_new, coef, cum_coef, cmatrix, epsilon = 0.001, G = 10^10, L = -20, H = 20) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- exp(eta) cmatrix <- cmatrix + (X_new %o% X_new) * min(as.numeric(mu), G) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) coef <- pmin(pmax(coef, L), H) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_poisson <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + exp(u) + lfactorial(Y) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_poisson <- function(data, beta, B = 10, trim = 0.03, epsilon = 0.001, G = 10^10, L = -20, H = 20) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm(x = as.matrix(data[index == i, 1:p]), y = data[index == i, p + 1], family = \"poisson\") coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- pmin(pmax(matrix(coef.int[1, ], p, 1), L), H) e_eta <- exp(coef %*% X1) const <- e_eta cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_poisson_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, epsilon = epsilon, G = G, L = L, H = H) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cum_coef_win <- pmin(pmax(cum_coef[, i] / (t - k + 1), L), H) if (t - k >= p - 1) cval[i] <- neg_log_lik_poisson(data[k:t, ], cum_coef_win) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- pmin(pmax(coef.int[index[t], ], L), H) e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"Poisson\") # nLL <- out$deviance/2 + nLL # } # output <- list(cp, nLL) # names(output) <- c(\"cp\", \"nLL\") output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from poisson regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_poisson <- function(n, d, true.coef, true.cp.loc, Sigma) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { mu <- exp(x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE]) group <- rpois(length(mu), mu) y <- c(y, group) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"penalized-linear-regression","dir":"Articles","previous_headings":"Setup","what":"Penalized linear regression","title":"Comparison with vanilla PELT","text":"","code":"#' Cost function for penalized linear regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param lambda Penalty coefficient. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_lasso <- function(data, lambda, family = \"gaussian\") { data <- as.matrix(data) n <- dim(data)[1] p <- dim(data)[2] - 1 out <- glmnet::glmnet(as.matrix(data[, 1:p]), data[, p + 1], family = family, lambda = lambda) return(deviance(out) / 2) } #' Implementation of vanilla PELT for penalized linear regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_lasso <- function(data, beta, B = 10, cost = cost_lasso, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 index <- rep(1:B, rep(n / B, B)) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= 1) cval[i] <- suppressWarnings(cost(data[k:t, ], lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1)))) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- glmnet(as.matrix(data_seg[, 1:p]), data_seg[, p+1], lambda=lambda, family=family) # nLL <- deviance(out)/2 + nLL # } # output <- list(cp, nLL) output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' @param a Coefficient to be updated. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Updated coefficient. soft_threshold <- function(a, lambda) { sign(a) * pmax(abs(a) - lambda, 0) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_lasso_update <- function(data_new, coef, cum_coef, cmatrix, lambda) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] mu <- X_new %*% coef cmatrix <- cmatrix + X_new %o% X_new # B <- as.vector(cmatrix_inv%*%X_new) # cmatrix_inv <- cmatrix_inv - B%o%B/(1+sum(X_new*B)) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix, lik_dev) nc <- norm(cmatrix, type = \"F\") # the choice of norm affects the speed. Spectral norm is more accurate but slower than F norm. coef <- soft_threshold(coef, lambda / nc) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_lasso <- function(data, b, lambda) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] resi <- Y - X %*% b L <- sum(resi^2) / 2 + lambda * sum(abs(b)) return(L) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_lasso <- function(data, beta, B = 10, trim = 0.025, epsilon = 1e-5, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef.int[i, ] <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef.int[i, ]) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef.int[i, ]) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) eta <- coef %*% X1 # c_int <- diag(1/epsilon,p) - X1%o%X1/epsilon^2/(1+sum(X1^2)/epsilon) # cmatrix_inv <- array(c_int, c(p,p,1)) cmatrix <- array(X1 %o% X1 + epsilon * diag(1, p), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] # cmatrix_inv_c <- cmatrix_inv[,,i] cmatrix_c <- cmatrix[, , i] k <- set[i] + 1 out <- cost_lasso_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] # cmatrix_inv[,,i] <- out[[3]] cmatrix[, , i] <- out[[3]] if (t - k >= 2) cval[i] <- neg_log_lik_lasso(data[k:t, ], cum_coef[, i] / (t - k + 1), lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] # cmatrix_inv_add <- diag(1/epsilon,p) - Xt%o%Xt/epsilon^2/(1+sum(Xt^2)/epsilon) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) # cmatrix_inv <- abind::abind(cmatrix_inv, cmatrix_inv_add, along=3) cmatrix <- abind::abind(cmatrix, Xt %o% Xt + epsilon * diag(1, p), along = 3) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] # cmatrix_inv <- cmatrix_inv[,,ind2,drop=FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries and merge change-points cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"binomial\") # nLL <- out$deviance/2 + nLL # } output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from penalized linear regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @param evar Error variance. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_lasso <- function(n, d, true.coef, true.cp.loc, Sigma, evar) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { Xb <- x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE] add <- Xb + rnorm(length(Xb), sd = sqrt(evar)) y <- c(y, add) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) }"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"logistic-regression-1","dir":"Articles","previous_headings":"","what":"Logistic regression","title":"Comparison with vanilla PELT","text":"","code":"set.seed(1) p <- 5 x <- matrix(rnorm(300 * p, 0, 1), ncol = p) # Randomly generate coefficients with different means. theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) # Randomly generate response variables based on the segmented data and # corresponding coefficients y <- c( rbinom(125, 1, 1 / (1 + exp(-x[1:125, ] %*% theta[1, ]))), rbinom(300 - 125, 1, 1 / (1 + exp(-x[(125 + 1):300, ] %*% theta[2, ]))) ) segd_binomial(cbind(x, y), (p + 1) * log(300) / 2, B = 5)$cp #> [1] 125 fastcpd.binomial( cbind(y, x), segment_count = 5, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125 pelt_vanilla_binomial(cbind(x, y), (p + 1) * log(300) / 2)$cp #> [1] 0 125 fastcpd.binomial( cbind(y, x), segment_count = 5, vanilla_percentage = 1, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"poisson-regression-1","dir":"Articles","previous_headings":"","what":"Poisson regression","title":"Comparison with vanilla PELT","text":"","code":"set.seed(1) n <- 1500 d <- 5 rho <- 0.9 Sigma <- array(0, c(d, d)) for (i in 1:d) { Sigma[i, ] <- rho^(abs(i - (1:d))) } delta <- c(5, 7, 9, 11, 13) a.sq <- 1 delta.new <- delta * sqrt(a.sq) / sqrt(as.numeric(t(delta) %*% Sigma %*% delta)) true.cp.loc <- c(375, 750, 1125) # regression coefficients true.coef <- matrix(0, nrow = d, ncol = length(true.cp.loc) + 1) true.coef[, 1] <- c(1, 1.2, -1, 0.5, -2) true.coef[, 2] <- true.coef[, 1] + delta.new true.coef[, 3] <- true.coef[, 1] true.coef[, 4] <- true.coef[, 3] - delta.new out <- data_gen_poisson(n, d, true.coef, true.cp.loc, Sigma) data <- out[[1]] g_tr <- out[[2]] beta <- log(n) * (d + 1) / 2 segd_poisson( data, beta, trim = 0.03, B = 10, epsilon = 0.001, G = 10^10, L = -20, H = 20 )$cp #> [1] 380 751 1136 1251 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), beta = beta, cost_adjustment = \"BIC\", epsilon = 0.001, segment_count = 10, r.progress = FALSE )@cp_set #> [1] 380 751 1136 1251 pelt_vanilla_poisson(data, beta)$cp #> [1] 0 374 752 1133 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), segment_count = 10, vanilla_percentage = 1, beta = beta, cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 374 752 1133"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"penalized-linear-regression-1","dir":"Articles","previous_headings":"","what":"Penalized linear regression","title":"Comparison with vanilla PELT","text":"","code":"set.seed(1) n <- 1000 s <- 3 d <- 50 evar <- 0.5 Sigma <- diag(1, d) true.cp.loc <- c(100, 300, 500, 800, 900) seg <- length(true.cp.loc) + 1 true.coef <- matrix(rnorm(seg * s), s, seg) true.coef <- rbind(true.coef, matrix(0, d - s, seg)) out <- data_gen_lasso(n, d, true.coef, true.cp.loc, Sigma, evar) data <- out[[1]] beta <- log(n) / 2 # beta here has different meaning segd_lasso(data, beta, B = 10, trim = 0.025)$cp #> [1] 100 300 520 800 901 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 100 300 520 800 901 pelt_vanilla_lasso(data, beta, cost = cost_lasso)$cp #> [1] 100 #> [1] 200 #> [1] 300 #> [1] 400 #> [1] 500 #> [1] 600 #> [1] 700 #> [1] 800 #> [1] 900 #> [1] 1000 #> [1] 0 103 299 510 800 900 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), vanilla_percentage = 1, epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 103 299 510 800 900"},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Comparison with vanilla PELT","text":"evaluation vignette set FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/articles/comparison-pelt.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Comparison with vanilla PELT","text":"","code":"knitr::opts_chunk$set( collapse = TRUE, comment = \"#>\", eval = FALSE, cache = FALSE, warning = FALSE, fig.width = 8, fig.height = 5 ) library(fastcpd) #' Cost function for Logistic regression, i.e. binomial family in GLM. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_binomial <- function(data, family = \"binomial\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm( as.matrix(data[, 1:p]), data[, p + 1], family = family ) return(out$deviance / 2) } #' Implementation of vanilla PELT for logistic regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_binomial <- function(data, beta, cost = cost_glm_binomial) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) } cp <- cp_set[[n + 1]] nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_logistic_update <- function( data_new, coef, cum_coef, cmatrix, epsilon = 1e-10) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- 1 / (1 + exp(-eta)) cmatrix <- cmatrix + (X_new %o% X_new) * as.numeric((1 - mu) * mu) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_binomial <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + log(1 + exp(u)) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_binomial <- function(data, beta, B = 10, trim = 0.025) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm( as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = \"binomial\" ) coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) e_eta <- exp(coef %*% X1) const <- e_eta / (1 + e_eta)^2 cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_logistic_update(data[t, ], coef_c, cum_coef_c, cmatrix_c) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cval[i] <- 0 if (t - k >= p - 1) { cval[i] <- neg_log_lik_binomial(data[k:t, ], cum_coef[, i] / (t - k + 1)) } } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t / (1 + e_eta_t)^2 cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (seq_len(length(cp)))[(cp < trim * n) | (cp > (1 - trim) * n)] cp <- cp[-ind3] } nLL <- 0 cp_loc <- unique(c(0, cp, n)) for (i in 1:(length(cp_loc) - 1)) { seg <- (cp_loc[i] + 1):cp_loc[i + 1] data_seg <- data[seg, ] out <- fastglm::fastglm( as.matrix(data_seg[, 1:p]), data_seg[, p + 1], family = \"binomial\" ) nLL <- out$deviance / 2 + nLL } output <- list(cp, nLL) names(output) <- c(\"cp\", \"nLL\") return(output) } #' Cost function for Poisson regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_glm_poisson <- function(data, family = \"poisson\") { data <- as.matrix(data) p <- dim(data)[2] - 1 out <- fastglm::fastglm(as.matrix(data[, 1:p]), data[, p + 1], family = family) return(out$deviance / 2) } #' Implementation of vanilla PELT for poisson regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_poisson <- function(data, beta, cost = cost_glm_poisson) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= p - 1) cval[i] <- suppressWarnings(cost(data[k:t, ])) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) # if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_poisson_update <- function(data_new, coef, cum_coef, cmatrix, epsilon = 0.001, G = 10^10, L = -20, H = 20) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] eta <- X_new %*% coef mu <- exp(eta) cmatrix <- cmatrix + (X_new %o% X_new) * min(as.numeric(mu), G) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix + epsilon * diag(1, p), lik_dev) coef <- pmin(pmax(coef, L), H) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_poisson <- function(data, b) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] u <- as.numeric(X %*% b) L <- -Y * u + exp(u) + lfactorial(Y) return(sum(L)) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param G Upper bound for the coefficient. #' @param L Winsorization lower bound. #' @param H Winsorization upper bound. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_poisson <- function(data, beta, B = 10, trim = 0.03, epsilon = 0.001, G = 10^10, L = -20, H = 20) { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) for (i in 1:B) { out <- fastglm::fastglm(x = as.matrix(data[index == i, 1:p]), y = data[index == i, p + 1], family = \"poisson\") coef.int[i, ] <- coef(out) } X1 <- data[1, 1:p] cum_coef <- coef <- pmin(pmax(matrix(coef.int[1, ], p, 1), L), H) e_eta <- exp(coef %*% X1) const <- e_eta cmatrix <- array((X1 %o% X1) * as.numeric(const), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] cmatrix_c <- cmatrix[, , i] out <- cost_poisson_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, epsilon = epsilon, G = G, L = L, H = H) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] cmatrix[, , i] <- out[[3]] k <- set[i] + 1 cum_coef_win <- pmin(pmax(cum_coef[, i] / (t - k + 1), L), H) if (t - k >= p - 1) cval[i] <- neg_log_lik_poisson(data[k:t, ], cum_coef_win) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- pmin(pmax(coef.int[index[t], ], L), H) e_eta_t <- exp(coef_add %*% Xt) const <- e_eta_t cmatrix_add <- (Xt %o% Xt) * as.numeric(const) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) cmatrix <- abind::abind(cmatrix, cmatrix_add, along = 3) # Adding a momentum term (TBD) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"Poisson\") # nLL <- out$deviance/2 + nLL # } # output <- list(cp, nLL) # names(output) <- c(\"cp\", \"nLL\") output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from poisson regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_poisson <- function(n, d, true.coef, true.cp.loc, Sigma) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { mu <- exp(x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE]) group <- rpois(length(mu), mu) y <- c(y, group) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) } #' Cost function for penalized linear regression. #' #' @param data Data to be used to calculate the cost values. The last column is #' the response variable. #' @param lambda Penalty coefficient. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return Cost value for the corresponding segment of data. cost_lasso <- function(data, lambda, family = \"gaussian\") { data <- as.matrix(data) n <- dim(data)[1] p <- dim(data)[2] - 1 out <- glmnet::glmnet(as.matrix(data[, 1:p]), data[, p + 1], family = family, lambda = lambda) return(deviance(out) / 2) } #' Implementation of vanilla PELT for penalized linear regression type data. #' #' @param data Data to be used for change point detection. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param cost Cost function to be used to calculate cost values. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list consisting of two: change point locations and negative log #' likelihood values for each segment. pelt_vanilla_lasso <- function(data, beta, B = 10, cost = cost_lasso, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 index <- rep(1:B, rep(n / B, B)) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:m) { k <- set[i] + 1 if (t - k >= 1) cval[i] <- suppressWarnings(cost(data[k:t, ], lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1)))) else cval[i] <- 0 } obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) Fobj <- c(Fobj, min_val) if (t %% 100 == 0) print(t) } cp <- cp_set[[n + 1]] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- glmnet(as.matrix(data_seg[, 1:p]), data_seg[, p+1], lambda=lambda, family=family) # nLL <- deviance(out)/2 + nLL # } # output <- list(cp, nLL) output <- list(cp) names(output) <- c(\"cp\") return(output) } #' Function to update the coefficients using gradient descent. #' @param a Coefficient to be updated. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Updated coefficient. soft_threshold <- function(a, lambda) { sign(a) * pmax(abs(a) - lambda, 0) } #' Function to update the coefficients using gradient descent. #' #' @param data_new New data point used to update the coeffient. #' @param coef Previous coeffient to be updated. #' @param cum_coef Summation of all the past coefficients to be used in #' averaging. #' @param cmatrix Hessian matrix in gradient descent. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return A list of values containing the new coefficients, summation of #' coefficients so far and all the Hessian matrices. cost_lasso_update <- function(data_new, coef, cum_coef, cmatrix, lambda) { p <- length(data_new) - 1 X_new <- data_new[1:p] Y_new <- data_new[p + 1] mu <- X_new %*% coef cmatrix <- cmatrix + X_new %o% X_new # B <- as.vector(cmatrix_inv%*%X_new) # cmatrix_inv <- cmatrix_inv - B%o%B/(1+sum(X_new*B)) lik_dev <- as.numeric(-(Y_new - mu)) * X_new coef <- coef - solve(cmatrix, lik_dev) nc <- norm(cmatrix, type = \"F\") # the choice of norm affects the speed. Spectral norm is more accurate but slower than F norm. coef <- soft_threshold(coef, lambda / nc) cum_coef <- cum_coef + coef return(list(coef, cum_coef, cmatrix)) } #' Calculate negative log likelihood given data segment and guess of #' coefficient. #' #' @param data Data to be used to calculate the negative log likelihood. #' @param b Guess of the coefficient. #' @param lambda Penalty coefficient. #' @keywords internal #' #' @noRd #' @return Negative log likelihood. neg_log_lik_lasso <- function(data, b, lambda) { p <- dim(data)[2] - 1 X <- data[, 1:p, drop = FALSE] Y <- data[, p + 1, drop = FALSE] resi <- Y - X %*% b L <- sum(resi^2) / 2 + lambda * sum(abs(b)) return(L) } #' Find change points using dynamic programming with pruning and SeGD. #' #' @param data Data used to find change points. #' @param beta Penalty coefficient for the number of change points. #' @param B Initial guess on the number of change points. #' @param trim Propotion of the data to ignore the change points at the #' beginning, ending and between change points. #' @param epsilon Small adjustment to avoid singularity when doing inverse on #' the Hessian matrix. #' @param family Family of the distribution. #' @keywords internal #' #' @noRd #' @return A list containing potential change point locations and negative log #' likelihood for each segment based on the change points guess. segd_lasso <- function(data, beta, B = 10, trim = 0.025, epsilon = 1e-5, family = \"gaussian\") { n <- dim(data)[1] p <- dim(data)[2] - 1 Fobj <- c(-beta, 0) cp_set <- list(NULL, 0) set <- c(0, 1) # choose the initial values based on pre-segmentation index <- rep(1:B, rep(n / B, B)) coef.int <- matrix(NA, B, p) err_sd <- act_num <- rep(NA, B) for (i in 1:B) { cvfit <- glmnet::cv.glmnet(as.matrix(data[index == i, 1:p]), data[index == i, p + 1], family = family) coef.int[i, ] <- coef(cvfit, s = \"lambda.1se\")[-1] resi <- data[index == i, p + 1] - as.matrix(data[index == i, 1:p]) %*% as.numeric(coef.int[i, ]) err_sd[i] <- sqrt(mean(resi^2)) act_num[i] <- sum(abs(coef.int[i, ]) > 0) } err_sd_mean <- mean(err_sd) # only works if error sd is unchanged. act_num_mean <- mean(act_num) beta <- (act_num_mean + 1) * beta # seems to work but there might be better choices X1 <- data[1, 1:p] cum_coef <- coef <- matrix(coef.int[1, ], p, 1) eta <- coef %*% X1 # c_int <- diag(1/epsilon,p) - X1%o%X1/epsilon^2/(1+sum(X1^2)/epsilon) # cmatrix_inv <- array(c_int, c(p,p,1)) cmatrix <- array(X1 %o% X1 + epsilon * diag(1, p), c(p, p, 1)) for (t in 2:n) { m <- length(set) cval <- rep(NA, m) for (i in 1:(m - 1)) { coef_c <- coef[, i] cum_coef_c <- cum_coef[, i] # cmatrix_inv_c <- cmatrix_inv[,,i] cmatrix_c <- cmatrix[, , i] k <- set[i] + 1 out <- cost_lasso_update(data[t, ], coef_c, cum_coef_c, cmatrix_c, lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) coef[, i] <- out[[1]] cum_coef[, i] <- out[[2]] # cmatrix_inv[,,i] <- out[[3]] cmatrix[, , i] <- out[[3]] if (t - k >= 2) cval[i] <- neg_log_lik_lasso(data[k:t, ], cum_coef[, i] / (t - k + 1), lambda = err_sd_mean * sqrt(2 * log(p) / (t - k + 1))) else cval[i] <- 0 } # the choice of initial values requires further investigation cval[m] <- 0 Xt <- data[t, 1:p] cum_coef_add <- coef_add <- coef.int[index[t], ] # cmatrix_inv_add <- diag(1/epsilon,p) - Xt%o%Xt/epsilon^2/(1+sum(Xt^2)/epsilon) coef <- cbind(coef, coef_add) cum_coef <- cbind(cum_coef, cum_coef_add) # cmatrix_inv <- abind::abind(cmatrix_inv, cmatrix_inv_add, along=3) cmatrix <- abind::abind(cmatrix, Xt %o% Xt + epsilon * diag(1, p), along = 3) obj <- cval + Fobj[set + 1] + beta min_val <- min(obj) ind <- which(obj == min_val)[1] cp_set_add <- c(cp_set[[set[ind] + 1]], set[ind]) cp_set <- append(cp_set, list(cp_set_add)) ind2 <- (cval + Fobj[set + 1]) <= min_val set <- c(set[ind2], t) coef <- coef[, ind2, drop = FALSE] cum_coef <- cum_coef[, ind2, drop = FALSE] cmatrix <- cmatrix[, , ind2, drop = FALSE] # cmatrix_inv <- cmatrix_inv[,,ind2,drop=FALSE] Fobj <- c(Fobj, min_val) } # Remove change-points close to the boundaries and merge change-points cp <- cp_set[[n + 1]] if (length(cp) > 0) { ind3 <- (1:length(cp))[(cp < trim * n) | (cp > (1 - trim) * n)] if (length(ind3) > 0) cp <- cp[-ind3] } cp <- sort(unique(c(0, cp))) index <- which((diff(cp) < trim * n) == TRUE) if (length(index) > 0) cp <- floor((cp[-(index + 1)] + cp[-index]) / 2) cp <- cp[cp > 0] # nLL <- 0 # cp_loc <- unique(c(0,cp,n)) # for(i in 1:(length(cp_loc)-1)) # { # seg <- (cp_loc[i]+1):cp_loc[i+1] # data_seg <- data[seg,] # out <- fastglm(as.matrix(data_seg[, 1:p]), data_seg[, p+1], family=\"binomial\") # nLL <- out$deviance/2 + nLL # } output <- list(cp) names(output) <- c(\"cp\") return(output) } # Generate data from penalized linear regression models with change-points #' @param n Number of observations. #' @param d Dimension of the covariates. #' @param true.coef True regression coefficients. #' @param true.cp.loc True change-point locations. #' @param Sigma Covariance matrix of the covariates. #' @param evar Error variance. #' @keywords internal #' #' @noRd #' @return A list containing the generated data and the true cluster #' assignments. data_gen_lasso <- function(n, d, true.coef, true.cp.loc, Sigma, evar) { loc <- unique(c(0, true.cp.loc, n)) if (dim(true.coef)[2] != length(loc) - 1) stop(\"true.coef and true.cp.loc do not match\") x <- mvtnorm::rmvnorm(n, mean = rep(0, d), sigma = Sigma) y <- NULL for (i in 1:(length(loc) - 1)) { Xb <- x[(loc[i] + 1):loc[i + 1], , drop = FALSE] %*% true.coef[, i, drop = FALSE] add <- Xb + rnorm(length(Xb), sd = sqrt(evar)) y <- c(y, add) } data <- cbind(x, y) true_cluster <- rep(1:(length(loc) - 1), diff(loc)) result <- list(data, true_cluster) return(result) } set.seed(1) p <- 5 x <- matrix(rnorm(300 * p, 0, 1), ncol = p) # Randomly generate coefficients with different means. theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) # Randomly generate response variables based on the segmented data and # corresponding coefficients y <- c( rbinom(125, 1, 1 / (1 + exp(-x[1:125, ] %*% theta[1, ]))), rbinom(300 - 125, 1, 1 / (1 + exp(-x[(125 + 1):300, ] %*% theta[2, ]))) ) segd_binomial(cbind(x, y), (p + 1) * log(300) / 2, B = 5)$cp #> [1] 125 fastcpd.binomial( cbind(y, x), segment_count = 5, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125 pelt_vanilla_binomial(cbind(x, y), (p + 1) * log(300) / 2)$cp #> [1] 0 125 fastcpd.binomial( cbind(y, x), segment_count = 5, vanilla_percentage = 1, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 125 set.seed(1) n <- 1500 d <- 5 rho <- 0.9 Sigma <- array(0, c(d, d)) for (i in 1:d) { Sigma[i, ] <- rho^(abs(i - (1:d))) } delta <- c(5, 7, 9, 11, 13) a.sq <- 1 delta.new <- delta * sqrt(a.sq) / sqrt(as.numeric(t(delta) %*% Sigma %*% delta)) true.cp.loc <- c(375, 750, 1125) # regression coefficients true.coef <- matrix(0, nrow = d, ncol = length(true.cp.loc) + 1) true.coef[, 1] <- c(1, 1.2, -1, 0.5, -2) true.coef[, 2] <- true.coef[, 1] + delta.new true.coef[, 3] <- true.coef[, 1] true.coef[, 4] <- true.coef[, 3] - delta.new out <- data_gen_poisson(n, d, true.coef, true.cp.loc, Sigma) data <- out[[1]] g_tr <- out[[2]] beta <- log(n) * (d + 1) / 2 segd_poisson( data, beta, trim = 0.03, B = 10, epsilon = 0.001, G = 10^10, L = -20, H = 20 )$cp #> [1] 380 751 1136 1251 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), beta = beta, cost_adjustment = \"BIC\", epsilon = 0.001, segment_count = 10, r.progress = FALSE )@cp_set #> [1] 380 751 1136 1251 pelt_vanilla_poisson(data, beta)$cp #> [1] 0 374 752 1133 fastcpd.poisson( cbind(data[, d + 1], data[, 1:d]), segment_count = 10, vanilla_percentage = 1, beta = beta, cost_adjustment = \"BIC\", r.progress = FALSE )@cp_set #> [1] 374 752 1133 set.seed(1) n <- 1000 s <- 3 d <- 50 evar <- 0.5 Sigma <- diag(1, d) true.cp.loc <- c(100, 300, 500, 800, 900) seg <- length(true.cp.loc) + 1 true.coef <- matrix(rnorm(seg * s), s, seg) true.coef <- rbind(true.coef, matrix(0, d - s, seg)) out <- data_gen_lasso(n, d, true.coef, true.cp.loc, Sigma, evar) data <- out[[1]] beta <- log(n) / 2 # beta here has different meaning segd_lasso(data, beta, B = 10, trim = 0.025)$cp #> [1] 100 300 520 800 901 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 100 300 520 800 901 pelt_vanilla_lasso(data, beta, cost = cost_lasso)$cp #> [1] 100 #> [1] 200 #> [1] 300 #> [1] 400 #> [1] 500 #> [1] 600 #> [1] 700 #> [1] 800 #> [1] 900 #> [1] 1000 #> [1] 0 103 299 510 800 900 fastcpd.lasso( cbind(data[, d + 1], data[, 1:d]), vanilla_percentage = 1, epsilon = 1e-5, beta = beta, cost_adjustment = \"BIC\", pruning_coef = 0, r.progress = FALSE )@cp_set #> [1] 103 299 510 800 900"},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"multiple-epochs","dir":"Articles","previous_headings":"","what":"Multiple epochs","title":"Advanced examples","text":"","code":"result_multiple_epochs <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < 20) 1 else 0, r.progress = FALSE ) summary(result_multiple_epochs) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < #> 20) 1 else 0, r.progress = FALSE) #> #> Change points: #> 79 199 320 #> #> Cost values: #> 125.1501 155.4486 164.1284 177.9478 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.898713954 -3.006202485 #> [2,] -2.668688731 -2.473539196 4.209460633 3.096081968 #> [3,] -4.354248176 0.983488542 5.011651466 3.718141515 #> [4,] -4.662566697 0.279505605 2.623742216 -3.130632564 #> [5,] -4.820139326 0.496565230 4.616444609 3.175562645 #> [6,] -3.860308911 -2.576467859 3.336301862 4.342890940 #> [7,] 0.117234534 -0.219145186 -0.039861426 -0.016395368 #> [8,] 0.117552281 0.035701649 -0.075921029 -0.022022294 #> [9,] -0.188756652 0.133653604 0.052907904 0.070683538 #> [10,] -0.074530965 0.010173561 0.003352400 0.012791767 #> [11,] -0.061721013 -0.176769170 -0.044814526 -0.166042077 #> [12,] 0.021874954 -0.133777712 0.103526247 0.077820866 #> [13,] 0.067777695 0.129187849 -0.072098209 0.077436979 #> [14,] -0.122270920 0.110865328 0.095306898 0.114046507 #> [15,] -0.014192027 0.086100909 0.006037145 -0.015966855 #> [16,] -0.135970691 -0.043211895 0.026749332 -0.095751983 #> [17,] 0.007608049 -0.079445537 -0.003130812 -0.010689927 #> [18,] -0.010343806 0.132392790 -0.156866781 -0.138057246 #> [19,] 0.252797503 0.253113165 -0.167436237 0.158967697 #> [20,] -0.044505498 -0.049962311 0.177560915 0.030169316 #> [21,] 0.084292730 0.366833682 -0.224849869 -0.181503446 #> [22,] -0.373913824 -0.060767726 0.003860114 0.045649997 #> [23,] 0.316816197 -0.191114879 -0.118941359 -0.025305755 #> [24,] 0.008174405 0.158066938 -0.135680402 -0.137396624 #> [25,] -0.388590727 0.098542973 -0.064383316 0.022336212 #> [26,] -0.109916673 0.042028223 0.017815134 -0.059782090 #> [27,] -0.040845604 0.023057708 0.053296378 0.030431412 #> [28,] 0.080696347 -0.097192796 0.122665492 -0.080706668 #> [29,] -0.117530646 0.002589360 0.062791240 -0.051542901 #> [30,] -0.037935138 -0.091390269 0.122976438 0.048892128 #> [31,] 0.109674339 0.158620750 -0.181785506 0.075434045 #> [32,] 0.069971569 -0.425139230 0.030472706 0.033375101 #> [33,] 0.050674415 0.050518682 0.034305534 0.070983107 #> [34,] 0.065490413 0.142178491 0.164899160 0.032575571 #> [35,] -0.045676268 -0.057128729 0.022794426 -0.134534869 #> [36,] 0.368217440 0.096573493 0.114099285 -0.020673038 #> [37,] 0.037286542 -0.056101952 -0.023029273 0.102043216 #> [38,] 0.094379823 -0.263914331 0.003288922 -0.052763017 #> [39,] 0.153890231 0.071449851 -0.097509620 -0.120642848 #> [40,] -0.146742186 -0.117294226 -0.208865036 -0.045775725 #> [41,] -0.274840831 0.009051742 0.127312770 0.123910218 #> [42,] -0.008508998 0.110465493 0.118453225 -0.005505113 #> [43,] 0.014213371 -0.041499271 0.113699538 -0.056131976 #> [44,] -0.080890432 0.094198355 0.051912519 0.013524984 #> [45,] 0.051925330 -0.007981636 0.085966205 -0.033628617 #> [46,] 0.213031014 0.168676929 0.035738422 -0.029655833 #> [47,] 0.175716867 0.045500673 0.175504506 0.119507574 #> [48,] -0.040155598 -0.120432098 -0.139162874 -0.054349982 #> [49,] 0.164103981 0.024976184 0.029374949 -0.056092814 #> [50,] -0.103164763 -0.135032022 0.047475507 -0.032491294"},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"vanilla-percentage","dir":"Articles","previous_headings":"","what":"Vanilla percentage","title":"Advanced examples","text":"","code":"result_vanilla_percentage <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE ) summary(result_vanilla_percentage) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE) #> #> Change points: #> 79 199 317 #> #> Cost values: #> 125.1501 155.4486 160.4363 274.2228 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.900559900 -2.749985232 #> [2,] -2.668688731 -2.473539196 4.198921150 3.057387294 #> [3,] -4.354248176 0.983488542 5.054325016 3.735347545 #> [4,] -4.662566697 0.279505605 2.628354481 -3.216027015 #> [5,] -4.820139326 0.496565230 4.611603593 3.231304715 #> [6,] -3.860308911 -2.576467859 3.304903523 4.577405578 #> [7,] 0.117234534 -0.219145186 -0.015091394 -0.086048806 #> [8,] 0.117552281 0.035701649 -0.127986984 -0.138807479 #> [9,] -0.188756652 0.133653604 0.038250526 -0.037807789 #> [10,] -0.074530965 0.010173561 0.021979969 -0.047707753 #> [11,] -0.061721013 -0.176769170 -0.059312332 -0.135633390 #> [12,] 0.021874954 -0.133777712 0.028889403 -0.130808437 #> [13,] 0.067777695 0.129187849 -0.026100120 0.002593292 #> [14,] -0.122270920 0.110865328 0.084858637 0.233903944 #> [15,] -0.014192027 0.086100909 0.030268607 0.243927135 #> [16,] -0.135970691 -0.043211895 -0.032229963 -0.134840827 #> [17,] 0.007608049 -0.079445537 -0.013675459 0.102010778 #> [18,] -0.010343806 0.132392790 -0.151237533 -0.123955960 #> [19,] 0.252797503 0.253113165 -0.197620792 0.099319824 #> [20,] -0.044505498 -0.049962311 0.233024977 0.176211754 #> [21,] 0.084292730 0.366833682 -0.220176991 -0.035984666 #> [22,] -0.373913824 -0.060767726 0.036430343 -0.105248408 #> [23,] 0.316816197 -0.191114879 -0.127549606 -0.112775343 #> [24,] 0.008174405 0.158066938 -0.121000263 0.232613924 #> [25,] -0.388590727 0.098542973 -0.045207238 0.003838229 #> [26,] -0.109916673 0.042028223 0.031122592 -0.173521656 #> [27,] -0.040845604 0.023057708 0.067839093 0.084198705 #> [28,] 0.080696347 -0.097192796 0.099373268 -0.166716428 #> [29,] -0.117530646 0.002589360 0.051425084 0.098244052 #> [30,] -0.037935138 -0.091390269 0.154099931 0.100237663 #> [31,] 0.109674339 0.158620750 -0.181802621 -0.112714822 #> [32,] 0.069971569 -0.425139230 -0.020032195 -0.193571920 #> [33,] 0.050674415 0.050518682 -0.010950361 -0.068936556 #> [34,] 0.065490413 0.142178491 0.174034864 0.072663828 #> [35,] -0.045676268 -0.057128729 0.050522160 -0.165689753 #> [36,] 0.368217440 0.096573493 0.120128008 0.020252183 #> [37,] 0.037286542 -0.056101952 -0.018563948 0.263830281 #> [38,] 0.094379823 -0.263914331 -0.002747609 -0.010850423 #> [39,] 0.153890231 0.071449851 -0.075881371 -0.060456457 #> [40,] -0.146742186 -0.117294226 -0.255229801 -0.124537184 #> [41,] -0.274840831 0.009051742 0.124862149 0.074433697 #> [42,] -0.008508998 0.110465493 0.215727137 0.269240087 #> [43,] 0.014213371 -0.041499271 0.097848087 0.105722611 #> [44,] -0.080890432 0.094198355 0.004240823 -0.187634605 #> [45,] 0.051925330 -0.007981636 0.044460236 -0.066537564 #> [46,] 0.213031014 0.168676929 0.097190703 0.046563300 #> [47,] 0.175716867 0.045500673 0.175902385 -0.030590597 #> [48,] -0.040155598 -0.120432098 -0.137764051 -0.088812037 #> [49,] 0.164103981 0.024976184 0.015955563 -0.232780151 #> [50,] -0.103164763 -0.135032022 0.029553283 0.267389634"},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Advanced examples","text":"evaluation vignette set FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/articles/examples-advanced.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Advanced examples","text":"","code":"knitr::opts_chunk$set(collapse = TRUE, comment = \"#>\", eval = FALSE) library(fastcpd) set.seed(1) p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(480, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:480, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", r.progress = FALSE ) summary(result) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", r.progress = FALSE) #> #> Change points: #> 79 199 317 #> #> Cost values: #> 125.1501 155.4486 160.4363 274.2228 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.900559900 -2.749985232 #> [2,] -2.668688731 -2.473539196 4.198921150 3.057387294 #> [3,] -4.354248176 0.983488542 5.054325016 3.735347545 #> [4,] -4.662566697 0.279505605 2.628354481 -3.216027015 #> [5,] -4.820139326 0.496565230 4.611603593 3.231304715 #> [6,] -3.860308911 -2.576467859 3.304903523 4.577405578 #> [7,] 0.117234534 -0.219145186 -0.015091394 -0.086048806 #> [8,] 0.117552281 0.035701649 -0.127986984 -0.138807479 #> [9,] -0.188756652 0.133653604 0.038250526 -0.037807789 #> [10,] -0.074530965 0.010173561 0.021979969 -0.047707753 #> [11,] -0.061721013 -0.176769170 -0.059312332 -0.135633390 #> [12,] 0.021874954 -0.133777712 0.028889403 -0.130808437 #> [13,] 0.067777695 0.129187849 -0.026100120 0.002593292 #> [14,] -0.122270920 0.110865328 0.084858637 0.233903944 #> [15,] -0.014192027 0.086100909 0.030268607 0.243927135 #> [16,] -0.135970691 -0.043211895 -0.032229963 -0.134840827 #> [17,] 0.007608049 -0.079445537 -0.013675459 0.102010778 #> [18,] -0.010343806 0.132392790 -0.151237533 -0.123955960 #> [19,] 0.252797503 0.253113165 -0.197620792 0.099319824 #> [20,] -0.044505498 -0.049962311 0.233024977 0.176211754 #> [21,] 0.084292730 0.366833682 -0.220176991 -0.035984666 #> [22,] -0.373913824 -0.060767726 0.036430343 -0.105248408 #> [23,] 0.316816197 -0.191114879 -0.127549606 -0.112775343 #> [24,] 0.008174405 0.158066938 -0.121000263 0.232613924 #> [25,] -0.388590727 0.098542973 -0.045207238 0.003838229 #> [26,] -0.109916673 0.042028223 0.031122592 -0.173521656 #> [27,] -0.040845604 0.023057708 0.067839093 0.084198705 #> [28,] 0.080696347 -0.097192796 0.099373268 -0.166716428 #> [29,] -0.117530646 0.002589360 0.051425084 0.098244052 #> [30,] -0.037935138 -0.091390269 0.154099931 0.100237663 #> [31,] 0.109674339 0.158620750 -0.181802621 -0.112714822 #> [32,] 0.069971569 -0.425139230 -0.020032195 -0.193571920 #> [33,] 0.050674415 0.050518682 -0.010950361 -0.068936556 #> [34,] 0.065490413 0.142178491 0.174034864 0.072663828 #> [35,] -0.045676268 -0.057128729 0.050522160 -0.165689753 #> [36,] 0.368217440 0.096573493 0.120128008 0.020252183 #> [37,] 0.037286542 -0.056101952 -0.018563948 0.263830281 #> [38,] 0.094379823 -0.263914331 -0.002747609 -0.010850423 #> [39,] 0.153890231 0.071449851 -0.075881371 -0.060456457 #> [40,] -0.146742186 -0.117294226 -0.255229801 -0.124537184 #> [41,] -0.274840831 0.009051742 0.124862149 0.074433697 #> [42,] -0.008508998 0.110465493 0.215727137 0.269240087 #> [43,] 0.014213371 -0.041499271 0.097848087 0.105722611 #> [44,] -0.080890432 0.094198355 0.004240823 -0.187634605 #> [45,] 0.051925330 -0.007981636 0.044460236 -0.066537564 #> [46,] 0.213031014 0.168676929 0.097190703 0.046563300 #> [47,] 0.175716867 0.045500673 0.175902385 -0.030590597 #> [48,] -0.040155598 -0.120432098 -0.137764051 -0.088812037 #> [49,] 0.164103981 0.024976184 0.015955563 -0.232780151 #> [50,] -0.103164763 -0.135032022 0.029553283 0.267389634 set.seed(1) p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(480, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:480, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result_multiple_epochs <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < 20) 1 else 0, r.progress = FALSE ) summary(result_multiple_epochs) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", multiple_epochs = function(segment_length) if (segment_length < #> 20) 1 else 0, r.progress = FALSE) #> #> Change points: #> 79 199 320 #> #> Cost values: #> 125.1501 155.4486 164.1284 177.9478 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.898713954 -3.006202485 #> [2,] -2.668688731 -2.473539196 4.209460633 3.096081968 #> [3,] -4.354248176 0.983488542 5.011651466 3.718141515 #> [4,] -4.662566697 0.279505605 2.623742216 -3.130632564 #> [5,] -4.820139326 0.496565230 4.616444609 3.175562645 #> [6,] -3.860308911 -2.576467859 3.336301862 4.342890940 #> [7,] 0.117234534 -0.219145186 -0.039861426 -0.016395368 #> [8,] 0.117552281 0.035701649 -0.075921029 -0.022022294 #> [9,] -0.188756652 0.133653604 0.052907904 0.070683538 #> [10,] -0.074530965 0.010173561 0.003352400 0.012791767 #> [11,] -0.061721013 -0.176769170 -0.044814526 -0.166042077 #> [12,] 0.021874954 -0.133777712 0.103526247 0.077820866 #> [13,] 0.067777695 0.129187849 -0.072098209 0.077436979 #> [14,] -0.122270920 0.110865328 0.095306898 0.114046507 #> [15,] -0.014192027 0.086100909 0.006037145 -0.015966855 #> [16,] -0.135970691 -0.043211895 0.026749332 -0.095751983 #> [17,] 0.007608049 -0.079445537 -0.003130812 -0.010689927 #> [18,] -0.010343806 0.132392790 -0.156866781 -0.138057246 #> [19,] 0.252797503 0.253113165 -0.167436237 0.158967697 #> [20,] -0.044505498 -0.049962311 0.177560915 0.030169316 #> [21,] 0.084292730 0.366833682 -0.224849869 -0.181503446 #> [22,] -0.373913824 -0.060767726 0.003860114 0.045649997 #> [23,] 0.316816197 -0.191114879 -0.118941359 -0.025305755 #> [24,] 0.008174405 0.158066938 -0.135680402 -0.137396624 #> [25,] -0.388590727 0.098542973 -0.064383316 0.022336212 #> [26,] -0.109916673 0.042028223 0.017815134 -0.059782090 #> [27,] -0.040845604 0.023057708 0.053296378 0.030431412 #> [28,] 0.080696347 -0.097192796 0.122665492 -0.080706668 #> [29,] -0.117530646 0.002589360 0.062791240 -0.051542901 #> [30,] -0.037935138 -0.091390269 0.122976438 0.048892128 #> [31,] 0.109674339 0.158620750 -0.181785506 0.075434045 #> [32,] 0.069971569 -0.425139230 0.030472706 0.033375101 #> [33,] 0.050674415 0.050518682 0.034305534 0.070983107 #> [34,] 0.065490413 0.142178491 0.164899160 0.032575571 #> [35,] -0.045676268 -0.057128729 0.022794426 -0.134534869 #> [36,] 0.368217440 0.096573493 0.114099285 -0.020673038 #> [37,] 0.037286542 -0.056101952 -0.023029273 0.102043216 #> [38,] 0.094379823 -0.263914331 0.003288922 -0.052763017 #> [39,] 0.153890231 0.071449851 -0.097509620 -0.120642848 #> [40,] -0.146742186 -0.117294226 -0.208865036 -0.045775725 #> [41,] -0.274840831 0.009051742 0.127312770 0.123910218 #> [42,] -0.008508998 0.110465493 0.118453225 -0.005505113 #> [43,] 0.014213371 -0.041499271 0.113699538 -0.056131976 #> [44,] -0.080890432 0.094198355 0.051912519 0.013524984 #> [45,] 0.051925330 -0.007981636 0.085966205 -0.033628617 #> [46,] 0.213031014 0.168676929 0.035738422 -0.029655833 #> [47,] 0.175716867 0.045500673 0.175504506 0.119507574 #> [48,] -0.040155598 -0.120432098 -0.139162874 -0.054349982 #> [49,] 0.164103981 0.024976184 0.029374949 -0.056092814 #> [50,] -0.103164763 -0.135032022 0.047475507 -0.032491294 set.seed(1) p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(480, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:480, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result_vanilla_percentage <- fastcpd( formula = y ~ . - 1, data = data.frame(y = y, x = x), family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE ) summary(result_vanilla_percentage) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data.frame(y = y, x = x), #> family = \"lasso\", vanilla_percentage = 0.2, r.progress = FALSE) #> #> Change points: #> 79 199 317 #> #> Cost values: #> 125.1501 155.4486 160.4363 274.2228 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.428039439 4.900559900 -2.749985232 #> [2,] -2.668688731 -2.473539196 4.198921150 3.057387294 #> [3,] -4.354248176 0.983488542 5.054325016 3.735347545 #> [4,] -4.662566697 0.279505605 2.628354481 -3.216027015 #> [5,] -4.820139326 0.496565230 4.611603593 3.231304715 #> [6,] -3.860308911 -2.576467859 3.304903523 4.577405578 #> [7,] 0.117234534 -0.219145186 -0.015091394 -0.086048806 #> [8,] 0.117552281 0.035701649 -0.127986984 -0.138807479 #> [9,] -0.188756652 0.133653604 0.038250526 -0.037807789 #> [10,] -0.074530965 0.010173561 0.021979969 -0.047707753 #> [11,] -0.061721013 -0.176769170 -0.059312332 -0.135633390 #> [12,] 0.021874954 -0.133777712 0.028889403 -0.130808437 #> [13,] 0.067777695 0.129187849 -0.026100120 0.002593292 #> [14,] -0.122270920 0.110865328 0.084858637 0.233903944 #> [15,] -0.014192027 0.086100909 0.030268607 0.243927135 #> [16,] -0.135970691 -0.043211895 -0.032229963 -0.134840827 #> [17,] 0.007608049 -0.079445537 -0.013675459 0.102010778 #> [18,] -0.010343806 0.132392790 -0.151237533 -0.123955960 #> [19,] 0.252797503 0.253113165 -0.197620792 0.099319824 #> [20,] -0.044505498 -0.049962311 0.233024977 0.176211754 #> [21,] 0.084292730 0.366833682 -0.220176991 -0.035984666 #> [22,] -0.373913824 -0.060767726 0.036430343 -0.105248408 #> [23,] 0.316816197 -0.191114879 -0.127549606 -0.112775343 #> [24,] 0.008174405 0.158066938 -0.121000263 0.232613924 #> [25,] -0.388590727 0.098542973 -0.045207238 0.003838229 #> [26,] -0.109916673 0.042028223 0.031122592 -0.173521656 #> [27,] -0.040845604 0.023057708 0.067839093 0.084198705 #> [28,] 0.080696347 -0.097192796 0.099373268 -0.166716428 #> [29,] -0.117530646 0.002589360 0.051425084 0.098244052 #> [30,] -0.037935138 -0.091390269 0.154099931 0.100237663 #> [31,] 0.109674339 0.158620750 -0.181802621 -0.112714822 #> [32,] 0.069971569 -0.425139230 -0.020032195 -0.193571920 #> [33,] 0.050674415 0.050518682 -0.010950361 -0.068936556 #> [34,] 0.065490413 0.142178491 0.174034864 0.072663828 #> [35,] -0.045676268 -0.057128729 0.050522160 -0.165689753 #> [36,] 0.368217440 0.096573493 0.120128008 0.020252183 #> [37,] 0.037286542 -0.056101952 -0.018563948 0.263830281 #> [38,] 0.094379823 -0.263914331 -0.002747609 -0.010850423 #> [39,] 0.153890231 0.071449851 -0.075881371 -0.060456457 #> [40,] -0.146742186 -0.117294226 -0.255229801 -0.124537184 #> [41,] -0.274840831 0.009051742 0.124862149 0.074433697 #> [42,] -0.008508998 0.110465493 0.215727137 0.269240087 #> [43,] 0.014213371 -0.041499271 0.097848087 0.105722611 #> [44,] -0.080890432 0.094198355 0.004240823 -0.187634605 #> [45,] 0.051925330 -0.007981636 0.044460236 -0.066537564 #> [46,] 0.213031014 0.168676929 0.097190703 0.046563300 #> [47,] 0.175716867 0.045500673 0.175902385 -0.030590597 #> [48,] -0.040155598 -0.120432098 -0.137764051 -0.088812037 #> [49,] 0.164103981 0.024976184 0.015955563 -0.232780151 #> [50,] -0.103164763 -0.135032022 0.029553283 0.267389634"},{"path":"https://fastcpd.xingchi.li/articles/examples-custom-model.html","id":"notes","dir":"Articles","previous_headings":"","what":"Notes","title":"Custom logistic regression model","text":"evaluation vignette set FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/articles/examples-custom-model.html","id":"appendix-all-code-snippets","dir":"Articles","previous_headings":"","what":"Appendix: all code snippets","title":"Custom logistic regression model","text":"","code":"knitr::opts_chunk$set(collapse = TRUE, comment = \"#>\", eval = FALSE) library(fastcpd) set.seed(1) x <- matrix(rnorm(1500, 0, 1), ncol = 5) theta <- rbind(rnorm(5, 0, 1), rnorm(5, 2, 1)) y <- c( rbinom(125, 1, 1 / (1 + exp(-x[1:125, ] %*% theta[1, ]))), rbinom(175, 1, 1 / (1 + exp(-x[126:300, ] %*% theta[2, ]))) ) binomial_data <- data.frame(y = y, x = x) result <- fastcpd.binomial(cbind(y, x), r.progress = FALSE, cost_adjustment = NULL) #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred #> Warning: fit_glm: fitted probabilities numerically 0 or 1 occurred summary(result) #> #> Call: #> fastcpd.binomial(data = cbind(y, x), r.progress = FALSE, cost_adjustment = NULL) #> #> Change points: #> 126 #> #> Cost values: #> 56.90525 30.76875 #> #> Parameters: #> segment 1 segment 2 #> 1 0.7259293 1.878525 #> 2 -1.0294802 2.704376 #> 3 1.0576503 3.702310 #> 4 -0.8812767 2.258796 #> 5 0.2419351 2.524173 logistic_loss <- function(data, theta) { x <- data[, -1] y <- data[, 1] u <- x %*% theta nll <- -y * u + log(1 + exp(u)) nll[u > 10] <- -y[u > 10] * u[u > 10] + u[u > 10] sum(nll) } logistic_gradient <- function(data, theta) { x <- data[nrow(data), -1] y <- data[nrow(data), 1] c(-(y - 1 / (1 + exp(-x %*% theta)))) * x } logistic_hessian <- function(data, theta) { x <- data[nrow(data), -1] prob <- 1 / (1 + exp(-x %*% theta)) (x %o% x) * c((1 - prob) * prob) } result <- fastcpd( y ~ . - 1, binomial_data, epsilon = 1e-5, cost = logistic_loss, cost_gradient = logistic_gradient, cost_hessian = logistic_hessian, r.progress = FALSE ) summary(result) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = binomial_data, cost = logistic_loss, #> cost_gradient = logistic_gradient, cost_hessian = logistic_hessian, #> epsilon = 1e-05, r.progress = FALSE) #> #> Change points: #> 22 125 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 -59.20045 0.8170446 1.902379 #> 2 -34.56676 -0.9600438 2.751578 #> 3 216.53373 0.9353306 3.734179 #> 4 -80.96420 -0.7393653 2.247423 #> 5 51.25224 0.1390591 2.535372"},{"path":"https://fastcpd.xingchi.li/authors.html","id":null,"dir":"","previous_headings":"","what":"Authors","title":"Authors and Citation","text":"Xingchi Li. Author, maintainer, copyright holder. Xianyang Zhang. Author, copyright holder.","code":""},{"path":"https://fastcpd.xingchi.li/authors.html","id":"citation","dir":"","previous_headings":"","what":"Citation","title":"Authors and Citation","text":"Li X, Zhang X (2024). “fastcpd: Fast Change Point Detection R.” doi:10.48550/arXiv.2404.05933. Zhang X, Dawn T (2023). “Sequential Gradient Descent Quasi-Newton's Method Change-Point Analysis.” Ruiz, Francisco, Dy, Jennifer, van de Meent, Jan-Willem (eds.), Proceedings 26th International Conference Artificial Intelligence Statistics, volume 206 series Proceedings Machine Learning Research, 1129–1143. https://proceedings.mlr.press/v206/zhang23b.html.","code":"@Misc{, title = {fastcpd: Fast Change Point Detection in R}, author = {Xingchi Li and Xianyang Zhang}, year = {2024}, doi = {10.48550/arXiv.2404.05933}, publisher = {arXiv}, } @InProceedings{, title = {Sequential Gradient Descent and Quasi-Newton's Method for Change-Point Analysis}, author = {Xianyang Zhang and Trisha Dawn}, year = {2023}, booktitle = {Proceedings of The 26th International Conference on Artificial Intelligence and Statistics}, volume = {206}, pages = {1129--1143}, editor = {{Ruiz} and {Francisco} and {Dy} and {Jennifer} and {van de Meent} and {Jan-Willem}}, series = {Proceedings of Machine Learning Research}, month = {25--27 Apr}, publisher = {PMLR}, pdf = {https://proceedings.mlr.press/v206/zhang23b/zhang23b.pdf}, url = {https://proceedings.mlr.press/v206/zhang23b.html}, abstract = {One common approach to detecting change-points is minimizing a cost function over possible numbers and locations of change-points. The framework includes several well-established procedures, such as the penalized likelihood and minimum description length. Such an approach requires finding the cost value repeatedly over different segments of the data set, which can be time-consuming when (i) the data sequence is long and (ii) obtaining the cost value involves solving a non-trivial optimization problem. This paper introduces a new sequential updating method (SE) to find the cost value effectively. The core idea is to update the cost value using the information from previous steps without re-optimizing the objective function. The new method is applied to change-point detection in generalized linear models and penalized regression. Numerical studies show that the new approach can be orders of magnitude faster than the Pruned Exact Linear Time (PELT) method without sacrificing estimation accuracy.}, }"},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"overview","dir":"","previous_headings":"","what":"Overview","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd (fast change point detection) fast implmentation change point detection methods R. fastcpd package designed find change points fast manner. easy install extensible kinds change point problems user specified cost function apart built-cost functions. learn behind algorithms: fastcpd: Fast Change Point Detection R Sequential Gradient Descent Quasi-Newton’s Method Change-Point Analysis","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"installation","dir":"","previous_headings":"","what":"Installation","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"","code":"# Install from CRAN install.packages(\"fastcpd\") # Development version from r-universe with CRAN version as a fallback install.packages( \"fastcpd\", repos = c(\"https://doccstat.r-universe.dev\", \"https://cloud.r-project.org\") ) ## install.packages(\"pak\") pak::pak(\"doccstat/fastcpd\") ## install.packages(\"devtools\") devtools::install_github(\"doccstat/fastcpd\") # conda-forge is a fork from CRAN and may not be up-to-date # Use mamba mamba install r-fastcpd # Use conda conda install -c conda-forge r-fastcpd"},{"path":"https://fastcpd.xingchi.li/index.html","id":"faq","dir":"","previous_headings":"Installation","what":"FAQ","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd depends following packages: Rcpp, C++ source code compilation. RcppArmadillo, fast linear algebra. fastglm, fast generalized linear models. glmnet, penalized regression. ggplot2, data visualization. ’re compiling source, can run following command see complete set system packages needed machine. suggested packages required main functionality package. required vignettes. want learn package comparison vignettes, either check vignettes CRAN pkgdown generated documentation. package able install Mac Linux distribution without problems dependencies installed. However, encountered problems related gfortran, might RcppArmadillo installed previously. Try Mac OSX stackoverflow solution Linux stackover solution trouble installing RcppArmadillo.","code":"pak::pkg_sysreqs(\"doccstat/fastcpd\") #> ── Install scripts ───────────────────────────────────────────── Ubuntu 20.04 #> apt-get -y update #> apt-get -y install libcurl4-openssl-dev libssl-dev zlib1g-dev make #> #> ── Packages and their system dependencies ─────────────────────────────────── #> curl – libcurl4-openssl-dev, libssl-dev #> data.table – zlib1g-dev #> fs – make #> openssl – libssl-dev"},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"r-shiny-app","dir":"","previous_headings":"Cheatsheet","what":"R Shiny App","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Available soon: rshiny.fastcpd.xingchi.li","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"usage","dir":"","previous_headings":"","what":"Usage","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"hard demonstrate features fastcpd single example due flexibility package. examples, please refer function reference. r.progress = FALSE used suppress progress bar. Users expected see progress bar running code default.","code":"set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } result <- fastcpd::fastcpd.ar(x[3 + seq_len(n)], 3, r.progress = FALSE) summary(result) #> #> Call: #> fastcpd::fastcpd.ar(data = x[3 + seq_len(n)], order = 3, r.progress = FALSE) #> #> Change points: #> 614 #> #> Cost values: #> 2743.759 2028.588 #> #> Parameters: #> segment 1 segment 2 #> 1 0.57120256 0.2371809 #> 2 -0.20985108 0.4031244 #> 3 0.08221978 0.2290323 plot(result)"},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"main-function","dir":"","previous_headings":"Examples","what":"Main function","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"time-series","dir":"","previous_headings":"Examples > Wrapper functions","what":"Time series","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"AR(p): fastcpd_ar ARIMA(p, d, q): fastcpd_arima ARMA(p, q): fastcpd_arma GARCH(p, q): fastcpd_garch VAR(p): fastcpd_var General time series: fastcpd_ts","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"unlabeled-data","dir":"","previous_headings":"Examples > Wrapper functions","what":"Unlabeled data","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Mean change: fastcpd_mean Variance change: fastcpd_variance Mean /variance change: fastcpd_meanvariance","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"regression-data","dir":"","previous_headings":"Examples > Wrapper functions","what":"Regression data","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Logistic regression: fastcpd_binomial Penalized linear regression: fastcpd_lasso Linear regression: fastcpd_lm Poisson regression: fastcpd_poisson","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/index.html","id":"variance-estimation","dir":"","previous_headings":"Examples > Utility functions","what":"Variance estimation","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Variance estimation ARMA models: variance_arma Variance estimation linear models: variance_lm Variance estimation mean change models: variance_mean Variance estimation median change models: variance_median","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"class-methods","dir":"","previous_headings":"Examples > Utility functions","what":"Class methods","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Plot: plot Print: print Show: show Summary: summary","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"data","dir":"","previous_headings":"Examples","what":"Data","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Bitcoin Market Price (USD): bitcoin Occupancy Detection Data Set: occupancy Transcription Profiling 57 Human Bladder Carcinoma Samples: transcriptome UK Seatbelts Data: uk_seatbelts Well-log Dataset Numerical Bayesian Methods Applied Signal Processing: well_log","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"main-class","dir":"","previous_headings":"Examples","what":"Main class","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"fastcpd","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"make-contributions","dir":"","previous_headings":"","what":"Make contributions","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"Fork repo. Create new branch main branch. Make changes commit . Please follow Google’s R style guide naming variables functions. adding new family models new cost functions corresponding gradient Hessian, please add src/fastcpd_class_cost.cc proper example tests vignettes/gallery.Rmd tests/testthat/test-gallery.R. Add family name src/fastcpd_constants.h. [Recommended] Add new wrapper function R/fastcpd_wrappers.R new family models move examples new wrapper function roxygen examples. Add new wrapper function corresponding section _pkgdown.yml. Push changes fork. Create pull request. Make sure pull request create new warnings errors devtools::check().","code":""},{"path":"https://fastcpd.xingchi.li/index.html","id":"contact-us","dir":"","previous_headings":"","what":"Contact us","title":"Fast Change Point Detection via Sequential Gradient Descent","text":"File ticket GitHub Issues. Contact authors specified DESCRIPTION.","code":""},{"path":[]},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":null,"dir":"Reference","previous_headings":"","what":"Bitcoin Market Price (USD) — bitcoin","title":"Bitcoin Market Price (USD) — bitcoin","text":"average USD market price across major bitcoin exchanges.","code":""},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Bitcoin Market Price (USD) — bitcoin","text":"","code":"bitcoin"},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Bitcoin Market Price (USD) — bitcoin","text":"data frame 1354 rows 2 variables: date POSIXct,POSIXt (TZ: \"UTC\") 2019-01-02 2023-10-28 price average USD market price across major bitcoin exchanges","code":""},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Bitcoin Market Price (USD) — bitcoin","text":"","code":""},{"path":"https://fastcpd.xingchi.li/reference/bitcoin.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Bitcoin Market Price (USD) — bitcoin","text":"","code":"# \\donttest{ if (requireNamespace(\"ggplot2\", quietly = TRUE)) { p <- ggplot2::ggplot(bitcoin, ggplot2::aes(x = date, y = price)) + ggplot2::geom_line() print(p) result <- suppressWarnings(fastcpd.garch( diff(log(bitcoin$price[600:900])), c(1, 1), beta = \"BIC\", cost_adjustment = \"BIC\" )) summary(result) bitcoin$date[result@cp_set + 600] plot(result) cp_dates <- bitcoin[600 + result@cp_set + 1, \"date\"] ggplot2::ggplot( data = data.frame( x = bitcoin$date[600:900], y = bitcoin$price[600:900] ), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_line(color = \"steelblue\") + ggplot2::geom_vline( xintercept = cp_dates, color = \"red\", linetype = \"dotted\", linewidth = 0.5, alpha = 0.7 ) + ggplot2::labs( x = \"Year\", y = \"Bitcoin price in USD\" ) + ggplot2::annotate( \"text\", x = cp_dates, y = 2000, label = as.character(cp_dates), color = \"steelblue\" ) + ggplot2::theme_bw() } #> Registered S3 method overwritten by 'quantmod': #> method from #> as.zoo.data.frame zoo #> #> Call: #> fastcpd.garch(data = diff(log(bitcoin$price[600:900])), order = c(1, #> 1), beta = \"BIC\", cost_adjustment = \"BIC\") #> #> Change points: #> 128 #> #> Cost values: #> -328.3151 -324.7008 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd-class.html","id":null,"dir":"Reference","previous_headings":"","what":"An S4 class to store the output created with fastcpd() — fastcpd-class","title":"An S4 class to store the output created with fastcpd() — fastcpd-class","text":"S4 class stores output fastcpd() fastcpd.family. fastcpd object consist several slots including call fastcpd(), data used, family model, change points, cost values, residuals, estimated parameters boolean indicating whether model fitted change points change points parameters, can select using @.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd-class.html","id":"slots","dir":"Reference","previous_headings":"","what":"Slots","title":"An S4 class to store the output created with fastcpd() — fastcpd-class","text":"call call function. data data passed function. order order time series model. family family model. cp_set set change points. cost_values cost function values segment. residuals residuals model change points. Used built-families. thetas estimated parameters segment. Used built-families. cp_only boolean indicating whether fastcpd() run return change points change points estimated parameters cost values segment.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently — fastcpd","title":"Find change points efficiently — fastcpd","text":"fastcpd() takes formulas, data, families extra parameters returns fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently — fastcpd","text":"","code":"fastcpd( formula = y ~ . - 1, data, beta = \"MBIC\", cost_adjustment = \"MBIC\", family = NULL, cost = NULL, cost_gradient = NULL, cost_hessian = NULL, line_search = c(1), lower = rep(-Inf, p), upper = rep(Inf, p), pruning_coef = 0, segment_count = 10, trim = 0.02, momentum_coef = 0, multiple_epochs = function(x) 0, epsilon = 1e-10, order = c(0, 0, 0), p = ncol(data) - 1, cp_only = FALSE, vanilla_percentage = 0, warm_start = FALSE, ... )"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently — fastcpd","text":"formula formula object specifying model fitted. (optional) response variable LHS formula, covariates RHS. naming variables used formula consistent column names data frame provided data. intercept term removed formula. response variable needed mean/variance change models time series models. default, intercept column added data, similar lm() function. Thus, suggested users remove intercept term appending - 1 formula. Note fastcpd.family functions require formula input. data data frame dimension \\(T \\times d\\) containing data segmented (row denotes data point \\(z_t \\\\mathbb{R}^d\\) \\(t = 1, \\ldots, T\\)) required main function, matrix vector input also accepted fastcpd.family functions. beta Penalty criterion number change points. parameter takes string value \"BIC\", \"MBIC\", \"MDL\" numeric value. numeric value provided, value used penalty. default, mBIC criterion used, \\(\\beta = (p + 2) \\log(T) / 2\\). parameter usage paired cost_adjustment described . Discussions penalty criterion can found references. cost_adjustment Cost adjustment criterion. can \"BIC\", \"MBIC\", \"MDL\" NULL. default, cost adjustment criterion set \"MBIC\". \"MBIC\" \"MDL\" criteria modify cost function adding negative adjustment term cost function. \"BIC\" NULL modify cost function. Details can found references. family Family class change point model. can \"mean\" mean change, \"variance\" variance change, \"meanvariance\" mean /variance change, \"lm\" linear regression, \"binomial\" logistic regression, \"poisson\" Poisson regression, \"lasso\" penalized linear regression, \"ar\" AR(\\(p\\)) models, \"arma\" ARMA(\\(p\\), \\(q\\)) models, \"arima\" ARIMA(\\(p\\), \\(d\\), \\(q\\)) models, \"garch\" GARCH(\\(p\\), \\(q\\)) models, \"var\" VAR(\\(p\\)) models \"custom\" user-specified custom models. Omitting parameter specifying parameter \"custom\" NULL, case, users must specify custom cost function. cost Cost function used. cost, cost_gradient, cost_hessian specified time family built-families cost functions implemented C++ provide better performance. specified, default negative log-likelihood corresponding family. Custom cost functions can provided following two formats: cost = function(data) {...} cost = function(data, theta) {...} Users can specify loss function using second format used calculate cost value. formats, input data subset original data frame form matrix (matrix single column case univariate data set). first format, specified cost function directly calculates cost value. fastcpd() performs vanilla PELT algorithm, cost_gradient cost_hessian provided since parameter updating necessary vanilla PELT. second format, loss function \\(\\sum_{= s}^t l(z_i, \\theta)\\) provided, optimized parameter \\(\\theta\\) obtain cost value. detailed discussion custom cost function usage can found references. cost_gradient Gradient custom cost function. Example usage: gradient function takes two inputs, first matrix representing segment data, similar format used cost function, second parameter needs optimized. gradient function returns value gradient loss function, .e., \\(\\sum_{= s}^t \\nabla l(z_i, \\theta)\\). cost_hessian Hessian custom loss function. Hessian function takes two inputs, first matrix representing segment data, similar format used cost function, second parameter needs optimized. gradient function returns Hessian loss function, .e., \\(\\sum_{= s}^t \\nabla^2 l(z_i, \\theta)\\). line_search vector numeric values provided, line search performed find optimal step size update. Detailed usage line_search can found references. lower Lower bound parameters. Used specify domain parameters gradient descent step. specified, lower bound set -Inf parameters. lower especially useful estimated parameters take positive values, noise variance. upper Upper bound parameters. Used specify domain parameters gradient descent step. specified, upper bound set Inf parameters. pruning_coef Pruning coefficient $c_0$ used pruning step PELT algorithm default value 0. cost_adjustment specified \"MBIC\", adjustment term \\(p\\log(2)\\) added pruning coefficient. cost_adjustment specified \"MDL\", adjustment term \\(p\\log_2(2)\\) added pruning coefficient. Detailed discussion pruning coefficient can found references. segment_count initial guess number segments. specified, initial guess number segments 10. initial guess affects initial estimates parameters SeGD. trim Trimming boundary change points change point close boundary counted change point. parameter also specifies minimum distance two change points. several change points mutual distances smaller trim * nrow(data), change points merged one single change point. value parameter 0 1. momentum_coef Momentum coefficient applied update. parameter used loss function bad-shaped maintaining momentum previous update desired. Default value 0, meaning algorithm maintain momentum default. multiple_epochs function can specified adaptive number multiple epochs can utilized improve algorithm's performance. multiple_epochs function length data segment. function returns integer indicating many epochs performed apart default update. default, function returns zero, meaning multiple epochs used update parameters. Example usage: function let SeGD perform parameter updates additional epoch segment length less 100 additional epoch segments lengths greater equal 100. epsilon Epsilon avoid numerical issues. used Hessian computation Logistic Regression Poisson Regression. order Order AR(\\(p\\)), VAR(\\(p\\)) ARIMA(\\(p\\), \\(d\\), \\(q\\)) model. p Number covariates model. specified, number covariates inferred data, .e., p = ncol(data) - 1. parameter superseded order case time series models: \"ar\", \"var\", \"arima\". cp_only TRUE, change points returned. Otherwise, cost function values together estimated parameters segment also returned. default value set FALSE plot can used visualize results built-model. cp_only performance impact algorithm, since cost values estimated parameters segment need calculated stored. users interested change points, setting cp_only TRUE help computational cost. vanilla_percentage parameter \\(v\\) zero one. segment, length \\(vT\\), cost value computed performing exact minimization loss function parameter. length greater \\(vT\\), cost value approximated SeGD. Therefore, parameter induces algorithm can interpreted interpolation dynamic programming SeGD (\\(v = 0\\)) vanilla PELT (\\(v = 1\\)). readers referred references details. warm_start TRUE, algorithm use estimated parameters previous segment initial value current segment. parameter used \"glm\" families. ... parameters specific models. include.mean used determine mean/intercept term included ARIMA(\\(p\\), \\(d\\), \\(q\\)) GARCH(\\(p\\), \\(q\\)) models. r.clock used create RcppClock object record time spent C++ code. Default empty string. set non-empty string, object specified name created. Usage: library(RcppClock); plot(VARIABLE_NAME). r.progress used control progress bar. default progress bar shown. disable , set r.progress = FALSE. p.response used specify number response variables. parameter especially useful linear models multivariate responses.","code":"cost_gradient = function(data, theta) { ... return(gradient) } multiple_epochs = function(segment_length) { if (segment_length < 100) 1 else 0 }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently — fastcpd","text":"fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"gallery","dir":"Reference","previous_headings":"","what":"Gallery","title":"Find change points efficiently — fastcpd","text":"https://github.com/doccstat/fastcpd/tree/main/tests/testthat/examples","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"references","dir":"Reference","previous_headings":"","what":"References","title":"Find change points efficiently — fastcpd","text":"Xingchi Li, Xianyang Zhang (2024). ``fastcpd: Fast Change Point Detection R.'' arXiv:2404.05933, https://arxiv.org/abs/2404.05933. Xianyang Zhang, Trisha Dawn (2023). ``Sequential Gradient Descent Quasi-Newton's Method Change-Point Analysis.'' Ruiz, Francisco, Dy, Jennifer, van de Meent, Jan-Willem (eds.), Proceedings 26th International Conference Artificial Intelligence Statistics, volume 206 series Proceedings Machine Learning Research, 1129-1143.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently — fastcpd","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 200 p <- 4 d <- 2 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_1 <- matrix(runif(8, -3, -1), nrow = p) theta_2 <- matrix(runif(8, -1, 3), nrow = p) y <- rbind( x[1:125, ] %*% theta_1 + mvtnorm::rmvnorm(125, rep(0, d), 3 * diag(d)), x[126:n, ] %*% theta_2 + mvtnorm::rmvnorm(75, rep(0, d), 3 * diag(d)) ) result_mlm <- fastcpd( cbind(y.1, y.2) ~ . - 1, cbind.data.frame(y = y, x = x), family = \"lm\" ) summary(result_mlm) } #> #> Call: #> fastcpd(formula = cbind(y.1, y.2) ~ . - 1, data = cbind.data.frame(y = y, #> x = x), family = \"lm\") #> #> Change points: #> 125 #> #> Cost values: #> 501.0668 296.4026 if ( requireNamespace(\"mvtnorm\", quietly = TRUE) && requireNamespace(\"stats\", quietly = TRUE) ) { set.seed(1) n <- 400 + 300 + 500 p <- 5 x <- mvtnorm::rmvnorm(n, mean = rep(0, p), sigma = diag(p)) theta <- rbind( mvtnorm::rmvnorm(1, mean = rep(0, p - 3), sigma = diag(p - 3)), mvtnorm::rmvnorm(1, mean = rep(5, p - 3), sigma = diag(p - 3)), mvtnorm::rmvnorm(1, mean = rep(9, p - 3), sigma = diag(p - 3)) ) theta <- cbind(theta, matrix(0, 3, 3)) theta <- theta[rep(seq_len(3), c(400, 300, 500)), ] y_true <- rowSums(x * theta) factor <- c( 2 * stats::rbinom(400, size = 1, prob = 0.95) - 1, 2 * stats::rbinom(300, size = 1, prob = 0.95) - 1, 2 * stats::rbinom(500, size = 1, prob = 0.95) - 1 ) y <- factor * y_true + stats::rnorm(n) data <- cbind.data.frame(y, x) huber_threshold <- 1 huber_loss <- function(data, theta) { residual <- data[, 1] - data[, -1, drop = FALSE] %*% theta indicator <- abs(residual) <= huber_threshold sum( residual^2 / 2 * indicator + huber_threshold * ( abs(residual) - huber_threshold / 2 ) * (1 - indicator) ) } huber_loss_gradient <- function(data, theta) { residual <- c(data[nrow(data), 1] - data[nrow(data), -1] %*% theta) if (abs(residual) <= huber_threshold) { -residual * data[nrow(data), -1] } else { -huber_threshold * sign(residual) * data[nrow(data), -1] } } huber_loss_hessian <- function(data, theta) { residual <- c(data[nrow(data), 1] - data[nrow(data), -1] %*% theta) if (abs(residual) <= huber_threshold) { outer(data[nrow(data), -1], data[nrow(data), -1]) } else { 0.01 * diag(length(theta)) } } huber_regression_result <- fastcpd( formula = y ~ . - 1, data = data, beta = (p + 1) * log(n) / 2, cost = huber_loss, cost_gradient = huber_loss_gradient, cost_hessian = huber_loss_hessian ) summary(huber_regression_result) } #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data, beta = (p + 1) * log(n)/2, #> cost = huber_loss, cost_gradient = huber_loss_gradient, cost_hessian = huber_loss_hessian) #> #> Change points: #> 418 726 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 -0.464532607 2.760546489 8.744706508 #> 2 -0.995964940 5.059690163 9.506534878 #> 3 -0.093877079 0.023321737 -0.008908851 #> 4 -0.002000477 -0.090091843 -0.047909865 #> 5 0.009364910 -0.009543696 0.025171681 # \\donttest{ set.seed(1) p <- 5 x <- matrix(rnorm(375 * p, 0, 1), ncol = p) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(200, 1, 1 / (1 + exp(-x[1:200, ] %*% theta[1, ]))), rbinom(175, 1, 1 / (1 + exp(-x[201:375, ] %*% theta[2, ]))) ) data <- data.frame(y = y, x = x) result_builtin <- suppressWarnings(fastcpd.binomial(data)) logistic_loss <- function(data, theta) { x <- data[, -1] y <- data[, 1] u <- x %*% theta nll <- -y * u + log(1 + exp(u)) nll[u > 10] <- -y[u > 10] * u[u > 10] + u[u > 10] sum(nll) } logistic_loss_gradient <- function(data, theta) { x <- data[nrow(data), -1] y <- data[nrow(data), 1] c(-(y - 1 / (1 + exp(-x %*% theta)))) * x } logistic_loss_hessian <- function(data, theta) { x <- data[nrow(data), -1] prob <- 1 / (1 + exp(-x %*% theta)) (x %o% x) * c((1 - prob) * prob) } result_custom <- fastcpd( formula = y ~ . - 1, data = data, epsilon = 1e-5, cost = logistic_loss, cost_gradient = logistic_loss_gradient, cost_hessian = logistic_loss_hessian ) cat( \"Change points detected by built-in logistic regression model: \", result_builtin@cp_set, \"\\n\", \"Change points detected by custom logistic regression model: \", result_custom@cp_set, \"\\n\", sep = \"\" ) #> Change points detected by built-in logistic regression model: 202 #> Change points detected by custom logistic regression model: 202 result_custom_two_epochs <- fastcpd( formula = y ~ . - 1, data = data, multiple_epochs = function(segment_length) 1, epsilon = 1e-5, cost = logistic_loss, cost_gradient = logistic_loss_gradient, cost_hessian = logistic_loss_hessian ) summary(result_custom_two_epochs) #> #> Call: #> fastcpd(formula = y ~ . - 1, data = data, cost = logistic_loss, #> cost_gradient = logistic_loss_gradient, cost_hessian = logistic_loss_hessian, #> multiple_epochs = function(segment_length) 1, epsilon = 1e-05) #> #> Change points: #> 202 #> #> Parameters: #> segment 1 segment 2 #> 1 -0.4884741 1.9829469 #> 2 -1.5637585 1.6145734 #> 3 -1.6874162 4.6191745 #> 4 -0.4192355 -0.1568114 #> 5 2.0315690 2.3956803 # } # \\donttest{ if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 480 p_true <- 6 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) small_lasso_data <- cbind.data.frame(y, x) result_no_vp <- fastcpd.lasso( small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, pruning_coef = 0 ) summary(result_no_vp) result_20_vp <- fastcpd.lasso( small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, vanilla_percentage = 0.2, pruning_coef = 0 ) summary(result_20_vp) } #> #> Call: #> fastcpd.lasso(data = small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, #> pruning_coef = 0) #> #> Change points: #> 79 202 325 #> #> Cost values: #> 13.72919 225.1447 85.69365 47.51894 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.580352409 -2.2528808428 4.848522259 -2.995657982 #> [2,] -2.668688731 -2.2387392752 4.183821811 3.100656318 #> [3,] -4.354248176 1.1867292005 4.878538526 3.711902167 #> [4,] -4.662566697 0.5043975083 2.724613946 -3.128550136 #> [5,] -4.820139326 0.3907766479 4.658932261 3.187247302 #> [6,] -3.860308911 -2.4708867317 3.364992430 4.322270139 #> [7,] 0.117234534 -0.2337451460 0.037468301 -0.005247904 #> [8,] 0.117552281 -0.0774905233 -0.171677542 -0.029653585 #> [9,] -0.188756652 0.4850028633 -0.149864384 0.068109708 #> [10,] -0.074530965 -0.2693267166 -0.099057468 0.007239663 #> [11,] -0.061721013 -0.3929366442 0.057377332 -0.168703949 #> [12,] 0.021874954 -0.4294667473 0.112828002 0.080008740 #> [13,] 0.067777695 0.3501129775 0.065290363 0.081191140 #> [14,] -0.122270920 0.4348764010 0.124026153 0.093730519 #> [15,] -0.014192027 -0.1009201441 0.055676406 -0.022302179 #> [16,] -0.135970691 -0.3228209051 -0.322266525 -0.093584779 #> [17,] 0.007608049 -0.0697777666 -0.173314905 0.003534974 #> [18,] -0.010343806 0.1576855458 0.036483478 -0.169347885 #> [19,] 0.252797503 0.5568680926 0.079891379 0.146785657 #> [20,] -0.044505498 -0.5831313510 -0.136705472 0.008129199 #> [21,] 0.084292730 0.5462807777 -0.201700276 -0.168701992 #> [22,] -0.373913824 0.3760916945 0.061397643 0.032865121 #> [23,] 0.316816197 -0.2806604425 -0.162557542 -0.007149145 #> [24,] 0.008174405 0.2296689003 -0.036906468 -0.133226098 #> [25,] -0.388590727 0.6583142640 -0.162051056 0.017998768 #> [26,] -0.109916673 0.0687814570 0.068395416 -0.060802578 #> [27,] -0.040845604 -0.0868845012 -0.208878583 0.028223323 #> [28,] 0.080696347 -0.1975246890 -0.131401498 -0.070869199 #> [29,] -0.117530646 0.0006050774 -0.034054322 -0.066685344 #> [30,] -0.037935138 0.2909132628 0.054106263 0.030597693 #> [31,] 0.109674339 0.2194249070 -0.124483059 0.076983144 #> [32,] 0.069971569 -0.7939801174 0.065441362 0.040494938 #> [33,] 0.050674415 -0.2109295276 0.002639198 0.069521670 #> [34,] 0.065490413 0.6682523717 0.207420084 0.045711660 #> [35,] -0.045676268 -0.5333420170 0.171445009 -0.109004764 #> [36,] 0.368217440 -0.0662157788 -0.032932140 -0.018136377 #> [37,] 0.037286542 -0.2511805574 -0.363798390 0.117605382 #> [38,] 0.094379823 -0.0542487631 0.164432960 -0.049479163 #> [39,] 0.153890231 -0.0926699875 -0.025621414 -0.121494687 #> [40,] -0.146742186 -0.1892763351 -0.102276477 -0.048861175 #> [41,] -0.274840831 -0.0377048905 0.059701562 0.110453514 #> [42,] -0.008508998 -0.0555288478 -0.052656118 0.006037477 #> [43,] 0.014213371 -0.4104375842 -0.017832766 -0.058840493 #> [44,] -0.080890432 0.0356020878 -0.065307053 -0.003562026 #> [45,] 0.051925330 0.0175766032 -0.016616109 -0.016646624 #> [46,] 0.213031014 -0.0719719280 0.025295461 -0.024318200 #> [47,] 0.175716867 0.1820716574 0.253325834 0.118459579 #> [48,] -0.040155598 -0.1721349330 -0.276607899 -0.058926739 #> [49,] 0.164103981 0.0096690123 -0.109759177 -0.055922804 #> [50,] -0.103164763 0.0179753021 0.228753665 -0.031882016 #> #> Call: #> fastcpd.lasso(data = small_lasso_data, beta = \"BIC\", cost_adjustment = NULL, #> vanilla_percentage = 0.2, pruning_coef = 0) #> #> Change points: #> 80 202 320 #> #> Cost values: #> 15.30678 221.5585 30.87735 48.53091 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.615455985 -2.309283420 4.8750817914 -3.006202485 #> [2,] -2.678202261 -2.259223507 4.2707257840 3.096081968 #> [3,] -4.388362187 1.150652841 5.1265068595 3.718141515 #> [4,] -4.586413963 0.532197076 2.6470978568 -3.130632564 #> [5,] -4.852159806 0.429244510 4.6494743503 3.175562645 #> [6,] -3.823465522 -2.436663308 3.3618026781 4.342890940 #> [7,] 0.116154596 -0.264242115 -0.0727840814 -0.016395368 #> [8,] 0.160209372 -0.109364822 -0.0084325962 -0.022022294 #> [9,] -0.254798925 0.467114039 0.1063025022 0.070683538 #> [10,] -0.012955289 -0.319108692 0.0158283310 0.012791767 #> [11,] 0.018525857 -0.400042642 0.0460933309 -0.166042077 #> [12,] 0.015730602 -0.472837555 0.1629334863 0.077820866 #> [13,] 0.020134823 0.345191970 -0.0685630320 0.077436979 #> [14,] -0.019808550 0.469540855 0.0999989714 0.114046507 #> [15,] -0.040890576 -0.151353540 0.0281131032 -0.015966855 #> [16,] -0.213173204 -0.410553236 -0.0786859735 -0.095751983 #> [17,] 0.002688288 -0.026211969 -0.0836390903 -0.010689927 #> [18,] -0.013245030 0.110930058 -0.0648479795 -0.138057246 #> [19,] 0.153187151 0.519156799 -0.1634987155 0.158967697 #> [20,] -0.029584251 -0.562329939 0.0092242673 0.030169316 #> [21,] 0.100615839 0.571051467 -0.1762195594 -0.181503446 #> [22,] -0.313718826 0.415755188 0.1355664556 0.045649997 #> [23,] 0.246017007 -0.316648014 -0.1498815469 -0.025305755 #> [24,] 0.008216104 0.225545561 -0.0316699448 -0.137396624 #> [25,] -0.325075840 0.679118982 -0.0740016267 0.022336212 #> [26,] -0.057726812 0.117150669 0.0487654853 -0.059782090 #> [27,] 0.001998315 -0.033072437 0.0703865789 0.030431412 #> [28,] -0.019721901 -0.226018232 0.0632399484 -0.080706668 #> [29,] -0.100102752 0.034648703 0.0026192106 -0.051542901 #> [30,] -0.002193672 0.273435850 0.1065809370 0.048892128 #> [31,] 0.096046658 0.202400480 -0.1630891704 0.075434045 #> [32,] 0.151150818 -0.756337115 0.0007249262 0.033375101 #> [33,] 0.041988689 -0.224816396 0.0347939295 0.070983107 #> [34,] 0.053090539 0.656857298 0.1766941914 0.032575571 #> [35,] 0.048963552 -0.523861501 0.0843866955 -0.134534869 #> [36,] 0.426376926 -0.057992972 0.0393842237 -0.020673038 #> [37,] 0.090812237 -0.282803290 -0.0408241929 0.102043216 #> [38,] 0.179330109 -0.048614561 0.0909078160 -0.052763017 #> [39,] 0.138776838 -0.045196412 -0.0771155793 -0.120642848 #> [40,] -0.240934510 -0.179273912 -0.2239376855 -0.045775725 #> [41,] -0.343869848 -0.035394612 0.0549633763 0.123910218 #> [42,] 0.010690576 -0.111879602 0.0832100573 -0.005505113 #> [43,] 0.081490861 -0.376798426 0.0415210753 -0.056131976 #> [44,] -0.171008829 -0.007286424 0.0731242211 0.013524984 #> [45,] 0.047604778 -0.015973066 0.0405922084 -0.033628617 #> [46,] 0.127081043 -0.074390064 -0.0201907706 -0.029655833 #> [47,] 0.138821573 0.144223868 0.1718247772 0.119507574 #> [48,] -0.044021065 -0.180536724 -0.2437603101 -0.054349982 #> [49,] 0.198647494 0.008399368 0.0231802136 -0.056092814 #> [50,] -0.092465571 0.015580790 0.0812578961 -0.032491294 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"fastcpd_ar() fastcpd.ar() wrapper functions fastcpd() find change points AR(\\(p\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"","code":"fastcpd_ar(data, order = 0, ...) fastcpd.ar(data, order = 0, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"data numeric vector, matrix, data frame time series object. order positive integer specifying order AR model. ... arguments passed fastcpd(), example, segment_count. One special argument can passed include.mean, logical value indicating whether mean included model. default value TRUE.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ar.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in AR(\\(p\\)) models — fastcpd_ar","text":"","code":"set.seed(1) n <- 1000 x <- rep(0, n + 3) for (i in 1:600) { x[i + 3] <- 0.6 * x[i + 2] - 0.2 * x[i + 1] + 0.1 * x[i] + rnorm(1, 0, 3) } for (i in 601:1000) { x[i + 3] <- 0.3 * x[i + 2] + 0.4 * x[i + 1] + 0.2 * x[i] + rnorm(1, 0, 3) } result <- fastcpd.ar(x[3 + seq_len(n)], 3) summary(result) #> #> Call: #> fastcpd.ar(data = x[3 + seq_len(n)], order = 3) #> #> Change points: #> 614 #> #> Cost values: #> 2743.759 2028.588 #> #> Parameters: #> segment 1 segment 2 #> 1 0.57120256 0.2371809 #> 2 -0.20985108 0.4031244 #> 3 0.08221978 0.2290323 plot(result)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"fastcpd_arima() fastcpd.arima() wrapper functions fastcpd() find change points ARIMA(\\(p\\), \\(d\\), \\(q\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"","code":"fastcpd_arima(data, order = 0, ...) fastcpd.arima(data, order = 0, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"data numeric vector, matrix, data frame time series object. order vector length three specifying order ARIMA model. ... arguments passed fastcpd(), example, segment_count. One special argument can passed include.mean, logical value indicating whether mean included model. default value TRUE.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arima.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in ARIMA(\\(p\\), \\(d\\), \\(q\\)) models — fastcpd_arima","text":"","code":"# \\donttest{ set.seed(1) n <- 271 w <- rnorm(n + 1, 0, 3) dx <- rep(0, n + 1) x <- rep(0, n + 1) for (i in 1:180) { dx[i + 1] <- 0.8 * dx[i] + w[i + 1] - 0.5 * w[i] x[i + 1] <- x[i] + dx[i + 1] } for (i in 181:n) { dx[i + 1] <- -0.6 * dx[i] + w[i + 1] + 0.3 * w[i] x[i + 1] <- x[i] + dx[i + 1] } result <- fastcpd.arima( diff(x[1 + seq_len(n)]), c(1, 0, 1), segment_count = 3, include.mean = FALSE ) summary(result) #> #> Call: #> fastcpd.arima(data = diff(x[1 + seq_len(n)]), order = c(1, 0, #> 1), segment_count = 3, include.mean = FALSE) #> #> Change points: #> 178 #> #> Cost values: #> 435.4238 227.0583 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"fastcpd_arma() fastcpd.arma() wrapper functions fastcpd() find change points ARMA(\\(p\\), \\(q\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"","code":"fastcpd_arma(data, order = c(0, 0), ...) fastcpd.arma(data, order = c(0, 0), ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"data numeric vector, matrix, data frame time series object. order vector length two specifying order ARMA model. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_arma.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in ARMA(\\(p\\), \\(q\\)) models — fastcpd_arma","text":"","code":"# \\donttest{ set.seed(1) n <- 300 w <- rnorm(n + 3, 0, 3) x <- rep(0, n + 3) for (i in 1:200) { x[i + 3] <- 0.1 * x[i + 2] - 0.3 * x[i + 1] + 0.1 * x[i] + 0.1 * w[i + 2] + 0.5 * w[i + 1] + w[i + 3] } for (i in 201:n) { x[i + 3] <- 0.3 * x[i + 2] + 0.1 * x[i + 1] - 0.3 * x[i] - 0.6 * w[i + 2] - 0.1 * w[i + 1] + w[i + 3] } result <- suppressWarnings( fastcpd.arma( data = x[3 + seq_len(n)], order = c(3, 2), segment_count = 3, lower = c(rep(-1, 3 + 2), 1e-10), upper = c(rep(1, 3 + 2), Inf), line_search = c(1, 0.1, 1e-2), beta = \"BIC\", cost_adjustment = \"BIC\" ) ) summary(result) #> #> Call: #> fastcpd.arma(data = x[3 + seq_len(n)], order = c(3, 2), segment_count = 3, #> lower = c(rep(-1, 3 + 2), 1e-10), upper = c(rep(1, 3 + 2), #> Inf), line_search = c(1, 0.1, 0.01), beta = \"BIC\", cost_adjustment = \"BIC\") #> #> Change points: #> 200 #> #> Cost values: #> 491.6904 251.4816 #> #> Parameters: #> segment 1 segment 2 #> 1 0.001350478 0.74342384 #> 2 -0.828668713 -0.07333667 #> 3 0.200084507 -0.36950210 #> 4 0.208486751 -1.27312849 #> 5 0.999994041 0.45204533 #> 6 7.831992812 8.83494421 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in logistic regression models — fastcpd_binomial","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"fastcpd_binomial() fastcpd.binomial() wrapper functions fastcpd() find change points logistic regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"","code":"fastcpd_binomial(data, ...) fastcpd.binomial(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_binomial.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in logistic regression models — fastcpd_binomial","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 500 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(rnorm(p, 0, 1), rnorm(p, 2, 1)) y <- c( rbinom(300, 1, 1 / (1 + exp(-x[1:300, ] %*% theta[1, ]))), rbinom(200, 1, 1 / (1 + exp(-x[301:n, ] %*% theta[2, ]))) ) result <- suppressWarnings(fastcpd.binomial(cbind(y, x))) summary(result) plot(result) } #> #> Call: #> fastcpd.binomial(data = cbind(y, x)) #> #> Change points: #> 302 #> #> Cost values: #> 124.4554 54.26381 #> #> Parameters: #> segment 1 segment 2 #> 1 -0.9260182 2.1294962 #> 2 -1.6033835 2.7583247 #> 3 1.0343338 2.3818010 #> 4 0.3653870 0.7261152"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_family.html","id":null,"dir":"Reference","previous_headings":"","what":"Wrapper functions for fastcpd — fastcpd_family","title":"Wrapper functions for fastcpd — fastcpd_family","text":"Wrapper functions fastcpd find change points various models.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"fastcpd_garch() fastcpd.garch() wrapper functions fastcpd() find change points GARCH(\\(p\\), \\(q\\)) models. function similar fastcpd() except data default one-column matrix univariate vector thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"","code":"fastcpd_garch(data, order = c(0, 0), ...) fastcpd.garch(data, order = c(0, 0), ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"data numeric vector, matrix, data frame time series object. order positive integer vector length two specifying order GARCH model. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_garch.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in GARCH(\\(p\\), \\(q\\)) models — fastcpd_garch","text":"","code":"# \\donttest{ set.seed(1) n <- 400 sigma_2 <- rep(1, n + 1) x <- rep(0, n + 1) for (i in seq_len(200)) { sigma_2[i + 1] <- 20 + 0.5 * x[i]^2 + 0.1 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } for (i in 201:400) { sigma_2[i + 1] <- 1 + 0.1 * x[i]^2 + 0.5 * sigma_2[i] x[i + 1] <- rnorm(1, 0, sqrt(sigma_2[i + 1])) } result <- suppressWarnings( fastcpd.garch(x[-1], c(1, 1), include.mean = FALSE) ) summary(result) #> #> Call: #> fastcpd.garch(data = x[-1], order = c(1, 1), include.mean = FALSE) #> #> Change points: #> 205 #> #> Cost values: #> 449.6614 178.5686 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"fastcpd_lasso() fastcpd.lasso() wrapper functions fastcpd() find change points penalized linear regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"","code":"fastcpd_lasso(data, ...) fastcpd.lasso(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lasso.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in penalized linear regression models — fastcpd_lasso","text":"","code":"# \\donttest{ if ( requireNamespace(\"dplyr\", quietly = TRUE) && requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"mvtnorm\", quietly = TRUE) && requireNamespace(\"reshape2\", quietly = TRUE) ) { set.seed(1) n <- 480 p_true <- 5 p <- 50 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind( runif(p_true, -5, -2), runif(p_true, -3, 3), runif(p_true, 2, 5), runif(p_true, -5, 5) ) theta_0 <- cbind(theta_0, matrix(0, ncol = p - p_true, nrow = 4)) y <- c( x[1:80, ] %*% theta_0[1, ] + rnorm(80, 0, 1), x[81:200, ] %*% theta_0[2, ] + rnorm(120, 0, 1), x[201:320, ] %*% theta_0[3, ] + rnorm(120, 0, 1), x[321:n, ] %*% theta_0[4, ] + rnorm(160, 0, 1) ) result <- fastcpd.lasso( cbind(y, x), multiple_epochs = function(segment_length) if (segment_length < 30) 1 else 0 ) summary(result) plot(result) thetas <- result@thetas thetas <- cbind.data.frame(thetas, t(theta_0)) names(thetas) <- c( \"segment 1\", \"segment 2\", \"segment 3\", \"segment 4\", \"segment 1 truth\", \"segment 2 truth\", \"segment 3 truth\", \"segment 4 truth\" ) thetas$coordinate <- c(seq_len(p_true), rep(\"rest\", p - p_true)) molten <- reshape2::melt(thetas, id.vars = \"coordinate\") molten <- dplyr::mutate( molten, segment = gsub(\"segment \", \"\", variable), segment = gsub(\" truth\", \"\", segment), height = as.numeric(gsub(\"segment.*\", \"\", segment)) + 0.2 * as.numeric(grepl(\"truth\", variable)), parameter = ifelse(grepl(\"truth\", variable), \"truth\", \"estimated\") ) ggplot2::ggplot() + ggplot2::geom_point( data = molten, ggplot2::aes( x = value, y = height, shape = coordinate, color = parameter ), size = 4 ) + ggplot2::ylim(0.8, 4.4) + ggplot2::ylab(\"segment\") + ggplot2::theme_bw() } #> #> Call: #> fastcpd.lasso(data = cbind(y, x), multiple_epochs = function(segment_length) if (segment_length < #> 30) 1 else 0) #> #> Change points: #> 80 200 320 #> #> Cost values: #> -33.53772 -2.964751 -4.538601 19.48282 #> #> Parameters: #> 50 x 4 sparse Matrix of class \"dgCMatrix\" #> segment 1 segment 2 segment 3 segment 4 #> [1,] -2.687893678 -1.347364754 3.65464618 -2.68320467 #> [2,] -2.641775791 -2.722142529 2.25369487 3.91484879 #> [3,] -4.297655293 -2.279064483 4.73193630 -1.25039274 #> [4,] -4.356637349 1.117644847 4.19753119 -3.02828978 #> [5,] -4.563162167 -0.040732304 5.12149272 2.94566719 #> [6,] -0.037544892 0.093563258 0.06929427 0.19337556 #> [7,] -0.032180502 0.183459849 0.07694356 0.08343640 #> [8,] -0.121340500 0.143508371 -0.14206767 0.20150137 #> [9,] 0.228114500 0.087096913 -0.12489127 -0.05441234 #> [10,] 0.249594205 0.127790123 0.01855028 0.06403369 #> [11,] 0.121185946 -0.082768796 -0.04036376 -0.06970833 #> [12,] 0.086436979 -0.074336995 -0.25546800 -0.14816071 #> [13,] 0.033878715 0.164515279 0.08546067 0.07043841 #> [14,] -0.116324402 -0.147577009 0.02165927 -0.03678181 #> [15,] 0.088055326 0.040483693 0.18363877 0.07635422 #> [16,] -0.006839643 0.145071723 -0.01270428 -0.03708197 #> [17,] 0.042459016 0.004187653 0.02101507 -0.02227376 #> [18,] -0.044886170 -0.121886946 -0.06794188 0.02104911 #> [19,] 0.064254844 0.025662919 0.06775764 -0.06292499 #> [20,] 0.264382648 -0.030473566 0.09266098 0.05610440 #> [21,] -0.103292295 -0.024421785 0.02619333 -0.13185732 #> [22,] 0.224819510 0.117787479 -0.26153170 -0.09608036 #> [23,] -0.127726743 -0.113070877 0.09848171 -0.04150170 #> [24,] 0.185177947 0.007586467 -0.13529912 0.07395925 #> [25,] 0.132166684 -0.033427156 0.15292622 -0.10491541 #> [26,] -0.204139455 -0.284791058 0.12597351 -0.01359206 #> [27,] -0.203091983 -0.079616474 -0.12028823 -0.07730226 #> [28,] 0.163081293 0.012938143 0.01038811 0.19519095 #> [29,] 0.215170449 -0.135372096 0.03345574 -0.19036979 #> [30,] 0.199697345 -0.024836934 -0.10536087 0.01297619 #> [31,] 0.093075706 0.008479655 0.10480234 0.12841112 #> [32,] -0.231128671 0.056259142 0.07898864 0.00344448 #> [33,] 0.321769381 -0.143982228 -0.10975100 0.06738358 #> [34,] 0.089703182 -0.116702312 0.09320772 -0.10698341 #> [35,] 0.139247995 0.063062479 -0.10850835 0.15595021 #> [36,] 0.129089775 0.077552759 0.13091543 -0.15788233 #> [37,] -0.167999629 -0.040832358 -0.06478776 0.16724727 #> [38,] -0.126037849 0.110736887 -0.15924920 -0.04970495 #> [39,] -0.211132385 -0.229198706 -0.09567662 0.06684922 #> [40,] 0.321557058 -0.256192844 0.07924873 -0.03288319 #> [41,] 0.429749205 0.026979297 -0.07309375 -0.05840023 #> [42,] -0.011319985 -0.011363023 0.06767687 -0.04725431 #> [43,] 0.234802656 -0.019449223 0.13028901 -0.03556068 #> [44,] 0.184529802 0.020282379 -0.20988569 -0.03096108 #> [45,] -0.384355502 0.172850905 0.08402851 -0.02255405 #> [46,] 0.223274950 0.132941542 0.12073814 0.10020210 #> [47,] -0.313123582 -0.202596128 0.05480104 -0.07697244 #> [48,] -0.376893471 0.007438720 0.09792671 0.02638471 #> [49,] 0.089255534 -0.011341307 -0.08469776 -0.02137857 #> [50,] -0.041427938 0.181537606 -0.17195726 -0.10186665 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in linear regression models — fastcpd_lm","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"fastcpd_lm() fastcpd.lm() wrapper functions fastcpd() find change points linear regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"","code":"fastcpd_lm(data, ...) fastcpd.lm(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_lm.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in linear regression models — fastcpd_lm","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_0 <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta_0[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta_0[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta_0[3, ] + rnorm(100, 0, 3) ) result_lm <- fastcpd.lm(cbind(y, x)) summary(result_lm) plot(result_lm) set.seed(1) n <- 600 p <- 4 d <- 2 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta_1 <- matrix(runif(8, -3, -1), nrow = p) theta_2 <- matrix(runif(8, -1, 3), nrow = p) y <- rbind( x[1:350, ] %*% theta_1 + mvtnorm::rmvnorm(350, rep(0, d), 3 * diag(d)), x[351:n, ] %*% theta_2 + mvtnorm::rmvnorm(250, rep(0, d), 3 * diag(d)) ) result_mlm <- fastcpd.lm(cbind.data.frame(y = y, x = x), p.response = 2) summary(result_mlm) } #> #> Call: #> fastcpd.lm(data = cbind(y, x)) #> #> Change points: #> 97 201 #> #> Cost values: #> 517.5695 412.9626 459.8569 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 0.74291290 -0.6153049 0.8733473 #> 2 3.69465275 -0.5034948 0.3222868 #> 3 -1.24746871 2.2522352 1.0188455 #> 4 0.09579985 -1.9875126 2.2761340 #> #> Call: #> fastcpd.lm(data = cbind.data.frame(y = y, x = x), p.response = 2) #> #> Change points: #> 350 #> #> Cost values: #> 1405.82 993.8667"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in mean change models — fastcpd_mean","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"fastcpd_mean() fastcpd.mean() wrapper functions fastcpd() find mean change. function similar fastcpd() except data default matrix data frame vector row / element observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"","code":"fastcpd_mean(data, ...) fastcpd.mean(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"data matrix, data frame vector. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_mean.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in mean change models — fastcpd_mean","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 3 data <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) result <- fastcpd.mean(data) summary(result) } #> #> Call: #> fastcpd.mean(data = data) #> #> Change points: #> 300 700 #> #> Cost values: #> 2558.102 3417.845 2551.517 set.seed(1) data <- c(rnorm(10000), rnorm(1000) + 1) (result_time <- system.time( result <- fastcpd.mean(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 0.102 0.114 0.075 result@cp_set #> [1] 10007 # \\donttest{ set.seed(1) data <- c(rnorm(10000), rnorm(10000, 1), rnorm(10000)) (result_time <- system.time( result <- fastcpd.mean(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 0.569 0.285 0.528 result@cp_set #> [1] 10007 19998 # } # \\donttest{ set.seed(1) data <- c(rnorm(10000), rnorm(10000, 1), rnorm(10000)) (result_time <- system.time( result <- fastcpd.mean( data, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE, cp_only = TRUE ) )) #> user system elapsed #> 0.241 0.268 0.189 result@cp_set #> [1] 10034 20082 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"fastcpd_meanvariance(), fastcpd.meanvariance(), fastcpd_mv(), fastcpd.mv() wrapper functions fastcpd() find meanvariance change. function similar fastcpd() except data default matrix data frame vector row / element observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"","code":"fastcpd_meanvariance(data, ...) fastcpd.meanvariance(data, ...) fastcpd_mv(data, ...) fastcpd.mv(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"data matrix, data frame vector. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_meanvariance.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in mean variance change models — fastcpd_meanvariance","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 1 result <- fastcpd.mv( rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) ) summary(result) plot(result) } #> #> Call: #> fastcpd.mv(data = rbind(mvtnorm::rmvnorm(300, mean = rep(0, p), #> sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, #> p), sigma = diag(100, p)))) #> #> Change points: #> 300 700 1001 1300 1700 #> #> Cost values: #> 412.1898 586.8914 1139.531 433.9691 562.2071 1142.934 if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 4 result <- fastcpd.mv( rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, p), sigma = diag(100, p)) ) ) summary(result) } #> #> Call: #> fastcpd.mv(data = rbind(mvtnorm::rmvnorm(300, mean = rep(0, p), #> sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(0, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(400, mean = rep(10, #> p), sigma = diag(1, p)), mvtnorm::rmvnorm(300, mean = rep(10, #> p), sigma = diag(100, p)))) #> #> Change points: #> 300 700 1000 1300 1700 #> #> Cost values: #> 1714.968 2299.119 4500.027 1654.917 2259.023 4444.359 # \\donttest{ set.seed(1) data <- c(rnorm(2000, 0, 1), rnorm(2000, 1, 1), rnorm(2000, 1, 2)) (result_time <- system.time( result <- fastcpd.variance(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 1.405 0.000 1.405 result@cp_set #> [1] 1985 4000 # } # \\donttest{ set.seed(1) data <- c(rnorm(2000, 0, 1), rnorm(2000, 1, 1), rnorm(2000, 1, 2)) (result_time <- system.time( result <- fastcpd.variance( data, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = TRUE, cp_only = TRUE ) )) #> user system elapsed #> 1.143 0.001 1.144 result@cp_set #> [1] 1985 4000 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in Poisson regression models — fastcpd_poisson","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"fastcpd_poisson() fastcpd.poisson() wrapper functions fastcpd() find change points Poisson regression models. function similar fastcpd() except data default matrix data frame response variable first column thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"","code":"fastcpd_poisson(data, ...) fastcpd.poisson(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"data matrix data frame response variable first column. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_poisson.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in Poisson regression models — fastcpd_poisson","text":"","code":"# \\donttest{ if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 1100 p <- 3 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) delta <- rnorm(p) theta_0 <- c(1, 0.3, -1) y <- c( rpois(500, exp(x[1:500, ] %*% theta_0)), rpois(300, exp(x[501:800, ] %*% (theta_0 + delta))), rpois(200, exp(x[801:1000, ] %*% theta_0)), rpois(100, exp(x[1001:1100, ] %*% (theta_0 - delta))) ) result <- fastcpd.poisson(cbind(y, x)) summary(result) plot(result) } #> #> Call: #> fastcpd.poisson(data = cbind(y, x)) #> #> Change points: #> 498 805 1003 #> #> Cost values: #> 230.0866 190.1381 82.77324 38.45199 #> #> Parameters: #> segment 1 segment 2 segment 3 segment 4 #> 1 1.020002 0.6391880 1.0424108 1.4451928 #> 2 0.275458 -0.2585056 0.2620085 0.9910079 #> 3 -1.048875 -0.5768481 -0.9632918 -1.4354638 # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in time series data — fastcpd_ts","title":"Find change points efficiently in time series data — fastcpd_ts","text":"fastcpd_ts() fastcpd.ts() wrapper functions fastcpd() find change points time series data. function similar fastcpd() except data time series family one \"ar\", \"var\", \"arma\", \"arima\" \"garch\".","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in time series data — fastcpd_ts","text":"","code":"fastcpd_ts(data, family = NULL, order = c(0, 0, 0), ...) fastcpd.ts(data, family = NULL, order = c(0, 0, 0), ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in time series data — fastcpd_ts","text":"data numeric vector, matrix, data frame time series object. family character string specifying family time series. value one \"ar\", \"var\", \"arima\" \"garch\". order positive integer vector length less four specifying order time series. Possible combinations family : \"ar\", NUMERIC(1): AR(\\(p\\)) model using linear regression. \"var\", NUMERIC(1): VAR(\\(p\\)) model using linear regression. \"arima\", NUMERIC(3): ARIMA(\\(p\\), \\(d\\), \\(q\\)) model using forecast::Arima(). \"garch\", NUMERIC(2): GARCH(\\(p\\), \\(q\\)) model using tseries::garch(). ... arguments passed fastcpd(), example, segment_count. One special argument can passed include.mean, logical value indicating whether mean included model. default value TRUE.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in time series data — fastcpd_ts","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_ts.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in time series data — fastcpd_ts","text":"","code":"# \\donttest{ set.seed(1) n <- 400 w <- rnorm(n + 4, 0, 0.1) x <- rep(NA, n) for (i in 1:200) { x[i] <- w[i + 4] - 5 / 3 * w[i + 3] + 11 / 12 * w[i + 2] - 5 / 12 * w[i + 1] + 1 / 6 * w[i] } for (i in 201:n) { x[i] <- w[i + 4] - 4 / 3 * w[i + 3] + 7 / 9 * w[i + 2] - 16 / 27 * w[i + 1] + 4 / 27 * w[i] } result <- fastcpd.ts( x, \"arma\", c(0, 4), lower = c(-2, -2, -2, -2, 1e-10), upper = c(2, 2, 2, 2, Inf), line_search = c(1, 0.1, 1e-2), trim = 0.05 ) summary(result) #> #> Call: #> fastcpd.ts(data = x, family = \"arma\", order = c(0, 4), lower = c(-2, #> -2, -2, -2, 1e-10), upper = c(2, 2, 2, 2, Inf), line_search = c(1, #> 0.1, 0.01), trim = 0.05) #> #> Change points: #> 209 #> #> Cost values: #> -188.7341 -168.4595 #> #> Parameters: #> segment 1 segment 2 #> 1 -1.626949633 -1.366413227 #> 2 0.925842329 0.850647424 #> 3 -0.472212640 -0.677337192 #> 4 0.181290578 0.193108171 #> 5 0.009523233 0.009914231 plot(result) # }"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"fastcpd_var() fastcpd.var() wrapper functions fastcpd_ts() find change points VAR(\\(p\\)) models. function similar fastcpd_ts() except data default matrix row observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"","code":"fastcpd_var(data, order = 0, ...) fastcpd.var(data, order = 0, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"data matrix, data frame time series object. order positive integer specifying order VAR model. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_var.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in VAR(\\(p\\)) models — fastcpd_var","text":"","code":"set.seed(1) n <- 300 p <- 2 theta_1 <- matrix(c(-0.3, 0.6, -0.5, 0.4, 0.2, 0.2, 0.2, -0.2), nrow = p) theta_2 <- matrix(c(0.3, -0.4, 0.1, -0.5, -0.5, -0.2, -0.5, 0.2), nrow = p) x <- matrix(0, n + 2, p) for (i in 1:200) { x[i + 2, ] <- theta_1 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } for (i in 201:n) { x[i + 2, ] <- theta_2 %*% c(x[i + 1, ], x[i, ]) + rnorm(p, 0, 1) } result <- fastcpd.var(x, 2) summary(result) #> #> Call: #> fastcpd.var(data = x, order = 2) #> #> Change points: #> 204 #> #> Cost values: #> 561.1008 283.7941"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":null,"dir":"Reference","previous_headings":"","what":"Find change points efficiently in variance change models — fastcpd_variance","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"fastcpd_variance() fastcpd.variance() wrapper functions fastcpd() find variance change. function similar fastcpd() except data default matrix data frame vector row / element observation thus formula required .","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"","code":"fastcpd_variance(data, ...) fastcpd.variance(data, ...)"},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"data matrix, data frame vector. ... arguments passed fastcpd(), example, segment_count.","code":""},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"fastcpd object.","code":""},{"path":[]},{"path":"https://fastcpd.xingchi.li/reference/fastcpd_variance.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Find change points efficiently in variance change models — fastcpd_variance","text":"","code":"set.seed(1) data <- c(rnorm(300, 0, 1), rnorm(400, 0, 100), rnorm(300, 0, 1)) result <- fastcpd.variance(data) summary(result) #> #> Call: #> fastcpd.variance(data = data) #> #> Change points: #> 300 700 #> #> Cost values: #> 703.3755 2430.33 705.6112 if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 3 result <- fastcpd.variance( rbind( mvtnorm::rmvnorm( 300, rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p)) ), mvtnorm::rmvnorm( 400, rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p)) ), mvtnorm::rmvnorm( 300, rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p)) ) ) ) summary(result) } #> #> Call: #> fastcpd.variance(data = rbind(mvtnorm::rmvnorm(300, rep(0, p), #> crossprod(matrix(runif(p^2) * 2 - 1, p))), mvtnorm::rmvnorm(400, #> rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p))), mvtnorm::rmvnorm(300, #> rep(0, p), crossprod(matrix(runif(p^2) * 2 - 1, p))))) #> #> Change points: #> 300 700 #> #> Cost values: #> 753.6359 1706.144 1280.607 # \\donttest{ set.seed(1) data <- c(rnorm(3000, 0, 1), rnorm(3000, 0, 2), rnorm(3000, 0, 1)) (result_time <- system.time( result <- fastcpd.variance(data, r.progress = FALSE, cp_only = TRUE) )) #> user system elapsed #> 2.09 0.00 2.09 result@cp_set #> [1] 2997 5989 # } # \\donttest{ set.seed(1) data <- c(rnorm(3000, 0, 1), rnorm(3000, 0, 2), rnorm(3000, 0, 1)) (result_time <- system.time( result <- fastcpd.variance( data, beta = \"BIC\", cost_adjustment = \"BIC\", r.progress = FALSE, cp_only = TRUE ) )) #> user system elapsed #> 2.033 0.000 2.033 result@cp_set #> [1] 2997 5989 # }"},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":null,"dir":"Reference","previous_headings":"","what":"Occupancy Detection Data Set — occupancy","title":"Occupancy Detection Data Set — occupancy","text":"Data set binary classification room occupancy temperature, humidity, light CO2 measurements. Ground-truth occupancy obtained time stamped pictures taken every minute.","code":""},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Occupancy Detection Data Set — occupancy","text":"","code":"occupancy"},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Occupancy Detection Data Set — occupancy","text":"data frame 9752 rows 7 variables: date Character format \"YYYY-MM-DD hh:mm:ss\" 2015-02-11 14:48:00 2015-02-18 09:19:00 Temperature Temperature Celsius Humidity Humidity Light Light CO2 CO2 HumidityRatio Humidity Ratio Occupancy Binary variable values 0 (unoccupied) 1","code":""},{"path":"https://fastcpd.xingchi.li/reference/occupancy.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Occupancy Detection Data Set — occupancy","text":"","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":null,"dir":"Reference","previous_headings":"","what":"Plot the data and the change points for a fastcpd object — plot.fastcpd","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"Plot data change points fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"","code":"# S3 method for fastcpd plot( x, color_max_count = Inf, data_point_alpha = 0.8, data_point_linewidth = 0.5, data_point_size = 1, legend_position = \"none\", panel_background = ggplot2::element_blank(), panel_border = ggplot2::element_rect(fill = NA, colour = \"grey20\"), panel_grid_major = ggplot2::element_line(colour = \"grey98\"), panel_grid_minor = ggplot2::element_line(colour = \"grey98\"), segment_separator_alpha = 0.8, segment_separator_color = \"grey\", segment_separator_linetype = \"dashed\", strip_background = ggplot2::element_rect(fill = \"grey85\", colour = \"grey20\"), xlab = NULL, ylab = NULL, ... ) # S4 method for fastcpd,missing plot( x, color_max_count = Inf, data_point_alpha = 0.8, data_point_linewidth = 0.5, data_point_size = 1, legend_position = \"none\", panel_background = ggplot2::element_blank(), panel_border = ggplot2::element_rect(fill = NA, colour = \"grey20\"), panel_grid_major = ggplot2::element_line(colour = \"grey98\"), panel_grid_minor = ggplot2::element_line(colour = \"grey98\"), segment_separator_alpha = 0.8, segment_separator_color = \"grey\", segment_separator_linetype = \"dashed\", strip_background = ggplot2::element_rect(fill = \"grey85\", colour = \"grey20\"), xlab = NULL, ylab = NULL, ... )"},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"x fastcpd object. color_max_count Maximum number colors use plotting segments. data_point_alpha Alpha data points. data_point_linewidth Linewidth data points. data_point_size Size data points. legend_position Position legend. panel_background Background panel. panel_border Border panel. panel_grid_major Major grid lines panel. panel_grid_minor Minor grid lines panel. segment_separator_alpha Alpha segment separator lines. segment_separator_color Color segment separator lines. segment_separator_linetype Linetype segment separator lines. strip_background Background strip. xlab Label x-axis. ylab Label y-axis. ... Ignored.","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"return value, called plotting.","code":""},{"path":"https://fastcpd.xingchi.li/reference/plot.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Plot the data and the change points for a fastcpd object — plot.fastcpd","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 1 x <- mvtnorm::rmvnorm(300, rep(0, p), diag(p)) theta_0 <- matrix(c(1, -1, 0.5)) y <- c( x[1:100, ] * theta_0[1, ] + rnorm(100, 0, 1), x[101:200, ] * theta_0[2, ] + rnorm(100, 0, 1), x[201:300, ] * theta_0[3, ] + rnorm(100, 0, 1) ) result <- fastcpd.lm(cbind(y, x), r.clock = \"fastcpd_profiler\") summary(result) plot(result) if (requireNamespace(\"RcppClock\", quietly = TRUE)) { library(RcppClock) plot(fastcpd_profiler) } } #> #> Call: #> fastcpd.lm(data = cbind(y, x), r.clock = \"fastcpd_profiler\") #> #> Change points: #> 100 201 #> #> Cost values: #> 48.16996 66.1816 45.66268 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 0.9520606 -0.8307605 0.4593161"},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":null,"dir":"Reference","previous_headings":"","what":"Print the call and the change points for a fastcpd object — print.fastcpd","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"Print call change points fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"","code":"# S3 method for fastcpd print(x, ...) # S4 method for fastcpd print(x, ...)"},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"x fastcpd object. ... Ignored.","code":""},{"path":"https://fastcpd.xingchi.li/reference/print.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Print the call and the change points for a fastcpd object — print.fastcpd","text":"Return (temporarily) invisible copy fastcpd object. Called primarily printing change points model.","code":""},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":null,"dir":"Reference","previous_headings":"","what":"Show the available methods for a fastcpd object — show.fastcpd","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"Show available methods fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"","code":"# S3 method for fastcpd show(object) # S4 method for fastcpd show(object)"},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"object fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/show.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Show the available methods for a fastcpd object — show.fastcpd","text":"return value, called showing list available methods fastcpd object.","code":""},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":null,"dir":"Reference","previous_headings":"","what":"Show the summary of a fastcpd object — summary.fastcpd","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"Show summary fastcpd object","code":""},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"","code":"# S3 method for fastcpd summary(object, ...) # S4 method for fastcpd summary(object, ...)"},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"object fastcpd object. ... Ignored.","code":""},{"path":"https://fastcpd.xingchi.li/reference/summary.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Show the summary of a fastcpd object — summary.fastcpd","text":"Return (temporarily) invisible copy fastcpd object. Called primarily printing summary model including call, change points, cost values estimated parameters.","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":null,"dir":"Reference","previous_headings":"","what":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"Transcriptome analysis 57 bladder carcinomas Affymetrix HG-U95A HG-U95Av2 microarrays","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"","code":"transcriptome"},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"data frame 2215 rows 43 variables: 3 Individual 3 4 Individual 4 5 Individual 5 6 Individual 6 7 Individual 7 8 Individual 8 9 Individual 9 10 Individual 10 14 Individual 14 15 Individual 15 16 Individual 16 17 Individual 17 18 Individual 18 19 Individual 19 21 Individual 21 22 Individual 22 24 Individual 24 26 Individual 26 28 Individual 28 30 Individual 30 31 Individual 31 33 Individual 33 34 Individual 34 35 Individual 35 36 Individual 36 37 Individual 37 38 Individual 38 39 Individual 39 40 Individual 40 41 Individual 41 42 Individual 42 43 Individual 43 44 Individual 44 45 Individual 45 46 Individual 46 47 Individual 47 48 Individual 48 49 Individual 49 50 Individual 50 51 Individual 51 53 Individual 53 54 Individual 54 57 Individual 57","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":" ","code":""},{"path":"https://fastcpd.xingchi.li/reference/transcriptome.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Transcription Profiling of 57 Human Bladder Carcinoma Samples — transcriptome","text":"","code":"# \\donttest{ if (requireNamespace(\"ggplot2\", quietly = TRUE)) { result <- fastcpd.mean(transcriptome$\"10\", trim = 0.005) summary(result) plot(result) result_all <- fastcpd.mean( transcriptome, beta = (ncol(transcriptome) + 1) * log(nrow(transcriptome)) / 2 * 5, trim = 0 ) plots <- lapply( seq_len(ncol(transcriptome)), function(i) { ggplot2::ggplot( data = data.frame( x = seq_along(transcriptome[, i]), y = transcriptome[, i] ), ggplot2::aes(x = x, y = y) ) + ggplot2::geom_line(color = \"steelblue\") + ggplot2::geom_vline( xintercept = result_all@cp_set, color = \"red\", linetype = \"dotted\", linewidth = 0.5, alpha = 0.7 ) + ggplot2::theme_void() } ) if (requireNamespace(\"gridExtra\", quietly = TRUE)) { gridExtra::grid.arrange(grobs = plots, ncol = 1, nrow = ncol(transcriptome)) } } #> #> Call: #> fastcpd.mean(data = transcriptome$\"10\", trim = 0.005) #> #> Change points: #> 178 264 401 534 601 656 788 811 869 934 971 1055 1142 1286 1319 1386 1657 1724 1906 1972 1996 2041 #> #> Cost values: #> -312.3778 -152.8685 -223.128 -194.7351 28.05096 -110.2493 -234.4602 -48.13672 -104.2513 -95.29414 -34.57256 -118.7807 -135.2328 -228.7489 -66.44221 -93.27936 -388.8495 141.1922 -326.8845 -94.17653 -10.70687 -28.44282 -267.2412 # }"},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":null,"dir":"Reference","previous_headings":"","what":"UK Seatbelts Data — uk_seatbelts","title":"UK Seatbelts Data — uk_seatbelts","text":"Road Casualties Great Britain 1969–84.","code":""},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"UK Seatbelts Data — uk_seatbelts","text":"","code":"uk_seatbelts"},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"UK Seatbelts Data — uk_seatbelts","text":"uk_seatbelts multiple time series, columns DriversKilled car drivers killed. front front-seat passengers killed seriously injured. rear rear-seat passengers killed seriously injured. kms distance driven. PetrolPrice petrol price. VanKilled number van (‘light goods vehicle’) drivers. law 0/1: law effect month?","code":""},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"UK Seatbelts Data — uk_seatbelts","text":"R package datasets","code":""},{"path":"https://fastcpd.xingchi.li/reference/uk_seatbelts.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"UK Seatbelts Data — uk_seatbelts","text":"","code":"if ( requireNamespace(\"ggplot2\", quietly = TRUE) && requireNamespace(\"lubridate\", quietly = TRUE) && requireNamespace(\"zoo\", quietly = TRUE) ) { result_ar <- fastcpd.ar(diff(uk_seatbelts[, \"drivers\"], 12), 1, beta = \"BIC\") summary(result_ar) plot(result_ar) result_lm <- suppressMessages(fastcpd.lm( diff(uk_seatbelts[, c(\"drivers\", \"kms\", \"PetrolPrice\", \"law\")], lag = 12) )) cp_dates <- as.Date(\"1969-01-01\", format = \"%Y-%m-%d\") cp_dates <- cp_dates + lubridate::period(month = 1 + result_lm@cp_set + 12) cp_dates <- zoo::as.yearmon(cp_dates) dates <- zoo::as.yearmon(time(uk_seatbelts)) uk_seatbelts_df <- data.frame( dates = dates, drivers = c(uk_seatbelts[, \"drivers\"]), color = as.factor((dates < cp_dates[1]) + (dates < cp_dates[2])) ) ggplot2::ggplot() + ggplot2::geom_line( data = uk_seatbelts_df, mapping = ggplot2::aes(x = dates, y = drivers, color = color) ) + ggplot2::geom_vline( xintercept = cp_dates, linetype = \"dashed\", color = \"red\" ) + zoo::scale_x_yearmon() + ggplot2::annotate( \"text\", x = cp_dates, y = 1025, label = as.character(cp_dates), color = \"blue\" ) + ggplot2::theme_bw() + ggplot2::theme(legend.position = \"none\") } #> #> Call: #> fastcpd.ar(data = diff(uk_seatbelts[, \"drivers\"], 12), order = 1, #> beta = \"BIC\") #> #> Change points: #> 71 158 #> #> Cost values: #> 1294597 914674.5 310957 #> #> Parameters: #> segment 1 segment 2 segment 3 #> 1 0.5543967 -0.07501079 0.7825427"},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for ARMA model with change points — variance_arma","title":"Variance estimation for ARMA model with change points — variance_arma","text":"Estimate variance block take average.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for ARMA model with change points — variance_arma","text":"","code":"variance_arma(data, p, q, max_order = p * q) variance.arma(data, p, q, max_order = p * q)"},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for ARMA model with change points — variance_arma","text":"data one-column matrix vector. p order autoregressive part. q order moving average part. max_order maximum order AR model consider.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for ARMA model with change points — variance_arma","text":"numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_arma.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for ARMA model with change points — variance_arma","text":"","code":"set.seed(1) n <- 300 w <- rnorm(n + 3, 0, 10) x <- rep(0, n + 3) for (i in 1:200) { x[i + 3] <- 0.1 * x[i + 2] - 0.3 * x[i + 1] + 0.1 * x[i] + 0.1 * w[i + 2] + 0.5 * w[i + 1] + w[i + 3] } for (i in 201:n) { x[i + 3] <- 0.3 * x[i + 2] + 0.1 * x[i + 1] - 0.3 * x[i] - 0.6 * w[i + 2] - 0.1 * w[i + 1] + w[i + 3] } (result <- variance.arma(x[-seq_len(3)], p = 3, q = 2)) #> $table #> sigma2 AIC BIC #> AR(1) 104.8647 4.659337 4.671683 #> AR(2) 100.2594 4.621094 4.645786 #> AR(3) 115.3102 4.767626 4.804664 #> AR(4) 105.4397 4.684806 4.734190 #> AR(5) 115.4258 4.781962 4.843691 #> AR(6) 117.0668 4.802745 4.876821 #> #> $sigma2_aic #> [1] 100.2594 #> #> $sigma2_bic #> [1] 100.2594 #>"},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for linear models with change points — variance_lm","title":"Variance estimation for linear models with change points — variance_lm","text":"Estimate variance block take average.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for linear models with change points — variance_lm","text":"","code":"variance_lm(data, d = 1, block_size = ncol(data) - d + 1, outlier_iqr = Inf) variance.lm(data, d = 1, block_size = ncol(data) - d + 1, outlier_iqr = Inf)"},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for linear models with change points — variance_lm","text":"data matrix data frame response variable first column. d dimension response variable. block_size size blocks use variance estimation. outlier_iqr number interquartile ranges use threshold outlier detection.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for linear models with change points — variance_lm","text":"numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_lm.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for linear models with change points — variance_lm","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) n <- 300 p <- 4 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- rbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) y <- c( x[1:100, ] %*% theta[1, ] + rnorm(100, 0, 3), x[101:200, ] %*% theta[2, ] + rnorm(100, 0, 3), x[201:n, ] %*% theta[3, ] + rnorm(100, 0, 3) ) (sigma2 <- variance.lm(cbind(y, x))) set.seed(1) n <- 300 p <- 4 d <- 2 x <- mvtnorm::rmvnorm(n, rep(0, p), diag(p)) theta <- cbind(c(1, 3.2, -1, 0), c(-1, -0.5, 2.5, -2), c(0.8, 0, 1, 2)) theta <- cbind(theta, theta) y <- rbind( x[1:100, ] %*% theta[, 1:2] + mvtnorm::rmvnorm(100, rep(0, d), diag(3, d)), x[101:200, ] %*% theta[, 3:4] + mvtnorm::rmvnorm(100, rep(0, d), diag(3, d)), x[201:n, ] %*% theta[, 5:6] + mvtnorm::rmvnorm(100, rep(0, d), diag(3, d)) ) (sigma <- variance.lm(cbind(y, x), d = d)) } #> [,1] [,2] #> [1,] 3.71946553 0.01962422 #> [2,] 0.03459023 3.59825273"},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for mean change models — variance_mean","title":"Variance estimation for mean change models — variance_mean","text":"Implement Rice estimator variance mean change models.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for mean change models — variance_mean","text":"","code":"variance_mean(data) variance.mean(data)"},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for mean change models — variance_mean","text":"data matrix data frame data points row.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for mean change models — variance_mean","text":"matrix representing variance-covariance matrix numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_mean.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for mean change models — variance_mean","text":"","code":"if (requireNamespace(\"mvtnorm\", quietly = TRUE)) { set.seed(1) p <- 3 data <- rbind( mvtnorm::rmvnorm(300, mean = rep(0, p), sigma = diag(100, p)), mvtnorm::rmvnorm(400, mean = rep(50, p), sigma = diag(100, p)), mvtnorm::rmvnorm(300, mean = rep(2, p), sigma = diag(100, p)) ) (sigma <- variance.mean(data)) } #> [,1] [,2] [,3] #> [1,] 113.5442077 -0.1433207 -2.973472 #> [2,] -0.1433207 106.1627730 8.430343 #> [3,] -2.9734719 8.4303433 112.602989"},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":null,"dir":"Reference","previous_headings":"","what":"Variance estimation for median change models — variance_median","title":"Variance estimation for median change models — variance_median","text":"Implement Rice estimator.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Variance estimation for median change models — variance_median","text":"","code":"variance_median(data) variance.median(data)"},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"arguments","dir":"Reference","previous_headings":"","what":"Arguments","title":"Variance estimation for median change models — variance_median","text":"data vector data points.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"value","dir":"Reference","previous_headings":"","what":"Value","title":"Variance estimation for median change models — variance_median","text":"numeric value representing variance.","code":""},{"path":"https://fastcpd.xingchi.li/reference/variance_median.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Variance estimation for median change models — variance_median","text":"","code":"(sigma2 <- variance.median(well_log)) #> [1] 5803645"},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":null,"dir":"Reference","previous_headings":"","what":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"well-known well-log dataset used many changepoint papers obtained Alan Turing Institute GitHub repository licensed MIT license.","code":""},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"ref-usage","dir":"Reference","previous_headings":"","what":"Usage","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"","code":"well_log"},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"format","dir":"Reference","previous_headings":"","what":"Format","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"Time-Series length 4050.","code":""},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"source","dir":"Reference","previous_headings":"","what":"Source","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"","code":""},{"path":"https://fastcpd.xingchi.li/reference/well_log.html","id":"ref-examples","dir":"Reference","previous_headings":"","what":"Examples","title":"Well-log Dataset from Numerical Bayesian Methods Applied to Signal Processing — well_log","text":"","code":"result <- fastcpd.mean(well_log, trim = 0.001) summary(result) #> #> Call: #> fastcpd.mean(data = well_log, trim = 0.001) #> #> Change points: #> 7 19 65 356 445 717 792 1034 1070 1215 1368 1428 1526 1684 1866 2047 2409 2469 2531 2591 2775 3166 3314 3490 3533 3673 3744 3855 3886 3945 3963 4035 #> #> Cost values: #> 70.90268 131.7555 383.0472 2445.132 758.3834 2296.059 637.0301 1996.247 302.8254 1840.756 2267.745 605.6831 1054.037 1310.089 1530.8 1508.578 2990.751 491.6914 513.248 487.8511 1872.188 3796.243 1220.895 1461.687 396.427 1172.664 602.3776 918.5697 277.6224 593.5602 197.5627 611.3688 133.8662 plot(result) # \\donttest{ if (requireNamespace(\"matrixStats\", quietly = TRUE)) { sigma2 <- variance.median(well_log) median_loss <- function(data) { sum(abs(data - matrixStats::colMedians(data))) / sqrt(sigma2) / 2 } result <- fastcpd( formula = ~ x - 1, data = cbind.data.frame(x = well_log), cost = median_loss, trim = 0.002 ) summary(result) segment_starts <- c(1, result@cp_set) segment_ends <- c(result@cp_set - 1, length(well_log)) residual <- NULL for (segment_index in seq_along(segment_starts)) { segment <- well_log[segment_starts[segment_index]:segment_ends[segment_index]] residual <- c(residual, segment - median(segment)) } result@residuals <- matrix(residual) result@data <- data.frame(x = c(well_log)) plot(result) } #> #> Call: #> fastcpd(formula = ~x - 1, data = cbind.data.frame(x = well_log), #> cost = median_loss, trim = 0.002) #> #> Change points: #> 19 577 1034 1070 1216 1361 1428 1526 1685 1866 2047 2409 2469 2531 2591 2775 3744 3855 3945 3963 #> Built-in plot should only work for built-in families. # }"},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0145","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.5","title":"fastcpd 0.14.5","text":"Add pre-compiled vignettes.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0144","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.4","title":"fastcpd 0.14.4","text":"Simplify code. Expose fastcpd_impl API use packages.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0143","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.3","title":"fastcpd 0.14.3","text":"CRAN release: 2024-04-26 Skip certain tests CRAN.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0142","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.2","title":"fastcpd 0.14.2","text":"Fix CRAN noSuggests error.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0141","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.1","title":"fastcpd 0.14.1","text":"CRAN release: 2024-04-18 Fix possible numeric issues CRAN breaking updates packages.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0140","dir":"Changelog","previous_headings":"","what":"fastcpd 0.14.0","title":"fastcpd 0.14.0","text":"CRAN release: 2024-04-16 Correct mBIC. Add citation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0132","dir":"Changelog","previous_headings":"","what":"fastcpd 0.13.2","title":"fastcpd 0.13.2","text":"Remove package check examples. Use proper pruning coefficients MBIC MDL. Make residuals matrices. Use preprocess faster mean change detection. Update examples demonstrating faster mean change. Replace comparison packages vignettes strange dependencies results eval = FALSE.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0131","dir":"Changelog","previous_headings":"","what":"fastcpd 0.13.1","title":"fastcpd 0.13.1","text":"CRAN release: 2024-04-01 Default pruning lasso. Comment gfpop due https://github.com/doccstat/fastcpd/issues/10.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0130","dir":"Changelog","previous_headings":"","what":"fastcpd 0.13.0","title":"fastcpd 0.13.0","text":"Update documentations.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0124","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.4","title":"fastcpd 0.12.4","text":"Customizable pretty plots.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0123","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.3","title":"fastcpd 0.12.3","text":"Remove pruning parameter replace convexity_coef = -Inf.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0122","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.2","title":"fastcpd 0.12.2","text":"CRAN release: 2024-03-19 Update vignettes.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0121","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.1","title":"fastcpd 0.12.1","text":"CRAN release: 2024-03-12 Remove useless C++ codes. Add debug points C++. Add examples data well_log. Add detection comparison well_log data. Add variance estimator median change. Deprecate winsorize_minval winsorize_maxval.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0120","dir":"Changelog","previous_headings":"","what":"fastcpd 0.12.0","title":"fastcpd 0.12.0","text":"Add Rice estimation ARMA model variance estimation. Add time comparison using Well-log data vignettes.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0113","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.3","title":"fastcpd 0.11.3","text":"Add Rice estimator mean change variance estimation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0112","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.2","title":"fastcpd 0.11.2","text":"Export variance estimator function linear models.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0111","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.1","title":"fastcpd 0.11.1","text":"Add package comparison CptNonPar, gfpop, InspectChangepoint, jointseg, Rbeast VARDetect.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0110","dir":"Changelog","previous_headings":"","what":"fastcpd 0.11.0","title":"fastcpd 0.11.0","text":"Note: now , MBIC used default penalty selection beta parameter. Add penalty selection criteria using BIC: (p + 1) * log(nrow(data)) / 2 Modified BIC: (p + 2) * log(nrow(data)) / 2 adjusted cost function. MDL: (p + 2) * log(nrow(data)) / 2 adjusted cost function. mean time, numeric value can passed beta well explicitly specify penalty BIC.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0103","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.3","title":"fastcpd 0.10.3","text":"CRAN release: 2024-01-24 Add package check examples tests.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0102","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.2","title":"fastcpd 0.10.2","text":"Remove bcp according ","code":"Package ‘bcp’ was removed from the CRAN repository. Formerly available versions can be obtained from the archive. Archived on 2024-01-12 as email to the maintainer is undeliverable. A summary of the most recent check results can be obtained from the check results archive. Please use the canonical form https://CRAN.R-project.org/package=bcp to link to this page."},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0101","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.1","title":"fastcpd 0.10.1","text":"CRAN release: 2024-01-09 Increase test coverage. Use interactive() check current R session interactive.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-0100","dir":"Changelog","previous_headings":"","what":"fastcpd 0.10.0","title":"fastcpd 0.10.0","text":"CRAN release: 2023-12-21 Add package comparison packages.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-099","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.9","title":"fastcpd 0.9.9","text":"CRAN release: 2023-12-11 Add small shiny app.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-098","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.8","title":"fastcpd 0.9.8","text":"Preliminary support ARMA(p, q) model parameter order = c(p, q) family \"arma\". Add fastcpd.arma / fastcpd_arma ARMA(p, q) model. Add adaptive increasing beta values.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-097","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.7","title":"fastcpd 0.9.7","text":"Add lower upper parameters denote lower upper bounds parameters. Add line search. Add hardcoded ARMA(3, 2).","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-096","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.6","title":"fastcpd 0.9.6","text":"Add bitcoin well_log data. Add residual calculation mean family. Add plots bitcoin data. Fix residual calculation time series data seegments small. Handle variance estimation errors.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-095","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.5","title":"fastcpd 0.9.5","text":"Add wrapper functions AR(p) family: fastcpd.ar / fastcpd_ar, ARIMA(p, d, q) family: fastcpd.arima / fastcpd_arima, GARCH(p, q) family: fastcpd.garch / fastcpd_garch, linear regression family: fastcpd.lm / fastcpd_lm, logistic regression family: fastcpd.binomial / fastcpd_binomial, poisson regression family: fastcpd.poisson / fastcpd_poisson, penalized linear regression family: fastcpd.lasso / fastcpd_lasso, MA(q) model: fastcpd.ma / fastcpd_ma, mean change: fastcpd.mean / fastcpd_mean, variance change: fastcpd.variance / fastcpd_variance, mean variance change: fastcpd.meanvariance / fastcpd_meanvariance / fastcpd.mv / fastcpd_mv. Replace \"gaussian\" family \"lm\". Add progress bar. Fix design matrix formula bug.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-094","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.4","title":"fastcpd 0.9.4","text":"Fix sanity check. Add small AR(1) data gallery. Fix VAR(p) model bug. Add VAR(2) example Gallery. Remove commented code.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-093","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.3","title":"fastcpd 0.9.3","text":"Deprecate “vanilla” family vanilla_percentage parameter. Add check utility functions. Add MA(4) example. Fix bug beta updated old beta still use. Remove tests estimating variance “gaussian” family dynamically. Merge beta updating get_segment_statistics.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-092","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.2","title":"fastcpd 0.9.2","text":"Add gallery vignettes. Remove cheatsheets pdf package. Use forecast package ARIMA model. Use fGarch package GARCH model.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-091","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.1","title":"fastcpd 0.9.1","text":"Wrap && around || parentheses. Add ma(4) example using custom cost function. Add full support AR(p), MA(q) ARIMA(p, d, q) models.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-090","dir":"Changelog","previous_headings":"","what":"fastcpd 0.9.0","title":"fastcpd 0.9.0","text":"CRAN release: 2023-10-19 Submit CRAN update.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-084","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.4","title":"fastcpd 0.8.4","text":"Add cheatsheets. Refactor code utilize cost_function_wrapper. Optimize warm start.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-083","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.3","title":"fastcpd 0.8.3","text":"Add fastcpd.ts / fastcpd_ts time series data. Fix pre segmengation bug lasso. Fix bug related vanilla_percentage parameter lasso. Add tests invalid family fastcpd.ts. Remove cp_only = TRUE default family “custom”. Improved plotting “ar” “var” families. Add test coverage cp_only = TRUE fastcpd_ts. Increase test coverage. Provide user selection ggplot2 installed.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-082","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.2","title":"fastcpd 0.8.2","text":"Add cheatsheets WIP. Add smaller examples test penalized linear regression.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-081","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.1","title":"fastcpd 0.8.1","text":"Add new “ar” family autoregressive models. Add new “var” family vector autoregressive models.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-080","dir":"Changelog","previous_headings":"","what":"fastcpd 0.8.0","title":"fastcpd 0.8.0","text":"Deal following: Separate use internal C++ cost functions user-defined R cost functions. Add Codecov Icicle plot README. Remove cost_optim cost_update RcppExports.R. Estimate variance “gaussian” family dynamically.","code":"Due to the excessive calls to `glmnet` between R and C++, it is better to use the R implementation of `fastcpd` for lasso."},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-076","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.6","title":"fastcpd 0.7.6","text":"Move default cost functions definition inside fastcpd definition. Define constant unordered set store family sets. Avoid using length(formals(cost)) check number arguments cost function. Introduce internal family “vanilla”.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-075","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.5","title":"fastcpd 0.7.5","text":"Add variance estimation example linear regression. Update reference page. Add validation family. Add user selection ggplot2 installed. Add AR(1) using forecast example tests.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-074","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.4","title":"fastcpd 0.7.4","text":"Update website UI. Update fastcpd documentation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-073","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.3","title":"fastcpd 0.7.3","text":"Allow multiple response variables formula. Add fastcpd logo README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-072","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.2","title":"fastcpd 0.7.2","text":"CRAN release: 2023-09-23 Add suggested package checking tests. Try solve amazing clang-ASAN error CRAN:","code":"Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/data/gannet/ripley/R/test-clang/mvtnorm/libs/mvtnorm.so': /data/gannet/ripley/R/test-clang/mvtnorm/libs/mvtnorm.so: undefined symbol: _ZNK7Fortran7runtime10Terminator5CrashEPKcz Calls: ... asNamespace -> loadNamespace -> library.dynam -> dyn.load"},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-071","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.1","title":"fastcpd 0.7.1","text":"Add package citation.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-070","dir":"Changelog","previous_headings":"","what":"fastcpd 0.7.0","title":"fastcpd 0.7.0","text":"CRAN release: 2023-09-21 Remove C++ unit tests using catch commented code since new version development version Rcpp yet available CRAN. Related pull request: https://github.com/RcppCore/Rcpp/pull/1274. Add documentation fastcpd method.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-065","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.5","title":"fastcpd 0.6.5","text":"Add experiments.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-064","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.4","title":"fastcpd 0.6.4","text":"Check warning messages tests. encapsulation FastcpdParameters members.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-063","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.3","title":"fastcpd 0.6.3","text":"Add CRAN release badge.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-062","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.2","title":"fastcpd 0.6.2","text":"CRAN release: 2023-09-14 Address CRAN comments. Add experiments.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-061","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.1","title":"fastcpd 0.6.1","text":"Address CRAN comments.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-060","dir":"Changelog","previous_headings":"","what":"fastcpd 0.6.0","title":"fastcpd 0.6.0","text":"Submit CRAN release.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-057","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.7","title":"fastcpd 0.5.7","text":"Fix loss function custom mean variance change.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-056","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.6","title":"fastcpd 0.5.6","text":"Add stargazers README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-055","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.5","title":"fastcpd 0.5.5","text":"Add example test multivariate mean shift. Add example test multivariate variance change. Add example test multivariate mean variance change. Add test linear regression multi-dimensional responses.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-054","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.4","title":"fastcpd 0.5.4","text":"Fix bug change point detected.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-053","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.3","title":"fastcpd 0.5.3","text":"Add experiments commented sake test time without affecting test coverage. Add examples README. Add CRAN manual using R CMD Rd2pdf . --output=man/figures/manual.pdf --force ---preview stackoverflow. Add example multiple epochs using custom cost functions. Add table contents README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-052","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.2","title":"fastcpd 0.5.2","text":"Add one-dimensional linear regression example plot.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-051","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.1","title":"fastcpd 0.5.1","text":"Prepare CRAN release.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-050","dir":"Changelog","previous_headings":"","what":"fastcpd 0.5.0","title":"fastcpd 0.5.0","text":"Rewrite whole package C++ except LASSO due excessive calls R C++ glmnet.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-040","dir":"Changelog","previous_headings":"","what":"fastcpd 0.4.0","title":"fastcpd 0.4.0","text":"Add transition vanilla PELT SeN using vanilla_percentage parameter.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-033","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.3","title":"fastcpd 0.3.3","text":"Merge implementation vanilla PELT SeN. Encapsulate implementation binding new coefficients previous coefficients. Rewrite fastcpd parameters updating C++.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-032","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.2","title":"fastcpd 0.3.2","text":"Integrate initialization update theta_hat, theta_sum hessian. Combine theta estimation single function. Add parameter vanilla_percentage denote method switching vanilla PETL SeN. Add documentation cp_only parameter. Add preparation merging vanilla PELT SeN.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-031","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.1","title":"fastcpd 0.3.1","text":"Add examples tests fastcpd. Rearrange C++ functions. Add precondition check.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-030","dir":"Changelog","previous_headings":"","what":"fastcpd 0.3.0","title":"fastcpd 0.3.0","text":"Bump test coverage class methods fastcpd.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-029","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.9","title":"fastcpd 0.2.9","text":"Fix Poisson regression bug related lfactorial.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-028","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.8","title":"fastcpd 0.2.8","text":"Make penalized linear regression estimated coefficients output sparse.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-027","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.7","title":"fastcpd 0.2.7","text":"Fix mean change example bug. Update documentation redirect README pkgdown generated webpage. Add contact methods ways file ticket.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-026","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.6","title":"fastcpd 0.2.6","text":"Add C++ sanity check Logistic regression data, .e. binomial family. Add examples tests fastcpd. Rename C++ source files follow Unix convention. Update documentation link README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-025","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.5","title":"fastcpd 0.2.5","text":"Hide internal functions documentation. Export fastcpd class.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-024","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.4","title":"fastcpd 0.2.4","text":"Add column name thetas slot fastcpd class. Fix plot residuals responses appear plot. Default cp_only FALSE. Remove residuals summary method.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-023","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.3","title":"fastcpd 0.2.3","text":"Add missing examples linear regression LASSO.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-022","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.2","title":"fastcpd 0.2.2","text":"Add examples illustrate use fastcpd function. Indicating internal functions users use .","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-021","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.1","title":"fastcpd 0.2.1","text":"Add examples README.","code":""},{"path":"https://fastcpd.xingchi.li/news/index.html","id":"fastcpd-020","dir":"Changelog","previous_headings":"","what":"fastcpd 0.2.0","title":"fastcpd 0.2.0","text":"Added NEWS.md file track changes package.","code":""}]