This function is a wrapper based on emmeans, and needs a ordinary linear model produced by simple_model or a mixed effects model produced by mixed_model or mixed_model_slopes (or generated directly with lm, lme4 or lmerTest calls). It also needs to know the fixed factor(s), which should match those in the model and data table.

posthoc_Pairwise(Model, Fixed_Factor, P_Adj = "fdr", Factor, ...)

Arguments

Model

a model object fit using simple_model or mixed_model or related.

Fixed_Factor

one or more categorical variables, provided as a vector (see Examples), whose levels you wish to compare pairwise. Names of Fixed_Factor should match Fixed_Factor used to fit the model. When more than one factor is provided e.g. Fixed_factor = c("A", "B"), this function passes this on as specs = A:B (note the colon between the two Fixed_Factor) to emmeans to produce pairwise comparisons.

P_Adj

method for correcting P values for multiple comparisons. Default is set to false discovery rate ("fdr"), can be changed to "none", "tukey", "bonferroni", "sidak". See Interaction analysis in emmeans in the manual for emmeans.

Factor

old argument name for Fixed_Factor; retained for backward compatibility.

...

additional arguments for emmeans such as lmer.df or others. See help for sophisticated models in emmeans.

Value

returns an "emm_list" object containing contrasts and emmeans through emmeans.

Details

The function will generate pairwise comparisons of every level of every factor (as described in Comparisons and contrasts in emmeans). Too many comparisons will be generated and only use this when necessary. By default, P values are corrected by the FDR method (which can be changed). If the model was fit by transforming the quantitative response variable using "log", "logit", "sqrt" etc., results will still be on the original scale, i.e. type = "response" is the default; data will be back-transformed (check results to confirm this), and for log or logit see Transformations and link functions in emmeans, ratios will be compared. The first part of the emmeans results has the estimated marginal means, SE and CI ($emmeans), which are generated from the fitted model, and not the original data table. The second part has the results of the comparisons ($contrasts).

Examples

#make linear models first
DoublMod <- simple_model(data = data_doubling_time, 
Y_value =  "Doubling_time", Fixed_Factor = "Student") 
CholMod <- mixed_model(data = data_cholesterol, 
Y_value = "Cholesterol",
Fixed_Factor = c("Hospital", "Treatment"), 
Random_Factor = "Subject") 

posthoc_Pairwise(Model = DoublMod, 
Fixed_Factor = "Student")
#> $emmeans
#>  Student emmean   SE df lower.CL upper.CL
#>  A         20.0 1.15 20     17.6     22.4
#>  B         19.6 1.15 20     17.2     22.0
#>  C         19.4 1.15 20     17.0     21.8
#>  D         18.8 1.15 20     16.4     21.2
#>  E         19.3 1.15 20     16.9     21.7
#>  F         20.7 1.15 20     18.3     23.1
#>  G         20.5 1.15 20     18.1     22.8
#>  H         19.2 1.15 20     16.8     21.5
#>  I         20.6 1.15 20     18.2     23.0
#>  J         21.2 1.15 20     18.8     23.6
#> 
#> Confidence level used: 0.95 
#> 
#> $contrasts
#>  contrast estimate   SE df t.ratio p.value
#>  A - B      0.3713 1.62 20   0.229  0.9554
#>  A - C      0.5929 1.62 20   0.365  0.9554
#>  A - D      1.1728 1.62 20   0.722  0.9554
#>  A - E      0.6286 1.62 20   0.387  0.9554
#>  A - F     -0.7416 1.62 20  -0.457  0.9554
#>  A - G     -0.4885 1.62 20  -0.301  0.9554
#>  A - H      0.8083 1.62 20   0.498  0.9554
#>  A - I     -0.6775 1.62 20  -0.417  0.9554
#>  A - J     -1.2115 1.62 20  -0.746  0.9554
#>  B - C      0.2216 1.62 20   0.136  0.9554
#>  B - D      0.8014 1.62 20   0.493  0.9554
#>  B - E      0.2572 1.62 20   0.158  0.9554
#>  B - F     -1.1130 1.62 20  -0.685  0.9554
#>  B - G     -0.8598 1.62 20  -0.529  0.9554
#>  B - H      0.4370 1.62 20   0.269  0.9554
#>  B - I     -1.0488 1.62 20  -0.646  0.9554
#>  B - J     -1.5829 1.62 20  -0.975  0.9554
#>  C - D      0.5798 1.62 20   0.357  0.9554
#>  C - E      0.0356 1.62 20   0.022  0.9827
#>  C - F     -1.3346 1.62 20  -0.822  0.9554
#>  C - G     -1.0814 1.62 20  -0.666  0.9554
#>  C - H      0.2154 1.62 20   0.133  0.9554
#>  C - I     -1.2704 1.62 20  -0.782  0.9554
#>  C - J     -1.8044 1.62 20  -1.111  0.9554
#>  D - E     -0.5442 1.62 20  -0.335  0.9554
#>  D - F     -1.9144 1.62 20  -1.179  0.9554
#>  D - G     -1.6612 1.62 20  -1.023  0.9554
#>  D - H     -0.3644 1.62 20  -0.224  0.9554
#>  D - I     -1.8503 1.62 20  -1.139  0.9554
#>  D - J     -2.3843 1.62 20  -1.468  0.9554
#>  E - F     -1.3702 1.62 20  -0.844  0.9554
#>  E - G     -1.1170 1.62 20  -0.688  0.9554
#>  E - H      0.1798 1.62 20   0.111  0.9554
#>  E - I     -1.3061 1.62 20  -0.804  0.9554
#>  E - J     -1.8401 1.62 20  -1.133  0.9554
#>  F - G      0.2532 1.62 20   0.156  0.9554
#>  F - H      1.5500 1.62 20   0.954  0.9554
#>  F - I      0.0642 1.62 20   0.040  0.9827
#>  F - J     -0.4699 1.62 20  -0.289  0.9554
#>  G - H      1.2968 1.62 20   0.798  0.9554
#>  G - I     -0.1890 1.62 20  -0.116  0.9554
#>  G - J     -0.7231 1.62 20  -0.445  0.9554
#>  H - I     -1.4858 1.62 20  -0.915  0.9554
#>  H - J     -2.0199 1.62 20  -1.244  0.9554
#>  I - J     -0.5340 1.62 20  -0.329  0.9554
#> 
#> P value adjustment: fdr method for 45 tests 
#> 

