Skip to contents

This vignette demonstrates residual and influence diagnostics for:

  1. Univariate models
  2. Multilevel models
  3. Multivariate models

using the mars package functions:

  • residuals(fit, type = ...)
  • residual_diagnostics(fit)
  • influence_diagnostics(fit)

The package includes plot_residual_panel() for a compact three-panel diagnostic plot.

Univariate Example

fit_uni <- mars(
  formula = yi ~ 1,
  data = bcg,
  studyID = "trial",
  variance = "vi",
  varcov_type = "univariate",
  structure = "univariate",
  estimation_method = "MLE"
)

summary(fit_uni)
#> Results generated with MARS:v 0.6.0.8 
#> Friday, November 20, 2026
#> 
#> Model Type: 
#> univariate
#> 
#> Estimation Method: 
#> Maximum Likelihood
#> 
#> Coefficient covariance: gls
#> Optimizer convergence code: 0
#> 
#> 
#> Model Formula: 
#> yi ~ 1
#> 
#> Data Summary: 
#> Number of Effect Sizes: 13
#> Number of Fixed Effects: 1
#> Number of Random Effects: 1
#> 
#> Random Components: 
#>       term  var     SD
#>  intercept 0.28 0.5292
#> 
#> Fixed Effects Estimates: 
#>    attribute estimate     SE z_test   p_value  lower   upper
#>  (Intercept)  -0.7112 0.1719 -4.137 3.513e-05 -1.048 -0.3743
#> 
#> Model Fit Statistics: 
#>  logLik   Dev   AIC   BIC AICc
#>  -12.67 31.33 29.33 30.46 37.9
#> 
#> Q Error: 152.233 (12), p < 0.0001
#> 
#> I2 (General): 91.37828
#> 
#> Residual Diagnostics:
#>  Diagnostic                            Value   
#>  Observations                          13      
#>  Finite raw residuals                  13      
#>  Mean raw residual                     -0.02945
#>  SD of raw residuals                   0.6938  
#>  Root mean squared residual            0.6672  
#>  Mean absolute residual                0.5958  
#>  Sum of squared Pearson residuals      13.16   
#>  Mean |studentized residual|           0.9527  
#>  Maximum |studentized residual|        1.434   
#>  Proportion |studentized residual| > 2 0       
#>  Proportion |studentized residual| > 3 0       
#> 
#> Normality screen (Shapiro-Wilk, whitened residuals):
#>   N     W p-value
#>  13 0.886 0.08592

Residual vectors:

head(residuals(fit_uni, type = "raw"))
#> [1] -0.17811220 -0.87418952 -0.63687401 -0.73035205  0.49365181 -0.07491645
head(residuals(fit_uni, type = "pearson"))
#> [1] -0.2288738 -1.2689295 -0.7637257 -1.3333495  0.8577297 -0.1398577
head(residuals(fit_uni, type = "studentized"))
#> [1] -0.2346703 -1.3103761 -0.7804878 -1.4042904  0.8987531 -0.1476677
head(residuals(fit_uni, type = "whitened"))
#> [1] -0.2288738 -1.2689295 -0.7637257 -1.3333495  0.8577297 -0.1398577

Diagnostics summary:

rd_uni <- residual_diagnostics(fit_uni)
rd_uni$summary
#>    n n_finite_raw    mean_raw   sd_raw      rmse      mae q_pearson
#> 1 13           13 -0.02945125 0.693805 0.6672366 0.595824   13.1642
#>   mean_abs_studentized max_abs_studentized prop_abs_studentized_gt2
#> 1            0.9526839            1.433625                        0
#>   prop_abs_studentized_gt3
#> 1                        0
rd_uni$normality
#>                    test n_tested statistic   p_value    status reason
#> 1 shapiro_wilk_whitened       13 0.8859613 0.0859246 available
rd_uni$heteroscedasticity
#>    n corr_abs_raw_fitted slope p_value      status
#> 1 13                  NA    NA      NA unavailable
#>                                                                            reason
#> 1 Fitted values are constant; a residual-versus-fitted trend cannot be estimated.

