#Example of a data set of length 1000 with changes in location
#(model 1 of Haynes, K et al. (2016)) with the empirical distribution cost function.
set.seed(12)
J <- function(x){
(1+sign(x))/2
}
n <- 1000
tau <- c(0.1,0.13,0.15,0.23,0.25,0.4,0.44,0.65,0.76,0.78,0.81)*n
h <- c(2.01, -2.51, 1.51, -2.01, 2.51, -2.11, 1.05, 2.16, -1.56, 2.56, -2.11)
sigma <- 0.5
t <- seq(0,1,length.out = n)
data <- array()
for (i in 1:n){
data[i] <- sum(h*J(n*t[i] - tau)) + (sigma * rnorm(1))
}
out <- cpt.np(data, penalty = "SIC",method="PELT",test.stat="empirical_distribution",
class=TRUE,minseglen=2, nquantiles =4*log(length(data)))
cpts(out)
#returns 100 130 150 230 250 400 440 650 760 780 810 as the changepoint locations.
plot(out)
# \donttest{
#Example 2 uses the heart rate data .
data(HeartRate)
cptHeartRate <- cpt.np(HeartRate, penalty = "CROPS", pen.value = c(5,200),
method="PELT", test.stat="empirical_distribution",
class=TRUE,minseglen=2,
nquantiles =4*log(length(HeartRate)))
plot(cptHeartRate, diagnostic = TRUE)
plot(cptHeartRate, ncpts = 11)
# }
Run the code above in your browser using DataLab