#basic use with two Fixed_Factor provided as a vector
posthoc_Pairwise(Model = CholMod, 
Fixed_Factor = c("Treatment", "Hospital"))
#> $emmeans
#>  Treatment   Hospital emmean   SE   df lower.CL upper.CL
#>  After_drug  Hosp_a      147 23.3 20.3     98.4      196
#>  Before_drug Hosp_a      173 23.3 20.3    124.6      222
#>  After_drug  Hosp_b      142 23.3 20.3     93.8      191
#>  Before_drug Hosp_b      161 23.3 20.3    111.9      209
#>  After_drug  Hosp_c      105 23.3 20.3     56.4      154
#>  Before_drug Hosp_c      124 23.3 20.3     75.6      173
#>  After_drug  Hosp_d      163 23.3 20.3    114.2      211
#>  Before_drug Hosp_d      162 23.3 20.3    113.1      210
#>  After_drug  Hosp_e      162 23.3 20.3    113.8      211
#>  Before_drug Hosp_e      163 23.3 20.3    114.2      211
#> 
#> Degrees-of-freedom method: kenward-roger 
#> Confidence level used: 0.95 
#> 
#> $contrasts
#>  contrast                                estimate    SE   df t.ratio p.value
#>  After_drug Hosp_a - Before_drug Hosp_a  -26.1648  4.13 20.0  -6.341  0.0002
#>  After_drug Hosp_a - After_drug Hosp_b     4.5865 32.99 20.3   0.139  0.9994
#>  After_drug Hosp_a - Before_drug Hosp_b  -13.5032 32.99 20.3  -0.409  0.9994
#>  After_drug Hosp_a - After_drug Hosp_c    42.0038 32.99 20.3   1.273  0.7505
#>  After_drug Hosp_a - Before_drug Hosp_c   22.8400 32.99 20.3   0.692  0.9994
#>  After_drug Hosp_a - After_drug Hosp_d   -15.7699 32.99 20.3  -0.478  0.9994
#>  After_drug Hosp_a - Before_drug Hosp_d  -14.7329 32.99 20.3  -0.447  0.9994
#>  After_drug Hosp_a - After_drug Hosp_e   -15.4323 32.99 20.3  -0.468  0.9994
#>  After_drug Hosp_a - Before_drug Hosp_e  -15.7946 32.99 20.3  -0.479  0.9994
#>  Before_drug Hosp_a - After_drug Hosp_b   30.7514 32.99 20.3   0.932  0.9056
#>  Before_drug Hosp_a - Before_drug Hosp_b  12.6616 32.99 20.3   0.384  0.9994
#>  Before_drug Hosp_a - After_drug Hosp_c   68.1686 32.99 20.3   2.066  0.5390
#>  Before_drug Hosp_a - Before_drug Hosp_c  49.0049 32.99 20.3   1.485  0.6876
#>  Before_drug Hosp_a - After_drug Hosp_d   10.3949 32.99 20.3   0.315  0.9994
#>  Before_drug Hosp_a - Before_drug Hosp_d  11.4319 32.99 20.3   0.347  0.9994
#>  Before_drug Hosp_a - After_drug Hosp_e   10.7326 32.99 20.3   0.325  0.9994
#>  Before_drug Hosp_a - Before_drug Hosp_e  10.3703 32.99 20.3   0.314  0.9994
#>  After_drug Hosp_b - Before_drug Hosp_b  -18.0898  4.13 20.0  -4.384  0.0043
#>  After_drug Hosp_b - After_drug Hosp_c    37.4172 32.99 20.3   1.134  0.7505
#>  After_drug Hosp_b - Before_drug Hosp_c   18.2535 32.99 20.3   0.553  0.9994
#>  After_drug Hosp_b - After_drug Hosp_d   -20.3565 32.99 20.3  -0.617  0.9994
#>  After_drug Hosp_b - Before_drug Hosp_d  -19.3195 32.99 20.3  -0.586  0.9994
#>  After_drug Hosp_b - After_drug Hosp_e   -20.0188 32.99 20.3  -0.607  0.9994
#>  After_drug Hosp_b - Before_drug Hosp_e  -20.3811 32.99 20.3  -0.618  0.9994
#>  Before_drug Hosp_b - After_drug Hosp_c   55.5070 32.99 20.3   1.682  0.5390
#>  Before_drug Hosp_b - Before_drug Hosp_c  36.3433 32.99 20.3   1.102  0.7505
#>  Before_drug Hosp_b - After_drug Hosp_d   -2.2667 32.99 20.3  -0.069  0.9994
#>  Before_drug Hosp_b - Before_drug Hosp_d  -1.2297 32.99 20.3  -0.037  0.9994
#>  Before_drug Hosp_b - After_drug Hosp_e   -1.9290 32.99 20.3  -0.058  0.9994
#>  Before_drug Hosp_b - Before_drug Hosp_e  -2.2913 32.99 20.3  -0.069  0.9994
#>  After_drug Hosp_c - Before_drug Hosp_c  -19.1637  4.13 20.0  -4.645  0.0035
#>  After_drug Hosp_c - After_drug Hosp_d   -57.7737 32.99 20.3  -1.751  0.5390
#>  After_drug Hosp_c - Before_drug Hosp_d  -56.7367 32.99 20.3  -1.720  0.5390
#>  After_drug Hosp_c - After_drug Hosp_e   -57.4361 32.99 20.3  -1.741  0.5390
#>  After_drug Hosp_c - Before_drug Hosp_e  -57.7984 32.99 20.3  -1.752  0.5390
#>  Before_drug Hosp_c - After_drug Hosp_d  -38.6100 32.99 20.3  -1.170  0.7505
#>  Before_drug Hosp_c - Before_drug Hosp_d -37.5730 32.99 20.3  -1.139  0.7505
#>  Before_drug Hosp_c - After_drug Hosp_e  -38.2723 32.99 20.3  -1.160  0.7505
#>  Before_drug Hosp_c - Before_drug Hosp_e -38.6346 32.99 20.3  -1.171  0.7505
#>  After_drug Hosp_d - Before_drug Hosp_d    1.0370  4.13 20.0   0.251  0.9994
#>  After_drug Hosp_d - After_drug Hosp_e     0.3377 32.99 20.3   0.010  0.9994
#>  After_drug Hosp_d - Before_drug Hosp_e   -0.0247 32.99 20.3  -0.001  0.9994
#>  Before_drug Hosp_d - After_drug Hosp_e   -0.6993 32.99 20.3  -0.021  0.9994
#>  Before_drug Hosp_d - Before_drug Hosp_e  -1.0616 32.99 20.3  -0.032  0.9994
#>  After_drug Hosp_e - Before_drug Hosp_e   -0.3623  4.13 20.0  -0.088  0.9994
#> 
#> Degrees-of-freedom method: kenward-roger 
#> P value adjustment: fdr method for 45 tests 
#> 