The printed model summary shows residual metrics as a compact two-column table. Available normality and residual-trend screens each have a separate heading. The printed summary omits unavailable screens. Inspect residual_diagnostics(fit)$normality or $heteroscedasticity to see their status and reason; these structured results retain NA numeric values when a calculation is unavailable.

Both examples with ~ 1 have constant fitted means. There is no fitted-value variation against which to estimate |raw residual| ~ fitted, so its correlation, slope and p-value are unavailable. This does not indicate a failed model or prove homoscedasticity. Random effects can still be estimated in an intercept-only multilevel model. Plot jitter changes only the display; it does not make this trend estimable.

Shapiro-Wilk requires at least three nonconstant finite whitened residuals; constant residuals and insufficient data receive explicit unavailability reasons. The normality p-value and ordinary residual-trend p-value are exploratory screens, not calibrated confirmatory assumption tests for dependent fitted residuals. The trend regression does not adjust for within-study dependence, and whitening conditions on estimated model covariance. Interpret them with residual plots and substantive model checks rather than treating nonsignificance as proof that an assumption holds.

Influence summary:

inf_uni <- influence_diagnostics(fit_uni, top_n = 5)
head(inf_uni$influence)
#>   index     fitted         raw studentized   leverage cooks_distance
#> 1     1 -0.7111991 -0.17811220  -0.2346703 0.04879109    0.002824756
#> 2     2 -0.7111991 -0.87418952  -1.3103761 0.06225861    0.114000889
#> 3     3 -0.7111991 -0.63687401  -0.7804878 0.04249163    0.027032924
#> 4     4 -0.7111991 -0.73035205  -1.4042904 0.09848251    0.215426346
#> 5     5 -0.7111991  0.49365181   0.8987531 0.08920621    0.079114458
#> 6     6 -0.7111991 -0.07491645  -0.1476677 0.10298026    0.002503357
#>   influence_score flagged cluster
#> 1      0.05314843   FALSE       1
#> 2      0.33764018   FALSE       2
#> 3      0.16441692   FALSE       3
#> 4      0.46414044   FALSE       4
#> 5      0.28127292   FALSE       5
#> 6      0.05003356   FALSE       6
inf_uni$thresholds
#>    n p cooks_distance  leverage abs_studentized
#> 1 13 1      0.3076923 0.1538462               2
inf_uni$top
#>    index     fitted        raw studentized   leverage cooks_distance
#> 8      8 -0.7111991  0.7231515    1.433625 0.10404783      0.2386818
#> 4      4 -0.7111991 -0.7303521   -1.404290 0.09848251      0.2154263
#> 13    13 -0.7111991  0.6938852    1.223032 0.08408012      0.1373129
#> 10    10 -0.7111991 -0.6601457   -1.160646 0.08369429      0.1230424
#> 2      2 -0.7111991 -0.8741895   -1.310376 0.06225861      0.1140009
#>    influence_score flagged cluster
#> 8        0.4885507   FALSE       8
#> 4        0.4641404   FALSE       4
#> 13       0.3705575   FALSE      13
#> 10       0.3507740   FALSE      10
#> 2        0.3376402   FALSE       2
plot_residual_panel(fit_uni, main_prefix = "Univariate", fitted_jitter = 0.02)

Multilevel Example

fit_ml <- mars(
  formula = effect ~ 1 + (1 | district/study),
  data = school,
  studyID = "district",
  variance = "var",
  varcov_type = "multilevel",
  structure = "multilevel",
  estimation_method = "MLE"
)

summary(fit_ml)
#> Results generated with MARS:v 0.6.0.8 
#> Friday, November 20, 2026
#> 
#> Model Type: 
#> multilevel
#> 
#> Estimation Method: 
#> Maximum Likelihood
#> 
#> Coefficient covariance: gls
#> Optimizer convergence code: 0
#> 
#> 
#> Model Formula: 
#> effect ~ 1 + (1 | district/study)
#> 
#> Data Summary: 
#> Number of Effect Sizes: 56
#> Number of Fixed Effects: 1
#> Number of Random Effects: 2
#> 
#> Random Data Summary: 
#> Number unique district: 11 
#> Number unique study: 56
#> 
#> Random Components: 
#>      term     var     SD
#>  district 0.05774 0.2403
#>     study 0.03286 0.1813
#> 
#> Fixed Effects Estimates: 
#>    attribute estimate      SE z_test p_value   lower  upper
#>  (Intercept)   0.1845 0.08048  2.292 0.02191 0.02671 0.3422
#> 
#> Model Fit Statistics: 
#>  logLik   Dev   AIC   BIC AICc
#>  -8.395 24.79 22.79 28.87 24.5
#> 
#> Q Error: 578.864 (55), p < 0.0001
#> 
#> I2 (General): 
#>     names values
#>  district  92.11
#>     study  86.92
#> 
#> I2 (Jackson): 98.69621
#> I2 (Between): 86.37273
#> 
#> Residual Diagnostics:
#>  Diagnostic                            Value   
#>  Observations                          56      
#>  Finite raw residuals                  56      
#>  Mean raw residual                     -0.06446
#>  SD of raw residuals                   0.3258  
#>  Root mean squared residual            0.3293  
#>  Mean absolute residual                0.2632  
#>  Sum of squared Pearson residuals      96.8    
#>  Mean |studentized residual|           1.065   
#>  Maximum |studentized residual|        4.196   
#>  Proportion |studentized residual| > 2 0.1071  
#>  Proportion |studentized residual| > 3 0.03571 
#> 
#> Normality screen (Shapiro-Wilk, whitened residuals):
#>   N      W p-value
#>  56 0.9555 0.03766

Residual diagnostics with cluster-level summaries:

rd_ml <- residual_diagnostics(fit_ml)
rd_ml$summary
#>    n n_finite_raw    mean_raw    sd_raw      rmse       mae q_pearson
#> 1 56           56 -0.06445538 0.3258499 0.3292972 0.2632178  96.79506
#>   mean_abs_studentized max_abs_studentized prop_abs_studentized_gt2
#> 1             1.064822             4.19644                0.1071429
#>   prop_abs_studentized_gt3
#> 1               0.03571429
head(rd_ml$by_cluster)
#>   district  n    mean_raw      rmse mean_abs_studentized q_pearson
#> 1       11  4 -0.30195538 0.3650970            0.7534137  2.811818
#> 2       12  4 -0.08945538 0.2339386            0.6520306  3.617211
#> 3       18  3  0.18887796 0.1999094            0.6839751  1.522648
#> 4       27  4  0.25804462 0.3243544            1.2306961  8.539809
#> 5       56  4 -0.12195538 0.1513006            0.5560033  1.824672
#> 6       58 11 -0.26172810 0.2879608            1.1493136 16.901088

Influence diagnostics with cluster aggregation:

