R/seq_aipe_cv.R

Defines functions seq_aipe_cv

Documented in seq_aipe_cv

#' seq_aipe_cv
#'
#' @description Sequential approach to Accuracy in Parameter Estimation for Effect Sizes
#' (AIPE): Coefficient of Variation
#'
#' @param alpha The significance level., default is 0.05.
#' @param omega omega
#' @param data The dataset for which to calculate the coefficient of
#' variation.
#' @param pilot Shoulda pilot sample be generated.
#' @param m0 The initial sample size.
#' @param na.rm This parameter controls whether NA values are removed from
#' the data prior to calculation. Default is \code{TRUE}.
#'
#' @return The current sample size, the coefficient of variation, an indicator
#' of if the criterion is satisfied, and the confidence interval.
#'
#' @author Ken Kelley \email{KKelley@@nd.edu},
#' Francis Bilson Darku \email{FBilsonD@nd.edu},
#' Bhargab Chattopadhyay \email{Bhargab@iiitvadodara.ac.in}
#'
#' @export seq_aipe_cv
#'
#' @examples
#' pilot_ss <- seq_aipe_cv(alpha=0.05, omega=0.2, pilot=TRUE)
#' SLS <- rexp(pilot_ss, rate=0.05)
#' seq_aipe_cv(alpha=0.05, omega=0.2,data = SLS)
#'
seq_aipe_cv <- function(alpha = 0.05, omega, data = NULL,
                       pilot = FALSE, m0 = 4, na.rm=TRUE)
{

  if (missing(alpha) && missing(omega))
    stop("You must specify \'omega\' and \'alpha\'.")
  if (!is.data.frame(data) && !is.matrix(data) && !is.vector(data) & !is.null(data))
    stop("The argument 'data' must be a data.frame or matrix with one column")
  if (dim(data)[2] != 1 && !is.null(data) && !is.vector(data))
    stop("The argument 'data' must have only one column, or be 'NULL' for pilot = TRUE")
  if (alpha <= 0 || alpha >= 1)
    stop("The argument")
  if (pilot == FALSE)
  {
    if(na.rm){
      data <-  data[!is.na( data)]
    }
    stop <- FALSE
    n <- length(data)
    CV <- V2_cv(data)
    V2n <- V2_smd(data, data) # This is wrong
    Criterion <- ceiling((2*stats::qnorm(1 - alpha/2)/omega)^2 *
                           (V2n + 1/n))

    if (n >= Criterion) Stop <- TRUE
    if (n < Criterion) Stop <- FALSE
    lci <- CV - stats::qnorm(1 - alpha/2)*sqrt(V2n/n)
    uci <- CV + stats::qnorm(1 - alpha/2)*sqrt(V2n/n)
    ci <- c(lci, uci)
    if (Stop == FALSE){
      print("The stopping rule has not yet been met;
            sample size is not large enough")
      Outcome <- list("Current.n" = n, "Current.cv" = CV,
                      "Is.Satisfied?" = Stop)
    }

    if (Stop == TRUE){
      print("The stopping rule has been met; sample size is large enough.")
      Outcome <- list("Current.n" = n, "Current.cv" = CV,
                      "Is.Satisfied?" = Stop,
                      "Confidence Interval"= ci)
    }

  }
  if(pilot==TRUE){
    if(m0 < 4) stop("The value of 'm0' must be 4 or greater.")
    Outcome <- max(m0, ceiling(2*stats::qnorm(1 - alpha/2)/omega))
  }
  return(Outcome);
}
yelleKneK/SMSD documentation built on Nov. 23, 2022, 6:40 p.m.