R/RCSample.R

Defines functions RCSample

Documented in RCSample

#' Simulate A Random Continious Sampling of Equal Sizes.
#'
#' @param base.pop Define the desired continuos distribution to be sampled from.  The function can handle any of the following
#' continous distributions: "Normal", "Exponential", "Logistic", "Log Normal", "Uniform", & "Weibull".
#' @param n.sample On each sampling trial, i.e. t.trail, this is the number of repeat samples to be taken and is thought of as
#' the measure of the processes repeatability at that moment in time.  Only a single number may be used and it is assumed that
#' equal proporation sampling will be done on each trial with this sample size.
#' @param t.trials This defines the number of sampling points throughout the manufacturin process were a defined number of repeat
#' samples, i.e. n.sample, will be taken.  The total collection of trials is thought of as the measure of the processes
#' reproducability.  Unlike n.sample, t.trials may be a single number or a vector of numbers.  For example, if only a single
#' number is provided, e.g. 1, its assumed that there is only one instance where n.sample is executed and the t.trials is
#' essentially ignored; better thought of as pulling a large random sampling to approximate the underlying population.  However,
#' if a vector of numbers is provided, e.g. 1:5, the output assumes that for the 1st though the 5th trials n.samples will be
#' drawn for approximating the processes repeatability and reproducability.  In addition, one could set this to a delayed
#' sampling, e.g. 6:10, where shifts in the manufacturing process may occur and t.trials from 1:5 can be combined with the
#' output from 6:10 to craete population shifts.
#' @param seed Becuase this is a simulation of random numbers, a random number generator is used and a "seed" is specified
#' for reproducability.  This is used to set the "set.seed" function from the "base" package.  See documention around the
#' "set.seed" function for more details.
#' @param norm.mean One of two paramaters requried for the "rnorm" function in the "stats" package.  See documention around the
#' "rnorm" function for more details. Normal Distribution random sampling.
#' @param norm.sd One of two paramaters requried for the "rnorm" function in the "stats" package.  See documention around the
#' "rnorm" function for more details.Normal Distribution random sampling.
#' @param exp.rate Single paramater requried for the "rexp" function in the "stats" package.  See documention around the
#' "rexp" function for more details.  Exponential Distribution random sampling.
#' @param log.location One of two paramaters requried for the "rlogis" function in the "stats" package.  See documention around
#' the "rlogis" function for more details.  Logistic Distribution random sampling.
#' @param log.scale One of two paramaters requried for the "rlogis" function in the "stats" package.  See documention around
#' the "rlogis" function for more details.  Logistic Distribution random sampling.
#' @param lnorm.mean One of two paramaters requried for the "rlnorm" function in the "stats" package.  See documention around
#' the "rlnorm" function for more details.  Log Normal Distribution random sampling.
#' @param lnorm.sd One of two paramaters requried for the "rlnorm" function in the "stats" package.  See documention around
#' the "rlnorm" function for more details.  Log Normal Distribution random sampling.
#' @param uni.min One of two paramaters requried for the "runif" function in the "stats" package.  See documention around
#' the "runif" function for more details.  Uniform Distribution random sampling.
#' @param uni.max One of two paramaters requried for the "runif" function in the "stats" package.  See documention around
#' the "runif" function for more details.  Uniform Distribution random sampling.
#' @param wei.shape One of two paramaters requried for the "rweibull" function in the "stats" package.  See documention around
#' the "rweibull" function for more details.  Weibull Distribution random sampling.
#' @param wei.scale One of two paramaters requried for the "rweibull" function in the "stats" package.  See documention around
#' the "rweibull" function for more details.  Weibull Distribution random sampling.
#' @return A tibble, i.e. data frame, consiting of the result from the paramaters of \code{base.pop}, \code{n.sample},
#' \code{t.trails}, \code{seed}, and the necessary paramaters for the desired continuose populatoins.
#' @export
#' @examples
#' RCSample(base.pop = "Normal", t.trials = 1, n.sample = 100, seed = 647, norm.mean = 100, norm.sd = 10)
#' RCSample(base.pop = "Normal", t.trials = 1:10, n.sample = 30, seed = 52, norm.mean = 100, norm.sd = 10)
#' RCSample(base.pop = "Exponential", t.trials = 3:5, n.sample = 5, seed = 12345, exp.rate = 0.5)
#' RCSample(base.pop = "Logistic", t.trials = 1:5, n.sample = 10, seed = 89, log.location = 0, log.scale = 1)
#' RCSample(base.pop = "Log Normal", t.trials = 1:30, n.sample = 15, seed = 10, lnorm.mean = 10, lnorm.sd = 2)
#' RCSample(base.pop = "Uniform", t.trials = 8:9, n.sample = 25, seed = 872, uni.min = 0, uni.max = 10)
#' RCSample(base.pop = "Weibull", t.trials = 1:100, n.sample = 7, seed = 2, wei.shape = 10, wei.scale = 5)

RCSample <- function(base.pop, t.trials, n.sample, seed,
                     norm.mean = NULL,
                     norm.sd = NULL,
                     exp.rate = NULL,
                     log.location = NULL,
                     log.scale = NULL,
                     lnorm.mean = NULL,
                     lnorm.sd = NULL,
                     uni.min = NULL,
                     uni.max = NULL,
                     wei.shape = NULL,
                     wei.scale = NULL
                     ){

  set.seed(seed)

  ifelse(base.pop == "Normal", drawing.pop <- rnorm(n = 100000, mean = norm.mean, sd = norm.sd),
    ifelse(base.pop == "Exponential", drawing.pop <- rexp(n = 100000, rate = exp.rate),
      ifelse(base.pop == "Logistic", drawing.pop <- rlogis(n = 100000, location = log.location, scale = log.scale),
        ifelse(base.pop == "Log Normal", drawing.pop <- rlnorm(n = 100000, meanlog = lnorm.mean, sdlog = lnorm.sd),
          ifelse(base.pop == "Uniform", drawing.pop <- runif(n = 100000, min = uni.min, max = uni.max),
            ifelse(base.pop == "Weibull", drawing.pop <- rweibull(n = 100000, shape = wei.shape, scale = wei.scale),
              stop('Must use a valid continuous distribution!')
                    )
                  )
                )
              )
            )
         )

  sampled <- data_frame()

  for(i in t.trials){

    set.seed(seed + i)
    step <- i
    result <- sample(drawing.pop, size = n.sample)
    smp.id <- 1:length(result)
    sampling <- data_frame("Sampled.Pop" = base.pop,
                           "Trial" = i,
                           "Sample" = smp.id,
                           "Result" = result)
    sampled <- rbind(sampled, sampling)

  }

  return(sampled)

}
upi-qe/qualityr documentation built on Nov. 5, 2019, 11:05 a.m.