inf_ml <- influence_diagnostics(fit_ml, top_n = 8)
head(inf_ml$influence)
#>   index    fitted         raw studentized   leverage cooks_distance
#> 1     1 0.1844554 -0.36445538  -0.8686211 0.01775368   0.0136373094
#> 2     2 0.1844554 -0.40445538  -0.9639547 0.01775368   0.0167950459
#> 3     3 0.1844554  0.04554462   0.1014940 0.01514380   0.0001583954
#> 4     4 0.1844554 -0.48445538  -1.0795853 0.01514380   0.0179215673
#> 5     5 0.1844554 -0.05445538  -0.2253868 0.02430887   0.0012656382
#> 6     6 0.1844554 -0.44445538  -1.8395685 0.02430887   0.0843110221
#>   influence_score flagged cluster
#> 1      0.11677889   FALSE      11
#> 2      0.12959570   FALSE      11
#> 3      0.01258552   FALSE      11
#> 4      0.13387146   FALSE      11
#> 5      0.03557581   FALSE      12
#> 6      0.29036360    TRUE      12
head(inf_ml$by_cluster)
#>   district  n max_abs_studentized max_leverage   cook_sum   cook_max n_flagged
#> 1       11  4           1.0795853   0.01775368 0.04851232 0.01792157         0
#> 2       12  4           1.8395685   0.02430887 0.09112575 0.08431102         1
#> 3       18  3           0.9849679   0.03537231 0.04377596 0.02836553         0
#> 4       27  4           2.1496240   0.02633069 0.22966072 0.12496138         2
#> 5       56  4           1.1282760   0.02641106 0.04847415 0.03453352         0
#> 6       58 11           2.0187561   0.01123359 0.15388773 0.03330213         1
inf_ml$top
#>    index    fitted        raw studentized   leverage cooks_distance
#> 33    33 0.1844554  1.0055446    4.196440 0.03103163     0.56397148
#> 32    32 0.1844554  0.7955446    3.295056 0.03032420     0.33953804
#> 13    13 0.1844554  0.4655446    2.149624 0.02633069     0.12496138
#> 48    48 0.1844554 -0.7044554   -2.574602 0.01822990     0.12308204
#> 15    15 0.1844554  0.4155446    1.864418 0.02434919     0.08675148
#> 6      6 0.1844554 -0.4444554   -1.839568 0.02430887     0.08431102
#> 43    43 0.1844554  0.4755446    2.113683 0.01657821     0.07531431
#> 52    52 0.1844554 -0.5244554   -1.929235 0.01851989     0.07023068
#>    influence_score flagged cluster
#> 33       0.7509803    TRUE      71
#> 32       0.5826989    TRUE      71
#> 13       0.3534988    TRUE      27
#> 48       0.3508305    TRUE     108
#> 15       0.2945360    TRUE      27
#> 6        0.2903636    TRUE      12
#> 43       0.2744345    TRUE      91
#> 52       0.2650107   FALSE     108
plot_residual_panel(fit_ml, main_prefix = "Multilevel", fitted_jitter = 0.02)

Multivariate Example

fit_mv <- mars(
  data = becker09,
  studyID = "ID",
  effectID = "numID",
  sample_size = "N",
  effectsize_type = "cor",
  varcov_type = "weighted",
  variable_names = c(
    "Cognitive_Performance",
    "Somatic_Performance",
    "Selfconfidence_Performance",
    "Somatic_Cognitive",
    "Selfconfidence_Cognitive",
    "Selfconfidence_Somatic"
  )
)
#> Warning in return_estimates(q_f = q_f, q_r = q_r, estimated_pars =
#> optim_values[["par"]], : Hessian inversion failed: Hessian is singular,
#> indefinite, or numerically unidentified.

