## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)

## -----------------------------------------------------------------------------
library(PiChange)

set.seed(12)
y <- c(rnorm(50, 0, 0.35), rnorm(50, 2, 0.35))

penalty <- construct_penalty(
  n = length(y),
  centers = 48,
  width = 6,
  method = "mbic",
  family = "normal"
)

fit <- pi_change(y, penalty, min_seg_len = 10)
fit
changepoints(fit)
summary(fit)

## -----------------------------------------------------------------------------
plot(fit)

## -----------------------------------------------------------------------------
dates <- as.Date("2020-01-01") + seq_along(y) - 1
dated_penalty <- construct_penalty(
  time = dates,
  centers = as.Date("2020-02-18"),
  width = 6,
  method = "mbic",
  family = "normal"
)
dated_fit <- pi_change(y, dated_penalty, min_seg_len = 10)
changepoints(dated_fit, scale = "time")

## -----------------------------------------------------------------------------
widths <- c(3, 6, 12)
sensitivity <- lapply(widths, function(w) {
  p <- construct_penalty(
    n = length(y), centers = 48, width = w,
    method = "mbic", family = "normal"
  )
  changepoints(pi_change(y, p, min_seg_len = 10))
})
names(sensitivity) <- widths
sensitivity

