diff --git a/R/anova.art.R b/R/anova.art.R index 8198fbc..ec0ba8c 100755 --- a/R/anova.art.R +++ b/R/anova.art.R @@ -1,27 +1,27 @@ # ANOVAs for art objects -# +# # Author: mjskay ############################################################################### #' Aligned Rank Transform Analysis of Variance -#' +#' #' Conduct analyses of variance on aligned rank transformed data. -#' +#' #' This function runs several ANOVAs: one for each fixed effect term in the #' model \code{object}. In each ANOVA, the independent variables are the same, #' but the response is aligned by a different fixed effect term (if response is #' "aligned") or aligned and ranked by that fixed effect term (if response is #' "art"). These models are generated using \code{\link{artlm}}. -#' +#' #' From each model, only the relevant output rows are kept (unless #' \code{all.rows} is \code{TRUE}, in which case all rows are kept). -#' +#' #' When \code{response} is \code{"art"} (the default), only one row is kept #' from each ANOVA: the row corresponding to fixed effect term the response was #' aligned and ranked by. These results represent nonparametric tests of #' significance for the effect of each term on the original response variable. -#' +#' #' When \code{response} is \code{"aligned"}, all rows \emph{except} the row #' corresponding to the fixed effect term the response was aligned by are kept. #' If the ART procedure is appropriate for this data, these tests should have @@ -29,7 +29,7 @@ #' case, another analysis should be considered. This diagnostic is tested by #' \code{\link{summary.art}} and a warning generated if the F values are not #' all approximately 0. -#' +#' #' @name anova.art #' @rdname anova.art #' @aliases anova.art print.anova.art @@ -55,9 +55,9 @@ #' (\code{FALSE}), shows only the rows that are relevant depending on the type #' of \code{response}. #' @param x An object of class \code{\link{art}}. -#' @param verbose When \code{TRUE}, sums of squares and reisdual sum of squares -#' in addition to degrees of freedom are printed in some Anova types (e.g. -#' repeated measures Anovas). Default \code{FALSE}, for brevity. +#' @param verbose When \code{TRUE}, sums of squares and residual sum of squares +#' in addition to degrees of freedom are printed in some ANOVA types (e.g. +#' repeated measures ANOVAs). Default \code{FALSE}, for brevity. #' @param digits Digits of output in printed table; see \code{\link{print}}. #' @param \dots Additional arguments passed to \code{\link{Anova}} or #' \code{\link{anova}} by \code{anova.art} or to \code{\link{print}} by @@ -72,91 +72,91 @@ #' Human Factors in Computing Systems (CHI '11)}. Vancouver, British Columbia #' (May 7-12, 2011). New York: ACM Press, pp. 143-146. #' @keywords nonparametric -#' +#' #' @export -anova.art = function(object, +anova.art = function(object, response=c("art", "aligned"), - type=c("III", "II", "I", 3, 2, 1), + type=c("III", "II", "I", 3, 2, 1), factor.contrasts="contr.sum", - test=c("F", "Chisq"), + test=c("F", "Chisq"), all.rows=FALSE, ... ) { #sensible names for generic parameters m = object - + #match enum arguments response = match.arg(response) type = as.character(type) type = match.arg(type) test = match.arg(test) - + #get transformed responses based on response type requested - responses = switch(response, - aligned=m$aligned, + responses = switch(response, + aligned=m$aligned, art=m$aligned.ranks) - + #determine anova type to use type = if (type %in% c(1,"I")) "I" else if (type %in% c(2,"II")) "II" else if (type %in% c(3,"III")) "III" - + #are we going to need to show the term we aligned by #for each row of the output? show.aligned.by.term = response == "aligned" || all.rows - + #run linear models and anovas df = m$data anovas = NULL - table.description = "" + table.description = "" for (j in 1:ncol(responses)) { #can't use ldply here because it appears to drop row names when binding rows aligned.by.term = colnames(responses)[[j]] - + #get linear model m.l = artlm(m, aligned.by.term, response=response, factor.contrasts=factor.contrasts) - - #run anova and extract desired results + + #run anova and extract desired results anova.j = flat.anova(m.l, type=type, test=test, ...) if (j == 1) table.description = attr(anova.j, "description") - + #extract desired result rows from anova #for art, this is the one row correponding to the effect we aligned and ranked by - #for aligned, this is every effect *except* the one we aligned and ranked by + #for aligned, this is every effect *except* the one we aligned and ranked by if (!all.rows) { include.row = anova.j$Term == aligned.by.term if (response == "aligned") include.row = !include.row anova.j = anova.j[include.row,] } - + #Add "Aligned By" column when needed to disambiguate - if (nrow(anova.j) > 0) { - #if only one fixed effect we can get no rows here, e.g. if response="aligned" + if (nrow(anova.j) > 0) { + #if only one fixed effect we can get no rows here, e.g. if response="aligned" #and all.rows=FALSE, so the above guard is necessary if (show.aligned.by.term) { anova.j = cbind(anova.j[,1,drop=FALSE], `Aligned By`=aligned.by.term, anova.j[,-1,drop=FALSE]) } } - + anovas = rbind(anovas, anova.j) } - + #fill in the rest of the anova table metadata and return class(anovas) = c("anova.art", "anova", "data.frame") - attr(anovas, "model") = - if (m$n.grouping.terms > 0) "lmer" - else if (m$n.error.terms > 0) "aov" + attr(anovas, "model") = + if (m$n.grouping.terms > 0) "lmer" + else if (m$n.error.terms > 0) "aov" else "lm" attr(anovas, "table.description") = table.description attr(anovas, "response") = response attr(anovas, "response.term") = colnames(m$cell.means)[1] - anovas + anovas } ### Generate p stars for a vector of p values #' @importFrom stats symnum p.stars = function(p.values) { - unclass(symnum(p.values, corr = FALSE, na = FALSE, cutpoints = c(0, - 0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", + unclass(symnum(p.values, corr = FALSE, na = FALSE, cutpoints = c(0, + 0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", " "))) } @@ -173,7 +173,7 @@ print.anova.art = function(x, verbose=FALSE, digits=5, ...) { lmer = "Mixed Effects (lmer)\n", )) cat(sep="", "Response: ", attr(x, "response"), "(", attr(x, "response.term"), ")\n\n") - + #format p values p.col = last(which(grepl("^(P|Pr)\\(", names(x)))) stars.legend = if (!is.na(p.col)) { @@ -193,13 +193,13 @@ print.anova.art = function(x, verbose=FALSE, digits=5, ...) { #abbreviate columns if (!is.null(x$Error)) x$Error %<>% abbreviate(5) if (!is.null(x$`Aligned By`)) x$`Aligned By` %<>% abbreviate(10) - - #drop "Sum Sq" (etc) columns when not doing verbose output + + #drop "Sum Sq" (etc) columns when not doing verbose output if (!verbose) x %<>% select(everything(), -contains("Sum Sq"), -contains("Mean Sq")) - + #print table print.data.frame(x, digits=digits, ...) - + #print legend if (!is.null(stars.legend)) { cat("---\nSignif. codes: ", stars.legend, "\n") diff --git a/R/art.R b/R/art.R index 140476a..aa5d9ee 100755 --- a/R/art.R +++ b/R/art.R @@ -41,9 +41,9 @@ #' terms (but not both). Should be a formula with a single response variable #' (left-hand side) and one or more terms with all interactions on the #' right-hand side, e.g. \code{y ~ x} or \code{y ~ a*b*c} or \code{y ~ a + b + -#' b:c}. If you want to run a mixed effects Anova on the transformed data using +#' b:c}. If you want to run a mixed effects ANOVA on the transformed data using #' \code{\link{lmer}}, you can include grouping terms, as in \code{y ~ a*b*c + -#' (1|d)}. If you want to run a repeated measures Anova using +#' (1|d)}. If you want to run a repeated measures ANOVA using #' \code{\link{aov}}, you can include error terms, as in \code{y ~ a*b*c + #' Error(d)}. See 'Details'. #' @param data An optional data frame containing the variables in the model. diff --git a/man/anova.art.Rd b/man/anova.art.Rd index 56f3197..4bbd527 100755 --- a/man/anova.art.Rd +++ b/man/anova.art.Rd @@ -46,9 +46,9 @@ of \code{response}.} \item{x}{An object of class \code{\link{art}}.} -\item{verbose}{When \code{TRUE}, sums of squares and reisdual sum of squares -in addition to degrees of freedom are printed in some Anova types (e.g. -repeated measures Anovas). Default \code{FALSE}, for brevity.} +\item{verbose}{When \code{TRUE}, sums of squares and residual sum of squares +in addition to degrees of freedom are printed in some ANOVA types (e.g. +repeated measures ANOVAs). Default \code{FALSE}, for brevity.} \item{digits}{Digits of output in printed table; see \code{\link{print}}.} } diff --git a/man/art.Rd b/man/art.Rd index 7ab14e3..5f5cd5a 100755 --- a/man/art.Rd +++ b/man/art.Rd @@ -13,9 +13,9 @@ art(formula, data, terms (but not both). Should be a formula with a single response variable (left-hand side) and one or more terms with all interactions on the right-hand side, e.g. \code{y ~ x} or \code{y ~ a*b*c} or \code{y ~ a + b + -b:c}. If you want to run a mixed effects Anova on the transformed data using +b:c}. If you want to run a mixed effects ANOVA on the transformed data using \code{\link{lmer}}, you can include grouping terms, as in \code{y ~ a*b*c + -(1|d)}. If you want to run a repeated measures Anova using +(1|d)}. If you want to run a repeated measures ANOVA using \code{\link{aov}}, you can include error terms, as in \code{y ~ a*b*c + Error(d)}. See 'Details'.}