The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Checking the model

Predicted means, their standard errors and the tests are only as good as the model behind them. Before reading them, two functions help check the model:

Both work for every model class that predictmeans() handles: lm, aov (also with Error()), glm, gls, lme, lmer, glmer, glmmTMB and gam.

A split plot: the basic checks

Yates’ oats: varieties on whole plots within blocks, nitrogen rates on sub-plots.

library(predictmeans)
Oats <- as.data.frame(nlme::Oats)
Oats$nitro <- factor(Oats$nitro)
fm <- lmer(yield ~ nitro * Variety + (1 | Block/Variety), data = Oats)
pm_check(fm)
#> Checks of a lmerMod model (72 observations)
#> 
#> Fit
#>   OK Fit                                         no warnings                   
#> 
#> Residuals
#>   OK Outliers (|residual| > 3)                   0 of 72 (expected 0.2)        
#>   OK Normality of residuals                      Shapiro-Wilk             0.270
#>   OK Normality of random effects: Variety:Block  Shapiro-Wilk, 18 le...   0.564
#>   OK Normality of random effects: Block          Shapiro-Wilk, 6 levels   0.031
#> 
#> Variance
#>   OK Equal variances: nitro                      SD ratio 1.32 (0.4 ...   0.340
#>   OK Equal variances: Variety                    SD ratio 1.37 (Gold...   0.060
#>   OK Spread vs fitted values                     Spearman rho 0.04        0.751
#> 
#> Design
#>   OK Balance of cells                            balanced (6 per cell)         
#> 
#> Terms
#>   OK Interaction: nitro:Variety                  Type III test            0.932
#>   OK Straight line in nitro                      curvature, 2 df          0.265
#> 
#> Notes
#> - Normality of random effects: Variety:Block: Shapiro-Wilk, 18 levels.
#> - Normality of random effects: Block: Few levels: little power.
#> - Equal variances: nitro: SD ratio 1.32 (0.4 / 0).
#> - Equal variances: Variety: SD ratio 1.37 (Golden Rain / Marvellous).
#> - Interaction: nitro:Variety: No evidence of interaction; keep the term if
#>     the design calls for it.
#> - Straight line in nitro: The means of nitro are near a straight line:
#>     nitro could also enter as a number (a response line); as a factor it
#>     compares the levels.
#> 
#> Nothing flagged.
#> (! worth a look, i information, OK no concern.) Checks inform, they do not decide; see residplot().

Rows marked ! are worth a look. Here none are. The notes say what each row means for the analysis:

residplot(fm)

Four diagnostic plots of the split-plot model

The four panels:

  1. Residuals against fitted values, with a smooth: a trend points to a missing term or a wrong link, and a funnel to a variance that changes with the mean.
  2. Normal Q–Q plot of the residuals: the points should lie near the line.
  3. Q–Q plot of the random effects of one term (level chooses which: here 1, the whole plots Variety:Block; level = 2 gives the blocks).
  4. Fitted against observed values.

The three most extreme points are labelled with their row names (id changes how many).

For other classes, panels 3 and 4 differ:

Model Panel 3 Panel 4
lmer, lme, glmer, glmmTMB Q–Q plot of the random effects fitted against observed
gls autocorrelation of the normalized residuals fitted against observed
lm, aov, glm scale-location (√|residual| against fitted) Cook’s distances
0/1 responses as above calibration: observed proportions against fitted probabilities

The residuals are those that should look normal when the model is right: standardized for lm, normalized (the correlation removed) for gls and lme, and randomized quantile residuals (Dunn and Smyth 1996) for Poisson, binomial and negative binomial models. Raw or Pearson residuals of counts are not normal even for a correct model, while quantile residuals are exactly normal, so the plots read the same way as for a normal model.

Incomplete blocks: an alpha lattice

24 oat genotypes in 3 replicates, each of 6 incomplete blocks of 4 plots (data(john_alpha), from the agridat package; John and Williams 1995, Cyclic and Computer Generated Designs, Chapman & Hall). A block holds only 4 of the 24 genotypes, so the blocks are random and the genotype means borrow information between blocks:

data(john_alpha)
fa <- lmer(yield ~ rep + gen + (1 | rep:block), data = john_alpha)
pm_check(fa)
#> Checks of a lmerMod model (72 observations)
#> 
#> Fit
#>   OK Fit                                     no warnings                       
#> 
#> Residuals
#>   OK Outliers (|residual| > 3)               0 of 72 (expected 0.2)            
#>   OK Normality of residuals                  Shapiro-Wilk                 0.013
#>   OK Normality of random effects: rep:block  Shapiro-Wilk, 18 levels      0.253
#> 
#> Variance
#>   OK Equal variances: rep                    SD ratio 1.93 (R1 / R3)      0.187
#>   OK Equal variances: gen                    SD ratio 18.79 (G05 / G07)   0.837
#>   OK Spread vs fitted values                 Spearman rho 0.01            0.932
#> 
#> Design
#>   OK Balance of cells                        balanced (1 per cell)             
#>   OK Collinearity (GVIF^(1/(2 df)))          largest 1.00 (rep)                
#> 
#> Nothing flagged.
#> (! worth a look, i information, OK no concern.) Checks inform, they do not decide; see residplot().

Nothing is flagged. The normality test of the residuals gives p = 0.013, but with 72 residuals the Q–Q plot is the better guide: two or three large positive residuals in the upper tail, not a skewed distribution. “Equal variances: gen” reports an SD ratio of 18.8, but with 3 plots per genotype these SDs are each from 3 values, and the test (p = 0.84) finds no evidence against equal variances.

residplot(fa)

Four diagnostic plots of the alpha-lattice model

