Type: Package
Title: Linear Mixed Models - An Introduction with Applications in Veterinary Research
Version: 1.1.0
Description: R Codes and Datasets for Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998). Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
Depends: R (≥ 4.1.0)
Suggests: collapse, emmeans, ggplot2, lme4, lmerTest, multcomp, nlme, quarto, report, testthat (≥ 3.0.0)
VignetteBuilder: quarto
License: GPL-2
URL: https://myaseen208.com/VetResearchLMM/, https://github.com/myaseen208/VetResearchLMM/
BugReports: https://github.com/myaseen208/VetResearchLMM/issues
Encoding: UTF-8
Language: en-US
LazyData: TRUE
RoxygenNote: 7.3.3
Config/testthat/edition: 3
Config/Needs/website: pkgdown, quarto
Note: School of Mathematical and Statistical Sciences, Clemson University, Clemson, South Carolina, USA. Department of Mathematics and Statistics, University of Agriculture Faisalabad, Faisalabad-Pakistan.
NeedsCompilation: no
Packaged: 2026-05-05 06:57:35 UTC; myaseen208
Author: Muhammad Yaseen [aut, cre] (affiliation: School of Mathematical and Statistical Sciences, Clemson University, Clemson, South Carolina, USA), Luc Duchateau [ctb], Paul Janssen [ctb], John Rowlands [ctb]
Maintainer: Muhammad Yaseen <myaseen208@gmail.com>
Repository: CRAN
Date/Publication: 2026-05-05 11:20:22 UTC

VetResearchLMM: Linear Mixed Models for Veterinary Research Examples

Description

The VetResearchLMM package provides the datasets and reproducible R examples that accompany Duchateau, Janssen, and Rowlands (1998), Linear Mixed Models: An Introduction with Applications in Veterinary Research. The package is intended for readers who want to reproduce, inspect, and adapt the linear mixed model examples from the book using current R tooling.

Details

The package focuses on small applied veterinary research examples involving fixed effects, random effects, variance components, nested designs, repeated measurements, and basic hypothesis tests for linear mixed models. The package also includes report_mixed_model() and emmeans_mixed_model(), small helpers that delegate fitted model interpretation and post hoc marginal-mean inference to the optional report and emmeans packages when they are installed. The main user-facing data objects are the book datasets:

ex121

Dose comparison data for packed cell volume.

ex124

Herd, drug, and dose packed cell volume data.

ex125

Region, drug, and dose split-plot data.

ex127

Sire-level weaning weight data.

ex31

Designed experiment data for PCV response.

ex32

Breed, sire, sex, age, and weaning weight data.

ex33

Longitudinal PCV data by animal and breed.

The example help pages reproduce the corresponding analyses with modern R packages such as lme4, lmerTest, nlme, multcomp, collapse, ggplot2, and emmeans where those packages are available. Numerical results can differ slightly from the book because the book reports SAS output and modern R packages use their own optimizers, parameterizations, and degrees-of-freedom methods.

A typical workflow is:

  1. Load one of the included datasets with data().

  2. Inspect the matching example help page, such as ?Examp2.4.2.2.

  3. Fit the fixed effect or mixed model shown in the example.

  4. Compare estimates, variance components, and tests with the book.

  5. Use report_mixed_model() for an optional narrative model report when report is installed.

  6. Use emmeans_mixed_model() for optional estimated marginal means and post hoc comparisons when emmeans is installed.

  7. Use the package vignettes for chapter-level narrative examples.

Affiliation

School of Mathematical and Statistical Sciences, Clemson University, Clemson, South Carolina, USA.

Vignettes

The Quarto vignettes provide an introduction, methodology overview, chapter/example walkthrough, and plotting guide:

Author(s)

Muhammad Yaseen myaseen208@gmail.com

References

Duchateau, L., Janssen, P., and Rowlands, G. J. (1998). Linear Mixed Models: An Introduction with Applications in Veterinary Research. International Livestock Research Institute.

See Also

Useful links:


