-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.R
More file actions
47 lines (36 loc) · 1.16 KB
/
example.R
File metadata and controls
47 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Example usage of epidemic changepoint detectors (Algorithms 1 and 2)
# as described in Juodakis J. and Marsland S.:
# Epidemic changepoint detection in the presence of nuisance changes
# Load the detector functions
source("MAIN-algorithm.R")
# Generate some data
ts = c(rnorm(30, 0, 1),
rnorm(25, 2, 1),
rnorm(15, 0, 1),
rnorm(10, -2, 1),
rnorm(20, 0, 1))
plot(ts)
# Set penalty to default (3 * log(n) ^ 1.1)
pen = autoset_penalty(ts)
# Set maximum segment length
lookback = 30
# Provide an estimate of the background sigma
SD = 1
# Provide an estimate of the global background mean (Algorithm 2 only)
mu0 = median(ts)
# Apply Algorithm 1 (online version):
res = shortsgd(ts, lookback, pen, SD)
# Final detections
res$segs
# Final background estimate
res$wt[length(res$wt)]
# Apply Algorithm 2 (with global pruning):
res2 = fulldetector_prune(ts, theta0=mu0, MAXLOOKBACK=lookback, PEN=pen, PEN2=pen, SD=SD, prune=2)
# Final detections
res2$segs
# With nicer formatting
res2df = data.frame(res2$segs)
colnames(res2df) = c("start", "end", "theta", "segtype")
rownames(res2df) = NULL
res2df$segtype = ifelse(res2df$segtype==1, "S", "N")
res2df