Skip to content

Commit

Permalink
aov_ez() works with mutltiple covariates, solves issue #29
Browse files Browse the repository at this point in the history
  • Loading branch information
singmann committed May 17, 2017
1 parent 4028b17 commit ccada51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/aov_car.R
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ aov_4 <- function(formula, data, observed = NULL, fun_aggregate = NULL, type = a

aov_ez <- function(id, dv, data, between = NULL, within = NULL, covariate = NULL, observed = NULL, fun_aggregate = NULL, type = afex_options("type"), factorize = afex_options("factorize"), check_contrasts = afex_options("check_contrasts"), return = afex_options("return_aov"), anova_table = list(), ..., print.formula = FALSE) {
if (is.null(between) & is.null(within)) stop("Either between or within need to be non-NULL!")
if (!is.null(covariate)) covariate <- str_c(covariate, collapse = "+")
if (!is.null(covariate)) {
covariate <- escape_vars(covariate)
covariate <- str_c(covariate, collapse = "+")
}
#browser()

id <- escape_vars(id)
dv <- escape_vars(dv)
between <- escape_vars(between)
within <- escape_vars(within)
covariate <- escape_vars(covariate)

rh <- if (!is.null(between) || !is.null(covariate)) str_c(if (!is.null(between)) str_c(between, collapse = " * ") else NULL, covariate, sep = " + ") else "1"
error <- str_c(" + Error(", id, if (!is.null(within)) "/(" else "", str_c(within, collapse = " * "), if (length(within) > 0) ")" else "", ")")
formula <- str_c(dv, " ~ ", rh, error)
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-aov_car-bugs.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,18 @@ test_that("aov_car works with column names containing spaces: https://github.com
expect_is(aov_car(dependent ~ `RM Factor 1` + Error(subject/(`RM Factor 1`)), data), "afex_aov")
expect_is(aov_4(dependent ~ `RM Factor 1` + (`RM Factor 1`|subject), data), "afex_aov")
expect_is(aov_ez("subject", "dependent", data, within = "RM Factor 1"), "afex_aov")
})


test_that("aov_ez works with multiple covariates", {
skip_if_not_installed("psych")
require(psych)
data(msq)
msq2 <- msq[!is.na(msq$Extraversion),]
msq2 <- droplevels(msq2[msq2$ID != "18",])
msq2$TOD <- msq2$TOD-mean(msq2$TOD)
msq2$MSQ_Time <- msq2$MSQ_Time-mean(msq2$MSQ_Time)
msq2$condition <- msq2$condition-mean(msq2$condition) # that is somewhat stupid
expect_is(aov_ez(data=msq2, dv="Extraversion", id = "ID", between = "condition",
covariate=c("TOD", "MSQ_Time"), factorize=FALSE, fun_aggregate = mean), "afex_aov")
})

0 comments on commit ccada51

Please sign in to comment.