R/wrap.anova.simple.R

Defines functions wrap.anova.simple

Documented in wrap.anova.simple

#' ANOVA (simple main effects)
#'
#' @description Computes simple main effects for a two-way, between-subjects ANOVA.
#' The function delegates the primary computations to \code{\link[phia]{testInteractions}}.
#' Note that this function assumes categorical (i.e., unordered) independent variables
#' and fixed effects. In the output, hp2 denotes partial eta squared.
#'
#' @param dv1 Column vector containing the dependent variable
#' @param iv1,iv2 Column vectors containing the between-subjects independent variables.
#' The function will test for simple main effects of \code{iv1} separately at each level
#' of \code{iv2}.
#' @param adjustment String representing the method of p value adjustment
#' ("none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", or "fdr").
#'
#' @seealso \code{\link[phia]{testInteractions}}
#'
#' @examples
#' wrap.anova.simple(dv1 = bdata$DV1, iv1 = bdata$IV1, iv2 = bdata$IV2)
#'
#' @import stringr effsize stats
#' @export
wrap.anova.simple <- function(dv1,iv1,iv2,adjustment="none") {

  if (!requireNamespace("phia", quietly = TRUE)) {
    stop("This function requires first installing the package \"phia\". Please install this package.",
         call. = FALSE)
  }
  if (!requireNamespace("car", quietly = TRUE)) {
    stop("This function requires first installing the package \"car\". Please install this package.",
         call. = FALSE)
  }

  requireNamespace("phia"); requireNamespace("car")

  # Error checks
  if(is.null(dv1)) {return(paste("Cannot find the column vector inputted to parameter dv1."))}
  if(is.null(iv1)) {return(paste("Cannot find the column vector inputted to parameter iv1."))}
  if(is.null(iv2)) {return(paste("Cannot find the column vector inputted to parameter iv2."))}
  if(is.null(iv1)==F) {if(is.factor(iv1)==F) {print("Note: Argument iv1 will be converted to a factor variable.")}}
  if(is.null(iv2)==F) {if(is.factor(iv2)==F) {print("Note: Argument iv2 will be converted to a factor variable.")}}
  if(is.null(dv1)==F) {if(is.numeric(dv1)==F) {return("Argument dv1 must be numeric.")}}

  iv2 <- factor(iv2); iv1 <- factor(iv1)

  levels_iv2 <- nlevels(iv2)
  levels_variable <- nlevels(iv1)

  # compute simple effects
  anovamodel <- lm(dv1 ~ iv2*iv1)
  interaction <- phia::testInteractions(anovamodel,fixed=names(anovamodel$xlevels)[1],across=names(anovamodel$xlevels)[2],adjustment=adjustment)

  # subset the dv1 for each level of the IV, so that you can analyze each level separately
  df <- data.frame(iv2,iv1,dv1)
  colnames(df) <- c("fixed","variable","dv1")
  hp2.list <- as.list(rep(0,levels_iv2))

  for (i in 1:levels_iv2) {
    hp2.list[[i]] <- interaction["Sum of Sq"][[1]][i]/(interaction["Sum of Sq"][[1]][i]+interaction["Sum of Sq"][[1]][levels_iv2+1])
  }

  print("ASSUMPTIONS: The function assumes categorical (i.e., unordered) independent variables and fixed effects.")
  clip <- ""
  for (i in 1:levels_iv2) {
    clip <- paste(clip,"# ",rownames(interaction)[i],": ","F(",interaction$Df[i],", ",interaction$Df[levels_iv2+1],") = ",wrap.rd0(interaction$F[i],2),", p",if(as.numeric(interaction$'Pr(>F)'[i]) < .001) {" < .001"},if(as.numeric(interaction$'Pr(>F)'[i]) >= .001) {" = "},if (as.numeric(interaction$'Pr(>F)'[i]) >= .001) {wrap.rd((interaction$'Pr(>F)'[i]),3)},", hp2 = ",wrap.rd(hp2.list[[i]],2),sep="")
  }
  wrap.writeClipboard(clip)

  return(
    for (i in 1:levels_iv2) {
      cat("\n","# ",rownames(interaction)[i],": ","F(",interaction$Df[i],", ",interaction$Df[levels_iv2+1],") = ",wrap.rd0(interaction$F[i],2),", p",if(as.numeric(interaction$'Pr(>F)'[i]) < .001) {" < .001"},if(as.numeric(interaction$'Pr(>F)'[i]) >= .001) {" = "},if (as.numeric(interaction$'Pr(>F)'[i]) >= .001) {wrap.rd((interaction$'Pr(>F)'[i]),3)},", hp2 = ",wrap.rd(hp2.list[[i]],2),sep="")
    }
  )
}
michaelkardas/behavioralwrappers documentation built on Jan. 2, 2020, 7:46 a.m.