summary(fit_mv)
#> Results generated with MARS:v 0.6.0.8 
#> Friday, November 20, 2026
#> 
#> Model Type: 
#> multivariate
#> 
#> Estimation Method: 
#> Restricted Maximum Likelihood
#> 
#> Coefficient covariance: gls
#> Optimizer convergence code: 0
#> 
#> 
#> Model Formula: 
#> NULL
#> 
#> Data Summary: 
#> Number of Effect Sizes: 54
#> Number of Fixed Effects: 6
#> Number of Random Effects: 6
#> 
#> Random Components: 
#>         ri_1     ri_2     ri_3      ri_4      ri_5      ri_6
#> ri_1  0.1263  0.07697 -0.03975  0.005319 -0.018838  0.005678
#> ri_2  0.8801  0.06057 -0.03959  0.006390 -0.018856  0.001176
#> ri_3 -0.4483 -0.64450  0.06228 -0.011109  0.018174 -0.012527
#> ri_4  0.3331  0.57781 -0.99068  0.002019 -0.003211  0.002126
#> ri_5 -0.5011 -0.72416  0.68835 -0.675316  0.011193 -0.001087
#> ri_6  0.2047  0.06121 -0.64310  0.606249 -0.131591  0.006093
#> 
#> Fixed Effects Estimates: 
#>  attribute estimate      SE  z_test   p_value    lower   upper
#>          1 -0.03392 0.12243 -0.2771 7.817e-01 -0.27388  0.2060
#>          2 -0.07108 0.08998 -0.7900 4.295e-01 -0.24743  0.1053
#>          3  0.23285 0.09194  2.5326 1.132e-02  0.05265  0.4131
#>          4  0.54416 0.03359 16.1977 5.233e-59  0.47832  0.6100
#>          5 -0.45298 0.05156 -8.7859 1.551e-18 -0.55403 -0.3519
#>          6 -0.39757 0.04483 -8.8691 7.374e-19 -0.48543 -0.3097
#> 
#> Model Fit Statistics: 
#>  logLik    Dev   AIC   BIC  AICc
#>   16.24 -32.48 21.52 72.04 32.29
#> 
#> Q Error: 198.662 (48), p < 0.0001
#> 
#> I2 (General): 
#>  names values
#>   ri_1  93.75
#>   ri_2  87.79
#>   ri_3  88.09
#>   ri_4  19.34
#>   ri_5  57.06
#>   ri_6  41.97
#> 
#> I2 (Jackson): 
#>  names values
#>   ri_1  89.58
#>   ri_2  81.11
#>   ri_3  84.71
#>   ri_4  20.61
#>   ri_5  55.58
#>   ri_6  41.00
#> 
#> I2 (Between): 84.15488
#> 
#> Residual Diagnostics:
#>  Diagnostic                            Value    
#>  Observations                          54       
#>  Finite raw residuals                  54       
#>  Mean raw residual                     -0.006088
#>  SD of raw residuals                   0.2431   
#>  Root mean squared residual            0.2409   
#>  Mean absolute residual                0.1832   
#>  Sum of squared Pearson residuals      112.6    
#>  Mean |studentized residual|           1.228    
#>  Maximum |studentized residual|        4.244    
#>  Proportion |studentized residual| > 2 0.1852   
#>  Proportion |studentized residual| > 3 0.05556  
#> 
#> Normality screen (Shapiro-Wilk, whitened residuals):
#>   N     W p-value
#>  54 0.977  0.3838
#> 
#> Heteroscedasticity screen (|raw residual| vs fitted):
#>   N Correlation    Slope p-value
#>  54    0.003616 0.001683  0.9793

Outcome-level residual diagnostics:

rd_mv <- residual_diagnostics(fit_mv)
rd_mv$summary
#>    n n_finite_raw     mean_raw   sd_raw      rmse       mae q_pearson
#> 1 54           54 -0.006088321 0.243133 0.2409482 0.1832197  112.6168
#>   mean_abs_studentized max_abs_studentized prop_abs_studentized_gt2
#> 1             1.228348             4.24383                0.1851852
#>   prop_abs_studentized_gt3
#> 1               0.05555556
head(rd_mv$by_outcome)
#>   numID  n     mean_raw      rmse mean_abs_studentized q_pearson
#> 1     1 10  0.003921713 0.3668997            1.4906597 26.560064
#> 2     2 10  0.009077490 0.2789236            1.5785541 35.214678
#> 3     3  9 -0.001742869 0.2409048            1.2446740 19.536181
#> 4     4  9 -0.021938407 0.1318615            0.8719998  8.534355
#> 5     5  8  0.010478291 0.1467261            1.0314387 11.455210
#> 6     6  8 -0.041182025 0.1413573            1.0421329 11.316294

Outcome-level influence diagnostics:

inf_mv <- influence_diagnostics(fit_mv, top_n = 10)
head(inf_mv$influence)
#>   index      fitted         raw studentized  leverage cooks_distance
#> 1     1 -0.03392171 -0.51607829  -3.0730944 0.1150698     0.20466943
#> 2     2 -0.07107749 -0.40892251  -4.2438295 0.1653619     0.59470549
#> 3     3  0.23285398  0.42714602   3.4450630 0.1472575     0.34158804
#> 4     4  0.54416063 -0.07416063  -1.4220719 0.2361086     0.10417705
#> 5     5 -0.45297829  0.07297829   0.9113530 0.1977864     0.03412938
#> 6     6 -0.39756797 -0.06243203  -0.9164031 0.2171342     0.03882064
#>   influence_score flagged cluster outcome
#> 1       1.1081591    TRUE       1       1
#> 2       1.8889767    TRUE       1       2
#> 3       1.4316174    TRUE       1       3
#> 4       0.7906088    TRUE       1       4
#> 5       0.4525221   FALSE       1       5
#> 6       0.4826219   FALSE       1       6
head(inf_mv$by_outcome)
#>   numID  n max_abs_studentized max_leverage  cook_sum  cook_max n_flagged
#> 1     1 10            3.073094    0.1150698 0.5678781 0.2046694         3
#> 2     2 10            4.243830    0.1653619 0.9606643 0.5947055         4
#> 3     3  9            3.445063    0.1472575 0.5794949 0.3415880         2
#> 4     4  9            1.843508    0.2361086 0.2243384 0.1041770         1
#> 5     5  8            2.342525    0.1977864 0.3225118 0.1120384         2
#> 6     6  8            2.221298    0.2171342 0.3225723 0.1314580         2
inf_mv$top
#>    index      fitted         raw studentized   leverage cooks_distance
#> 2      2 -0.07107749 -0.40892251   -4.243830 0.16536193      0.5947055
#> 3      3  0.23285398  0.42714602    3.445063 0.14725752      0.3415880
#> 1      1 -0.03392171 -0.51607829   -3.073094 0.11506982      0.2046694
#> 42    42 -0.39756797  0.12756797    1.774554 0.20030216      0.1314580
#> 32    32 -0.07107749 -0.35892251   -2.588180 0.09566879      0.1181083
#> 35    35 -0.45297829  0.27297829    2.342525 0.10913445      0.1120384
#> 7      7 -0.03392171  0.56392171    2.483741 0.09822009      0.1119854
#> 23    23 -0.07107749  0.38107749    2.580569 0.09138092      0.1116229
#> 29    29 -0.45297829  0.16297829    1.808596 0.16487169      0.1076279
#> 4      4  0.54416063 -0.07416063   -1.422072 0.23610859      0.1041770
#>    influence_score flagged cluster outcome
#> 2        1.8889767    TRUE       1       2
#> 3        1.4316174    TRUE       1       3
#> 1        1.1081591    TRUE       1       1
#> 42       0.8881149    TRUE      28       6
#> 32       0.8418136    TRUE      26       2
#> 35       0.8198967    TRUE      26       5
#> 7        0.8197025    TRUE       3       1
#> 23       0.8183748    TRUE      17       2
#> 29       0.8035966    TRUE      22       5
#> 4        0.7906088    TRUE       1       4
plot_residual_panel(fit_mv, main_prefix = "Multivariate", fitted_jitter = 0.02)

Set fitted_jitter = 0 to keep the original plot, or use a small positive value to spread overlapping fitted values in the raw-residual panel. The histogram panel automatically uses "Sturges" breaks for smaller samples and "FD" for larger ones, and adds a rug to show the observed residual values directly.

Practical Interpretation Notes

Use diagnostics as a combined signal:

  1. Large absolute studentized residuals suggest local model-data misfit.
  2. Large Cook’s distance and leverage identify influential points.
  3. Cluster-level aggregation (by_cluster) helps flag higher-level units that dominate multilevel fits.
  4. Outcome-level aggregation (by_outcome) helps detect systematic outcome-specific misfit in multivariate analyses.

No single threshold is definitive; use diagnostics with substantive context and sensitivity analyses.