Examp1.3.2 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp1.3.2 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 1.3.2 p-16
#-------------------------------------------------------------
 # PROC GLM DATA=ex124;
 # CLASS herd dose drug;
 # MODEL PCVdif=drug herd(drug) dose dose*drug;
 # RANDOM herd(drug);
 # RUN;

str(ex124)
summary(ex124)

ex124$herd1 <- factor(ex124$herd)
ex124$drug1 <- factor(ex124$drug)
ex124$dose1 <- factor(ex124$dose)

fm1.1 <-
  aov(
      formula     = PCVdif ~ drug1 + Error(herd1:drug1) + dose1 + dose1:drug1
    , data        = ex124
    , projections = FALSE
    , qr          = TRUE
    , contrasts   = NULL
  #  , ...
    )
if (requireNamespace("report", quietly = TRUE)) {
  fm1.1 |>
    report::report()
}
summary(fm1.1)

Examp2.2.1.7 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.2.1.7 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.2.1.7 p-42
#-------------------------------------------------------------
 # PROC GLM DATA=ex121;
 # CLASS dose;
 # MODEL PCVdif=dose;
 # ESTIMATE 'l vs mh' dose -0.5 1 -0.5;
 # CONTRAST 'l vs mh' dose -0.5 1 -0.5;
 # RUN;

str(ex121)
fm2.1 <-
 aov(
     formula     = PCVdiff ~ dose
   , data        = ex121
   , projections = FALSE
   , qr          = TRUE
   , contrasts   = NULL
 #  , ...
   )
if (requireNamespace("report", quietly = TRUE)) {
  fm2.1 |>
    report::report()
}
summary(fm2.1)
anova(fm2.1)

LvsMHConc <-
         matrix(
             data    = c(-0.5, 1, -0.5)
           , nrow    = length(levels(ex121$dose))
           , byrow   = FALSE
           , dimnames = list(
                             c(levels(ex121$dose))
                           , c("Low vs Mediam and Hight")
                           )
         )

contrasts(ex121$dose) <- LvsMHConc
fm2.2 <-
 aov(
     formula     = PCVdiff ~ dose
   , data        = ex121
   , projections = FALSE
   , qr          = TRUE
   , contrasts   = NULL
 #  , ...
   )
if (requireNamespace("report", quietly = TRUE)) {
  fm2.2 |>
    report::report()
}
summary(fm2.2, split = list(dose = list("Low vs Mediam and Hight" = 1)))

fm2.3 <-
     lm(
          formula     = PCVdiff ~ dose
        , data        = ex121
     #  , subset
     #  , weights
     #  , na.action
        , method      = "qr"
        , model       = TRUE
        , x           = FALSE
        , y           = FALSE
        , qr          = TRUE
        , singular.ok = TRUE
        , contrasts   = NULL
     #  , offset
     #  , ...
     )
if (requireNamespace("report", quietly = TRUE)) {
  fm2.3 |>
    report::report()
}
if (requireNamespace("emmeans", quietly = TRUE)) {
  emm2.3 <- emmeans::emmeans(fm2.3, ~ dose)
  print(emm2.3)
  print(emmeans::contrast(
    emm2.3,
    method = list(low_vs_medium_high = c(-0.5, 1, -0.5))
  ))
}
summary(fm2.3)
anova(fm2.3)
# multcomp::glht(
#      model       = fm2.3
#    , linfct      = LvsMHConc
#    , alternative = "two.sided" # c("two.sided", "less", "greater")
#    , rhs         = 0
#    )


Examp2.4.2.2 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.4.2.2 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.4.2.2 p-64
#-------------------------------------------------------------
# PROC MIXED DATA=ex125 METHOD=ML;
# CLASS drug dose region;
# MODEL pcv=drug dose drug*dose;
# RANDOM region drug*region;
# RUN;
# 
# PROC MIXED DATA=ex125 METHOD=REML;
# CLASS drug dose region;
# MODEL pcv=drug dose drug*dose;
# RANDOM region drug*region;
# RUN;
 
 

