#' Within-subject CIs
#'
#' Calculate within-subject confidence intervals (Baguley, 2012)
#'
#' @param data.frame Data frame
#' @param conf.level CI
#' @param difference Dunno
#' @return Something
#'
#' @examples
#'
#' #cm.ci()
#'
#' @export
#'
cm.ci <- function(data.frame, conf.level = 0.95, difference = TRUE) {
#cousineau-morey within-subject CIs
k = ncol(data.frame)
if (difference == TRUE)
diff.factor = 2^0.5/2
else diff.factor = 1
n <- nrow(data.frame)
df.stack <- utils::stack(data.frame)
index <- rep(1:n, k)
p.means <- tapply(df.stack$values, index, mean)
norm.df <- data.frame - p.means + (sum(data.frame)/(n * k))
t.mat <- matrix(, k, 1)
mean.mat <- matrix(, k, 1)
for (i in 1:k) t.mat[i, ] <- stats::t.test(norm.df[i])$statistic[1]
for (i in 1:k) mean.mat[i, ] <- colMeans(norm.df[i])
c.factor <- (k/(k - 1))^0.5
moe.mat <- mean.mat/t.mat * stats::qt(1 - (1 - conf.level)/2, n - 1) * c.factor *
diff.factor
ci.mat <- matrix(, k, 2)
dimnames(ci.mat) <- list(names(data.frame), c("lower", "upper"))
for (i in 1:k) {
ci.mat[i, 1] <- mean.mat[i] - moe.mat[i]
ci.mat[i, 2] <- mean.mat[i] + moe.mat[i]
}
ci.mat
}
#' Between-subject CIs
#'
#' Calculate between-subject confidence intervals (Baguley, 2012).
#'
#' @param data.frame Data frame
#' @param conf.level CI
#' @param difference Dunno
#' @return Something
#'
#' @examples
#'
#' #bs.ci()
#'
#' @export
#'
bs.ci <- function(data.frame, conf.level = 0.95, difference = FALSE) {
# between-subject CIs
k = ncol(data.frame)
n <- nrow(data.frame)
df.stack <- utils::stack(data.frame)
group.means <- colMeans(data.frame, na.rm = TRUE)
if (difference == TRUE)
ci.mat <- (stats::confint(stats::lm(values ~ 0 + ind, df.stack)) - group.means) *
2^0.5/2 + group.means
else ci.mat <- stats::confint(stats::lm(values ~ 0 + ind, df.stack))
dimnames(ci.mat) <- list(names(data.frame), c("lower", "upper"))
ci.mat
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.