t tests (or permutation t tests) of given contrasts among the predicted means of a model term, for the
models of predictmeans (aov, lm, glm, gls, lme, lmer,
glmer, glmmTMB, gam).
Usage
contrastmeans(model, modelterm, ctrmatrix, ctrnames=NULL, adj="none", permlist,
vcov_adjust=TRUE,
weights=c("equal", "proportional", "outer", "cells", "flat"),
df_method=c("auto", "Kenward-Roger", "Satterthwaite", "containment",
"between-within", "KR-expected", "KR-average", "inner-outer", "residual"),
joint=FALSE, conf_level=0.95, vcov=NULL, equivalence=NULL, ...)Arguments
- model
A fitted model, as for
predictmeans.- modelterm
Name (in "quotes") for indicating which factor term's contrast to be calculated. The
modeltermmust be given exactly as it appears in the printed model, e.g. "A" or "A:B".- ctrmatrix
A specified contrast matrix. If it is missing, an edit window asks for it (interactive sessions only). The columns can be named by level (e.g. from
specify_contrast); levels that are not given get zero weight.- ctrnames
Names of the specified contrasts, e.g. c("A vs D", "C vs B", ...)
- adj
Name (in "quote") for indicating a method for adjusting p-values of pairwise comparisons. The choices are "none", "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "sidak", "scheffe", "mvt" and "westfall", in any letter case. "sidak", "scheffe" (rank of
ctrmatrix) and "mvt" (single-step multivariate t, the exact counterpart of SASADJUST=SIMULATE; needs package mvtnorm) are as in emmeans. Withpermlist, "mvt" gives the single-step and "westfall" the step-down max-|t| adjustment of the permutation p-values (Westfall and Young, 1993); "westfall" needspermlist, and "scheffe" is not available with it. The default method is "none".- permlist
A list from
permmodels: each contrast is then tested by permutation, with p-value \((b + 1)/(B + 1)\) (\(b\) of the \(B\) permuted |t| at least the observed one), anddf_methodis not used. With the result ofpermmodels(method = "freedman-lane")(lmandaovmodels), each contrast is tested by Freedman-Lane permutations of the model in which that contrast is 0. With apermlist,vcov_adjustis not used (the permuted refits use the unadjusted covariance).- vcov_adjust
For models fitted by REML (
lmer,lme,gls) with Kenward-Roger df: ifTRUE(the default), standard errors use the Kenward-Roger adjusted covariance matrix of the fixed effects, as SASDDFM=KRand emmeans do, to go with the Kenward-Roger degrees of freedom.FALSEuses the unadjusted covariance and reproduces results of predictmeans 1.x. It has no effect for other models.- weights
Passed to
Kmatrix: how the cells of factors not inmodeltermare weighted when averaging over them ("equal", the default,"proportional","outer","cells"or"flat").- df_method
Denominator df, by method or as one number (a fixed df,
Dfbefore 2.0):"auto"(Kenward-Roger for REML fits oflmer,lmeandgls, Satterthwaite for ML fits),"Kenward-Roger","Satterthwaite"or"containment"(SASDDFM=CONTAIN), and forlmer,lmeandglsmodels"between-within"(SASDDFM=BETWITHIN), and"KR-expected"(Kenward-Roger with the expected information, as pbkrtest;"Kenward-Roger"uses the observed information, as SASDDFM=KR),"KR-average"(the average information, as Genstat); forlmeandglsalso"Satterthwaite","Kenward-Roger","KR-expected","KR-average"and"containment"(nlme's or the residual df, as predictmeans 1.x); seepredictmeans.- joint
Logical: also test all the contrasts together (all of them zero), as
pm_jointdoes: an F test with Kenward-Roger (REML fits, the scaled F of Kenward and Roger, 1997) or Satterthwaite (ML fits) denominator df, the residual df for other models (normal-based, i.e. chi-square / df1, when there is none). Contrasts that are linear combinations of others are dropped, sodf1is the rank ofctrmatrix. Withpermlist, also a permutation p-value of the joint Wald F statistic, from the same permutations (for Freedman-Lane, the model with all the contrasts zero). The default isFALSE.- conf_level
Confidence level of the limits of the contrasts (
level = 0.05before 2.0); the tests are at the significance level1 - conf_level. The limits are t limits of each contrast, or withpermlistpermutation-t limits: estimate +- c SE, with c the critical value of the permutation test (the k-th largest permuted |t|, k = floor((1 - conf_level) (nperm + 1))). Withadj"bonferroni", "sidak", "scheffe" or "mvt" (withpermlist: "bonferroni", "sidak", "mvt" or "westfall", from the largest permuted |t| of the contrasts) the table also hasadj.lower.CLandadj.upper.CL: simultaneous limits that exclude 0 exactly when the adjusted p-value is below1 - conf_level, as emmeans'confint()and SASAdjLower/AdjUpper. The step-down and false discovery rate methods have none.- vcov
Optional covariance of the coefficients to use instead of the model's. For
lm,aovandglmmodels:"HC0","HC1","HC2"or"HC3"(heteroscedasticity-consistent, assandwich::vcovHC()), or a one-sided formula naming a cluster variable of the data, e.g.~ Block(cluster-robust CR1, assandwich::vcovCL()). For any model: a covariance matrix of the coefficients, or a function that returns one from the model (for mixed models e.g. from the clubSandwich package). The degrees of freedom stay those of the model (the residual df; forlmerKenward-Roger or Satterthwaite df computed from the model), andvcov_adjustis not used. With apermlist,vcovis not used (for robust permutation tests seepermmodels(robust = TRUE)). The defaultNULLuses the model's covariance.- equivalence
Optional equivalence margins for equivalence tests (two one-sided t tests, TOST) of the contrasts: a positive number \(d\) (equivalence region \(-d\) to \(d\)) or
c(lower, upper), on the scale of the comparisons (the link scale for GLMs: e.g. log ratios). The TOST p-value is the larger of the two one-sided p-values; below1 - conf_level, the difference lies within the margins (as emmeans'test(..., side = "equivalence")). Not adjusted for multiplicity; with apermlistit is t-based (normal-based incontrastmeans).- ...
Not used: arguments removed in 2.0 (
Df,level) stop with their 2.0 spelling.
Value
An object of class "contrastmeans", a list. print() shows the contrasts, tidy() and
as.data.frame() return the table contrasts (what = "joint": the joint test). Its first
components are those of 1.x:
- The t tests of the specified contrasts
A table (rounded to 4 decimals) of the t tests of the contrasts, with the adjusted p-value in
Pr(>|t|).- K
A contrast matrix.
- Joint F test of the contrasts
With
joint = TRUE:df1,df2,F.ratioandp.valueof the joint test (andPerm_p_valuewithpermlist).
and then the tables in the layout of predictmeans, unrounded:
- contrasts
One row per contrast:
contrast(its name),over(contrasts of means over empty cells: the cells used),estimate,std.error,df,statistic(t),p.value(unadjusted; withpermlistthe permutation p-value),p.adj(adjusted byadj),p.equiv(withequivalence),conf.low,conf.high,adj.conf.low,adj.conf.high(simultaneous, with the adjustments that have them),p.method, and for a log or logit scale (link, or a log response)ratioorodds.ratiowith its limits when the weights of every contrast sum to zero.- joint
With
joint = TRUE: the joint test, unrounded.- settings
modelterm,level(significance level,1 - conf_level),adjust,df_method,vcov_adjust,vcov,equivalence,nperm,model_class,version.
References
Torsten Hothorn, Frank Bretz and Peter Westfall (2008), Simultaneous Inference in General Parametric Models. Biometrical, Journal 50(3), 346–363.
Examples
library(predictmeans)
Oats$nitro <- factor(Oats$nitro)
fm <- lme(yield ~ nitro*Variety, random=~1|Block/Variety, data=Oats)
# 1. Enter a contrast matrix in an edit window (interactive sessions only)
if (interactive()) contrastmeans(fm, "nitro:Variety")
# 2. Give the contrast matrix: Victory - Golden Rain at nitro 0, Victory at 0 - Marvellous at 0.4
cm <- rbind(c(-1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
c(0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0))
contrastmeans(fm, "nitro:Variety", ctrmatrix=cm)
#> Contrasts of 'nitro:Variety' (95% limits; p.adj: none; Kenward-Roger df)
#> contrast estimate std.error df statistic p.value p.adj conf.low
#> contrast 1 -8.50 9.715 30.23 -0.8749 3.885e-01 3.885e-01 -28.33
#> contrast 2 -45.67 9.715 30.23 -4.7006 5.332e-05 5.332e-05 -65.50
#> conf.high
#> 11.33
#> -25.83
# 3. ... and both contrasts tested together (an F test with 2 numerator df)
ct <- contrastmeans(fm, "nitro:Variety", ctrmatrix=cm, joint=TRUE)
ct
#> Contrasts of 'nitro:Variety' (95% limits; p.adj: none; Kenward-Roger df)
#> contrast estimate std.error df statistic p.value p.adj conf.low
#> contrast 1 -8.50 9.715 30.23 -0.8749 3.885e-01 3.885e-01 -28.33
#> contrast 2 -45.67 9.715 30.23 -4.7006 5.332e-05 5.332e-05 -65.50
#> conf.high
#> 11.33
#> -25.83
#> Joint F test: F(2, 30.2) = 12.499, p = 0.000111
as.data.frame(ct)
#> contrast estimate std.error df statistic p.value p.adj
#> 1 contrast 1 -8.50000 9.715029 30.23074 -0.8749331 3.885088e-01 3.885088e-01
#> 2 contrast 2 -45.66667 9.715029 30.23074 -4.7006208 5.331539e-05 5.331539e-05
#> conf.low conf.high p.method
#> 1 -28.33439 11.33439 t
#> 2 -65.50105 -25.83228 t