str(ex125)
if (requireNamespace("lme4", quietly = TRUE)) {
  fm2.4 <-
    lme4::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = FALSE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = NULL
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.4 |>
      report::report()
  }
  summary(fm2.4)
  anova(fm2.4)

  fm2.5 <-
    lme4::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = NULL
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.5 |>
      report::report()
  }
  summary(fm2.5)
  anova(fm2.5)
}

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm2.6 <-
    lmerTest::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = FALSE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = NULL
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.6 |>
      report::report()
  }
  summary(fm2.6)
  anova(fm2.6)

  fm2.7 <-
    lmerTest::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = NULL
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.7 |>
      report::report()
  }
  summary(fm2.7)
  anova(fm2.7)
}



Examp2.4.3.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.4.3.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.4.3.1 p-66
#-------------------------------------------------------------
 # PROC MIXED DATA=ex127;
 # CLASS sire;
 # MODEL ww=;
 # RANDOM sire/solution;
 # RUN;
 
str(ex127)
if (requireNamespace("lme4", quietly = TRUE)) {
  fm2.8 <-
    lme4::lmer(
           formula    = Ww ~ (1 | sire)
         , data       = ex127
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = NULL
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.8 |>
      report::report()
  }
  summary(fm2.8)
  lme4::fixef(fm2.8)
  lme4::ranef(fm2.8)
}


Examp2.5.1.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.5.1.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.5.1.1 p-67
#-------------------------------------------------------------
 # PROC MIXED DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=drug dose drug*dose / solution covb;
 # RANDOM region drug*region;
 # RUN;
 
str(ex125)
if (requireNamespace("lme4", quietly = TRUE)) {
  fm2.9 <-
    lme4::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose = "contr.SAS", Drug = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.9 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm2.9 <- emmeans::emmeans(fm2.9, ~ dose | Drug, lmer.df = "asymptotic")
    print(emm2.9)
    print(emmeans::contrast(emm2.9, method = "pairwise"))
  }
  summary(fm2.9)
  anova(fm2.9)
  summary(fm2.9)$vcov
}


Examp2.5.2.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.5.2.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.5.2.1 p-68
#-------------------------------------------------------------
 # PROC MIXED DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=drug dose drug*dose / solution covb;
 # RANDOM region drug*region;
 # LSMEANS drug*dose;
 # RUN;

str(ex125)
if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm2.10 <-
    lmerTest::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose = "contr.SAS", Drug = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.10 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm2.10 <- emmeans::emmeans(fm2.10, ~ dose | Drug, lmer.df = "asymptotic")
    print(emm2.10)
    print(emmeans::contrast(emm2.10, method = "pairwise"))
  }
  summary(fm2.10)
  anova(fm2.10)
  summary(fm2.10)$vcov
}

Examp2.5.3.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.5.3.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.5.3.1 p-70
#-------------------------------------------------------------
 # PROC GLM DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=region drug region*drug dose drug*dose;
 # RANDOM region drug*region;
 # RUN;


 # PROC MIXED DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=drug dose drug*dose / ddfm=satterth;
 # RANDOM region drug*region;
 # ESTIMATE 'drug dif' drug -1 1 drug*dose -0.5 -0.5 0.5 0.5;
 # ESTIMATE 'Samorin mean' INTERCEPT 1 drug 0 1 dose 0.5 0.5
 #                             drug*dose 0 0 0.5 0.5;
 # ESTIMATE 'Samorin HvsL' dose 1 -1 drug*dose 0 0 1 -1;
 # ESTIMATE 'Samorin high' INTERCEPT 1 drug 0 1 dose 1 0
 #                             drug*dose 0 0 1 0;
 # RUN;

str(ex125)
ex125$Region1 <- factor(ex125$Region)
fm2.11 <-
  aov(
      formula     = Pcv ~ Region1 + Drug + Error(Drug:Region1) + dose + dose:Drug
    , data        = ex125
    , projections = FALSE
    , qr          = TRUE
    , contrasts   = NULL
    )
