Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick multiple ANOVA testing using a matrix as input #410

Open
nuno-agostinho opened this issue Aug 6, 2020 · 0 comments
Open

Quick multiple ANOVA testing using a matrix as input #410

nuno-agostinho opened this issue Aug 6, 2020 · 0 comments

Comments

@nuno-agostinho
Copy link
Owner

Useful to quickly calculate multiple Levene tests:

# x is a matrix of values (including NAs) from two or more groups
# g contains the group for each column
leveneTest <- function(x, g, centers=c("median", "mean")) {
    centers <- match.arg(centers)
    dname <- paste(deparse(substitute(x)), "and", deparse(substitute(g)))

    # Convert groups to factors
    g <- factor(g)

    if (centers == "median") {
        res <- calculateRowMediansByGroups(x, g)
    } else if (centers == "mean") {
        res <- calculateRowMeansByGroups(x, g)
    }
    spread <- abs(t(x) - res[rep(g, nrow(x))])

    fit <- lm.fit(model.matrix(~ g), spread)
    # Modified `stats:::anova.lm()` to accept `stats::lm.fit()` with multiple results
    # All the attributes needed to run `stats::anova.lm()` are available in `fit`
    var <- modANOVA(fit)

    # Prepare results
    statistic <- var$`F value`
    pval <- var$`Pr(>F)`

    centers <- deparse(substitute(centers))
    rval <- list(statistic=c("W"=statistic), p.value=pval, data.name=dname,
                 method=paste0("Levene's test (using the ", centers, ")"))
    class(rval) <- "htest"
    return(rval)
}
@nuno-agostinho nuno-agostinho changed the title Quick ANOVA using a matrix as input Quick multiple ANOVA testing using a matrix as input Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant