Skip to content

Commit 68d10b1

Browse files
committed
mixed models
1 parent ac2682d commit 68d10b1

17 files changed

+259
-385
lines changed

R/1_model_parameters.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#' - [Correlations, t-tests, etc.][model_parameters.htest()]: **lmtest**, `htest`,
2020
#' `pairwise.htest`, ...
2121
#' - [Meta-Analysis][model_parameters.rma()]: **metaBMA**, **metafor**, **metaplus**, ...
22-
#' - [Mixed models][model_parameters.merMod()]: **cplm**, **glmmTMB**, **lme4**,
22+
#' - [Mixed models][model_parameters.glmmTMB()]: **cplm**, **glmmTMB**, **lme4**,
2323
#' **lmerTest**, **nlme**, **ordinal**, **robustlmm**, **spaMM**, `mixed`, `MixMod`, ...
2424
#' - [Multinomial, ordinal and cumulative link][model_parameters.mlm()]: **brglm2**,
2525
#' **DirichletReg**, **nnet**, **ordinal**, `mlm`, ...

R/compare_parameters.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#' used for all coefficient columns. If `NULL`, the name for the coefficient
2323
#' column will detected automatically (as in `model_parameters()`).
2424
#' @inheritParams model_parameters.default
25-
#' @inheritParams model_parameters.cpglmm
25+
#' @inheritParams model_parameters.glmmTMB
2626
#' @inheritParams print.parameters_model
2727
#'
2828
#' @details

R/equivalence_test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bayestestR::equivalence_test
2020
#' for details.
2121
#' @param verbose Toggle warnings and messages.
2222
#' @param ... Arguments passed to or from other methods.
23-
#' @inheritParams model_parameters.merMod
23+
#' @inheritParams model_parameters.glmmTMB
2424
#' @inheritParams p_value
2525
#'
2626
#' @seealso For more details, see [bayestestR::equivalence_test()]. Further