if (requireNamespace("report", quietly = TRUE)) {
  fm2.11 |>
    report::report()
}
summary(fm2.11)

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm2.12 <-
    lmerTest::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose = "contr.SAS", Drug = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.12 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm2.12 <- emmeans::emmeans(fm2.12, ~ dose | Drug, lmer.df = "asymptotic")
    print(emm2.12)
    print(emmeans::contrast(emm2.12, method = "pairwise"))
  }
  summary(fm2.12)
  anova(object = fm2.12, ddf = "Satterthwaite")

  if (requireNamespace("multcomp", quietly = TRUE)) {
    Contrasts1 <-
      matrix(
        c(1, 0.5, 0, 0, 0, 0, -1, -0.5, 1, 1, 0, 0, 0, 1, 0, 0),
        ncol = 4,
        byrow = TRUE,
        dimnames = list(
          c("C1", "C2", "C3", "C4"),
          rownames(summary(fm2.12)$coef)
        )
      )
    Contrasts1
    summary(multcomp::glht(fm2.12, linfct = Contrasts1))
  }
}


Examp2.5.4.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.5.4.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.5.4.1 p-74
#-------------------------------------------------------------
 # PROC MIXED DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=drug dose drug*dose / ddfm=satterth;
 # RANDOM region drug*region;
 # ESTIMATE 'Samorin mean' INTERCEPT 1 drug 0 1 dose 0.5 0.5
 #                             drug*dose 0 0 0.5 0.5;
 # RUN;
 
 # PROC GLM DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=region drug region*drug dose drug*dose;
 # ESTIMATE 'Samorin mean' INTERCEPT 1 drug 0 1 dose 0.5 0.5
 #                             drug*dose 0 0 0.5 0.5;
 # RUN;
  
str(ex125)
ex125$Region1 <- factor(ex125$Region)
if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm2.13 <-
    lmerTest::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose = "contr.SAS", Drug = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.13 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm2.13 <- emmeans::emmeans(fm2.13, ~ dose | Drug, lmer.df = "asymptotic")
    print(emm2.13)
    print(emmeans::contrast(emm2.13, method = "pairwise"))
  }
  summary(fm2.13)

  if (requireNamespace("multcomp", quietly = TRUE)) {
    Contrasts2 <-
      matrix(
        c(1, 0.5, 0, 0),
        ncol = 4,
        byrow = TRUE,
        dimnames = list(c("C5"), rownames(summary(fm2.13)$coef))
      )
    Contrasts2
    summary(multcomp::glht(fm2.13, linfct = Contrasts2))
  }
}


Examp2.6.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp2.6.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 2.6.1 p-76
#-------------------------------------------------------------
 # PROC MIXED DATA=ex125;
 # CLASS drug dose region;
 # MODEL pcv=drug dose drug*dose / ddfm=satterth;
 # RANDOM region drug*region;
 # CONTRAST 'drug dif' drug -1 1 drug*dose -0.5 -0.5 0.5 0.5;
 # CONTRAST 'all' drug 1 -1 dose 0  0 drug*dose 0.5  0.5 -0.5 -0.5,
 #                drug 0  0 dose 1 -1 drug*dose 0.5 -0.5  0.5 -0.5,
 #                drug 0  0 dose 0  0 drug*dose 0.5 -0.5 -0.5  0.5;
 # RUN;

str(ex125)
ex125$Region1 <- factor(ex125$Region)
if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm2.14 <-
    lmerTest::lmer(
           formula    = Pcv ~ dose * Drug + (1 | Region / Drug)
         , data       = ex125
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose = "contr.SAS", Drug = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm2.14 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm2.14 <- emmeans::emmeans(fm2.14, ~ dose | Drug, lmer.df = "asymptotic")
    print(emm2.14)
    print(emmeans::contrast(emm2.14, method = "pairwise"))
  }
  summary(fm2.14)
  anova(object = fm2.14, ddf = "Satterthwaite")

  Contrasts3 <-
    matrix(
      c(0, 0, -1, -0.5),
      ncol = 4,
      byrow = TRUE,
      dimnames = list(c("C1"), rownames(summary(fm2.14)$coef))
    )
  Contrasts3
  if (requireNamespace("multcomp", quietly = TRUE)) {
    summary(multcomp::glht(fm2.14, linfct = Contrasts3))
  }
  if (utils::packageVersion("lmerTest") >= "3.0") {
    lmerTest::contest(fm2.14, Contrasts3, joint = FALSE)
  }
}


