R/ttest.R

Defines functions writeup.htest one_sample two_sample format_mean_sd format_names use_wrapTTest

Documented in use_wrapTTest writeup.htest

#' Report t-test in APA style
#'
#' Report mean, st. dev., t, df, p.
#'
#' @section One Sample:
#' The difference between [mu-value] and the mean of [x] (M=[mean(x)], SD=[sd(x)]) was [not] significant at alpha < [threshold] as revealed by One Sample t-test, two.sided: (t=[r], df=[df], p=[p]).
#'
#' @section Two Sample Independent:
#' The difference in means of [x] (M=[mean(x)], SD=[sd(x)]) and [y] (M=[mean(y)], SD=[sd(y)]) was significant at alpha < [threshold] as revealed by Welch Two Sample t-test, two.sided: (t=[r], df=[df], p=[p]).
#'
#' @section One Sample Paired:
#' The difference in means of [x] (M=[mean(x)], SD=[sd(x)]) was not significant at alpha < [threshold] as revealed by Paired t-test, two.sided: (t=[r], df=[df], p=[p]).
#' @param x Object of class \code{htest}, see t.test

writeup.htest = function(x) {
  if (x$method == "One Sample t-test")
    one_sample(x)
  else
    two_sample(x)
}

one_sample = function(x) {
  paste("The difference between", x$null.value, "and the mean of",
        format_names(x), was_sig(x$p.value), use_wrapTTest(x))
}

two_sample = function(x) {
  paste("The", names(x$null.value), "of",
        paste(format_names(x), format_mean_sd(x), collapse = " and "),
        was_sig(x$p.value), use_wrapTTest(x))
}

format_mean_sd = function(x) {
  means = paste("M=", round(x$estimate, getOption("digits")), sep = "")
  stdevs = paste("SD=", round(x$stdev, getOption("digits")), sep = "")
  paste("(", means, ", ", stdevs, ")", sep = "")
}

format_names = function(x) {
  all_names = strsplit(x$data.name, "( by | and )")
  if (x$method == "Paired t-test")
    unlist(all_names)[1]
  else
    unlist(all_names)
}

#' Use wrapTTest
#'
#' Use wrapTTest() in a sentence
#' @param x An object of class \code{htest}
#' @importFrom sigr render wrapTTest

use_wrapTTest = function(x) {
  paste0(
    "as revealed by ",
    render(wrapTTest(x))
  )
}
jchrom/aparep documentation built on May 18, 2019, 10:22 p.m.