Skip to contents

predictmeans has its own code for lm, aov, glm (with MASS::glm.nb), gls, lme, lmer, glmer, glmmTMB, gam, semireg() fits, the ordinal and categorical models, mmrm, betareg and the survival models coxph, coxme and survreg. For any other class, the means functions (predictmeans(), contrastmeans(), covariatemeans(), pm_slopes(), pm_joint(), Kmatrix()) take the linear basis from another package, chosen once in this order:

source when what predictmeans takes from it
emmeans emmeans supports the class (rq, gee, geeglm, …) the design rows for predictmeans’ grid, coefficients, covariance, the df of each mean, contrast and joint test, the link
the default methods coef() and vcov() work and agree as for any new class (?pm_coef)
marginaleffects it can set the coefficients and predict on the link scale the derivatives of those predictions, used only when they are exactly linear in the coefficients

The grid, weights, empty cells, pairs, adjustments, letters and plots are always predictmeans’ own. Every result says where its basis came from: settings$source ("model", "emmeans" or "marginaleffects") and, for the last two, the advice line foreign_basis.

Repeated measures: mmrm

FEV1 of patients in two arms over four visits, with an unstructured covariance of the visits within a patient (mmrm::fev_data). The df method is chosen when the model is fitted (mmrm(method = )); predictmeans uses mmrm’s df for every mean and pair (mmrm::df_1d()) and for joint tests (mmrm::df_md()).

library(mmrm)
fit_mmrm <- mmrm(FEV1 ~ RACE + ARMCD * AVISIT + us(AVISIT | USUBJID), data = fev_data)
pm_mmrm <- predictmeans(fit_mmrm, "ARMCD:AVISIT", atvar = "AVISIT")
subset(pm_mmrm$pairs, !is.na(by), c(by, level1, level2, estimate, std.error, df, p.value))   # within each visit
##     by   level1   level2  estimate std.error       df      p.value
## 1 VIS1 PBO:VIS1 TRT:VIS1 -3.775557 1.0762774 146.2690 6.001485e-04
## 2 VIS2 PBO:VIS2 TRT:VIS2 -3.758183 0.8530774 148.3047 2.011686e-05
## 3 VIS3 PBO:VIS3 TRT:VIS3 -3.108025 0.6893227 131.6525 1.426107e-05
## 4 VIS4 PBO:VIS4 TRT:VIS4 -4.406501 1.6808180 133.3393 9.767860e-03
pm_mmrm$settings$df_method
## [1] "Satterthwaite (mmrm)"

The treatment differences at all four visits in one joint F test:

L <- t(sapply(1:4, function(i) replace(numeric(8), c(2 * i - 1, 2 * i), c(-1, 1))))
contrastmeans(fit_mmrm, "ARMCD:AVISIT", ctrmatrix = L, joint = TRUE)$joint
##   df1      df2  F.ratio     p.value
## 1   4 143.6541 28.23314 2.62429e-17

These numbers were checked against SAS PROC MIXED (tests/testthat/test-mmrm-betareg.R): an unstructured covariance and an AR(1) covariance with 5 of 48 values missing give the same means, SEs, df and F tests as DDFM=SATTERTHWAITE, all 77 and 118 printed values.

Kenward-Roger. Fit with method = "Kenward-Roger", vcov = "Kenward-Roger-Linear": that equals SAS DDFM=KR (and KR(FIRSTORDER) for AR(1) and other covariances that are not linear in their parameters), the same rule as predictmeans’ own Kenward-Roger for lmer, lme and gls. mmrm’s default Kenward-Roger covariance depends on how mmrm parametrises the covariance (in the SAS check of an unstructured covariance its SEs were smaller than the unadjusted ones), and predictmeans says so.

fit_kr <- mmrm(FEV1 ~ RACE + ARMCD * AVISIT + us(AVISIT | USUBJID), data = fev_data,
               method = "Kenward-Roger", vcov = "Kenward-Roger-Linear")