Block R2:B1 has the lowest predicted block effect, labelled in panel 3. Since the genotypes are compared within blocks, the standard errors of differences depend on whether two genotypes met in a block; they range from 0.262 to 0.277 (mean 0.271, as SAS PROC MIXED with DDFM=KR):

pa <- predictmeans(fa, "gen", pairwise = TRUE)
summary(pa$pairs$std.error)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.2618  0.2631  0.2748  0.2708  0.2761  0.2773

Counts: overdispersion

The number of warp breaks per loom, for two wools and three tensions, as a Poisson model:

gm <- glm(breaks ~ wool * tension, family = poisson, data = warpbreaks)
pm_check(gm)
#> Checks of a glm model (54 observations)
#> 
#> Fit
#>   OK Fit                                   no warnings                         
#> 
#> Residuals
#>   !  Outliers (|residual| > 3)             6 of 54 (expected 0.1): 5...        
#>   OK Influence (Cook's distance)           largest 0.341 (5)                   
#>   OK Normality of residuals (quantile)     Shapiro-Wilk                   0.296
#> 
#> Variance
#>   OK Equal variances: wool                 SD ratio 1.41 (A / B)          0.023
#>   OK Equal variances: tension              SD ratio 1.42 (L / H)          0.171
#> 
#> Distribution
#>   !  Dispersion (Pearson chi-square / df)  3.76 on 48 df                 <1e-04
#> 
#> Design
#>   OK Balance of cells                      balanced (9 per cell)               
#> 
#> Terms
#>   OK Interaction: wool:tension             Type III test                 <1e-04
#> 
#> Notes
#> ! Outliers (|residual| > 3): 6 of 54 (expected 0.1): 5 (3.6), 24 (3.4), 4
#>     (-3.2), .... Check these observations (data entry, unusual units)
#>     before anything else.
#> ! Dispersion (Pearson chi-square / df): Overdispersion: more variation than
#>     the poisson distribution allows, so SEs are too small and tests too
#>     liberal. Consider a negative binomial model, an observation-level
#>     random effect, or a quasi family (glm).
#> - Interaction: wool:tension: The effect of one factor depends on the other:
#>     compare the levels of one within each level of the other (atvar or
#>     pm_joint(by = ...)).
#> 
#> 2 worth a look: Outliers (|residual| > 3); Dispersion (Pearson chi-square / df).
#> (! worth a look, i information, OK no concern.) Checks inform, they do not decide; see residplot().

Two rows are flagged. Six residuals are beyond 3 where 0.1 are expected, and the Pearson chi-square is 3.8 times its df: the counts vary more than a Poisson distribution allows, so its standard errors are too small and its tests too liberal. The plots show the same:

residplot(gm)

Diagnostic plots of the Poisson model

A negative binomial model lets the variance grow faster than the mean:

nb <- MASS::glm.nb(breaks ~ wool * tension, data = warpbreaks)
pm_check(nb)
#> Checks of a negbin model (54 observations)
#> 
#> Fit
#>   OK Fit                                   no warnings                         
#> 
#> Residuals
#>   OK Outliers (|residual| > 3)             0 of 54 (expected 0.1)              
#>   OK Influence (Cook's distance)           largest 0.107 (24)                  
#>   OK Normality of residuals (quantile)     Shapiro-Wilk                   0.229
#> 
#> Variance
#>   OK Equal variances: wool                 SD ratio 1.32 (A / B)          0.044
#>   OK Equal variances: tension              SD ratio 1.19 (L / H)          0.531
#> 
#> Distribution
#>   OK Dispersion (Pearson chi-square / df)  1.08 on 48 df                  0.327
#> 
#> Design
#>   OK Balance of cells                      balanced (9 per cell)               
#> 
#> Terms
#>   OK Interaction: wool:tension             Type III test                  0.017
#> 
#> Notes
#> - Interaction: wool:tension: The effect of one factor depends on the other:
#>     compare the levels of one within each level of the other (atvar or
#>     pm_joint(by = ...)).
#> 
#> Nothing flagged.
#> (! worth a look, i information, OK no concern.) Checks inform, they do not decide; see residplot().

No outliers remain. The interaction is still there, so compare the wools within each tension:

pm_nb <- predictmeans(nb, "wool:tension", atvar = "tension", pairwise = TRUE)
pm_nb$pairs[!is.na(pm_nb$pairs$by), c("level1", "level2", "ratio", "ratio.conf.low",
                                      "ratio.conf.high", "p.value")]
#>   level1 level2     ratio ratio.conf.low ratio.conf.high     p.value
#> 1    A:L    B:L 1.5787402      1.1593116        2.149914 0.003752862
#> 2    A:M    B:M 0.8339768      0.6047698        1.150053 0.268175808
#> 3    A:H    B:H 1.3076923      0.9374866        1.824089 0.114150116

For a Poisson, binomial or negative binomial model, the tests are z tests: the dispersion is known, so the residual df do not apply (settings$df_method is "asymptotic (z)").

Interactive plots

With interactive = TRUE the plots are drawn with plotly, if it is installed. Hovering over a point shows its row of the data, which helps find an outlier in a large data set:

residplot(fm, interactive = TRUE)

group colours the points by a variable, for example residplot(fm, group = "Block"). pm_check(fm, plot = TRUE) draws the plots as well as giving the table.

What the checks do not do

The checks inform; they do not decide.

When a check fails, the usual remedies are a transformation (trans in predictmeans() then back-transforms the means), a variance structure (gls or lme with weights), a different distribution (negative binomial, glmmTMB), or a robust or permutation analysis (vignette("resampling", package = "predictmeans")).

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.