ts_changeindex: Find change indices

View source: R/ts.R

ts_changeindexR Documentation

Find change indices

Description

Find change indices

Usage

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⁠

See Also

Other Time series operations: ts_center(), ts_checkfix(), ts_detrend(), ts_diff(), ts_discrete(), ts_duration(), ts_embed(), ts_integrate(), ts_levels(), ts_peaks(), ts_permtest_block(), ts_permtest_transmat(), ts_rasterize(), ts_sd(), ts_slice(), ts_slopes(), ts_standardise(), ts_sumorder(), ts_symbolic(), ts_trimfill(), ts_windower()

Examples


 library(ggplot2)

 set.seed(1234)
 yy     <- noise_powerlaw(standardise = TRUE, N=50, alpha = -1)
 tr     <- ts_levels(yy, doTreePlot = TRUE)
 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())


FredHasselman/casnet documentation built on April 20, 2024, 3:05 p.m.