Examp3.1 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp3.1 is.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 3.1 Model 1 p-80
#-------------------------------------------------------------
# PROC MIXED DATA=ex31;
# CLASS drug dose herd;
# MODEL PCV2=drug dose(drug)/solution ddfm=satterth;
# RANDOM herd(drug);
# ESTIMATE 'Mean Samorin' intercept 1 drug 0 1 dose(drug) 0 0 1;
# ESTIMATE 'Berenil 2 doses' dose(drug) 1 -1 0;
# ESTIMATE 'Ber vs Sam at dose 1' drug 1 -1 dose(drug) 1 0 -1;
# CONTRAST 'Mean Samorin' intercept 1 drug 0 1 dose(drug) 0 0 1;
# CONTRAST 'Berenil dif 2 doses' dose(drug) 1 -1 0;
# CONTRAST 'Ber vs Sam at dose 1' drug 1 -1 dose(drug) 1 0 -l;
# CONTRAST 'some difference' drug 1 -1 dose(drug) 0.5 0.5 -1,
#           drug 0 0 dose(drug) 1 -1 0;
# LSMEANS dose(drug);
# RUN;

str(ex31)
ex31$herd1 <- factor(ex31$herd)
ex31$drug1 <- factor(ex31$drug)
ex31$dose1 <- factor(ex31$dose)
ex31$ber   <- as.integer(ex31$drug == "BERENIL")
ex31$ber1  <- as.integer(ex31$drug == "BERENIL" & ex31$dose == 1L)
ex31$pcv_ber1 <- ex31$PCV1 * as.integer(ex31$drug == "BERENIL" & ex31$dose == 1L)
ex31$pcv_ber2 <- ex31$PCV1 * as.integer(ex31$drug == "BERENIL" & ex31$dose == 2L)

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm3.1 <-
    lmerTest::lmer(
           formula    = PCV2 ~ drug1 + dose1:drug1 + (1 | herd1:drug1)
         , data       = ex31
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose1 = "contr.SAS", drug1 = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.1 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm3.1 <- emmeans::emmeans(fm3.1, ~ dose1 | drug1, lmer.df = "asymptotic")
    print(emm3.1)
    print(emmeans::contrast(emm3.1, method = "pairwise"))
  }
  summary(fm3.1)
  anova(object = fm3.1, ddf = "Satterthwaite")

#-------------------------------------------------------------
## Example 3.1 Model 2 p-84
#-------------------------------------------------------------
# PROC MIXED DATA=ex31;
# CLASS drug dose herd;
# MODEL PCV2=PCV1 drug dose(drug)/solution ddfm=satterth;
# RANDOM herd(drug);
# RUN;

  fm3.2 <-
    lmerTest::lmer(
           formula    = PCV2 ~ PCV1 + drug1 + dose1:drug1 + (1 | herd1:drug1)
         , data       = ex31
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose1 = "contr.SAS", drug1 = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.2 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm3.2 <- emmeans::emmeans(fm3.2, ~ dose1 | drug1, lmer.df = "asymptotic")
    print(emm3.2)
    print(emmeans::contrast(emm3.2, method = "pairwise"))
  }
  summary(fm3.2)
  anova(object = fm3.2, ddf = "Satterthwaite")

