Skip to content

Commit

Permalink
version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Zifeng Zhao authored and cran-robot committed Jul 6, 2023
0 parents commit d2b52ed
Show file tree
Hide file tree
Showing 46 changed files with 4,414 additions and 0 deletions.
33 changes: 33 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Package: SNSeg
Title: Self-Normalization(SN) Based Change-Point Estimation for Time
Series
Version: 1.0.0
Authors@R:
c(person("Shubo","Sun",role=c("aut"),email = "sxs3935@miami.edu"),
person("Zifeng","Zhao",role = c("aut","cre"),email = "zzhao2@nd.edu"),
person(given = "Feiyu",family = "Jiang",role = c("aut"),email = "jiangfy@fudan.edu.cn"),
person("Xiaofeng",family = "Shao",role = c("aut"),email = "xshao@illinois.edu")
)
Description: Implementations self-normalization (SN) based algorithms for
change-points estimation in time series data. This comprises nested
local-window algorithms for detecting changes in both univariate and
multivariate time series developed in Zhao, Jiang and Shao (2022)
<doi:10.1111/rssb.12552>.
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Depends: R (>= 3.5.0), stats, utils, graphics
LinkingTo: Rcpp
Imports: Rcpp, mvtnorm, truncnorm, evd
Suggests: rmarkdown, knitr
VignetteBuilder: knitr
NeedsCompilation: yes
Packaged: 2023-07-05 15:32:21 UTC; Shubo Sun
Author: Shubo Sun [aut],
Zifeng Zhao [aut, cre],
Feiyu Jiang [aut],
Xiaofeng Shao [aut]
Maintainer: Zifeng Zhao <zzhao2@nd.edu>
Repository: CRAN
Date/Publication: 2023-07-06 13:50:09 UTC
45 changes: 45 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
d971d1bae85b2b8cae49b96f6f1a26bd *DESCRIPTION
948a6410653dc44ecfa181fe5d76eca5 *NAMESPACE
250746f564bdb9f86121331481df88b5 *R/MAR_all.R
8c2b1ab1f398e682821767a3175a76d9 *R/RcppExports.R
44a911a6ba84c360de7f276d35272c85 *R/SNHD.R
52f4de16b6744017a1d84cf9300317ac *R/SNSeg.R
9d89cea3b91e89350acef6a60a69acf8 *R/SNSeg_HD.R
08017dbb1f983affad7c060b45bed699 *R/SNSeg_Multi.R
b8ce0cf6cddab0648e423f4ce57e3f82 *R/SNSeg_Uni.R
bf7df0e4d4b4d7786fadfda9f8179002 *R/SNSeg_Uni_multi_para.R
afe66deab5164bf7e728db5f894318ff *R/SNSeg_Uni_single_para.R
9b4aea565e59d70ba4c1b57ccc346a7c *R/SN_acf.R
6281a50ae0e626b8e33e5af140574b8d *R/SN_bivcor.R
36ac57f38cf8a2d16dbb292601efbfe2 *R/SN_divisive_path.R
880f6b19f08aa327de4cb5ff8304aa45 *R/SN_mean.R
f7661990e0f057764d205b8642a37253 *R/SN_quantile.R
12a05d768c20888f319c02dbbf17acec *R/SN_variance.R
83b596b2792cc14913022c55c7f30a33 *R/cumsum_constrast.R
7e5df6879c9def2bf445095b6580ee3f *R/data.R
a04e655e7ca31e669e581646eb8ca8c0 *R/max_SNsweep.R
2285dcacb667fb9a4783a35ac7e05c23 *R/mts_covariance.R
8e297ae2665c704231fa2e580ec4e73a *R/multimean.R
13671bf745bd66b725f88d7e813ba0d4 *R/multiparameter.R
8a9e17b6525b341039d9baf33fbb8862 *R/sysdata.rda
89d03f8079d1c8cba6f608ab4a0fab4d *build/vignette.rds
3fb15c729769bc117aa15876f53b2380 *data/critical_values_HD.rda
dad7a4b4e50c26b1b869b7b566d23b7a *data/critical_values_multi.rda
bfe54fd6b8192e42b42b3e0772968b5a *data/critical_values_single.rda
306d9643b29006081fbdf5ecf5c9d65b *inst/doc/SNSeg.R
5441f68e932b9959974bd4f9af49d75d *inst/doc/SNSeg.Rmd
87ef01c9e7d23d6aeb36b564b7812914 *inst/doc/SNSeg.html
f99a880f5760fef934523411dd846299 *man/MAR.Rd
a39eadafd7a5634213b42f4e5fdb4ca1 *man/MAR_MTS_Covariance.Rd
d5af6e4d0b2bb7597c613833cd6ec9b2 *man/MAR_Variance.Rd
95edd78f089a7eb47cec30636b971cbc *man/SNSeg.Rd
552b478d4179fa1d125819d53a142b37 *man/SNSeg_HD.Rd
6f0860409a4649c4f791d4eabd2e3f69 *man/SNSeg_Multi.Rd
0b9c1eac3d472300af753d4938562fd6 *man/SNSeg_Uni.Rd
905ae0ebdceaad0275dbc5a565e98fb4 *man/critical_values_HD.Rd
9ff7cba113816526d933fda675f1c7e5 *man/critical_values_multi.Rd
96493bae7e600515830fc39ca6cbb34a *man/critical_values_single.Rd
efb935050c10092fa012f95a16621b4f *man/max_SNsweep.Rd
92e3f0adbb3ed050399da54a2574689a *src/RcppExports.cpp
c0924f945eb01aaf699e4a5991fd2072 *src/SN_Util.cpp
5441f68e932b9959974bd4f9af49d75d *vignettes/SNSeg.Rmd
22 changes: 22 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by roxygen2: do not edit by hand

