R/xDifference.R

Defines functions graphDifference.formula graphDifference.default graphDifference nhstDifference .nhstDifference.htest .nhstDifference.formula .nhstDifference.default .nhstDifference ciDifference .ciDifference.htest .ciDifference.formula .ciDifference.default .ciDifference

# easiOrigin
## Mean Differences

### Confidence Intervals

.ciDifference <- function(x, ...) {
  UseMethod(".ciDifference")
}

.ciDifference.default <- function(frame, ...) {
  data <- data.frame(frame)
  x <- data[, 1]
  y <- data[, 2]
  object <- t.test(x, y, paired = TRUE, ...)
  .ciDifference(object, ...)
}

.ciDifference.formula <- function(formula, ...) {
  object <- t.test(formula, ...)
  .ciDifference(object, ...)
}

.ciDifference.htest <- function(object, conf.level = .95, ...) {
  if (length(object$estimate) == 1) {
    MD <- as.numeric(object$estimate)
  } else {
    MD <- as.numeric(object$estimate[1]) - as.numeric(object$estimate[2])
  }
  df <- as.numeric(object$parameter)
  SE <- as.numeric(object$stderr)
  LL <- object$conf.int[1]
  UL <- object$conf.int[2]
  results <- data.frame(Diff = MD, SE = SE, df = df, LL = LL, UL = UL)
  rownames(results) <- c("Comparison")
  results
}

ciDifference <- function(..., conf.level = .95, main = NULL, digits = 3) {
  results <- .ciDifference(..., conf.level = conf.level)
  if (is.null(main)) {
    main <- "Confidence Interval for the Mean Difference"
  }
  .formatList(list(results), main = main, digits = digits)
}

### Null Hypothesis Significance Tests

.nhstDifference <- function(x, ...) {
  UseMethod(".nhstDifference")
}

.nhstDifference.default <- function(frame, ...) {
  data <- data.frame(frame)
  x <- data[, 1]
  y <- data[, 2]
  object <- t.test(x, y, paired = TRUE, ...)
  .nhstDifference(object, ...)
}

.nhstDifference.formula <- function(formula, ...) {
  object <- t.test(formula, ...)
  .nhstDifference(object, ...)
}

.nhstDifference.htest <- function(object, ...) {
  mu <- as.numeric(object$null.value)
  if (length(object$estimate) == 1) {
    MD <- as.numeric(object$estimate) - mu
  } else {
    MD <- as.numeric(object$estimate[1]) - as.numeric(object$estimate[2]) - mu
  }
  df <- as.numeric(object$parameter)
  SE <- as.numeric(object$stderr)
  t <- as.numeric(object$statistic)
  df <- as.numeric(object$parameter)
  p <- as.numeric(object$p.value)
  results <- data.frame(Diff = MD, SE = SE, t = t, df = df, p = p)
  rownames(results) <- c("Comparison")
  results
}

nhstDifference <- function(..., main = NULL, digits = 3) {
  results <- .nhstDifference(...)
  if (is.null(main)) {
    main <- "Hypothesis Tests for the Mean Difference"
  }
  .formatList(list(results), main = main, digits = digits)
}

### Confidence Interval Plots

graphDifference <- function(x, ...) {
  UseMethod("graphDifference")
}

graphDifference.default <- function(frame, main = NULL, ylab = "Mean Difference", xlab = "", mu = NULL, rope = NULL, conf.level = .95, values = TRUE, ylim = NULL, digits = 3, pch = 17, col = "black", ...) {
  results <- .ciDifference(frame, conf.level = conf.level)[, c("Diff", "LL", "UL")]
  if (is.null(main)) {
    main <- "Confidence Interval for the Mean Difference"
  }
  .cipMain(results, main = main, ylab = ylab, xlab = xlab, mu = mu, rope = rope, values = values, ylim = ylim, digits = digits, connect = TRUE, pch = pch, col = col)
}

graphDifference.formula <- function(formula, main = NULL, ylab = "Mean Difference", xlab = "", mu = NULL, rope = NULL, conf.level = .95, values = TRUE, ylim = NULL, digits = 3, pch = 17, col = "black", ...) {
  results <- .ciDifference(formula, conf.level = conf.level)[, c("Diff", "LL", "UL")]
  if (is.null(main)) {
    main <- "Confidence Interval for the Mean Difference"
  }
  .cipMain(results, main = main, ylab = ylab, xlab = xlab, mu = mu, rope = rope, values = values, ylim = ylim, digits = digits, connect = FALSE, pch = pch, col = col)
}
cwendorf/EASIalt documentation built on Oct. 31, 2023, 1:20 a.m.