#-------------------------------------------------------------
## Example 3.1 Model 3 p-86
#-------------------------------------------------------------
# PROC MIXED DATA=ex31;
# CLASS drug dose herd;
# MODEL PCV2=drug dose(drug) PCV1*dose(drug)/solution ddfm=satterth;
# RANDOM herd(drug);
# RUN;

  fm3.3 <-
    lmerTest::lmer(
           formula    = PCV2 ~ drug1 + PCV1 * dose1:drug1 + (1 | herd1:drug1)
         , data       = ex31
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(dose1 = "contr.SAS", drug1 = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.3 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm3.3 <- emmeans::emmeans(fm3.3, ~ dose1 | drug1, lmer.df = "asymptotic")
    print(emm3.3)
    print(emmeans::contrast(emm3.3, method = "pairwise"))
  }
  summary(fm3.3)
  anova(object = fm3.3, ddf = "Satterthwaite")
}

Examp3.2 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp3.2 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 3.3 p-88
#-------------------------------------------------------------
# PROC MIXED DATA=ex32;
# CLASS sex sire_id breed;
# MODEL ww = sex agew breed/SOLUTION DDFM=SATTERTH;
# RANDOM sire_id(breed)/SOLUTION;
# LSMEANS breed/ADJUST = TUKEY;
# RUN;

str(ex32)
ex32$sire_id1 <- factor(ex32$sire_id)
ex32$breed1   <- factor(ex32$breed)
if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm3.4 <-
    lmerTest::lmer(
           formula    = Ww ~ sex + agew + breed1 + (1 | sire_id1:breed1)
         , data       = ex32
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(sex = "contr.SAS", breed1 = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.4 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    emm3.4 <- emmeans::emmeans(fm3.4, ~ breed1, lmer.df = "asymptotic")
    print(emm3.4)
    print(emmeans::contrast(emm3.4, method = "pairwise", adjust = "tukey"))
  }
  summary(fm3.4)
  anova(object = fm3.4, ddf = "Satterthwaite")
}

Examp3.3 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Examp3.3 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

ex124

Examples

#-------------------------------------------------------------
## Example 3.3 Model 1 p-88
#-------------------------------------------------------------
# PROC MIXED DATA=ex33;
# CLASS breed animal_id;
# MODEL pcv = breed breed*time/SOLUTION;
# RANDOM animal_id(breed)/SOLUTION;
# RUN;

