Calculates type-II or type-III analysis-of-variance tables for model objects produced by lm, glm, multinom (in the nnet package), polr (in the MASS package), coxph (in the survival package), coxme (in the coxme pckage), svyglm and svycoxph (in the survey package), rlm (in the MASS package), lmer (in the lme4 package), lme (in the nlme package), clm and clmm (in the ordinal package), and (by the default method) for most models with a linear predictor and asymptotically normal coefficients (see details below). For linear models, F-tests are calculated; for generalized linear models, likelihood-ratio chisquare, Wald chisquare, or F-tests are calculated; for multinomial logit and proportional-odds logit models, likelihood-ratio tests are calculated. Various test statistics are provided for multivariate linear models produced by lm or manova. Partial-likelihood-ratio tests or Wald tests are provided for Cox models. Wald chi-square tests are provided for fixed effects in linear and generalized linear mixed-effects models. Wald chi-square or F tests are provided in the default case.

Anova(mod, ...)

Manova(mod, ...)

# S3 method for class 'lm'
Anova(mod, error, type=c("II","III", 2, 3),
  white.adjust=c(FALSE, TRUE, "hc3", "hc0", "hc1", "hc2", "hc4"),
  vcov.=NULL, singular.ok, ...)

# S3 method for class 'aov'
Anova(mod, ...)

# S3 method for class 'glm'
Anova(mod, type=c("II","III", 2, 3),
    test.statistic=c("LR", "Wald", "F"),
    error, error.estimate=c("pearson", "dispersion", "deviance"),
   vcov.=vcov(mod, complete=TRUE),  singular.ok, ...)

# S3 method for class 'multinom'
Anova(mod, type = c("II","III", 2, 3), ...)

# S3 method for class 'polr'
Anova(mod, type = c("II","III", 2, 3), ...)

# S3 method for class 'mlm'
Anova(mod, type=c("II","III", 2, 3), SSPE, error.df,
    idata, idesign, icontrasts=c("contr.sum", "contr.poly"), imatrix,
    test.statistic=c("Pillai", "Wilks", "Hotelling-Lawley", "Roy"),...)

# S3 method for class 'manova'
Anova(mod, ...)

# S3 method for class 'mlm'
Manova(mod, ...)

# S3 method for class 'Anova.mlm'
print(x, ...)

# S3 method for class 'Anova.mlm'
summary(object, test.statistic, univariate=object$repeated,
    multivariate=TRUE, p.adjust.method, ...)

# S3 method for class 'summary.Anova.mlm'
print(x, digits = getOption("digits"),
    SSP=TRUE, SSPE=SSP, ... )

# S3 method for class 'univaov'
print(x, digits = max(getOption("digits") - 2L, 3L),
                          style=c("wide", "long"),
                          by=c("response", "term"),
                          ...)

# S3 method for class 'univaov'
as.data.frame(x, row.names, optional, by=c("response", "term"), ...)

# S3 method for class 'coxph'
Anova(mod, type=c("II", "III", 2, 3),
  test.statistic=c("LR", "Wald"), ...)

# S3 method for class 'coxme'
Anova(mod, type=c("II", "III", 2, 3),
    test.statistic=c("Wald", "LR"), ...)

# S3 method for class 'lme'
Anova(mod, type=c("II","III", 2, 3),
    vcov.=vcov(mod, complete=FALSE), singular.ok, ...)

# S3 method for class 'mer'
Anova(mod, type=c("II", "III", 2, 3),
  test.statistic=c("Chisq", "F"), vcov.=vcov(mod, complete=FALSE), singular.ok, ...)

# S3 method for class 'merMod'
Anova(mod, type=c("II", "III", 2, 3),
    test.statistic=c("Chisq", "F"), vcov.=vcov(mod, complete=FALSE), singular.ok, ...)

# S3 method for class 'svyglm'
Anova(mod, ...)

# S3 method for class 'svycoxph'
Anova(mod, type=c("II", "III", 2, 3),
  test.statistic="Wald", ...)

# S3 method for class 'rlm'
Anova(mod, ...)