pm_kr <- predictmeans(fit_kr, "ARMCD")
pm_kr$means[, c("ARMCD", "estimate", "std.error", "df")]
##   ARMCD estimate std.error       df
## 1   PBO 40.89386 0.4696595 178.9068
## 2   TRT 44.65593 0.4735666 158.0614
pm_kr$settings$df_method
## [1] "Kenward-Roger (mmrm, vcov = \"Kenward-Roger-Linear\")"
fit_kr_default <- mmrm(FEV1 ~ RACE + ARMCD * AVISIT + us(AVISIT | USUBJID), data = fev_data, method = "Kenward-Roger")
advice(predictmeans(fit_kr_default, "ARMCD"))$text
## [1] "The SEs use mmrm's default Kenward-Roger covariance, which differs from SAS DDFM=KR; refit with vcov = \"Kenward-Roger-Linear\" for the standard adjustment."

Proportions: betareg

Gasoline yield, a proportion, for 10 batches of crude oil and the temperature at which all gasoline has vaporised (betareg::GasolineYield). The means are of the mean model, on its logit scale, with the mean proportions (response) back-transformed by the inverse link; a precision model (| temp) does not enter the means. The tests are z tests (an ML fit with no residual variance), as betareg’s summary and emmeans.

library(betareg)
data("GasolineYield", package = "betareg")
fit_beta <- betareg(yield ~ batch + temp | temp, data = GasolineYield)
pm_beta <- predictmeans(fit_beta, "batch", adj = "tukey")
pm_beta$means[, c("batch", "estimate", "std.error", "response", "response.conf.low", "response.conf.high")]
##    batch   estimate  std.error   response response.conf.low response.conf.high
## 1      1 -0.8809293 0.04815599 0.29298525        0.27382272         0.31291095
## 2      2 -1.1856508 0.08091871 0.23403770        0.20681226         0.26365584
## 3      3 -0.9175787 0.08027454 0.28545150        0.25446971         0.31859340
## 4      4 -1.4528450 0.04913602 0.18956410        0.17520909         0.20480318
## 5      5 -1.3287540 0.04925376 0.20936555        0.19383368         0.22579339
## 6      6 -1.4634724 0.05221534 0.18793681        0.17281417         0.20405635
## 7      7 -1.8606580 0.05443315 0.13462638        0.12267435         0.14754704
## 8      8 -1.9183340 0.05620417 0.12804746        0.11624373         0.14085873
## 9      9 -2.1234780 0.06008033 0.10683574        0.09610835         0.11860338
## 10    10 -2.4829170 0.05629735 0.07706447        0.06957360         0.08528794
pm_beta$settings[c("source", "df_method")]
## $source
## [1] "model"
## 
## $df_method
## [1] "asymptotic (z)"

They equal emmeans’ (mode = "link"):

library(emmeans)
em <- emmeans(fit_beta, ~ batch, mode = "link")
all.equal(pm_beta$means$estimate, summary(em)$emmean)
## [1] TRUE
all.equal(pm_beta$means$response, summary(em, type = "response")$response)
## [1] TRUE
autoplot(pm_beta, scale = "response")

Through emmeans: quantile regression

The median yield of each batch (quantreg::rq, tau = 0.5). rq has no adapter in predictmeans, so the basis and the df (here one df for all rows, n - p) come from emmeans.

library(quantreg)
fit_rq <- rq(yield ~ batch + temp, data = GasolineYield, tau = 0.5)
pm_rq <- predictmeans(fit_rq, "batch")
head(pm_rq$means[, c("batch", "estimate", "std.error", "df")], 4)
##   batch  estimate   std.error df
## 1     1 0.3262578 0.007807313 21
## 2     2 0.2420435 0.017534644 21
## 3     3 0.2785792 0.009962022 21
## 4     4 0.2071150 0.007946153 21
pm_rq$settings[c("source", "df_method")]
## $source
## [1] "emmeans"
## 
## $df_method
## [1] "emmeans (one df for all rows)"
advice(pm_rq)$text
## [1] "Class 'rq' has no adapter in predictmeans: the means and SEs use emmeans' linear basis; df: emmeans (one df for all rows)."

Survival models