export(MAR)
export(MAR_MTS_Covariance)
export(MAR_Variance)
export(SNSeg_HD)
export(SNSeg_Multi)
export(SNSeg_Uni)
export(max_SNsweep)
importFrom(Rcpp,evalCpp)
importFrom(graphics,abline)
importFrom(graphics,par)
importFrom(graphics,plot)
importFrom(mvtnorm,rmvnorm)
importFrom(stats,approx)
importFrom(stats,cor)
importFrom(stats,pnorm)
importFrom(stats,quantile)
importFrom(stats,rnorm)
importFrom(stats,var)
importFrom(utils,data)
useDynLib(SNSeg, .registration=TRUE)
241 changes: 241 additions & 0 deletions R/MAR_all.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
#' @useDynLib SNSeg, .registration=TRUE
#' @importFrom stats rnorm
NULL

#' A funtion to generate a multivariate autoregressive process (MAR) in time
#' series
#'
#' The function \code{MAR} is used for generating MAR model(s) for examples
#' of the functions \code{SNSeg_Uni}, \code{SNSeg_Multi}, and \code{SNSeg_HD}.
#'
#' @param n the size (length) of time series to be generated
#' @param reptime the number of time series to be generated
#' @param rho value of autocorrelation
#'
#' @return Returns a matrix of the simulated MAR processes. The number of columns
#' of this matrix is equivalent to the value of input argument \code{reptime}, and
#' the number of rows is the value of input argument \code{n}.
#'
#' @examples
#' MAR(n = 1000, reptime = 2, rho = -0.7)
#'
#' @export MAR
MAR <- function(n, reptime, rho){
inter <- matrix(0, n+30, reptime)
epsilon <- matrix(rnorm((n+30)*reptime,0,1),(n+30),reptime)
for (j in 1:(n+29)){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho*inter[j,1:reptime]
}
return(inter[31:(n+30),1:reptime])
}

#' @useDynLib SNSeg, .registration=TRUE
#' @importFrom stats rnorm
NULL

