R/PlotChangePoints.R

Defines functions PlotChangePoints

Documented in PlotChangePoints PlotChangePoints

#'@title 
#'PlotChangePoints
#'
#'@description 
#'This function plots the data and the estimated stepwise signal given the estimated
#'change points and means. The function only applies to one dimensional data.
#'
#'@details
#'See Manual.pdf in "data" folder.
#'
#'@param
#'data.x     Observed data in vector form where each element represents a single observation.
#'@param
#'data.t     The one-dimensional time or seqeuential labeling for the data.
#'@param
#'index.ChPT The set of the index of change points in a vector. Must be in accending order. This could be obtained by \code{est.changepoints}.
#'@param
#'est.mean   The estimated mean in each segments in a vector. The length must be one plus the length of \code{index.ChPT}. \cr For normal and Poisson cases as in DKK2013, apply \code{est.mean.norm} and \code{est.mean.pois} respectively.
#'@param
#'type.data  (Opt.) The line type for the data. Options are the same as in plot() argument. Default is "l".
#'@param
#'col.data   (Opt.) The line color for the data. Options are the same as in plot() argument. Default is "red".
#'@param
#'col.est    (Opt.) The line color for the estimated stepwise signal. Options are the same as in plot() arguent. Default is "blue".
#'@param
#'main.plot  (Opt.) The overall title used in the plot, which is like the main in plot(). Default is NULL.}\item{sub.plot}{(Opt.) The sub title used in the plot, which is like the main in plot(). Default is NULL.
#'@param
#'xlab.plot  (Opt.) The title for the x axis used in the plot, which is like the main in plot(). Default is "data.t".
#'@param
#'ylab.plot  (Opt.) The title for the y axis used in the plot, which is like the main in plot(). Default is "data.x".
#'
#'@return
#'Plot for the data and the estimated change-points. Note that this function only apply to one-dimensional observation.
#'
#'@examples
#'library(StepSignalMargiLike)
#'
#'n <- 5
#'data.x <- rnorm(n, 1, 1)
#'data.x <- c(data.x, rnorm(n, 10,1))
#'data.x <- c(data.x, rnorm(n, 2,1))
#'data.x <- c(data.x, rnorm(n, 10,1))
#'data.x <- c(data.x, rnorm(n, 1,1))
#'data.t <- 1:(5*n)
#'
#'index.ChPT <- c(n,2*n,3*n,4*n)
#'est.mean <- c(1,10,2,10,2)
#'PlotChangePoints(data.x, data.t, index.ChPT, est.mean)
#'
#'PlotChangePoints(data.x, data.t, index.ChPT, est.mean, type.data="p", col.data="green", col.est="black", main="Stepwise Signal Estimation", sub="Using Marginal Likelihood", xlab="time", ylab="value")
#'
#'@export
PlotChangePoints <- function(data.x, data.t, index.ChPT, est.mean, type.data, col.data, col.est, main.plot, sub.plot, xlab.plot, ylab.plot)
{
  #if(hasArg(type.data)==FALSE)
  #{
  #  type.data <- "l"
  #}
  if(missing(type.data))
  {
    type.data <- "l"
  }
  if(missing(col.data))
  {
    col.data <- "red"
  }
  if(missing(col.est))
  {
    col.est <- "blue"
  }
  if(missing(main.plot))
  {
    main.plot <- ""
  }
  if(missing(sub.plot))
  {
    sub.plot <- ""
  }
  if(missing(xlab.plot))
  {
    xlab.plot <- "data.t"
  }
  if(missing(ylab.plot))
  {
    ylab.plot <- "data.x"
  }
  
  plot(data.t, data.x, type=type.data, col=col.data, main=main.plot, sub=sub.plot, xlab=xlab.plot, ylab=ylab.plot)
  num.ChangePTs <- length(index.ChPT)
  endPTs <- c(1, index.ChPT, length(data.t))
  seg.par <- c(est.mean[1], est.mean, est.mean[num.ChangePTs+1])
  sfun0 <- stepfun(data.t[endPTs], seg.par, f=0)
  lines(sfun0, col=col.est)  
  variable.list <- c("num.ChangePTs", "y0", "sfun0")
  rm(variable.list)
}

Try the StepSignalMargiLike package in your browser

Any scripts or data that you put into this service are public.

StepSignalMargiLike documentation built on May 2, 2019, 1:02 p.m.