coxph (with strata, cluster = and frailty terms), coxme and survreg have their own code, with Wald z tests as SAS PROC PHREG and LIFEREG; the results equal SAS on the same data. Cox means are log hazards relative to the data’s average linear predictor and their differences log hazard ratios; survreg means are on the log-time scale and their differences log time ratios. The survival article works through these models on one study: their checks (pm_check(), residplot()), hazard ratios, survival curves (survmeans()), random effects and the comparison with SAS.

What works for which class

Each main function on each class (yes), or a stop with a message that names the classes it handles (-). The table is checked by the package’s tests on one data set with every kind of response (tests/testthat/helper-conformance.R; written by dev/conformance-matrix.R); covariatemeans(), pm_joint() and Kmatrix() go with contrastmeans(), pm_check() with residplot(). For every class emmeans supports, the means and differences equal emmeans’ (to 1e-6, mixed models to 2%; Cox means differ from emmeans’ by a constant, since predictmeans centres them at the data’s average). permmodels() of a clmm needs a treatment applied to whole clusters, which these test data lack.

class predictmeans covariatemeans contrastmeans pm_joint pm_slopes varcomp R2_model residplot CookD permmodels bootmodels
lm yes yes yes yes yes yes yes yes yes yes yes
aov yes yes yes yes yes yes yes yes yes yes yes
aovlist yes yes yes yes yes yes yes yes - - -
glm_pois yes yes yes yes yes yes yes yes yes yes yes
glm_bin yes yes yes yes yes yes yes yes yes yes yes
glm_nb yes yes yes yes yes yes yes yes yes yes yes
gls yes yes yes yes yes yes yes yes yes yes yes
lme yes yes yes yes yes yes yes yes yes yes yes
lmer yes yes yes yes yes yes yes yes yes yes yes
glmer_pois yes yes yes yes yes yes yes yes yes yes yes
glmer_bin yes yes yes yes yes yes yes yes yes yes yes
tmb_pois yes yes yes yes yes yes yes yes yes yes yes
tmb_nb yes yes yes yes yes yes yes yes yes yes yes
gam yes yes yes yes yes yes yes yes yes yes -
semireg yes yes yes yes yes yes yes yes yes yes yes
polr yes yes yes yes yes - - - - - -
clm yes yes yes yes yes - - - - - -
clmm yes yes yes yes yes - - - - - yes
multinom yes - - - - - - - - - -
vglm yes yes yes yes yes - - - - - -
mmrm yes yes yes yes yes - - yes - - -
betareg yes yes yes yes yes - - yes - - -
coxph yes yes yes yes yes - - yes - - -
coxph_frailty yes yes yes yes yes - - yes - - -
coxme yes yes yes yes yes - - - - - -
survreg yes yes yes yes yes - - yes - - -
rq yes yes yes yes yes - - yes - - -

What stops, and why

predictmeans stops with a message when it cannot give a right answer:

  • emmeans gives a basis that is not linear in the coefficients (its response-scale modes, e.g. for zeroinfl), or one with more rows than the grid (rq with several tau): fit one model per quantile.
  • Bayesian fits (brmsfit, MCMCglmm): their means come from posterior draws; use emmeans.
  • bias_adjust = TRUE without sigma for a class without an adapter: predictmeans cannot know whether the model is normal on its scale.
  • marginaleffects’ link predictions that are not exactly linear, or hold an offset.
predictmeans(rq(yield ~ batch + temp, data = GasolineYield, tau = c(0.25, 0.75)), "batch")
## Error:
## ! For class 'rqs', emmeans' basis has 64 rows for 32 grid rows (a multivariate fit, e.g. several quantiles or responses): fit one model per response.

An offset is left out (set to 0), as for every class, where emmeans adds its mean; the advice line says so. Permutation tests, the bootstrap, varcomp(), R2_model(), CookD() and anova() still need code for the class and say which classes they handle.

A class neither package supports

Write methods for the adapter generics pm_coef(), pm_vcov() and pm_basis() (and pm_df() for df of your own); see ?pm_coef for a worked example. A class with its own methods always uses them, never the packages above.