#' A funtion to generate a multivariate autoregressive process (MAR) model in
#' time series for testing change points based on variance and
#' autocovariance
#'
#' The function \code{MAR_Variance} is used for generating MAR model(s) for
#' examples of the functions \code{SNSeg_Uni}, \code{SNSeg_Multi}, and \code{SNSeg_HD}.
#'
#' @param reptime The number of time series to be generated
#' @param type The type of time series for simulation, which includes V1, V2, V3
#' , A1, A2 and A3. The V-beginnings are for testing the variance, and the
#' A-beginnings are for testing the autocorrelation. The simulated time series
#' come from supplement of Zhao et al. (2022) <doi:10.1111/rssb.12552>.
#' Default \code{type} is \code{V3}.
#'
#' The time length and "true change-points locations" (cps) for each \code{type} are as follows:
#' \code{V1}: cps at 400 and 750 with a time length of 1024.
#' \code{V2}: cps at 125, 532 and 704 with a time length of 1024.
#' \code{V3}: cps at 512 and 768 with a time length of 1024.
#' \code{A1}: cps at 400 and 750 with a time length of 1024.
#' \code{A2}: cps at 50 with a time length of 1024.
#' \code{A3}: cps at 512 and 768 with a time length of 1024.
#'
#' @return Returns a matrix of the simulated MAR processes. The number of columns
#' of this matrix is equivalent to the value of input argument \code{reptime}.
#'
#' @examples
#' MAR_Variance(reptime = 2, type = "V1")
#'
#' @export MAR_Variance
MAR_Variance <- function(reptime, type='V3'){
burnin <- 50
if(type=='V1'){
n <- 1024
inter <- matrix(0, n+burnin, reptime)
epsilon <- matrix(rnorm((n+burnin)*reptime,0,1),(n+burnin),reptime)
cp_sets <- c(0,400,750,1024)
no_seg <- length(cp_sets)-1
rho_sets <- list(c(0.5),c(0.5),c(0.5))
sd_sets <- list(c(1),c(2),c(1))
for(j in 1:(burnin-1)){
inter[j+1,1:reptime] <- sd_sets[[1]]*epsilon[j+1,1:reptime]+rho_sets[[1]]*inter[j,1:reptime]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
for(j in tau1:tau2){
inter[j+1,1:reptime] <- sd_sets[[index]]*epsilon[j+1,1:reptime]+rho_sets[[index]]%*%inter[j:(j-length(rho_sets[[index]])+1),1:reptime]
}
}
}
if(type=='V2'){
n <- 1024
inter <- matrix(0, n+burnin, reptime)
epsilon <- matrix(rnorm((n+burnin)*reptime,0,1),(n+burnin),reptime)
cp_sets <- c(0,125,532,704,1024)
no_seg <- length(cp_sets)-1
rho_sets <- list(c(0.7),c(0.3),c(0.9),c(0.1))
sd_sets <- list(c(1,0.6),c(1,0.3),c(1),c(1,-0.5))
for(j in 2:(burnin-1)){
inter[j+1,1:reptime] <- sd_sets[[1]]%*%epsilon[(j+1):(j-length(sd_sets[[1]])+2),1:reptime]+rho_sets[[1]]*inter[j,1:reptime]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
for(j in tau1:tau2){
inter[j+1,1:reptime] <- sd_sets[[index]]%*%epsilon[(j+1):(j-length(sd_sets[[index]])+2),1:reptime]+rho_sets[[index]]*inter[j,1:reptime]
}
}
}
if(type=='V3'){
n <- 1024
inter <- matrix(0, n+burnin, reptime)
epsilon <- matrix(rnorm((n+burnin)*reptime,0,1),(n+burnin),reptime)
cp_sets <- c(0,512,768,1024)
no_seg <- length(cp_sets)-1
rho_sets <- list(c(0.9), c(1.69,-0.81), c(1.32,-0.81))
for(j in 1:(burnin-1)){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho_sets[[1]]*inter[j,1:reptime]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
for(j in tau1:tau2){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho_sets[[index]]%*%inter[j:(j-length(rho_sets[[index]])+1),1:reptime]
}
}
}
if(type=='A1'){
n <- 1024
inter <- matrix(0, n+burnin, reptime)
epsilon <- matrix(rnorm((n+burnin)*reptime,0,1),(n+burnin),reptime)
cp_sets <- c(0,400,750,1024)
no_seg <- length(cp_sets)-1
rho_sets <- list(c(0.5),c(0.9),c(0.3))
for(j in 1:(burnin-1)){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho_sets[[1]]*inter[j,1:reptime]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
for(j in tau1:tau2){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho_sets[[index]]%*%inter[j:(j-length(rho_sets[[index]])+1),1:reptime]
}
}
}
if(type=='A2'){
n <- 1024
inter <- matrix(0, n+burnin, reptime)
epsilon <- matrix(rnorm((n+burnin)*reptime,0,1),(n+burnin),reptime)
cp_sets <- c(0,50,1024)
no_seg <- length(cp_sets)-1
rho_sets <- list(c(0.75),c(-0.5))
for(j in 1:(burnin-1)){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho_sets[[1]]*inter[j,1:reptime]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
for(j in tau1:tau2){
inter[j+1,1:reptime] <- epsilon[j+1,1:reptime]+rho_sets[[index]]%*%inter[j:(j-length(rho_sets[[index]])+1),1:reptime]
}
}
}
if(type=='A3'){
n <- 1024
inter <- matrix(0, n+burnin, reptime)
epsilon <- matrix(rnorm((n+burnin)*reptime,0,1),(n+burnin),reptime)
cp_sets <- c(0,512,768,1024)
no_seg <- length(cp_sets)-1
rho_sets <- list(c(-0.9),c(0.9),c(0))
sd_sets <- list(c(1,0.7),c(1),c(1,-0.7))
for(j in 2:(burnin-1)){
inter[j+1,1:reptime] <- sd_sets[[1]]%*%epsilon[(j+1):(j-length(sd_sets[[1]])+2),1:reptime]+rho_sets[[1]]*inter[j,1:reptime]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
for(j in tau1:tau2){
inter[j+1,1:reptime] <- sd_sets[[index]]%*%epsilon[(j+1):(j-length(sd_sets[[index]])+2),1:reptime]+rho_sets[[index]]*inter[j,1:reptime]
}
}
}
return(inter[(burnin+1):(n+burnin),1:reptime])
}

