Find change indices

ts_changeindex(
  y,
  returnRectdata = FALSE,
  groupVar = NULL,
  labelVar = NULL,
  discretize = FALSE,
  nbins = 5
)

Arguments

y

An indicator variable representing different levels of a variable or factor

returnRectdata

Return a dataframe suitable for shading a ggplot2 graph with ggplot2::geom_rect()

groupVar

Pass a value (length 1) or variable (length of y) that can be used as a variable to join the indices by if returnRectdata = TRUE

labelVar

If y is not a character vector, provide a vector of labels equal to length(y)

discretize

If y is a continuous variable, setting discretize = TRUE will partition the values of y into nbins number of bins, each value of y will be replaced by its bin number.

nbins

Number of bins to use to change a continuous y (if discretize = TRUE) into a variable with nbins levels

Value

Either a vector with the indices of change in y, or, a data frame with variables xmin,xmax,ymin,ymax,label

Examples


 library(ggplot2)

 set.seed(1234)
 yy     <- noise_powerlaw(standardise = TRUE, N=50, alpha = -1)
 tr     <- ts_levels(yy, doTreePlot = TRUE)
#> Skipping adjustment by argument minChange...

 breaks <- ts_changeindex(tr$pred$p, returnRectdata = TRUE)
 breaks$cols <- casnet::getColours(length(breaks$label))

 ggplot(tr$pred) +
   geom_rect(data = breaks,
   aes(xmin = xmin, xmax=xmax, ymin=ymin, ymax=ymax, colour = label, fill = label),
   alpha = .3) +
   scale_colour_manual(values = breaks$cols) +
   scale_fill_manual(values = breaks$cols) +
   scale_x_continuous("time", expand = c(0,0)) +
   geom_line(aes(x=x,y=y)) +
   geom_step(aes(x=x,y=p), colour = "red3", size=1) +
   theme_bw() + theme(panel.grid.minor = element_blank())
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#>  Please use `linewidth` instead.