Title: | Learning and Communicating Linear Mixed Models Without Data |
---|---|
Description: | Summarizes characteristics of linear mixed effects models without data or a fitted model by converting code for fitting lmer() from 'lme4' and lme() from 'nlme' into tables, equations, and visuals. Outputs can be used to learn how to fit linear mixed effects models in 'R' and to communicate about these models in presentations, manuscripts, and analysis plans. |
Authors: | Katherine Zavez [aut, cre], Ofer Harel [aut] |
Maintainer: | Katherine Zavez <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.0.0 |
Built: | 2025-03-08 05:02:11 UTC |
Source: | https://github.com/kzavez/learnvizlmm |
extract_equation()
takes the nlme::lme()
or lme4::lmer()
code for
fitting a linear mixed effect model and returns the corresponding model
equation written in 'LaTeX' notation.
extract_equation( model, cat_vars = NULL, cat_vars_nlevels = NULL, output_type = "latex" )
extract_equation( model, cat_vars = NULL, cat_vars_nlevels = NULL, output_type = "latex" )
model |
Code for fitting a |
cat_vars |
Optional character vector of the names of categorical
predictor variables included in the |
cat_vars_nlevels |
Optional numeric vector of the number of levels (i.e.
categories) for each variable in |
output_type |
Output type can be |
None (invisible NULL) (output_type = "latex"
), a string
(output_type = "string"
), or no output (output_type = "none"
).
# Different ways to write the same lme model extract_equation(model = "lme(score ~ age, random=~age|subject)") extract_equation(model = "lme(score ~ age, random=list(subject=~age))") # Correlated vs. Uncorrelated extract_equation(model = "lmer(score ~ age + (age|subject))") extract_equation(model = "lmer(score ~ age + (age||subject))") # Add a categorical predictor and interaction extract_equation(model = "lmer(score ~ age*treat + (age|subject))", cat_vars = "treat", cat_vars_nlevels = 3)
# Different ways to write the same lme model extract_equation(model = "lme(score ~ age, random=~age|subject)") extract_equation(model = "lme(score ~ age, random=list(subject=~age))") # Correlated vs. Uncorrelated extract_equation(model = "lmer(score ~ age + (age|subject))") extract_equation(model = "lmer(score ~ age + (age||subject))") # Add a categorical predictor and interaction extract_equation(model = "lmer(score ~ age*treat + (age|subject))", cat_vars = "treat", cat_vars_nlevels = 3)
extract_structure
generates an image of the multilevel data structure. It
does this in two steps. First, characteristics of the group(s) or grouping
factor(s) are identified via the model
input or the n_gf
,
gf_description
, and gf_names
inputs. Second, this information is used to
run DiagrammeR::grViz()
, which returns an image.
extract_structure( model = NULL, n_gf = NULL, gf_description = NULL, gf_names = NULL, gf_nlevels = NULL, gf3_index = "i", label_levels = "yes", export_type = "print" )
extract_structure( model = NULL, n_gf = NULL, gf_description = NULL, gf_names = NULL, gf_nlevels = NULL, gf3_index = "i", label_levels = "yes", export_type = "print" )
model |
Code for fitting a |
n_gf |
Number of groups or grouping factors: |
gf_description |
Description of the structure of the groups or grouping
factors: |
gf_names |
Character vector of the names of group(s) or grouping
factor(s). For nested, order names by level from highest to lowest. Must be
a vector of length equal to |
gf_nlevels |
Optional numeric or character vector of the number of
levels for each group or grouping factor in the |
gf3_index |
String for the index of the highest-level group or grouping
factor. Only applies if |
label_levels |
Indicates whether levels of the data structure should be
labeled on the left-hand side of the figure (default) or not
( |
export_type |
Export type can be |
A PNG (export_type = "png"
), character
(export_type = "text"
), or object of class htmlwidget that will print
in the R console, within R Markdown documents, and within Shiny output
bindings (export_type = "print"
).
# Using the model input extract_structure(model = "lme(Score ~ type, random=list(School=pdDiag(~1+type),Class=~1))") extract_structure(model = "lme(Weight ~ Time, random=~Time|Subject, data)", gf_nlevels = 47) extract_structure(model = "lmer(Strength ~ 1 + (1|Machine) + (1|Worker))", gf_nlevels = c("23", "J")) # Using the n_gf, gf_description, and gf_names inputs extract_structure(n_gf = 1, gf_names = "Subject") extract_structure(n_gf = 3, gf_description = "nested", gf_names = c("District", "School", "Class"), gf_nlevels = c(8, 15, 5), label_levels = "no")
# Using the model input extract_structure(model = "lme(Score ~ type, random=list(School=pdDiag(~1+type),Class=~1))") extract_structure(model = "lme(Weight ~ Time, random=~Time|Subject, data)", gf_nlevels = 47) extract_structure(model = "lmer(Strength ~ 1 + (1|Machine) + (1|Worker))", gf_nlevels = c("23", "J")) # Using the n_gf, gf_description, and gf_names inputs extract_structure(n_gf = 1, gf_names = "Subject") extract_structure(n_gf = 3, gf_description = "nested", gf_names = c("District", "School", "Class"), gf_nlevels = c(8, 15, 5), label_levels = "no")
extract_variables()
returns a data frame of information of the variables in
a nlme::lme()
or lme4::lmer()
model. The columns of the data frame
include: Effect
(whether the effect is random or fixed), Group
(group or
grouping factor associated with random effects), Term
(notation used to
include the variable in the model), Description
(description of the Term
),
and Parameter
(parameter estimated when the model is fit).
extract_variables(model, cat_vars = NULL, cat_vars_nlevels = NULL)
extract_variables(model, cat_vars = NULL, cat_vars_nlevels = NULL)
model |
Code for fitting a |
cat_vars |
Optional character vector of the names of categorical
predictor variables included in the |
cat_vars_nlevels |
Optional numeric vector of the number of levels (i.e.
categories) for each variable in |
A data frame.
# lme() extract_variables(model = "lme(Score~type,random=list(School=pdDiag(~1+type),Class=~1))", cat_vars = "type", cat_vars_nlevels = 2) extract_variables(model = "lme(weight~1+Time+I(Time^2),random=~Time+I(Time^2)|ID)") # lmer() extract_variables(model = "lmer(Strength ~ 1 + (1|Machine) + (1|Worker))") extract_variables(model = "lmer(score ~ age*treat + (age|subject))", cat_vars = "treat", cat_vars_nlevels = 3)
# lme() extract_variables(model = "lme(Score~type,random=list(School=pdDiag(~1+type),Class=~1))", cat_vars = "type", cat_vars_nlevels = 2) extract_variables(model = "lme(weight~1+Time+I(Time^2),random=~Time+I(Time^2)|ID)") # lmer() extract_variables(model = "lmer(Strength ~ 1 + (1|Machine) + (1|Worker))") extract_variables(model = "lmer(score ~ age*treat + (age|subject))", cat_vars = "treat", cat_vars_nlevels = 3)