#' @useDynLib SNSeg, .registration=TRUE
#' @importFrom mvtnorm rmvnorm
NULL

#' A Funtion to generate a multivariate autoregressive process (MAR) model in
#' time series. It is used for testing change-points based on the change in multivariate
#' means or multivariate covariance for multivariate time series. It also works
#' for the change in correlations between two univariate time series.
#'
#' The function \code{MAR_MTS_Covariance} is used to generate MAR model(s) for
#' examples of the functions \code{SNSeg_Uni}, \code{SNSeg_Multi}, and \code{SNSeg_HD}.
#'
#' @param n the size of time series to be generated.
#' @param reptime the number of time series to be generated.
#' @param rho_sets autocorrelations for each univariate time series.
#' @param cp_sets numeric values of the true change-point locations (0, change-point
#' locations and the end point).
#' @param sigma_cross a list of matrices to generate the multivariate covariance
#' matrices.
#'
#' @returns Returns a list of matrices where each matrix is a MAR process. The
#' number of columns for each sub-matrix is equivalent to the value of input
#' argument \code{reptime}.
#'
#' @examples
#' n <- 1000
#' reptime <- 2
#' sigma_cross <- list(4*matrix(c(1,0.8,0.8,1), nrow=2),
#' matrix(c(1,0.2,0.2,1), nrow=2),
#' matrix(c(1,0.8,0.8,1), nrow=2))
#' cp_sets <- round(c(0,n/3,2*n/3,n))
#' noCP <- length(cp_sets)-2
#' rho_sets <- rep(0.5, noCP+1)
#' MAR_MTS_Covariance(n, reptime, rho_sets, cp_sets, sigma_cross)
#'
#' @export MAR_MTS_Covariance
MAR_MTS_Covariance <- function(n, reptime, rho_sets, cp_sets, sigma_cross){
no_ts <- dim(sigma_cross[[1]])[1]
ts_sim <- list()
burnin <- 100
no_seg <- length(cp_sets)-1
for(rep_index in 1:reptime){
epsilon <- rmvnorm(burnin, mean=rep(0,no_ts), sigma=sigma_cross[[1]])
inter <- matrix(0, n+burnin, no_ts)
for (j in 1:(burnin-1)){
inter[j+1,] <- epsilon[j+1,]+rho_sets[1]*inter[j,]
}
for(index in 1:no_seg){ # Mean shift
tau1 <- cp_sets[index]+burnin
tau2 <- cp_sets[index+1]+burnin-1
epsilon <- rmvnorm(tau2-tau1+1, mean=rep(0,no_ts), sigma=sigma_cross[[index]])
for(j in tau1:tau2){
inter[j+1,] <- epsilon[(j+1-tau1),]+rho_sets[index]*inter[j,]
}
}
ts_sim[[rep_index]] <- t(inter[(burnin+1):(n+burnin),])
}
return(ts_sim)
}


35 changes: 35 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

cpp_med2 <- function(xx) {
.Call(`_SNSeg_cpp_med2`, xx)
}

cpp_quantile <- function(xx, q) {
.Call(`_SNSeg_cpp_quantile`, xx, q)
}

cpp_cumquantile <- function(xx, q) {
.Call(`_SNSeg_cpp_cumquantile`, xx, q)
}

cumsum_median_constrast_Cpp <- function(ts, type) {
.Call(`_SNSeg_cumsum_median_constrast_Cpp`, ts, type)
}

cumsum_quantile_constrast_Cpp_check <- function(ts, type, q) {
.Call(`_SNSeg_cumsum_quantile_constrast_Cpp_check`, ts, type, q)
}

cumsum_quantile_constrast_Cpp <- function(ts, type, q) {
.Call(`_SNSeg_cumsum_quantile_constrast_Cpp`, ts, type, q)
}

cpp_acf <- function(ts) {
.Call(`_SNSeg_cpp_acf`, ts)
}

cumsum_acf_constrast_Cpp <- function(ts, type) {
.Call(`_SNSeg_cumsum_acf_constrast_Cpp`, ts, type)
}

0 comments on commit d2b52ed

Please sign in to comment.