R/methods_cplm.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
#' model_parameters(model)
6464
#' }
6565
#' @return A data frame of indices related to the model's parameters.
66-
#' @inheritParams simulate_model
6766
#' @export
6867
model_parameters.zcpglm <- function(model,
6968
ci = 0.95,
@@ -255,7 +254,6 @@ standard_error.cpglm <- function(model, ...) {
255254
########## .cpglmm ---------------
256255

257256

258-
#' @rdname model_parameters.merMod
259257
#' @export
260258
model_parameters.cpglmm <- function(model,
261259
ci = 0.95,
@@ -275,7 +273,7 @@ model_parameters.cpglmm <- function(model,
275273
...) {
276274
# p-values, CI and se might be based on different df-methods
277275
ci_method <- .check_df_method(ci_method)
278-
effects <- match.arg(effects, choices = c("fixed", "random", "all"))
276+
effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
279277

280278
# standardize only works for fixed effects...
281279
if (!is.null(standardize) && standardize != "refit") {

R/methods_glmmTMB.R

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,159 @@
44
# model_parameters -----
55

66

7+
#' @title Parameters from Mixed Models
8+
#' @name model_parameters.glmmTMB
9+
#'
10+
#' @description Parameters from (linear) mixed models.
11+
#'
12+
#' @param model A mixed model.
13+
#' @param effects Should parameters for fixed effects (`"fixed"`), random
14+
#' effects (`"random"`), or both (`"all"`) be returned? Only applies
15+
#' to mixed models. May be abbreviated. If the calculation of random effects
16+
#' parameters takes too long, you may use `effects = "fixed"`.
17+
#' @param wb_component Logical, if `TRUE` and models contains within- and
18+
#' between-effects (see `datawizard::demean()`), the `Component` column
19+
#' will indicate which variables belong to the within-effects,
20+
#' between-effects, and cross-level interactions. By default, the
21+
#' `Component` column indicates, which parameters belong to the
22+
#' conditional or zero-inflation component of the model.
23+
#' @param include_sigma Logical, if `TRUE`, includes the residual standard
24+
#' deviation. For mixed models, this is defined as the sum of the distribution-specific
25+
#' variance and the variance for the additive overdispersion term (see
26+
#' [insight::get_variance()] for details). Defaults to `FALSE` for mixed models
27+
#' due to the longer computation time.
28+
#' @param ci_random Logical, if `TRUE`, includes the confidence intervals for
29+
#' random effects parameters. Only applies if `effects` is not `"fixed"` and
30+
#' if `ci` is not `NULL`. Set `ci_random = FALSE` if computation of the model
31+
#' summary is too much time consuming. By default, `ci_random = NULL`, which
32+
#' uses a heuristic to guess if computation of confidence intervals for random
33+
#' effects is fast enough or not. For models with larger sample size and/or
34+
#' more complex random effects structures, confidence intervals will not be
35+
#' computed by default, for simpler models or fewer observations, confidence
36+
#' intervals will be included. Set explicitly to `TRUE` or `FALSE` to enforce
37+
#' or omit calculation of confidence intervals.
38+
#' @param ... Arguments passed to or from other methods. For instance, when
39+
#' `bootstrap = TRUE`, arguments like `type` or `parallel` are passed down to
40+
#' `bootstrap_model()`.
41+
#'
42+
#' Further non-documented arguments are:
43+
#'
44+
#' - `digits`, `p_digits`, `ci_digits` and `footer_digits` to set the number of
45+
#' digits for the output. `groups` can be used to group coefficients. These
46+
#' arguments will be passed to the print-method, or can directly be used in
47+
#' `print()`, see documentation in [`print.parameters_model()`].
48+
#' - If `s_value = TRUE`, the p-value will be replaced by the S-value in the
49+
#' output (cf. _Rafi and Greenland 2020_).
50+
#' - `pd` adds an additional column with the _probability of direction_ (see
51+
#' [`bayestestR::p_direction()`] for details). Furthermore, see 'Examples' for
52+
#' this function.
53+
#' - For developers, whose interest mainly is to get a "tidy" data frame of
54+
#' model summaries, it is recommended to set `pretty_names = FALSE` to speed
55+
#' up computation of the summary table.
56+
#'
57+
#' @inheritParams model_parameters.default
58+
#' @inheritParams model_parameters.stanreg
759
#' @inheritParams simulate_model
8-
#' @rdname model_parameters.merMod
60+
#'
61+
#' @inheritSection model_parameters.zcpglm Model components
62+
#'
63+
#' @inheritSection model_parameters Confidence intervals and approximation of degrees of freedom
64+
#'
65+
#' @section Confidence intervals for random effects variances:
66+
#' For models of class `merMod` and `glmmTMB`, confidence intervals for random
67+
#' effect variances can be calculated.
68+
#'
69+
#' - For models of from package **lme4**, when `ci_method` is either `"profile"`
70+
#' or `"boot"`, and `effects` is either `"random"` or `"all"`, profiled resp.
71+
#' bootstrapped confidence intervals are computed for the random effects.
72+
#'
73+
#' - For all other options of `ci_method`, and only when the **merDeriv**
74+
#' package is installed, confidence intervals for random effects are based on
75+
#' normal-distribution approximation, using the delta-method to transform
76+
#' standard errors for constructing the intervals around the log-transformed
77+
#' SD parameters. These are than back-transformed, so that random effect
78+
#' variances, standard errors and confidence intervals are shown on the original
79+
#' scale. Due to the transformation, the intervals are asymmetrical, however,
80+
#' they are within the correct bounds (i.e. no negative interval for the SD,
81+
#' and the interval for the correlations is within the range from -1 to +1).
82+
#'
83+
#' - For models of class `glmmTMB`, confidence intervals for random effect
84+
#' variances always use a Wald t-distribution approximation.
85+
#'
86+
#' @section Singular fits (random effects variances near zero):
87+
#' If a model is "singular", this means that some dimensions of the
88+
#' variance-covariance matrix have been estimated as exactly zero. This
89+
#' often occurs for mixed models with complex random effects structures.
90+
#'
91+
#' There is no gold-standard about how to deal with singularity and which
92+
#' random-effects specification to choose. One way is to fully go Bayesian
93+
#' (with informative priors). Other proposals are listed in the documentation
94+
#' of [`performance::check_singularity()`]. However, since version 1.1.9, the
95+
#' **glmmTMB** package allows to use priors in a frequentist framework, too. One
96+
#' recommendation is to use a Gamma prior (_Chung et al. 2013_). The mean may
97+
#' vary from 1 to very large values (like `1e8`), and the shape parameter should
98+
#' be set to a value of 2.5. You can then `update()` your model with the specified
99+
#' prior. In **glmmTMB**, the code would look like this:
100+
#'
101+
#' ```
102+
#' # "model" is an object of class gmmmTMB
103+
#' prior <- data.frame(
104+
#' prior = "gamma(1, 2.5)", # mean can be 1, but even 1e8
105+
#' class = "ranef" # for random effects
106+
#' )
107+
#' model_with_priors <- update(model, priors = prior)
108+
#' ```
109+
#'
110+
#' Large values for the mean parameter of the Gamma prior have no large impact
111+
#' on the random effects variances in terms of a "bias". Thus, if `1` doesn't
112+
#' fix the singular fit, you can safely try larger values.
113+
#'
114+
#' @section Dispersion parameters in *glmmTMB*:
115+
#' For some models from package **glmmTMB**, both the dispersion parameter and
116+
#' the residual variance from the random effects parameters are shown. Usually,
117+
#' these are the same but presented on different scales, e.g.
118+
#'
119+
#' ```
120+
#' model <- glmmTMB(Sepal.Width ~ Petal.Length + (1|Species), data = iris)
121+
#' exp(fixef(model)$disp) # 0.09902987
122+
#' sigma(model)^2 # 0.09902987
123+
#' ```
124+
#'
125+
#' For models where the dispersion parameter and the residual variance are
126+
#' the same, only the residual variance is shown in the output.
127+
#'
128+
#' @seealso [insight::standardize_names()] to
129+
#' rename columns into a consistent, standardized naming scheme.
130+
#'
131+
#' @note If the calculation of random effects parameters takes too long, you may
132+
#' use `effects = "fixed"`. There is also a [`plot()`-method](https://easystats.github.io/see/articles/parameters.html)
133+
#' implemented in the [**see**-package](https://easystats.github.io/see/).
134+
#'
135+
#' @references
136+
#' Chung Y, Rabe-Hesketh S, Dorie V, Gelman A, and Liu J. 2013. "A Nondegenerate
137+
#' Penalized Likelihood Estimator for Variance Parameters in Multilevel Models."
138+
#' Psychometrika 78 (4): 685–709. \doi{10.1007/s11336-013-9328-2}
139+
#'
140+
#' @examplesIf require("lme4") && require("glmmTMB")
141+
#' library(parameters)
142+
#' data(mtcars)
143+
#' model <- lme4::lmer(mpg ~ wt + (1 | gear), data = mtcars)
144+
#' model_parameters(model)
145+
#'
146+
#' \donttest{
147+
#' data(Salamanders, package = "glmmTMB")
148+
#' model <- glmmTMB::glmmTMB(
149+
#' count ~ spp + mined + (1 | site),
150+
#' ziformula = ~mined,
151+
#' family = poisson(),
152+
#' data = Salamanders
153+
#' )
154+
#' model_parameters(model, effects = "all")
155+
#'
156+
#' model <- lme4::lmer(mpg ~ wt + (1 | gear), data = mtcars)
157+
#' model_parameters(model, bootstrap = TRUE, iterations = 50, verbose = FALSE)
158+
#' }
159+
#' @return A data frame of indices related to the model's parameters.
9160
#' @export
10161
model_parameters.glmmTMB <- function(model,
11162
ci = 0.95,

R/methods_lme4.R

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,6 @@
11
############# .merMod -----------------
22

33

4-
#' @title Parameters from Mixed Models
5-
#' @name model_parameters.merMod
6-
#'
7-
#' @description Parameters from (linear) mixed models.
8-
#'
9-
#' @param model A mixed model.
10-
#' @param effects Should parameters for fixed effects (`"fixed"`), random
11-
#' effects (`"random"`), or both (`"all"`) be returned? Only applies
12-
#' to mixed models. May be abbreviated. If the calculation of random effects
13-
#' parameters takes too long, you may use `effects = "fixed"`.
14-
#' @param wb_component Logical, if `TRUE` and models contains within- and
15-
#' between-effects (see `datawizard::demean()`), the `Component` column
16-
#' will indicate which variables belong to the within-effects,
17-
#' between-effects, and cross-level interactions. By default, the
18-
#' `Component` column indicates, which parameters belong to the
19-
#' conditional or zero-inflation component of the model.
20-
#' @param include_sigma Logical, if `TRUE`, includes the residual standard
21-
#' deviation. For mixed models, this is defined as the sum of the distribution-specific
22-
#' variance and the variance for the additive overdispersion term (see
23-
#' [insight::get_variance()] for details). Defaults to `FALSE` for mixed models
24-
#' due to the longer computation time.
25-
#' @param ci_random Logical, if `TRUE`, includes the confidence intervals for
26-
#' random effects parameters. Only applies if `effects` is not `"fixed"` and
27-
#' if `ci` is not `NULL`. Set `ci_random = FALSE` if computation of the model
28-
#' summary is too much time consuming. By default, `ci_random = NULL`, which
29-
#' uses a heuristic to guess if computation of confidence intervals for random
30-
#' effects is fast enough or not. For models with larger sample size and/or
31-
#' more complex random effects structures, confidence intervals will not be
32-
#' computed by default, for simpler models or fewer observations, confidence
33-
#' intervals will be included. Set explicitly to `TRUE` or `FALSE` to enforce
34-
#' or omit calculation of confidence intervals.
35-
#' @param ... Arguments passed to or from other methods. For instance, when
36-
#' `bootstrap = TRUE`, arguments like `type` or `parallel` are passed down to
37-
#' `bootstrap_model()`.
38-
#'
39-
#' Further non-documented arguments are:
40-
#'
41-
#' - `digits`, `p_digits`, `ci_digits` and `footer_digits` to set the number of
42-
#' digits for the output. `groups` can be used to group coefficients. These
43-
#' arguments will be passed to the print-method, or can directly be used in
44-
#' `print()`, see documentation in [`print.parameters_model()`].
45-
#' - If `s_value = TRUE`, the p-value will be replaced by the S-value in the
46-
#' output (cf. _Rafi and Greenland 2020_).
47-
#' - `pd` adds an additional column with the _probability of direction_ (see
48-
#' [`bayestestR::p_direction()`] for details). Furthermore, see 'Examples' for
49-
#' this function.
50-
#' - For developers, whose interest mainly is to get a "tidy" data frame of
51-
#' model summaries, it is recommended to set `pretty_names = FALSE` to speed
52-
#' up computation of the summary table.
53-
#'
54-
#' @inheritParams model_parameters.default
55-
#' @inheritParams model_parameters.stanreg
56-
#'
57-
#' @inheritSection model_parameters Confidence intervals and approximation of degrees of freedom
58-
#'
59-
#' @section Confidence intervals for random effects variances:
60-
#' For models of class `merMod` and `glmmTMB`, confidence intervals for random
61-
#' effect variances can be calculated.
62-
#'
63-
#' - For models of from package **lme4**, when `ci_method` is either `"profile"`
64-
#' or `"boot"`, and `effects` is either `"random"` or `"all"`, profiled resp.
65-
#' bootstrapped confidence intervals are computed for the random effects.
66-
#'
67-
#' - For all other options of `ci_method`, and only when the **merDeriv**
68-
#' package is installed, confidence intervals for random effects are based on
69-
#' normal-distribution approximation, using the delta-method to transform
70-
#' standard errors for constructing the intervals around the log-transformed
71-
#' SD parameters. These are than back-transformed, so that random effect
72-
#' variances, standard errors and confidence intervals are shown on the original
73-
#' scale. Due to the transformation, the intervals are asymmetrical, however,
74-
#' they are within the correct bounds (i.e. no negative interval for the SD,
75-
#' and the interval for the correlations is within the range from -1 to +1).
76-
#'
77-
#' - For models of class `glmmTMB`, confidence intervals for random effect
78-
#' variances always use a Wald t-distribution approximation.
79-
#'
80-
#' @section Singular fits (random effects variances near zero):
81-
#' If a model is "singular", this means that some dimensions of the
82-
#' variance-covariance matrix have been estimated as exactly zero. This
83-
#' often occurs for mixed models with complex random effects structures.
84-
#'
85-
#' There is no gold-standard about how to deal with singularity and which
86-
#' random-effects specification to choose. One way is to fully go Bayesian
87-
#' (with informative priors). Other proposals are listed in the documentation
88-
#' of [`performance::check_singularity()`]. However, since version 1.1.9, the
89-
#' **glmmTMB** package allows to use priors in a frequentist framework, too. One
90-
#' recommendation is to use a Gamma prior (_Chung et al. 2013_). The mean may
91-
#' vary from 1 to very large values (like `1e8`), and the shape parameter should
92-
#' be set to a value of 2.5. You can then `update()` your model with the specified
93-
#' prior. In **glmmTMB**, the code would look like this:
94-
#'
95-
#' ```
96-
#' # "model" is an object of class gmmmTMB
97-
#' prior <- data.frame(
98-
#' prior = "gamma(1, 2.5)", # mean can be 1, but even 1e8
99-
#' class = "ranef" # for random effects
100-
#' )
101-
#' model_with_priors <- update(model, priors = prior)
102-
#' ```
103-
#'
104-
#' Large values for the mean parameter of the Gamma prior have no large impact
105-
#' on the random effects variances in terms of a "bias". Thus, if `1` doesn't
106-
#' fix the singular fit, you can safely try larger values.
107-
#'
108-
#' @section Dispersion parameters in *glmmTMB*:
109-
#' For some models from package **glmmTMB**, both the dispersion parameter and
110-
#' the residual variance from the random effects parameters are shown. Usually,
111-
#' these are the same but presented on different scales, e.g.
112-
#'
113-
#' ```
114-
#' model <- glmmTMB(Sepal.Width ~ Petal.Length + (1|Species), data = iris)
115-
#' exp(fixef(model)$disp) # 0.09902987
116-
#' sigma(model)^2 # 0.09902987
117-
#' ```
118-
#'
119-
#' For models where the dispersion parameter and the residual variance are
120-
#' the same, only the residual variance is shown in the output.
121-
#'
122-
#' @seealso [insight::standardize_names()] to
123-
#' rename columns into a consistent, standardized naming scheme.
124-
#'
125-
#' @note If the calculation of random effects parameters takes too long, you may
126-
#' use `effects = "fixed"`. There is also a [`plot()`-method](https://easystats.github.io/see/articles/parameters.html)
127-
#' implemented in the [**see**-package](https://easystats.github.io/see/).
128-
#'
129-
#' @references
130-
#' Chung Y, Rabe-Hesketh S, Dorie V, Gelman A, and Liu J. 2013. "A Nondegenerate
131-
#' Penalized Likelihood Estimator for Variance Parameters in Multilevel Models."
132-
#' Psychometrika 78 (4): 685–709. \doi{10.1007/s11336-013-9328-2}
133-
#'
134-
#' @examplesIf require("lme4") && require("glmmTMB")
135-
#' library(parameters)
136-
#' data(mtcars)
137-
#' model <- lme4::lmer(mpg ~ wt + (1 | gear), data = mtcars)
138-
#' model_parameters(model)
139-
#'
140-
#' \donttest{
141-
#' data(Salamanders, package = "glmmTMB")
142-
#' model <- glmmTMB::glmmTMB(
143-
#' count ~ spp + mined + (1 | site),
144-
#' ziformula = ~mined,
145-
#' family = poisson(),
146-
#' data = Salamanders
147-
#' )
148-
#' model_parameters(model, effects = "all")
149-
#'
150-
#' model <- lme4::lmer(mpg ~ wt + (1 | gear), data = mtcars)
151-
#' model_parameters(model, bootstrap = TRUE, iterations = 50, verbose = FALSE)
152-
#' }
153-
#' @return A data frame of indices related to the model's parameters.
1544
#' @export
1555
model_parameters.merMod <- function(model,
1566
ci = 0.95,

R/methods_mixed.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
#' @rdname model_parameters.merMod
21
#' @export
32
model_parameters.mixed <- model_parameters.glmmTMB

R/methods_mixmod.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#' @rdname model_parameters.merMod
21
#' @export
32
model_parameters.MixMod <- model_parameters.glmmTMB
43

R/methods_nlme.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
############### .lme --------------
44

5-
#' @rdname model_parameters.merMod
65
#' @export
76
model_parameters.lme <- model_parameters.merMod
87

R/methods_ordinal.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ model_parameters.clm2 <- function(model,
5353
}
5454

5555

56-
#' @rdname model_parameters.merMod
5756
#' @export
5857
model_parameters.clmm2 <- model_parameters.clm2
5958

6059

61-
#' @rdname model_parameters.merMod
6260
#' @export
6361
model_parameters.clmm <- model_parameters.cpglmm
6462

0 commit comments

Comments
 (0)