R/t_testAB.R

Defines functions `t_testAB`

`t_testAB` <-
function(x, y = NULL,
         alternative = c("two.sided", "less", "greater"), var.equal = FALSE,
         mu = 0, paired = FALSE, conf.level = 0.95, data) {
    
    if (identical(alternative, c("two.sided", "less", "greater"))) {
        alternative <- "two.sided"
    }

    if (is.element(alternative, c("two.sided", "!=", "two.tailed"))) {
        pmatching <- 1
    }
    else if (is.element(alternative, c("less", "<", "lower"))) {
        pmatching <- 2
    }
    else if (is.element(alternative, c("greater", ">", "higher"))) {
        pmatching <- 3
    }
    else {
        admisc::stopError("Unknown alternative hypothesis specification.")
    }

    alternative <- c("two.sided", "less", "greater")[pmatching]
    
    if (!paired) {
        homogeneity <- c("not equal", "equal")
        
        if (is.null(y)) {
            homogtest <- ansari.test(x, data = data, exact = FALSE)
        }
        else {
            homogtest <- ansari.test(x, y, exact = FALSE)
        }
        
        p.value <- homogtest$p.value
        var.equal <- ifelse(p.value > (1 - conf.level), TRUE, FALSE)
        cat (
            "\nThe Ansari-Bradley test for the homogeneity of variances has a value of",
            paste("p = ", round(p.value, 4), sep = ""),
            " therefore the variances are ",
            homogeneity[var.equal + 1],
            "\n",
            sep = ""
        )
    }
    
    if (is.null(y)) {
        test <- t.test(
            x,
            alternative = alternative,
            conf.level = conf.level,
            var.equal = var.equal,
            paired = paired, data = data
        )
        }
    else {
        test <- t.test(
            x,
            y,
            alternative = alternative,
            conf.level = conf.level,
            paired = paired,
            var.equal = var.equal
        )
        }
        
    print(test)
    
    if (paired) {
        return(invisible(list("t.test" = test)))
    }
    else {
        return(
            invisible(
                list("Homogeneity of variances" = homogtest, "t.test" = test)
            )
        )
    }
}
dusadrian/statistics documentation built on Jan. 26, 2023, 11:55 p.m.