pwr_base_n.version = "0.3.5";
# Required packages-----------
packages = c("pwr", "effsize");
#use this function to check if each package is on the local machine
#if a package is installed, it will be loaded
#if any are not, the missing package(s) will be installed and loaded
package.check <- lapply(packages, FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE);
}
})
###### End Packages
###### Documentation--------------------------------------------------
#' pwr.base_n
#'
#' This function finds base-n based on smallest number of participants (>10) needed to reach power >= .95
#' @param x Group 1
#' @param y Group 2
#' @param alpha alpha value for testing, defaults to .05
#' @param power pwr criteria, defaults to .95
#' @param paired.sample Boolean to run Paired-Sample t-Test, defaults to FALSE
#' @keywords base-n, pwr
#' @export
#' @examples
#' pwr_base_n(group1, group2, .85, paired.sample=TRUE)
#' pwr_base_n(group1, group2, power=.95)
#####Function Start
pwr.base_n <- function(g1, g2, alpha=.05, power=.85, paired=FALSE) {
# initialize pwr.results variable as data frame to keep participants, p values, effect size, and power values
pwr.results <- data.frame("participants"= 1, "p.value" = 1, "d" = 0, "power" = 0);
#Assign group data
group1 <- g1;
group2 <- g2;
# Incremental t-Tests========================
#i.e. 1-2, 1-3, 1-4, 1-5, etc..
for(k in 2:length(group1)) {
### Paired Sample = FALSE -------------------------
# Run independant Sample
if (paired == FALSE || missing(paired) == TRUE) {
#Makes sure variance is non-zero prior to running ind-sample t-test
if (var(group1[1:k]) != 0 & var(group2[1:k]) != 0){
#Saves iterative p values
p_value <- t.test(group1[1:k],group2[1:k])$p.value;
#save effect size
d_value <- cohen.d(group1[1:k],group2[1:k])$estimate;
#Saves iterative power values
pwr_value <- pwr.t.test(n=k, d_value, sig.level=alpha, power=NULL, type="two.sample")$power;
#combine current iteration results to new row
pwr.results <- rbind(pwr.results, list(k, p_value, d_value, pwr_value), stringsAsFactors=FALSE);
#If variance = 0, return n as -1 value
} else {
return(list(participants = -1))
}
} #end if
# Paired Sample = TRUE ----------------
# Run paired Sample
if (paired == TRUE) {
#Makes sure there is variance before running paired sample t-test
if (sd(group1[1:k] - group2[1:k]) != 0 ){
# tryCatch
tryCatch({ #checks for errors
#Saves iterative p values in pwr.results
p_value <- t.test(group1[1:k],group2[1:k], paired = TRUE)$p.value;
#save effect size
d_value <- cohen.d(group1[1:k], group2[1:k])$estimate;
#save iterative power test in pwr.results
pwr_value <- pwr.t.test(n = k, d_value, sig.level = alpha, power = NULL, type = "paired")$power;
#combine current iteration results with data frame
pwr.results <- rbind(pwr.results, list(k, p_value, d_value, pwr_value), stringsAsFactors=FALSE);
},
#If there is an error, return n as -1
error = function(err) {
return(list(participants = -1))
}); #End tryCatch---
#If there is not variance, return n as -1
} else {
return(list(participants = -1))
}
} #end if
} #end for loop
### Return base_n ------------
min_n <- 10;
#get first row that is >= power and >= min_n
min_power = head(pwr.results[pwr.results$power >= power & pwr.results$participants >= min_n,], 1);
###### If power criteria IS met---------------
if (!is.null(min_power$participants) & length(min_power$participants) > 0) {
# return participants, p.value, d, and power of row
return(list(participants = min_power[1,1], p.value = min_power[1,2], d = min_power[1,3], power = min_power[1,4]));
##### if power criteria NOT met
} else {
#if no power levels matching criterion are found, return base_n as 0 with summary data.
#max_pwr = max( pwr.results$power[10:NROW(pwr.results$power)]);
#sample_pwr = pwr.results$power[NROW(pwr.results$power)];
n = 0;
return(participants = n);
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.