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_vsRef(Model, Fixed_Factor, Ref_Level = 1, P_Adj = "fdr", Factor, ...)

Arguments

Model

a model object fit using simple_model or mixed_model or related.

Fixed_Factor

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 vertical | between the two Fixed_Factor) to emmeans. The specification internally is set to specs = trt.vs.ctrl, Ref_Level = 1 to compare each group in A to the reference first group in A, separately at each level of B.

Ref_Level

the level within that factor to be considered the reference or control to compare other levels to (to be provided as a number - by default R orders levels alphabetically); default Ref_Level = 1.

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 treatment vs control type of comparisons (as described in Comparisons and contrasts in emmeans), i.e. comparison of each level of a factor to a reference level, which is set by default to the first level in the factor (Ref_Level = 1). 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") 

#to compare all students with student #9
posthoc_vsRef(Model = DoublMod, 
Fixed_Factor = "Student", Ref_Level = 9)
#> $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 - I     -0.6775 1.62 20  -0.417  0.9588
#>  B - I     -1.0488 1.62 20  -0.646  0.9463
#>  C - I     -1.2704 1.62 20  -0.782  0.9463
#>  D - I     -1.8503 1.62 20  -1.139  0.9463
#>  E - I     -1.3061 1.62 20  -0.804  0.9463
#>  F - I      0.0642 1.62 20   0.040  0.9689
#>  G - I     -0.1890 1.62 20  -0.116  0.9689
#>  H - I     -1.4858 1.62 20  -0.915  0.9463
#>  J - I      0.5340 1.62 20   0.329  0.9588
#> 
#> P value adjustment: fdr method for 9 tests 
#> 

#for comparison between hospital_a to every other hospital, separately at levels of Treatment
posthoc_vsRef(Model = CholMod, 
Fixed_Factor = c("Hospital", "Treatment"), Ref_Level = 1)
#> $emmeans
#> Treatment = After_drug:
#>  Hospital emmean   SE   df lower.CL upper.CL
#>  Hosp_a      147 23.3 20.3     98.4      196
#>  Hosp_b      142 23.3 20.3     93.8      191
#>  Hosp_c      105 23.3 20.3     56.4      154
#>  Hosp_d      163 23.3 20.3    114.2      211
#>  Hosp_e      162 23.3 20.3    113.8      211
#> 
#> Treatment = Before_drug:
#>  Hospital emmean   SE   df lower.CL upper.CL
#>  Hosp_a      173 23.3 20.3    124.6      222
#>  Hosp_b      161 23.3 20.3    111.9      209
#>  Hosp_c      124 23.3 20.3     75.6      173
#>  Hosp_d      162 23.3 20.3    113.1      210
#>  Hosp_e      163 23.3 20.3    114.2      211
#> 
#> Degrees-of-freedom method: kenward-roger 
#> Confidence level used: 0.95 
#> 
#> $contrasts
#> Treatment = After_drug:
#>  contrast        estimate SE   df t.ratio p.value
#>  Hosp_b - Hosp_a    -4.59 33 20.3  -0.139  0.8908
#>  Hosp_c - Hosp_a   -42.00 33 20.3  -1.273  0.8599
#>  Hosp_d - Hosp_a    15.77 33 20.3   0.478  0.8599
#>  Hosp_e - Hosp_a    15.43 33 20.3   0.468  0.8599
#> 
#> Treatment = Before_drug:
#>  contrast        estimate SE   df t.ratio p.value
#>  Hosp_b - Hosp_a   -12.66 33 20.3  -0.384  0.7565
#>  Hosp_c - Hosp_a   -49.00 33 20.3  -1.485  0.6112
#>  Hosp_d - Hosp_a   -11.43 33 20.3  -0.347  0.7565
#>  Hosp_e - Hosp_a   -10.37 33 20.3  -0.314  0.7565
#> 
#> Degrees-of-freedom method: kenward-roger 
#> P value adjustment: fdr method for 4 tests 
#>