str(ex33)
if (requireNamespace("lme4", quietly = TRUE)) {
  fm3.5 <-
    lme4::lmer(
           formula    = PCV ~ breed + breed:time + (1 | animal_id:breed)
         , data       = ex33
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(breed = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.5 |>
      report::report()
  }
  summary(fm3.5)
  anova(fm3.5)
}

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm3.6 <-
    lmerTest::lmer(
           formula    = PCV ~ breed + breed:time + (1 | animal_id:breed)
         , data       = ex33
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(breed = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.6 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    trend3.6 <- emmeans::emtrends(fm3.6, ~ breed, var = "time",
                                  lmer.df = "asymptotic")
    print(trend3.6)
    print(emmeans::contrast(trend3.6, method = "pairwise"))
  }
  summary(fm3.6)
  anova(object = fm3.6, ddf = "Satterthwaite")
}

if (requireNamespace("nlme", quietly = TRUE)) {
  fm3.7 <-
    nlme::gls(
          model       = PCV ~ breed + breed:time
        , data        = ex33
        , correlation = nlme::corCompSymm(form = ~ 1 | animal_id / breed)
        , weights     = NULL
        , method      = "REML"
        , na.action   = na.fail
        , control     = list()
        )
  if (requireNamespace("report", quietly = TRUE)) {
    try(report::report(fm3.7), silent = TRUE)
  }
  summary(fm3.7)
  anova(fm3.7)
}

if (requireNamespace("lme4", quietly = TRUE)) {
  fm3.8 <-
    lme4::lmer(
           formula    = PCV ~ time + breed + breed:time + (1 | animal_id:breed)
         , data       = ex33
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(breed = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.8 |>
      report::report()
  }
  summary(fm3.8)
  anova(fm3.8)
}

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("lmerTest", quietly = TRUE)) {
  fm3.9 <-
    lmerTest::lmer(
           formula    = PCV ~ time + breed + breed:time + (1 | animal_id:breed)
         , data       = ex33
         , REML       = TRUE
         , control    = lme4::lmerControl()
         , start      = NULL
         , verbose    = 0L
         , contrasts  = list(breed = "contr.SAS")
         , devFunOnly = FALSE
         )
  if (requireNamespace("report", quietly = TRUE)) {
    fm3.9 |>
      report::report()
  }
  if (requireNamespace("emmeans", quietly = TRUE)) {
    trend3.9 <- emmeans::emtrends(fm3.9, ~ breed, var = "time",
                                  lmer.df = "asymptotic")
    print(trend3.9)
    print(emmeans::contrast(trend3.9, method = "pairwise"))
  }
  summary(fm3.9)
  anova(object = fm3.9, ddf = "Satterthwaite", type = 3)
}

if (requireNamespace("nlme", quietly = TRUE)) {
  fm3.10 <-
    nlme::gls(
          model       = PCV ~ breed + breed:time
        , data        = ex33
        , correlation = nlme::corAR1(form = ~ 1 | animal_id / breed)
        , weights     = NULL
        , method      = "REML"
        , na.action   = na.fail
        , control     = list()
        )
  if (requireNamespace("report", quietly = TRUE)) {
    try(report::report(fm3.10), silent = TRUE)
  }
  summary(fm3.10)
  anova(fm3.10)
}


Estimated Marginal Means for a Fitted Mixed Model

Description

Compute estimated marginal means or pairwise comparisons for a fitted linear mixed model using the optional emmeans package.

Usage

emmeans_mixed_model(
  model,
  specs,
  pairwise = FALSE,
  method = "pairwise",
  adjust = "tukey",
  ...
)

Arguments

model

A fitted model object, typically from lme4::lmer(), lmerTest::lmer(), or nlme::lme().

specs

Specifications for the marginal means, passed to emmeans::emmeans(); for example, ~ dose or ~ dose | Drug.

pairwise

Logical. If FALSE, return the estimated marginal means. If TRUE, return contrasts computed from those marginal means.

method

Contrast method passed to emmeans::contrast() when pairwise = TRUE. The default is "pairwise".

adjust

Multiplicity adjustment passed to emmeans::contrast() when pairwise = TRUE. The default is "tukey". Use NULL to leave the adjustment unspecified.

...

Additional arguments passed to emmeans::emmeans().

Details

Estimated marginal means, also called least-squares means, summarize model predictions for factor levels after accounting for the fitted model structure. They are useful after mixed model fitting because fixed-effect coefficients are often expressed relative to contrast coding, while marginal means and their contrasts are closer to the scientific comparisons shown in the book examples.

This helper complements report_mixed_model(). Use report_mixed_model() for narrative model interpretation and emmeans_mixed_model() for post hoc inference, estimated marginal means, and pairwise comparisons.

The helper keeps emmeans optional. It does not refit the model or change the estimates; it delegates marginal-mean calculations to emmeans::emmeans() and, when requested, contrasts to emmeans::contrast().

Value

An emmGrid object from emmeans. With pairwise = FALSE, this contains estimated marginal means. With pairwise = TRUE, this contains the requested contrasts.

References

Lenth, R. V. (2024). emmeans: Estimated Marginal Means, aka Least-Squares Means. R package. See utils::citation("emmeans").

Duchateau, L., Janssen, P., and Rowlands, G. J. (1998). Linear Mixed Models: An Introduction with Applications in Veterinary Research. International Livestock Research Institute.

See Also

report_mixed_model(), emmeans::emmeans(), emmeans::contrast().

Examples

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("emmeans", quietly = TRUE)) {
  data(ex125, package = "VetResearchLMM")
  fit <- lme4::lmer(
    Pcv ~ dose * Drug + (1 | Region / Drug),
    data = ex125,
    REML = TRUE
  )

  emmeans_mixed_model(fit, ~ dose | Drug, lmer.df = "asymptotic")
  emmeans_mixed_model(
    fit,
    ~ dose | Drug,
    pairwise = TRUE,
    lmer.df = "asymptotic"
  )
}