#same call with "tukey" adjustment
posthoc_Pairwise(Model = CholMod, 
Fixed_Factor = c("Treatment", "Hospital"), 
P_adj = "tukey")
#> $emmeans
#>  Treatment   Hospital emmean   SE   df lower.CL upper.CL
#>  After_drug  Hosp_a      147 23.3 20.3     98.4      196
#>  Before_drug Hosp_a      173 23.3 20.3    124.6      222
#>  After_drug  Hosp_b      142 23.3 20.3     93.8      191
#>  Before_drug Hosp_b      161 23.3 20.3    111.9      209
#>  After_drug  Hosp_c      105 23.3 20.3     56.4      154
#>  Before_drug Hosp_c      124 23.3 20.3     75.6      173
#>  After_drug  Hosp_d      163 23.3 20.3    114.2      211
#>  Before_drug Hosp_d      162 23.3 20.3    113.1      210
#>  After_drug  Hosp_e      162 23.3 20.3    113.8      211
#>  Before_drug Hosp_e      163 23.3 20.3    114.2      211
#> 
#> Degrees-of-freedom method: kenward-roger 
#> Confidence level used: 0.95 
#> 
#> $contrasts
#>  contrast                                estimate    SE   df t.ratio p.value
#>  After_drug Hosp_a - Before_drug Hosp_a  -26.1648  4.13 20.0  -6.341  0.0002
#>  After_drug Hosp_a - After_drug Hosp_b     4.5865 32.99 20.3   0.139  0.9994
#>  After_drug Hosp_a - Before_drug Hosp_b  -13.5032 32.99 20.3  -0.409  0.9994
#>  After_drug Hosp_a - After_drug Hosp_c    42.0038 32.99 20.3   1.273  0.7505
#>  After_drug Hosp_a - Before_drug Hosp_c   22.8400 32.99 20.3   0.692  0.9994
#>  After_drug Hosp_a - After_drug Hosp_d   -15.7699 32.99 20.3  -0.478  0.9994
#>  After_drug Hosp_a - Before_drug Hosp_d  -14.7329 32.99 20.3  -0.447  0.9994
#>  After_drug Hosp_a - After_drug Hosp_e   -15.4323 32.99 20.3  -0.468  0.9994
#>  After_drug Hosp_a - Before_drug Hosp_e  -15.7946 32.99 20.3  -0.479  0.9994
#>  Before_drug Hosp_a - After_drug Hosp_b   30.7514 32.99 20.3   0.932  0.9056
#>  Before_drug Hosp_a - Before_drug Hosp_b  12.6616 32.99 20.3   0.384  0.9994
#>  Before_drug Hosp_a - After_drug Hosp_c   68.1686 32.99 20.3   2.066  0.5390
#>  Before_drug Hosp_a - Before_drug Hosp_c  49.0049 32.99 20.3   1.485  0.6876
#>  Before_drug Hosp_a - After_drug Hosp_d   10.3949 32.99 20.3   0.315  0.9994
#>  Before_drug Hosp_a - Before_drug Hosp_d  11.4319 32.99 20.3   0.347  0.9994
#>  Before_drug Hosp_a - After_drug Hosp_e   10.7326 32.99 20.3   0.325  0.9994
#>  Before_drug Hosp_a - Before_drug Hosp_e  10.3703 32.99 20.3   0.314  0.9994
#>  After_drug Hosp_b - Before_drug Hosp_b  -18.0898  4.13 20.0  -4.384  0.0043
#>  After_drug Hosp_b - After_drug Hosp_c    37.4172 32.99 20.3   1.134  0.7505
#>  After_drug Hosp_b - Before_drug Hosp_c   18.2535 32.99 20.3   0.553  0.9994
#>  After_drug Hosp_b - After_drug Hosp_d   -20.3565 32.99 20.3  -0.617  0.9994
#>  After_drug Hosp_b - Before_drug Hosp_d  -19.3195 32.99 20.3  -0.586  0.9994
#>  After_drug Hosp_b - After_drug Hosp_e   -20.0188 32.99 20.3  -0.607  0.9994
#>  After_drug Hosp_b - Before_drug Hosp_e  -20.3811 32.99 20.3  -0.618  0.9994
#>  Before_drug Hosp_b - After_drug Hosp_c   55.5070 32.99 20.3   1.682  0.5390
#>  Before_drug Hosp_b - Before_drug Hosp_c  36.3433 32.99 20.3   1.102  0.7505
#>  Before_drug Hosp_b - After_drug Hosp_d   -2.2667 32.99 20.3  -0.069  0.9994
#>  Before_drug Hosp_b - Before_drug Hosp_d  -1.2297 32.99 20.3  -0.037  0.9994
#>  Before_drug Hosp_b - After_drug Hosp_e   -1.9290 32.99 20.3  -0.058  0.9994
#>  Before_drug Hosp_b - Before_drug Hosp_e  -2.2913 32.99 20.3  -0.069  0.9994
#>  After_drug Hosp_c - Before_drug Hosp_c  -19.1637  4.13 20.0  -4.645  0.0035
#>  After_drug Hosp_c - After_drug Hosp_d   -57.7737 32.99 20.3  -1.751  0.5390
#>  After_drug Hosp_c - Before_drug Hosp_d  -56.7367 32.99 20.3  -1.720  0.5390
#>  After_drug Hosp_c - After_drug Hosp_e   -57.4361 32.99 20.3  -1.741  0.5390
#>  After_drug Hosp_c - Before_drug Hosp_e  -57.7984 32.99 20.3  -1.752  0.5390
#>  Before_drug Hosp_c - After_drug Hosp_d  -38.6100 32.99 20.3  -1.170  0.7505
#>  Before_drug Hosp_c - Before_drug Hosp_d -37.5730 32.99 20.3  -1.139  0.7505
#>  Before_drug Hosp_c - After_drug Hosp_e  -38.2723 32.99 20.3  -1.160  0.7505
#>  Before_drug Hosp_c - Before_drug Hosp_e -38.6346 32.99 20.3  -1.171  0.7505
#>  After_drug Hosp_d - Before_drug Hosp_d    1.0370  4.13 20.0   0.251  0.9994
#>  After_drug Hosp_d - After_drug Hosp_e     0.3377 32.99 20.3   0.010  0.9994
#>  After_drug Hosp_d - Before_drug Hosp_e   -0.0247 32.99 20.3  -0.001  0.9994
#>  Before_drug Hosp_d - After_drug Hosp_e   -0.6993 32.99 20.3  -0.021  0.9994
#>  Before_drug Hosp_d - Before_drug Hosp_e  -1.0616 32.99 20.3  -0.032  0.9994
#>  After_drug Hosp_e - Before_drug Hosp_e   -0.3623  4.13 20.0  -0.088  0.9994
#> 
#> Degrees-of-freedom method: kenward-roger 
#> P value adjustment: fdr method for 45 tests 
#>