R/pred(ma).R

Defines functions predma.mse

Documented in predma.mse

#'Mean Squared Error using censored data generated from MA model

#' @description  (Accuracy of prediction)
#' One can find the mean squared error (MSE) to check how these
#' different methods (ind, dcbs, mrc, agg, mv) perform if the objective is to make prediction.
#'@param chpts changepoints that are obtained using any discussed method (ind, dcbs, mrc, agg, mv)
#' @param data.train divide generated censored data from MA model into data.train and
#' data.test. Here we consider n=500 (size of each series) and N=100 (number of series)
#' so we have a matrix of N*n. In data.train we leave out the five data points at the end
#' of each series.
#'@param data.test Remaining dataset (five time points at the end of each series) will
#'be considered as data.test.

#'@return  return mean squared error (MSE)
#'@export
#'@seealso MA1.data, indMA,  Bin_segMA, PELT.MVma
#' @examples
#' # example
#' #mean squared error to check the accuracy of ind method using
#' #censored data generated from MA model.
#' # data generated through MA model considering 60% censoring rate
#' #(Left censoring) and missing rate is equal to zero
#'library(cpcens)
#'sim = MA1.data ( n=500 , N = 100 , K = 5 , eps = 1 , rho=0.6,
#'mu = 0,  siga = 1, rates = c(0.6,NA), Mrate=0 )
#'data=sim$data
#'n=500
#'N=100
#'# training and test
#'data.train = sim$data[,1:(n-5)]
#'data.test = sim$data[,(n-4):n]
#' ##If pen is equal to zero, penalty term will be equal to 2*log(n)
#'indma.chpts=indMA(data.train, pen=0)
#'indma.mse = predma.mse( indma.chpts , data.train , data.test )
#'indma.mse
#'#example
#'#mean squared error to check the accuracy of dcbs method using
#'#censored data generated from MA model.
#'library(cpcens)
#'# data generated through MA model considering 20% censoring rate
#' #(Right censoring) and missing rate is equal to zero
#'sim = MA1.data ( n=500 , N = 100 , K = 5 , eps = 1 , rho=0.4,
#'mu = 0,  siga = 1, rates = c(NA,0.2), Mrate=0 )
#'data=sim$data
#'n=500
#'N=100
#'# training and test
#'data.train = sim$data[,1:(n-5)]
#'data.test = sim$data[,(n-4):n]

#'dcbsma.chpts= Bin_segMA(data.train, 10)
#'dcbsma.mse = predma.mse( dcbsma.chpts , data.train , data.test )
#'dcbsma.mse

#'#example
#'#mean squared error to check the accuracy of mv method using
#'#censored data generated from MA model.
#'library(cpcens)
#'# data generated through MA model considering 60% censoring rate
#' #(Right censoring) and missing rate is equal to zero
#'sim = MA1.data ( n=500 , N = 100 , K = 5 , eps = 1 , rho=0.4,
#'mu = 0,  siga = 1, rates = c(NA,0.6), Mrate=0 )
#'data=sim$data
#'n=500
#'N=100
#'# training and test
#'data.train = sim$data[,1:(n-5)]
#'data.test = sim$data[,(n-4):n]
#'pmv = PELT.MVma( data.train , 101*log(dim(data.train)[2]) )
#'mv.chpts =  rep( rev( pmv$cpts )[1] , N )
#'mv.mse = predma.mse( mv.chpts , data.train , data.test )


predma.mse = function( chpts , data.train , data.test ){

  N = dim(data.train)[1]
  train.n = dim(data.train)[2]

  # if MRC does not retur full vector
  if (length(chpts)<N){
    chpts = rep( unique(chpts)[1] , 100 )
  }

  ms.errs = numeric(N)
  for (i in 1:N){
    # mean of last segment
    mu = mean( data.train[ i , chpts[i]:train.n ]  )
    ms.errs[i] = mean( (data.test[i,] - mu)^2 )
  }

  return( mean(ms.errs) )
}

Try the cpcens package in your browser

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

cpcens documentation built on Aug. 2, 2019, 5:05 p.m.