R/simulate.data.R

Defines functions gen.curves simu.curves

Documented in gen.curves simu.curves

#' Simulate multiple curve data from a given model (either Normal or Poisson).
#'
#' @param curve.str curve type for true signal; used in the function \code{simulate_true_curve}.
#' @param curve.length length of curve data
#' @param stretch parameter used to generate true signal; used in the function \code{simulate_true_curve}.
#' @param offset parameter used to generate true signal; used in the function \code{simulate_true_curve}.
#' @param model.mode either Normal or Poisson; this function simulates \code{num.samples} number of curve data under a model provided as \code{model.mode}.
#' @param num.groups how many groups of samples should be simulated; currently this function can simulate data from one group.
#' @param num.samples how many curve data should be simulated.
#' @param normal.sigma standard deviation in Normal model; 
#'
#' @return \code{gen.curves} returns a list of the following elements
#' \item{true.mean.curve}{a vector of length \code{curve.length}-vector; it contains true mean curve generated by the function \code{simulate_true_curve}}
#' \item{data}{a matrix of \code{num.samples} by \code{curve.length}; it contains \code{num.samples} curve data; each row contains simulated data under a given model \code{model.mode} with mean curve \code{true.mean.curve}}
gen.curves <-function(curve.str, curve.length, stretch, offset, model.mode, num.groups, num.samples, normal.sigma)  {

  ## Generate true mean curve 
  true.mean.curve <- simulate_true_curve(curve.str, curve.length, stretch, offset)

  ## Simulate data for a given true mean curve and model
  switch(model.mode,
         "Normal"={ ## Normal model
           data = matrix(data=rnorm(length(true.mean.curve)*num.samples, true.mean.curve, normal.sigma), nr = num.samples, byrow = TRUE)
         },
         "Poisson"={ # Poisson model 
           data = matrix(data=rpois(length(true.mean.curve)*num.samples, true.mean.curve), nr = num.samples, byrow = TRUE)
         },
         ) # end switch
  
  return(list(true.mean.curve = true.mean.curve, data = data,model.mode=model.mode))
  
}


#' Simulate multiple curve data from a given model (either Normal or Poisson). It checks if parameters are valid and calls \code{sim.data} to simulate data. 
#'
#' @param curve.length: length of curve data 
#' @param model.mode: either Normal or Poisson; this function simulates \code{num.samples} number of curve data under a model provided as \code{model.mode}.
#' @param num.groups: 1 by default; how many groups of samples should be simulated; currently this function can simulate data from one group. 
#' @param num.samples: 10 by default; how many curve data should be simulated.
#' @param normal.sigma: 0.5 by default; standard deviation in Normal model; 
#' @param curve.str: "mine" by default; curve type for true signal; used in the function \code{simulate_true_curve}.
#' @param stretch: 5 by default; parameter used to generate true signal; used in the function \code{simulate_true_curve}.
#' @param offset; 15 by default; parameter used to generate true signal; used in the function \code{simulate_true_curve}.
#'
#' @return \code{simu.curves} returns a list of the following elements
#' \item{true.mean.curve}{a vector of length \code{curve.length}-vector; it contains true mean curve generated by the function \code{simulate_true_curve}}
#' \item{data}{a matrix of \code{num.samples} by \code{curve.length}; it contains \code{num.samples} curve data; each row contains simulated data under a given model \code{model.mode} with mean curve \code{true.mean.curve}}
#'
#' @examples
#' # set up parameters.
#' curve.length = 1024
#' model.mode = 'Poisson'
#' num.samples=10
#' set.seed(666)
#' res.pois = simu.curves(curve.length=curve.length, model.mode=model.mode, num.samples=num.samples)
#' str(res.pois)
#' 
#' @export
#' 
simu.curves <- function(curve.length, model.mode, num.groups=1, num.samples=10, normal.sigma=0.5, stretch=5, offset=15, curve.str="mine"){
  #check input
  #curve.length
  if(curve.length%%1!=0 || curve.length < 0){
    stop("curve.length is number of data, must be positive integer")
  }
  #model.mode
  if(model.mode!="Poisson" && model.mode!="Normal"){
    stop("model.mode should be either 'Poisson' or 'Normal' ")
  }
  #num.groups
  if(num.groups%%1!=0 || (num.groups != 1)){
    stop("num.groups indicates the number of groups for samples. For now, simulate.data can simulate multiple curves for one group. So num.groups should be one.")
  }
  #num.samples
  if(num.samples%%1!=0 || num.samples < 0){
    stop("num.samples indicates the number of curve data. So it must be positive integer")
  }
  #normal.sigma
  if(normal.sigma < 0){
    stop("normal.sigma is a standard deviation in Normal model, variance must be large than zero")
  }
  
  x <- gen.curves(curve.str, curve.length, stretch, offset, model.mode, num.groups, num.samples, normal.sigma)
  return(x)
}
shimlab/HMTree documentation built on May 29, 2019, 9:25 p.m.