# S3 method for class 'clm'
Anova(mod, ...)

# S3 method for class 'clmm'
Anova(mod, ...)

# Default S3 method
Anova(mod, type=c("II", "III", 2, 3),
  test.statistic=c("Chisq", "F"), vcov.=vcov(mod, complete=FALSE),
  singular.ok, error.df, ...)

Arguments

mod

lm, aov, glm, multinom, polr mlm, coxph, coxme, lme, mer, merMod, svyglm, svycoxph, rlm, clm, clmm, or other suitable model object.

error

for a linear model, an lm model object from which the error sum of squares and degrees of freedom are to be calculated. For F-tests for a generalized linear model, a glm object from which the dispersion is to be estimated. If not specified, mod is used.

type

type of test, "II", "III", 2, or 3. Roman numerals are equivalent to the corresponding Arabic numerals.

singular.ok

defaults to TRUE for type-II tests, and FALSE for type-III tests where the tests for models with aliased coefficients will not be straightforwardly interpretable; if FALSE, a model with aliased coefficients produces an error. This argument is available only for some Anova methods.

test.statistic

for a generalized linear model, whether to calculate "LR" (likelihood-ratio), "Wald", or "F" tests; for a Cox or Cox mixed-effects model, whether to calculate "LR" (partial-likelihood ratio) or "Wald" tests (with "LR" tests unavailable for Cox models using the tt argument); in the default case or for linear mixed models fit by lmer, whether to calculate Wald "Chisq" or Kenward-Roger "F" tests with Satterthwaite degrees of freedom (warning: the KR F-tests can be very time-consuming). For a multivariate linear model, the multivariate test statistic to compute — one of "Pillai", "Wilks", "Hotelling-Lawley", or "Roy", with "Pillai" as the default. The summary method for Anova.mlm objects permits the specification of more than one multivariate test statistic, and the default is to report all four.

error.estimate

for F-tests for a generalized linear model, base the dispersion estimate on the Pearson residuals ("pearson", the default); use the dispersion estimate in the model object ("dispersion"); or base the dispersion estimate on the residual deviance ("deviance"). For binomial or Poisson GLMs, where the dispersion is fixed to 1, setting error.estimate="dispersion" is changed to "pearson", with a warning.

white.adjust

if not FALSE, the default, tests use a heteroscedasticity-corrected coefficient covariance matrix; the various values of the argument specify different corrections. See the documentation for hccm for details. If white.adjust=TRUE then the "hc3" correction is selected.

SSPE

For Anova for a multivariate linear model, the error sum-of-squares-and-products matrix; if missing, will be computed from the residuals of the model; for the print method for the summary of an Anova of a multivariate linear model, whether or not to print the error SSP matrix (defaults to TRUE).

SSP

if TRUE (the default), print the sum-of-squares and cross-products matrix for the hypothesis and the response-transformation matrix.

error.df

The degrees of freedom for error; if error.df missing for a multivariate linear model (object of class "mlm"), the error degrees of freedom will be taken from the model.

