find_base_n.version = "1.5";
######Base n--------------------------------------------------
#' Find Base n
#'
#' This function finds the base n based on smallest number of participants (>10) needed for significance.
#' @param data_set Takes in a data.frame
#' @param alpha Alpha criteria, defaults to .05
#' @param paired Boolean to run Paired-Sample t-Test, defaults to FALSE
#' @keywords base-n
#' @export
#' @examples
#' find_base_n(dataset_1, .05, TRUE)
#' find_base_n(dataset_2, .001)
find_base_n <- function(data_set, alpha=.05, paired=FALSE) {
#initialize pvalues variable as vector
pvalues <- data.frame("participants"= 1, "p.value" = 1);
#Convert piped in dataset to a data frame
data_set <- as.data.frame(
data_set,
row.names = NULL,
optional = FALSE,
cut.names = FALSE,
col.names = names(data_set),
fix.empty.names = TRUE,
stringsAsFactors = FALSE);
#######Incremental t-Tests------------------------
#i.e. 1-2, 1-3, 1-4, 1-5, etc..
for(k in 2:nrow(data_set)) {
#Paired Sample = FALSE - Run independant Sample
if (paired == FALSE || missing(paired) == TRUE) {
#Makes sure there is variance prior to running ind-sample t-test
#Use apply to get variance values rows 1:k, in columns 1 and 2
#Use all to compare variance to 0
if (all(apply(data_set[1:k,], 2, var) != 0 )){
#Saves iterative p values in a vector
pvalues <- rbind(pvalues, c(k, t.test(data_set[1:k,1],data_set[1:k,2])$p.value));
}
else {
#if no variance found, return -1
return(-1);
}
} #end if
#Paired Sample = TRUE - Run paired Sample
if (paired == TRUE) {
#Makes sure there is variance before running paired sample t-test
#Use apply to get variance values in both rows and colums
#Use all to compare variances to 0
if (sum(apply(data_set[1:k,], 1, var)) > 0 &
sum(apply(data_set[1:k,], 2, var)) > 0 ){
#tryCatch---
tryCatch({ #checks for errors
#Saves iterative p values in a vector
pvalues <- rbind(pvalues, c(k, t.test(data_set[1:k,1],data_set[1:k,2], paired = TRUE)$p.value));
},
#If there is an error, return -1
error = function(err) {
return(-1);
}); #End tryCatch---
}
#If there is not variance, return -1
else {
return(-1);
}
} #end if
} #End For Loop
i=2; #starts with atleast 2 participants
######Minimum Sig. Value------------------
#For debugging, saves list of pvalues to Global env
assign('pvalues', pvalues, envir=.GlobalEnv);
###Minimum base_n allowed----------
min_n <- 10;
#Return number of participants needed for significance with a minimum number of participants examined
min_p = head(pvalues[pvalues$p.value <= alpha & pvalues$participants >= min_n,], 1);
if (length(min_p$participants) > 0) {
return(min_p$participants)
} else {
return(0)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.