iterative.ttest.version = "0.1";
# Required packages-----------
packages = c("pwr", "effsize", "ggplot2");
#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--------------------------------------------------
#' iterative.ttest()
#'
#' This function incrementally increases the number of participants and does t.test for each
#' @param x Group 1
#' @param y Group 2
#' @param alpha alpha value for testing, defaults to .05
#' @param paired Boolean to run Paired-Sample t-Test, defaults to FALSE
#' @keywords iterative, t.test, p trend
#' @export
#' @examples
#' iterative.ttest(group1, group2, alpha=.01 paired=TRUE)
iterative.ttest <- function(x, y, alpha=.05, paired = FALSE) {
# initialize results variable as data frame to keep participants, p values, effect size, and power values
results <- data.frame("participants"= 1, "statistic" = 1, "df" = 0, "p.value" = 1);
#Assign group data
group1 <- x;
group2 <- y;
# 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) {
test_results <- t.test(group1[1:k],group2[1:k]);
#combine current iteration results to new row
results <- rbind(results, list(k, test_results$statistic, test_results$parameter, test_results$p.value), stringsAsFactors=FALSE);
} #end if
# Paired Sample = TRUE ----------------
# Run paired Sample
if (paired == TRUE) {
test_results <- t.test(group1[1:k],group2[1:k], paired=TRUE);
#combine current iteration results to new row
results <- rbind(results, list(k, test_results$statistic, test_results$parameter, test_results$p.value), stringsAsFactors=FALSE);
} #end if
} #end for loop
assign("iterative_test",results, envir=.GlobalEnv);
ggplot(iterative_test, aes(iterative_test$participants, iterative_test$p.value)) +
geom_point() +
xlab("Participants") +
ylab("p value") +
geom_hline(yintercept=alpha, linetype="dashed", color = "red")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.