ex121 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Packed cell volume data for the dose comparison in Example 1.2.1.

Usage

data(ex121)

Format

A data.frame with 14 rows and 5 variables:

animals

Animal identifier within dose group.

dose

Dose group with levels H, L, and M.

PCV1

Packed cell volume measured at treatment.

PCV2

Packed cell volume measured after treatment.

PCVdiff

Difference between post-treatment and baseline packed cell volume.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Examples

data(ex121)

ex124 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Packed cell volume response data by herd, drug, and dose.

Usage

data(ex124)

Format

A data.frame with 40 rows and 4 variables:

herd

Herd identifier.

drug

Drug administered, Berenil or Samorin.

dose

Dose group, high (h) or low (l).

PCVdif

Change in packed cell volume after treatment.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

Examp1.3.2

Examples

data(ex124)

ex125 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Packed cell volume response data by region, drug, and dose.

Usage

data(ex125)

Format

A data.frame with 24 rows and 4 variables:

Region

Region identifier.

Drug

Drug administered, Berenil or Samorin.

dose

Dose group, high (h) or low (l).

Pcv

Packed cell volume response.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

Examp1.3.2

Examples

data(ex125)

ex127 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Weaning weight observations grouped by sire.

Usage

data(ex127)

Format

A data.frame with 43 rows and 2 variables:

sire

Sire identifier.

Ww

Weaning weight.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

Examp1.3.2

Examples

data(ex127)

ex31 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Packed cell volume data for two trypanosomosis drugs.

Usage

data(ex31)

Format

A data.frame with 38 rows and 6 variables:

herd

Herd identifier.

animal_id

Animal identifier.

PCV1

Packed cell volume measured at treatment.

PCV2

Packed cell volume measured one month after treatment.

dose

Dose level.

drug

Drug administered, Berenil or Samorin.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

Examp1.3.2

Examples

data(ex31)

ex32 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Weaning weight data by breed, sire, sex, and age.

Usage

data(ex32)

Format

A data.frame with 65 rows and 5 variables:

breed

Breed identifier.

sire_id

Sire identifier nested within breed.

sex

Sex of the animal, female (F) or male (M).

agew

Age at weighing.

Ww

Weaning weight.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

Examp1.3.2

Examples

data(ex32)

ex33 from Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

Description

Longitudinal packed cell volume data by animal, breed, and time.

Usage

data(ex33)

Format

A data.frame with 168 rows and 4 variables:

animal_id

Animal identifier.

breed

Breed group.

time

Time of packed cell volume measurement.

PCV

Packed cell volume response.

Author(s)

  1. Muhammad Yaseen (myaseen208@gmail.com)

References

  1. Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.

See Also

Examp1.3.2

Examples

data(ex33)

Report a Fitted Mixed Model

Description

Create an easystats-style narrative report for a fitted linear mixed model.

Usage

report_mixed_model(model, ...)

Arguments

model

A fitted model object, typically from lme4::lmer(), lmerTest::lmer(), or nlme::lme().

...

Additional arguments passed to report::report().

Details

This helper keeps the report package optional. It checks that a fitted model was supplied, verifies that report is installed, and then delegates the model interpretation to report::report(). This provides a stable package-level entry point for readers who want easystats-style interpretation of the fitted mixed models used throughout the book examples.

The helper does not change the fitted model, refit the model, or alter any estimates. It only formats and interprets the model object produced by the modelling package.

Value

A report object returned by report::report().

References

Duchateau, L., Janssen, P., and Rowlands, G. J. (1998). Linear Mixed Models: An Introduction with Applications in Veterinary Research. International Livestock Research Institute.

See utils::citation("report") for the citation for the optional easystats reporting package.

See Also

lme4::lmer, nlme::lme.

Examples

if (requireNamespace("lme4", quietly = TRUE) &&
    requireNamespace("report", quietly = TRUE)) {
  data(ex127, package = "VetResearchLMM")
  fit <- lme4::lmer(Ww ~ 1 + (1 | sire), data = ex127, REML = TRUE)
  report_mixed_model(fit)
}