| Title: | Linear Mixed Models - An Introduction with Applications in Veterinary Research |
|---|---|
| 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. |
| Authors: | Muhammad Yaseen [aut, cre], Luc Duchateau [ctb], Paul Janssen [ctb], John Rowlands [ctb] |
| Maintainer: | Muhammad Yaseen <[email protected]> |
| License: | GPL-2 |
| Version: | 1.1.0 |
| Built: | 2026-06-04 10:11:32 UTC |
| Source: | https://github.com/myaseen208/vetresearchlmm |
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.
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:
ex121Dose comparison data for packed cell volume.
ex124Herd, drug, and dose packed cell volume data.
ex125Region, drug, and dose split-plot data.
ex127Sire-level weaning weight data.
ex31Designed experiment data for PCV response.
ex32Breed, sire, sex, age, and weaning weight data.
ex33Longitudinal 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:
Load one of the included datasets with data().
Inspect the matching example help page, such as
?Examp2.4.2.2.
Fit the fixed effect or mixed model shown in the example.
Compare estimates, variance components, and tests with the book.
Use report_mixed_model() for an optional narrative
model report when report is installed.
Use emmeans_mixed_model() for optional estimated
marginal means and post hoc comparisons when emmeans is installed.
Use the package vignettes for chapter-level narrative examples.
School of Mathematical and Statistical Sciences, Clemson University, Clemson, South Carolina, USA.
The Quarto vignettes provide an introduction, methodology overview, chapter/example walkthrough, and plotting guide:
vetresearchlmm-introduction
vetresearchlmm-methodology
vetresearchlmm-examples
vetresearchlmm-plotting
Muhammad Yaseen [email protected]
Duchateau, L., Janssen, P., and Rowlands, G. J. (1998). Linear Mixed Models: An Introduction with Applications in Veterinary Research. International Livestock Research Institute.
Useful links:
Report bugs at https://github.com/myaseen208/VetResearchLMM/issues
Compute estimated marginal means or pairwise comparisons for a fitted linear mixed model using the optional emmeans package.
emmeans_mixed_model( model, specs, pairwise = FALSE, method = "pairwise", adjust = "tukey", ... )emmeans_mixed_model( model, specs, pairwise = FALSE, method = "pairwise", adjust = "tukey", ... )
model |
A fitted model object, typically from |
specs |
Specifications for the marginal means, passed to
|
pairwise |
Logical. If |
method |
Contrast method passed to |
adjust |
Multiplicity adjustment passed to |
... |
Additional arguments passed to |
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().
An emmGrid object from emmeans. With pairwise = FALSE,
this contains estimated marginal means. With pairwise = TRUE, this
contains the requested contrasts.
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.
report_mixed_model(), emmeans::emmeans(),
emmeans::contrast().
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" ) }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" ) }
Packed cell volume data for the dose comparison in Example 1.2.1.
data(ex121)data(ex121)
A data.frame with 14 rows and 5 variables:
Animal identifier within dose group.
Dose group with levels H, L, and M.
Packed cell volume measured at treatment.
Packed cell volume measured after treatment.
Difference between post-treatment and baseline packed cell volume.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex121)data(ex121)
Packed cell volume response data by herd, drug, and dose.
data(ex124)data(ex124)
A data.frame with 40 rows and 4 variables:
Herd identifier.
Drug administered, Berenil or Samorin.
Dose group, high (h) or low (l).
Change in packed cell volume after treatment.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex124)data(ex124)
Packed cell volume response data by region, drug, and dose.
data(ex125)data(ex125)
A data.frame with 24 rows and 4 variables:
Region identifier.
Drug administered, Berenil or Samorin.
Dose group, high (h) or low (l).
Packed cell volume response.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex125)data(ex125)
Weaning weight observations grouped by sire.
data(ex127)data(ex127)
A data.frame with 43 rows and 2 variables:
Sire identifier.
Weaning weight.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex127)data(ex127)
Packed cell volume data for two trypanosomosis drugs.
data(ex31)data(ex31)
A data.frame with 38 rows and 6 variables:
Herd identifier.
Animal identifier.
Packed cell volume measured at treatment.
Packed cell volume measured one month after treatment.
Dose level.
Drug administered, Berenil or Samorin.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex31)data(ex31)
Weaning weight data by breed, sire, sex, and age.
data(ex32)data(ex32)
A data.frame with 65 rows and 5 variables:
Breed identifier.
Sire identifier nested within breed.
Sex of the animal, female (F) or male (M).
Age at weighing.
Weaning weight.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex32)data(ex32)
Longitudinal packed cell volume data by animal, breed, and time.
data(ex33)data(ex33)
A data.frame with 168 rows and 4 variables:
Animal identifier.
Breed group.
Time of packed cell volume measurement.
Packed cell volume response.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
data(ex33)data(ex33)
Examp1.3.2 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) str(ex124) summary(ex124) ex124 <- ex124 |> collapse::fmutate( herd1 = factor(herd), drug1 = factor(drug), dose1 = factor(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)#------------------------------------------------------------- ## 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; library(lme4) str(ex124) summary(ex124) ex124 <- ex124 |> collapse::fmutate( herd1 = factor(herd), drug1 = factor(drug), dose1 = factor(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 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) 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))) library(multcomp) 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 # )#------------------------------------------------------------- ## 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; library(lme4) 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))) library(multcomp) 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 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) str(ex125) fm2.4 <- lme4::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = FALSE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = NULL , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm2.5 |> report::report() } summary(fm2.5) anova(fm2.5) library(lmerTest) fm2.6 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = FALSE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = NULL , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm2.7 |> report::report() } summary(fm2.7) anova(fm2.7)#------------------------------------------------------------- ## 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; library(lme4) str(ex125) fm2.4 <- lme4::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = FALSE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = NULL , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm2.5 |> report::report() } summary(fm2.5) anova(fm2.5) library(lmerTest) fm2.6 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = FALSE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = NULL , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm2.7 |> report::report() } summary(fm2.7) anova(fm2.7)
Examp2.4.3.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## Example 2.4.3.1 p-66 #------------------------------------------------------------- # PROC MIXED DATA=ex127; # CLASS sire; # MODEL ww=; # RANDOM sire/solution; # RUN; library(lme4) str(ex127) fm2.8 <- lme4::lmer( formula = Ww~(1|sire) , data = ex127 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = NULL , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm2.8 |> report::report() } summary(fm2.8) lme4::fixef(fm2.8) lme4::ranef(fm2.8)#------------------------------------------------------------- ## Example 2.4.3.1 p-66 #------------------------------------------------------------- # PROC MIXED DATA=ex127; # CLASS sire; # MODEL ww=; # RANDOM sire/solution; # RUN; library(lme4) str(ex127) fm2.8 <- lme4::lmer( formula = Ww~(1|sire) , data = ex127 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) str(ex125) fm2.9 <- lme4::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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#------------------------------------------------------------- ## 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; library(lme4) str(ex125) fm2.9 <- lme4::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lmerTest) str(ex125) fm2.10 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 lsmeansLT(model = fm2.10)#------------------------------------------------------------- ## 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; library(lmerTest) str(ex125) fm2.10 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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 lsmeansLT(model = fm2.10)
Examp2.5.3.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) str(ex125) ex125 <- ex125 |> collapse::fmutate(Region1 = factor(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) fm2.12 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") library(multcomp) 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(glht(fm2.12, linfct=Contrasts1))#------------------------------------------------------------- ## 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; library(lme4) str(ex125) ex125 <- ex125 |> collapse::fmutate(Region1 = factor(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) fm2.12 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") library(multcomp) 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(glht(fm2.12, linfct=Contrasts1))
Examp2.5.4.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) str(ex125) ex125 <- ex125 |> collapse::fmutate(Region1 = factor(Region)) fm2.13 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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) library(multcomp) Contrasts2 <- matrix(c( 1, 0.5, 0, 0 ) , ncol = 4 , byrow = TRUE , dimnames = list( c("C5") , rownames(summary(fm2.13)$coef) ) ) Contrasts2 summary(glht(fm2.13, linfct=Contrasts2))#------------------------------------------------------------- ## 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; library(lme4) str(ex125) ex125 <- ex125 |> collapse::fmutate(Region1 = factor(Region)) fm2.13 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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) library(multcomp) Contrasts2 <- matrix(c( 1, 0.5, 0, 0 ) , ncol = 4 , byrow = TRUE , dimnames = list( c("C5") , rownames(summary(fm2.13)$coef) ) ) Contrasts2 summary(glht(fm2.13, linfct=Contrasts2))
Examp2.6.1 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lmerTest) str(ex125) ex125 <- ex125 |> collapse::fmutate(Region1 = factor(Region)) fm2.14 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") library(multcomp) Contrasts3 <- matrix(c( 0, 0, -1, -0.5 ) , ncol = 4 , byrow = TRUE , dimnames = list( c("C1") , rownames(summary(fm2.14)$coef) ) ) Contrasts3 summary(glht(fm2.14, linfct=Contrasts3)) if(packageVersion("lmerTest") >= "3.0") contest(fm2.14, Contrasts3, joint = FALSE)#------------------------------------------------------------- ## 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; library(lmerTest) str(ex125) ex125 <- ex125 |> collapse::fmutate(Region1 = factor(Region)) fm2.14 <- lmerTest::lmer( formula = Pcv ~ dose*Drug + (1|Region/Drug) , data = ex125 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") library(multcomp) Contrasts3 <- matrix(c( 0, 0, -1, -0.5 ) , ncol = 4 , byrow = TRUE , dimnames = list( c("C1") , rownames(summary(fm2.14)$coef) ) ) Contrasts3 summary(glht(fm2.14, linfct=Contrasts3)) if(packageVersion("lmerTest") >= "3.0") contest(fm2.14, Contrasts3, joint = FALSE)
Examp3.1 is.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lmerTest) str(ex31) ex31 <- ex31 |> collapse::fmutate( herd1 = factor(herd), drug1 = factor(drug), dose1 = factor(dose), ber = as.integer(drug == "BERENIL"), ber1 = as.integer(drug == "BERENIL" & dose == 1L), pcv_ber1 = PCV1 * as.integer(drug == "BERENIL" & dose == 1L), pcv_ber2 = PCV1 * as.integer(drug == "BERENIL" & dose == 2L) ) fm3.1 <- lmerTest::lmer( formula = PCV2 ~ drug1 + dose1:drug1 + (1|herd1:drug1) , data = ex31 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.1, test.effs = "dose1:drug1") #------------------------------------------------------------- ## 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.2, test.effs = "herd1:drug1") #------------------------------------------------------------- ## 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.3, test.effs = "dose1:drug1")#------------------------------------------------------------- ## 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; library(lmerTest) str(ex31) ex31 <- ex31 |> collapse::fmutate( herd1 = factor(herd), drug1 = factor(drug), dose1 = factor(dose), ber = as.integer(drug == "BERENIL"), ber1 = as.integer(drug == "BERENIL" & dose == 1L), pcv_ber1 = PCV1 * as.integer(drug == "BERENIL" & dose == 1L), pcv_ber2 = PCV1 * as.integer(drug == "BERENIL" & dose == 2L) ) fm3.1 <- lmerTest::lmer( formula = PCV2 ~ drug1 + dose1:drug1 + (1|herd1:drug1) , data = ex31 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.1, test.effs = "dose1:drug1") #------------------------------------------------------------- ## 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.2, test.effs = "herd1:drug1") #------------------------------------------------------------- ## 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 = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.3, test.effs = "dose1:drug1")
Examp3.2 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lmerTest) str(ex32) ex32 <- ex32 |> collapse::fmutate( sire_id1 = factor(sire_id), breed1 = factor(breed) ) fm3.4 <- lmerTest::lmer( formula = Ww ~ sex + agew + breed1 + (1|sire_id1:breed1) , data = ex32 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.4)#------------------------------------------------------------- ## 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; library(lmerTest) str(ex32) ex32 <- ex32 |> collapse::fmutate( sire_id1 = factor(sire_id), breed1 = factor(breed) ) fm3.4 <- lmerTest::lmer( formula = Ww ~ sex + agew + breed1 + (1|sire_id1:breed1) , data = ex32 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") lsmeansLT(model = fm3.4)
Examp3.3 is used for inspecting probability distribution and to define a plausible process through linear models and generalized linear models.
Muhammad Yaseen ([email protected])
Duchateau, L. and Janssen, P. and Rowlands, G. J. (1998).Linear Mixed Models. An Introduction with applications in Veterinary Research. International Livestock Research Institute.
#------------------------------------------------------------- ## 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; library(lme4) options(contrasts = c(factor = "contr.SAS", ordered = "contr.poly")) str(ex33) fm3.5 <- lme4::lmer( formula = PCV ~ breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = list(breed = "contr.SAS") , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm3.5 |> report::report() } summary(fm3.5) anova(fm3.5) library(lmerTest) fm3.6 <- lmerTest::lmer( formula = PCV ~ breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = breed breed*time/SOLUTION; # REPEATED/TYPE=CS SUB = animal_id(breed) R; # RUN; library(nlme) fm3.7 <- nlme::gls( model = PCV ~ breed + breed:time , data = ex33 , correlation = corCompSymm(, form = ~ 1|animal_id/breed) , weights = NULL # , subset = , method = "REML" # c("REML", "ML") , na.action = na.fail , control = list() ) if (requireNamespace("report", quietly = TRUE)) { try(report::report(fm3.7), silent = TRUE) } summary(fm3.7) anova(fm3.7) # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = time breed breed*time/SOLUTION; # RANDOM animal_id(breed)/SOLUTION; # RUN; fm3.8 <- lme4::lmer( formula = PCV ~ time + breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = list(breed = "contr.SAS") , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm3.8 |> report::report() } summary(fm3.8) anova(fm3.8) fm3.9 <- lmerTest::lmer( formula = PCV ~ time + breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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) # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = breed breed*time/SOLUTION; # REPEATED/TYPE=AR(1) SUBJET = animal_id(breed) R; # RUN; library(nlme) fm3.10 <- nlme::gls( model = PCV ~ breed + breed:time , data = ex33 , correlation = corAR1(, form = ~ 1|animal_id/breed) , weights = NULL # , subset = , method = "REML" # c("REML", "ML") , na.action = na.fail , control = list() ) if (requireNamespace("report", quietly = TRUE)) { try(report::report(fm3.10), silent = TRUE) } summary(fm3.10) anova(fm3.10) # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = breed breed*time/SOLUTION; # RANDOM INTERCEPT time/TYPE=UN SUBJET = animal_id(breed) SOLUTION; # RUN; library(nlme) # fm3.11 <- # nlme::gls( # model = PCV ~ breed + breed:time # , data = ex33 # , random = ~1|animal_id/breed # , correlation = corAR1(, form = ~ 1|animal_id/breed) # , weights = NULL # # , subset = # , method = "REML" # c("REML", "ML") # , na.action = na.fail # , control = list() # ) # summary(fm3.11) # anova(fm3.11)#------------------------------------------------------------- ## 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; library(lme4) options(contrasts = c(factor = "contr.SAS", ordered = "contr.poly")) str(ex33) fm3.5 <- lme4::lmer( formula = PCV ~ breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = list(breed = "contr.SAS") , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm3.5 |> report::report() } summary(fm3.5) anova(fm3.5) library(lmerTest) fm3.6 <- lmerTest::lmer( formula = PCV ~ breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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") # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = breed breed*time/SOLUTION; # REPEATED/TYPE=CS SUB = animal_id(breed) R; # RUN; library(nlme) fm3.7 <- nlme::gls( model = PCV ~ breed + breed:time , data = ex33 , correlation = corCompSymm(, form = ~ 1|animal_id/breed) , weights = NULL # , subset = , method = "REML" # c("REML", "ML") , na.action = na.fail , control = list() ) if (requireNamespace("report", quietly = TRUE)) { try(report::report(fm3.7), silent = TRUE) } summary(fm3.7) anova(fm3.7) # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = time breed breed*time/SOLUTION; # RANDOM animal_id(breed)/SOLUTION; # RUN; fm3.8 <- lme4::lmer( formula = PCV ~ time + breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , contrasts = list(breed = "contr.SAS") , devFunOnly = FALSE # , ... ) if (requireNamespace("report", quietly = TRUE)) { fm3.8 |> report::report() } summary(fm3.8) anova(fm3.8) fm3.9 <- lmerTest::lmer( formula = PCV ~ time + breed + breed:time + (1|animal_id:breed) , data = ex33 , REML = TRUE , control = lmerControl() , start = NULL , verbose = 0L # , subset # , weights # , na.action # , offset , 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) # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = breed breed*time/SOLUTION; # REPEATED/TYPE=AR(1) SUBJET = animal_id(breed) R; # RUN; library(nlme) fm3.10 <- nlme::gls( model = PCV ~ breed + breed:time , data = ex33 , correlation = corAR1(, form = ~ 1|animal_id/breed) , weights = NULL # , subset = , method = "REML" # c("REML", "ML") , na.action = na.fail , control = list() ) if (requireNamespace("report", quietly = TRUE)) { try(report::report(fm3.10), silent = TRUE) } summary(fm3.10) anova(fm3.10) # PROC MIXED DATA=ex33; # CLASS breed animal_id; # MODEL pcv = breed breed*time/SOLUTION; # RANDOM INTERCEPT time/TYPE=UN SUBJET = animal_id(breed) SOLUTION; # RUN; library(nlme) # fm3.11 <- # nlme::gls( # model = PCV ~ breed + breed:time # , data = ex33 # , random = ~1|animal_id/breed # , correlation = corAR1(, form = ~ 1|animal_id/breed) # , weights = NULL # # , subset = # , method = "REML" # c("REML", "ML") # , na.action = na.fail # , control = list() # ) # summary(fm3.11) # anova(fm3.11)
Create an easystats-style narrative report for a fitted linear mixed model.
report_mixed_model(model, ...)report_mixed_model(model, ...)
model |
A fitted model object, typically from |
... |
Additional arguments passed to |
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.
A report object returned by report::report().
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.
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) }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) }