For the default Anova method, if an F-test is requested and if error.df is missing, the error degrees of freedom will be computed by applying the df.residual function to the model; if df.residual returns NULL or NA, then a chi-square test will be substituted for the F-test (with a message to that effect.

idata

an optional data frame giving a factor or factors defining the intra-subject model for multivariate repeated-measures data. See Details for an explanation of the intra-subject design and for further explanation of the other arguments relating to intra-subject factors.

idesign

a one-sided model formula using the “data” in idata and specifying the intra-subject design.

icontrasts

names of contrast-generating functions to be applied by default to factors and ordered factors, respectively, in the within-subject “data”; the contrasts must produce an intra-subject model matrix in which different terms are orthogonal. The default is c("contr.sum", "contr.poly").

imatrix

as an alternative to specifying idata, idesign, and (optionally) icontrasts, the model matrix for the within-subject design can be given directly in the form of list of named elements. Each element gives the columns of the within-subject model matrix for a term to be tested, and must have as many rows as there are responses; the columns of the within-subject model matrix for different terms must be mutually orthogonal.

x, object

object of class "Anova.mlm" to print or summarize.

multivariate, univariate

compute and print multivariate and univariate tests for a repeated-measures ANOVA or multivariate linear model; the default is TRUE for both for repeated measures and TRUE for multivariate for a multivariate linear model.

p.adjust.method

if given for a multivariate linear model when univariate tests are requested, the univariate tests are corrected for simultaneous inference by term; if specified, should be one of the methods recognized by p.adjust or TRUE, in which case the default (Holm) adjustment is used.

digits

minimum number of significant digits to print.

style

for printing univariate tests if requested for a multivariate linear model; one of "wide", the default, or "long".

by

if univariate tests are printed in "long" style, they can be ordered by "response", the default, or by "term".

row.names, optional

not used.

vcov.

in the default method, an optional coefficient-covariance matrix or function to compute a covariance matrix, computed by default by applying the generic vcov function to the model object. A similar argument may be supplied to the lm method, and the default (NULL) is to ignore the argument; if both vcov. and white.adjust are supplied to the lm method, the latter is used. In the glm method, vcov. is ignored unless test="Wald"; in the mer and merMod methods, vcov. is ignored if test="F".

Note that arguments supplied to ... are not passed to vcov. when it's a function; in this case either use an anonymous function in which the additional arguments are set, or supply the coefficient covariance matrix directly (see the examples).

...

do not use.

Details

The designations "type-II" and "type-III" are borrowed from SAS, but the definitions used here do not correspond precisely to those employed by SAS. Type-II tests are calculated according to the principle of marginality, testing each term after all others, except ignoring the term's higher-order relatives; so-called type-III tests violate marginality, testing each term in the model after all of the others. This definition of Type-II tests corresponds to the tests produced by SAS for analysis-of-variance models, where all of the predictors are factors, but not more generally (i.e., when there are quantitative predictors). Be very careful in formulating the model for type-III tests, or the hypotheses tested will not make sense.

As implemented here, type-II Wald tests are a generalization of the linear hypotheses used to generate these tests in linear models.

For tests for linear models, multivariate linear models, and Wald tests for generalized linear models, Cox models, mixed-effects models, generalized linear models fit to survey data, and in the default case, Anova finds the test statistics without refitting the model. The svyglm method simply calls the default method and therefore can take the same arguments.

The standard R anova function calculates sequential ("type-I") tests. These rarely test interesting hypotheses in unbalanced designs.

A MANOVA for a multivariate linear model (i.e., an object of class "mlm" or "manova") can optionally include an intra-subject repeated-measures design. If the intra-subject design is absent (the default), the multivariate tests concern all of the response variables. To specify a repeated-measures design, a data frame is provided defining the repeated-measures factor or factors via idata, with default contrasts given by the icontrasts argument. An intra-subject model-matrix is generated from the formula specified by the idesign argument; columns of the model matrix corresponding to different terms in the intra-subject model must be orthogonal (as is insured by the default contrasts). Note that the contrasts given in icontrasts can be overridden by assigning specific contrasts to the factors in idata. As an alternative, the within-subjects model matrix can be specified directly via the imatrix argument. Manova is essentially a synonym for Anova for multivariate linear models.

If univariate tests are requested for the summary of a multivariate linear model, the object returned contains a univaov component of "univaov"; print and as.data.frame methods are provided for the "univaov" class.

For the default method to work, the model object must contain a standard terms element, and must respond to the vcov, coef, and model.matrix functions. If any of these requirements is missing, then it may be possible to supply it reasonably simply (e.g., by writing a missing vcov method for the class of the model object).

Value

An object of class "anova", or "Anova.mlm", which usually is printed. For objects of class "Anova.mlm", there is also a summary method, which provides much more detail than the print method about the MANOVA, including traditional mixed-model univariate F-tests with Greenhouse-Geisser and Huynh-Feldt corrections.

References

Fox, J. (2016) Applied Regression Analysis and Generalized Linear Models, Third Edition. Sage.

Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition, Sage.

Hand, D. J., and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures: A Practical Approach for Behavioural Scientists. Chapman and Hall.

O'Brien, R. G., and Kaiser, M. K. (1985) MANOVA method for analyzing repeated measures designs: An extensive primer. Psychological Bulletin 97, 316–333.

Author

John Fox jfox@mcmaster.ca; the code for the Mauchly test and Greenhouse-Geisser and Huynh-Feldt corrections for non-spericity in repeated-measures ANOVA are adapted from the functions stats:::stats:::mauchly.test.SSD and stats:::sphericity by R Core; summary.Anova.mlm and print.summary.Anova.mlm incorporates code contributed by Gabriel Baud-Bovy.

Warning

Be careful of type-III tests: For a traditional multifactor ANOVA model with interactions, for example, these tests will normally only be sensible when using contrasts that, for different terms, are orthogonal in the row-basis of the model, such as those produced by contr.sum, contr.poly, or contr.helmert, but not by the default contr.treatment. In a model that contains factors, numeric covariates, and interactions, main-effect tests for factors will be for differences over the origin. In contrast (pun intended), type-II tests are invariant with respect to (full-rank) contrast coding. If you don't understand this issue, then you probably shouldn't use Anova for type-III tests.

Examples


## Two-Way Anova

mod <- lm(conformity ~ fcategory*partner.status, data=Moore,
  contrasts=list(fcategory=contr.sum, partner.status=contr.sum))
Anova(mod)
#> Anova Table (Type II tests)
#> 
#> Response: conformity
#>                          Sum Sq Df F value   Pr(>F)   
#> fcategory                 11.61  2  0.2770 0.759564   
#> partner.status           212.21  1 10.1207 0.002874 **
#> fcategory:partner.status 175.49  2  4.1846 0.022572 * 
#> Residuals                817.76 39                    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Anova(mod, type=3)  # note use of contr.sum in call to lm()
#> Anova Table (Type III tests)
#> 
#> Response: conformity
#>                          Sum Sq Df  F value    Pr(>F)    
#> (Intercept)              5752.8  1 274.3592 < 2.2e-16 ***
#> fcategory                  36.0  2   0.8589  0.431492    
#> partner.status            239.6  1  11.4250  0.001657 ** 
#> fcategory:partner.status  175.5  2   4.1846  0.022572 *  
#> Residuals                 817.8 39                       
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

## use of vcov.; the following are equivalent:

Anova(mod, white.adjust = TRUE)
#> Coefficient covariances computed by hccm()
#> Analysis of Deviance Table (Type II tests)
#> 
#> Response: conformity
#>                          Df       F    Pr(>F)    
#> fcategory                 2  0.3766 0.6886545    
#> partner.status            1 14.0454 0.0005775 ***
#> fcategory:partner.status  2  2.8294 0.0712175 .  
#> Residuals                39                      
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Anova(mod, vcov. = hccm) # vcov. is a function, type = "hc3" is the default
#> Coefficient covariances computed by hccm
#> Analysis of Deviance Table (Type II tests)
#> 
#> Response: conformity
#>                          Df       F    Pr(>F)    
#> fcategory                 2  0.3766 0.6886545    
#> partner.status            1 14.0454 0.0005775 ***
#> fcategory:partner.status  2  2.8294 0.0712175 .  
#> Residuals                39                      
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Anova(mod, vcov. = hccm(mod, type = "hc3")) # vcov. is a matrix
#> Coefficient covariances computed by hccm(mod, type = "hc3")
#> Analysis of Deviance Table (Type II tests)
#> 
#> Response: conformity
#>                          Df       F    Pr(>F)    
#> fcategory                 2  0.3766 0.6886545    
#> partner.status            1 14.0454 0.0005775 ***
#> fcategory:partner.status  2  2.8294 0.0712175 .  
#> Residuals                39                      
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Anova(mod, vcov. = function(m) hccm(m, type = "hc3")) # passing type as an argument
#> Coefficient covariances computed by function(m) hccm(m, type = "hc3")
#> Analysis of Deviance Table (Type II tests)
#> 
#> Response: conformity
#>                          Df       F    Pr(>F)    
#> fcategory                 2  0.3766 0.6886545    
#> partner.status            1 14.0454 0.0005775 ***
#> fcategory:partner.status  2  2.8294 0.0712175 .  
#> Residuals                39                      
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

## One-Way MANOVA
## See ?Pottery for a description of the data set used in this example.

summary(Anova(lm(cbind(Al, Fe, Mg, Ca, Na) ~ Site, data=Pottery)))
#> 
#> Type II MANOVA Tests:
#> 
#> Sum of squares and products for error:
#>            Al          Fe          Mg          Ca         Na
#> Al 48.2881429  7.08007143  0.60801429  0.10647143 0.58895714
#> Fe  7.0800714 10.95084571  0.52705714 -0.15519429 0.06675857
#> Mg  0.6080143  0.52705714 15.42961143  0.43537714 0.02761571
#> Ca  0.1064714 -0.15519429  0.43537714  0.05148571 0.01007857
#> Na  0.5889571  0.06675857  0.02761571  0.01007857 0.19929286
#> 
#> ------------------------------------------
#>  
#> Term: Site 
#> 
#> Sum of squares and products for the hypothesis:
#>             Al          Fe          Mg         Ca         Na
#> Al  175.610319 -149.295533 -130.809707 -5.8891637 -5.3722648
#> Fe -149.295533  134.221616  117.745035  4.8217866  5.3259491
#> Mg -130.809707  117.745035  103.350527  4.2091613  4.7105458
#> Ca   -5.889164    4.821787    4.209161  0.2047027  0.1547830
#> Na   -5.372265    5.325949    4.710546  0.1547830  0.2582456
#> 
#> Multivariate Tests: Site
#>                  Df test stat  approx F num Df   den Df     Pr(>F)    
#> Pillai            3   1.55394   4.29839     15 60.00000 2.4129e-05 ***
#> Wilks             3   0.01230  13.08854     15 50.09147 1.8404e-12 ***
#> Hotelling-Lawley  3  35.43875  39.37639     15 50.00000 < 2.22e-16 ***
#> Roy               3  34.16111 136.64446      5 20.00000 9.4435e-15 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

## MANOVA for a randomized block design (example courtesy of Michael Friendly:
##  See ?Soils for description of the data set)

soils.mod <- lm(cbind(pH,N,Dens,P,Ca,Mg,K,Na,Conduc) ~ Block + Contour*Depth,
    data=Soils)
Manova(soils.mod)
#> 
#> Type II MANOVA Tests: Pillai test statistic
#>               Df test stat approx F num Df den Df    Pr(>F)    
#> Block          3    1.6758   3.7965     27     81 1.777e-06 ***
#> Contour        2    1.2453   4.7664     18     52 4.918e-06 ***
#> Depth          3    1.6149   3.4977     27     81 7.088e-06 ***
#> Contour:Depth  6    1.6666   1.2820     54    180    0.1164    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
summary(Anova(soils.mod), univariate=TRUE, multivariate=FALSE,
    p.adjust.method=TRUE)
#> 
#>  Type II Sums of Squares
#>               df       pH         N      Dens        P      Ca      Mg        K
#> Block          3  1.23247 0.0038257  0.111250   6591.2  14.601 13.8776 0.323442
#> Contour        2  0.15723 0.0046093 -0.017334  13606.0  29.886  7.0807 0.368833
#> Depth          3 22.48843 0.2819657  3.947295 580894.4 584.945 78.4243 2.312395
#> Contour:Depth  6  0.74919 0.0071199  0.189855  45892.9  14.917  9.1757 0.040532
#> residuals     33  4.24730 0.0358700  0.433050  55810.8  71.428 27.7522 0.409308
#>                     Na    Conduc
#> Block          15.4013    7.3116
#> Contour         9.2481    3.3744
#> Depth         778.0648 1202.5046
#> Contour:Depth   5.6031   18.8448
#> residuals      29.9400   46.7515
#> 
#>  F-tests
#>                  pH     N   Dens      P    Ca    Mg     K     Na Conduc
#> Block          3.19  1.76   2.83   0.65  2.25  8.25  8.69   2.83   1.72
#> Contour        0.61  1.41  -0.22   2.68  6.90  2.81  4.96   3.40   1.19
#> Depth         58.24 43.23 100.27 171.74 90.08 15.54 62.14 428.79 282.93
#> Contour:Depth  0.97  2.18   7.23   9.05  1.15  3.64  1.63   2.06   2.22
#> 
#>  p-values
#>               pH         N          Dens       P          Ca         Mg        
#> Block         0.03622480 0.18784329 0.05368061 0.69011331 0.10102309 0.00124231
#> Contour       0.54893702 0.25628127 1.00000000 0.06278455 0.00312799 0.05481753
#> Depth         2.8345e-13 3.0075e-14 < 2.22e-16 < 2.22e-16 5.6655e-16 2.2310e-08
#> Contour:Depth 0.46053891 0.10857889 0.00248264 0.00016303 0.35694240 0.02265820
#>               K          Na         Conduc    
#> Block         0.00021642 0.02469800 0.18188132
#> Contour       0.00102772 0.02912195 0.31666551
#> Depth         1.1525e-13 < 2.22e-16 < 2.22e-16
#> Contour:Depth 0.21056024 0.12471872 0.06607061
#> 
#>  p-values adjusted (by term) for simultaneous inference by holm method
#>               pH         N          Dens       P          Ca         Mg        
#> Block         0.2173488  0.5456440  0.2684030  0.6901133  0.4040923  0.0099385 
#> Contour       1.0000000  1.0000000  1.0000000  0.3289052  0.0250239  0.3289052 
#> Depth         5.6690e-13 1.2030e-13 7.0108e-16 < 2.22e-16 2.8328e-15 2.2310e-08
#> Contour:Depth 0.7138848  0.5428945  0.0198611  0.0014673  0.7138848  0.1586074 
#>               K          Na         Conduc    
#> Block         0.0019478  0.1728860  0.5456440 
#> Contour       0.0092495  0.2038537  1.0000000 
#> Depth         3.4574e-13 < 2.22e-16 < 2.22e-16
#> Contour:Depth 0.6316807  0.5428945  0.3964237 

## a multivariate linear model for repeated-measures data
## See ?OBrienKaiser for a description of the data set used in this example.

phase <- factor(rep(c("pretest", "posttest", "followup"), c(5, 5, 5)),
    levels=c("pretest", "posttest", "followup"))
hour <- ordered(rep(1:5, 3))
idata <- data.frame(phase, hour)
idata
#>       phase hour
#> 1   pretest    1
#> 2   pretest    2
#> 3   pretest    3
#> 4   pretest    4
#> 5   pretest    5
#> 6  posttest    1
#> 7  posttest    2
#> 8  posttest    3
#> 9  posttest    4
#> 10 posttest    5
#> 11 followup    1
#> 12 followup    2
#> 13 followup    3
#> 14 followup    4
#> 15 followup    5

mod.ok <- lm(cbind(pre.1, pre.2, pre.3, pre.4, pre.5,
                     post.1, post.2, post.3, post.4, post.5,
                     fup.1, fup.2, fup.3, fup.4, fup.5) ~  treatment*gender,
                data=OBrienKaiser)
(av.ok <- Anova(mod.ok, idata=idata, idesign=~phase*hour))
#> 
#> Type II Repeated Measures MANOVA Tests: Pillai test statistic
#>                             Df test stat approx F num Df den Df    Pr(>F)    
#> (Intercept)                  1   0.96954   318.34      1     10 6.532e-09 ***
#> treatment                    2   0.48092     4.63      2     10 0.0376868 *  
#> gender                       1   0.20356     2.56      1     10 0.1409735    
#> treatment:gender             2   0.36350     2.86      2     10 0.1044692    
#> phase                        1   0.85052    25.61      2      9 0.0001930 ***
#> treatment:phase              2   0.68518     2.61      4     20 0.0667354 .  
#> gender:phase                 1   0.04314     0.20      2      9 0.8199968    
#> treatment:gender:phase       2   0.31060     0.92      4     20 0.4721498    
#> hour                         1   0.93468    25.04      4      7 0.0003043 ***
#> treatment:hour               2   0.30144     0.35      8     16 0.9295212    
#> gender:hour                  1   0.29274     0.72      4      7 0.6023742    
#> treatment:gender:hour        2   0.57022     0.80      8     16 0.6131884    
#> phase:hour                   1   0.54958     0.46      8      3 0.8324517    
#> treatment:phase:hour         2   0.66367     0.25     16      8 0.9914415    
#> gender:phase:hour            1   0.69505     0.85      8      3 0.6202076    
#> treatment:gender:phase:hour  2   0.79277     0.33     16      8 0.9723693    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

summary(av.ok, multivariate=FALSE)
#> 
#> Univariate Type II Repeated-Measures ANOVA Assuming Sphericity
#> 
#>                             Sum Sq num Df Error SS den Df  F value    Pr(>F)
#> (Intercept)                 7260.0      1  228.056     10 318.3435 6.532e-09
#> treatment                    211.3      2  228.056     10   4.6323  0.037687
#> gender                        58.3      1  228.056     10   2.5558  0.140974
#> treatment:gender             130.2      2  228.056     10   2.8555  0.104469
#> phase                        167.5      2   80.278     20  20.8651 1.274e-05
#> treatment:phase               78.7      4   80.278     20   4.8997  0.006426
#> gender:phase                   1.7      2   80.278     20   0.2078  0.814130
#> treatment:gender:phase        10.2      4   80.278     20   0.6366  0.642369
#> hour                         106.3      4   62.500     40  17.0067 3.191e-08
#> treatment:hour                 1.2      8   62.500     40   0.0929  0.999257
#> gender:hour                    2.6      4   62.500     40   0.4094  0.800772
#> treatment:gender:hour          7.8      8   62.500     40   0.6204  0.755484
#> phase:hour                    11.1      8   96.167     80   1.1525  0.338317
#> treatment:phase:hour           6.3     16   96.167     80   0.3256  0.992814
#> gender:phase:hour              6.6      8   96.167     80   0.6900  0.699124
#> treatment:gender:phase:hour   14.2     16   96.167     80   0.7359  0.749562
#>                                
#> (Intercept)                 ***
#> treatment                   *  
#> gender                         
#> treatment:gender               
#> phase                       ***
#> treatment:phase             ** 
#> gender:phase                   
#> treatment:gender:phase         
#> hour                        ***
#> treatment:hour                 
#> gender:hour                    
#> treatment:gender:hour          
#> phase:hour                     
#> treatment:phase:hour           
#> gender:phase:hour              
#> treatment:gender:phase:hour    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> 
#> Mauchly Tests for Sphericity
#> 
#>                             Test statistic p-value
#> phase                              0.74927 0.27282
#> treatment:phase                    0.74927 0.27282
#> gender:phase                       0.74927 0.27282
#> treatment:gender:phase             0.74927 0.27282
#> hour                               0.06607 0.00760
#> treatment:hour                     0.06607 0.00760
#> gender:hour                        0.06607 0.00760
#> treatment:gender:hour              0.06607 0.00760
#> phase:hour                         0.00478 0.44939
#> treatment:phase:hour               0.00478 0.44939
#> gender:phase:hour                  0.00478 0.44939
#> treatment:gender:phase:hour        0.00478 0.44939
#> 
#> 
#> Greenhouse-Geisser and Huynh-Feldt Corrections
#>  for Departure from Sphericity
#> 
#>                              GG eps Pr(>F[GG])    
#> phase                       0.79953  7.323e-05 ***
#> treatment:phase             0.79953    0.01223 *  
#> gender:phase                0.79953    0.76616    
#> treatment:gender:phase      0.79953    0.61162    
#> hour                        0.46028  8.741e-05 ***
#> treatment:hour              0.46028    0.97879    
#> gender:hour                 0.46028    0.65346    
#> treatment:gender:hour       0.46028    0.64136    
#> phase:hour                  0.44950    0.34573    
#> treatment:phase:hour        0.44950    0.94019    
#> gender:phase:hour           0.44950    0.58903    
#> treatment:gender:phase:hour 0.44950    0.64634    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#>                                HF eps   Pr(>F[HF])
#> phase                       0.9278594 2.387543e-05
#> treatment:phase             0.9278594 8.089765e-03
#> gender:phase                0.9278594 7.984495e-01
#> treatment:gender:phase      0.9278594 6.319975e-01
#> hour                        0.5592802 2.014357e-05
#> treatment:hour              0.5592802 9.887716e-01
#> gender:hour                 0.5592802 6.911521e-01
#> treatment:gender:hour       0.5592802 6.692976e-01
#> phase:hour                  0.7330608 3.440460e-01
#> treatment:phase:hour        0.7330608 9.804731e-01
#> gender:phase:hour           0.7330608 6.552382e-01
#> treatment:gender:phase:hour 0.7330608 7.080122e-01

## A "doubly multivariate" design with two  distinct repeated-measures variables
## (example courtesy of Michael Friendly)
## See ?WeightLoss for a description of the dataset.

imatrix <- matrix(c(
  1,0,-1, 1, 0, 0,
  1,0, 0,-2, 0, 0,
  1,0, 1, 1, 0, 0,
  0,1, 0, 0,-1, 1,
  0,1, 0, 0, 0,-2,
  0,1, 0, 0, 1, 1), 6, 6, byrow=TRUE)
colnames(imatrix) <- c("WL", "SE", "WL.L", "WL.Q", "SE.L", "SE.Q")
rownames(imatrix) <- colnames(WeightLoss)[-1]
(imatrix <- list(measure=imatrix[,1:2], month=imatrix[,3:6]))
#> $measure
#>     WL SE
#> wl1  1  0
#> wl2  1  0
#> wl3  1  0
#> se1  0  1
#> se2  0  1
#> se3  0  1
#> 
#> $month
#>     WL.L WL.Q SE.L SE.Q
#> wl1   -1    1    0    0
#> wl2    0   -2    0    0
#> wl3    1    1    0    0
#> se1    0    0   -1    1
#> se2    0    0    0   -2
#> se3    0    0    1    1
#> 
contrasts(WeightLoss$group) <- matrix(c(-2,1,1, 0,-1,1), ncol=2)
(wl.mod<-lm(cbind(wl1, wl2, wl3, se1, se2, se3)~group, data=WeightLoss))
#> 
#> Call:
#> lm(formula = cbind(wl1, wl2, wl3, se1, se2, se3) ~ group, data = WeightLoss)
#> 
#> Coefficients:
#>              wl1       wl2       wl3       se1       se2       se3     
#> (Intercept)   5.34444   4.45000   2.17778  14.92778  13.79444  16.28333
#> group1        0.42222   0.55833   0.04722   0.08889  -0.26944   0.60000
#> group2        0.43333   1.09167  -0.02500   0.18333  -0.22500   0.71667
#> 
Anova(wl.mod, imatrix=imatrix, test="Roy")
#> 
#> Type II Repeated Measures MANOVA Tests: Roy test statistic
#>               Df test stat approx F num Df den Df    Pr(>F)    
#> measure        1    86.203  1293.04      2     30 < 2.2e-16 ***
#> group:measure  2     0.356     5.52      2     31  0.008906 ** 
#> month          1     9.407    65.85      4     28 7.807e-14 ***
#> group:month    2     1.772    12.84      4     29 3.909e-06 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

## mixed-effects models examples:

if (FALSE)  # loads nlme package
  library(nlme)
  example(lme)
#> Warning: no help found for ‘lme’
  Anova(fm2)
#> Error: object 'fm2' not found
 # \dontrun{}

if (FALSE)  # loads lme4 package
  library(lme4)
  example(glmer)
#> Warning: no help found for ‘glmer’
  Anova(gm1)
#> Error: object 'gm1' not found
 # \dontrun{}