One of four related functions for mixed effects analyses (based on lmer and as_lmerModLmerTest) to get a linear model for downstream steps, or an ANOVA table.

  1. mixed_model

  2. mixed_anova

  3. mixed_model_slopes

  4. mixed_anova_slopes.

mixed_anova(
  data,
  Y_value,
  Fixed_Factor,
  Random_Factor,
  Df_method = "Kenward-Roger",
  SS_method = "II",
  AvgRF = TRUE,
  Formula = NULL,
  ...
)

Arguments

data

a data table object, e.g. data.frame or tibble.

Y_value

name of column containing quantitative (dependent) variable, provided within "quotes". The following transformations are permitted: "log(Y_value)", "log(Y_value + c)" where c a positive number, "logit(Y_value)" or "logit(Y_value/100)" which may be useful when Y_value are percentages (note quotes outside the log or logit calls); "sqrt(Y_value)" or "(Y_value)^2" should also work. During posthoc-comparisons, log and logit transformations will be back-transformed to the original scale. Other transformations, e.g., "sqrt(Y_value)" will not be back-transformed. Check out the regrid and ref_grid for details if you need back-transformation to the response scale.

Fixed_Factor

name(s) of categorical fixed factors (independent variables) provided within quotes (e.g., "A") or as a vector if more than one (e.g., c("A", "B"). If a numeric variable is used, transformations similar to Y_value are permitted.

Random_Factor

name(s) of random factors to allow random intercepts; to be provided within quotes (e.g., "R") or as a vector when more than one (e.g., c("R1", "R2")).

Df_method

method for calculating degrees of freedom. Default is Kenward-Roger, can be changed to "Satterthwaite".

SS_method

type of sum of square, default is type II, can be changed to "I", "III", "1" or "2", or others.

AvgRF

this is a new argument since v5.0.0. The default AvgRF = TRUE will use the mean of Y_value (the response variable) grouped by levels of the Fixed_Factor and Random_Factor (using table_summary). This ensures that replicates within Random_Factor (or any other unused variable) are averaged (e.g., technical replicates nested within experimental blocks) before fitting a linear model and the denominator Df values are sensible. The name of the data frame in the model object will have (AvgRF) appended to it to indicate the averaging within levels of the Random_Factor. Using AvgRF = FALSE will lead to behaviour like versions <5.0.0.

Formula

directly provide an a formula (within quotes) as you would if you were using lmer. If Y_value, Fixed_Factor and Random_Factor are provided, they will be ignored. This is basically a wrapper, which may be useful if fitting more complex random factor structures.

...

any additional arguments to pass on to lmer if required.

Value

ANOVA table of class "anova" and "data.frame".

Details

These functions require a data table, one dependent variable (Y_value), one or more independent variables (Fixed_Factor), and at least one random factor (Random_Factor). These should match names of variables in the long-format data table exactly. Since v5.0.0, if AvgRF = TRUE, the response variable is averaged over levels of the fixed and random factors (to collapse replicate observations) and reduce the number of denominator degrees of freedom. If you do not want to do this, set AvgRF = FALSE.

Outputs of mixed_model and mixed_model_slopes can be used for post-hoc comparisons with posthoc_Pairwise, posthoc_Levelwise, posthoc_vsRef, posthoc_Trends_Pairwise, posthoc_Trends_Levelwise and posthoc_Trends_vsRefor with emmeans.

More than one fixed factors can be provided as a vector (e.g. c("A", "B")). A full model with interaction term is fitted. This means when Y_value = Y, Fixed_factor = c("A", "B"), Random_factor = "R" are entered as arguments, these are passed on as Y ~ A*B + (1|R) (which is equivalent to Y ~ A + B + A:B + (1|R)).

In mixed_model_slopes and mixed_anova_slopes, the following kind of formula is used: Y ~ A*B + (S|R) (which is equivalent to Y ~ A + B + A:B + (S|R)). In this experimental implementation, random slopes and intercepts are fitted ((Slopes_Factor|Random_Factor)). Only one term each is allowed for Slopes_Factor and Random_Factor.

Examples

#Usage with one fixed (Student) and random factor (Experiment)
mixed_anova(data = data_doubling_time, 
Y_value = "Doubling_time", 
Fixed_Factor = "Student", 
Random_Factor = "Experiment")
#> The new argument `AvgRF` is set to TRUE by default in >=5.0.0). Response variable is averaged over levels of Fixed and Random factors. Use help for details.
#> boundary (singular) fit: see help('isSingular')
#> Type II Analysis of Variance Table with Kenward-Roger's method
#>         Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
#> Student 16.824  1.8694     9    18  0.4725 0.8744

#with formula
mixed_anova(data = data_doubling_time, 
Formula = "Doubling_time ~ Student +(1|Experiment)")
#> boundary (singular) fit: see help('isSingular')
#> Type II Analysis of Variance Table with Kenward-Roger's method
#>         Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
#> Student 16.824  1.8694     9    18  0.4725 0.8744