Compute and extract model parameters of multiple regression
models. See model_parameters()
for further details.
compare_parameters(
...,
ci = 0.95,
effects = "fixed",
component = "conditional",
standardize = NULL,
exponentiate = FALSE,
ci_method = "wald",
p_adjust = NULL,
select = NULL,
column_names = NULL,
pretty_names = TRUE,
coefficient_names = NULL,
keep = NULL,
drop = NULL,
include_reference = FALSE,
groups = NULL,
verbose = TRUE
)compare_models(
...,
ci = 0.95,
effects = "fixed",
component = "conditional",
standardize = NULL,
exponentiate = FALSE,
ci_method = "wald",
p_adjust = NULL,
select = NULL,
column_names = NULL,
pretty_names = TRUE,
coefficient_names = NULL,
keep = NULL,
drop = NULL,
include_reference = FALSE,
groups = NULL,
verbose = TRUE
)
A data frame of indices related to the model's parameters.
One or more regression model objects, or objects returned by
model_parameters()
. Regression models may be of different model
types. Model objects may be passed comma separated, or as a list.
If model objects are passed with names or the list has named elements,
these names will be used as column names.
Confidence Interval (CI) level. Default to 0.95
(95%
).
Should parameters for fixed effects ("fixed"
), random
effects ("random"
), or both ("all"
) be returned? Only applies
to mixed models. May be abbreviated. If the calculation of random effects
parameters takes too long, you may use effects = "fixed"
.
Model component for which parameters should be shown. See
documentation for related model class in model_parameters()
.
The method used for standardizing the parameters. Can be
NULL
(default; no standardization), "refit"
(for re-fitting the model
on standardized data) or one of "basic"
, "posthoc"
, "smart"
,
"pseudo"
. See 'Details' in standardize_parameters()
.
Importantly:
The "refit"
method does not standardize categorical predictors (i.e.
factors), which may be a different behaviour compared to other R packages
(such as lm.beta) or other software packages (like SPSS). to mimic
such behaviours, either use standardize="basic"
or standardize the data
with datawizard::standardize(force=TRUE)
before fitting the model.
For mixed models, when using methods other than "refit"
, only the fixed
effects will be standardized.
Robust estimation (i.e., vcov
set to a value other than NULL
) of
standardized parameters only works when standardize="refit"
.
Logical, indicating whether or not to exponentiate the
coefficients (and related confidence intervals). This is typical for
logistic regression, or more generally speaking, for models with log or
logit links. It is also recommended to use exponentiate = TRUE
for models
with log-transformed response values. Note: Delta-method standard
errors are also computed (by multiplying the standard errors by the
transformed coefficients). This is to mimic behaviour of other software
packages, such as Stata, but these standard errors poorly estimate
uncertainty for the transformed coefficient. The transformed confidence
interval more clearly captures this uncertainty. For compare_parameters()
,
exponentiate = "nongaussian"
will only exponentiate coefficients from
non-Gaussian families.
Method for computing degrees of freedom for p-values
and confidence intervals (CI). See documentation for related model class
in model_parameters()
.
Character vector, if not NULL
, indicates the method to
adjust p-values. See stats::p.adjust()
for details. Further
possible adjustment methods are "tukey"
, "scheffe"
,
"sidak"
and "none"
to explicitly disable adjustment for
emmGrid
objects (from emmeans).
Determines which columns and and which layout columns are printed. There are three options for this argument:
Selecting columns by name or index
select
can be a character vector (or numeric index) of column names that
should be printed. There are two pre-defined options for selecting columns:
select = "minimal"
prints coefficients, confidence intervals and p-values,
while select = "short"
prints coefficients, standard errors and p-values.
A string expression with layout pattern
select
is a string with "tokens" enclosed in braces. These tokens will
be replaced by their associated columns, where the selected columns will
be collapsed into one column. However, it is possible to create multiple
columns as well. Following tokens are replaced by the related coefficients
or statistics: {estimate}
, {se}
, {ci}
(or {ci_low}
and {ci_high}
),
{p}
and {stars}
. The token {ci}
will be replaced by {ci_low}, {ci_high}
.
Furthermore, a |
separates values into new cells/columns. If
format = "html"
, a <br>
inserts a line break inside a cell. See
'Examples'.
A string indicating a pre-defined layout
select
can be one of the following string values, to create one of the
following pre-defined column layouts:
"ci"
: Estimates and confidence intervals, no asterisks for p-values.
This is equivalent to select = "{estimate} ({ci})"
.
"se"
: Estimates and standard errors, no asterisks for p-values. This is
equivalent to select = "{estimate} ({se})"
.
"ci_p"
: Estimates, confidence intervals and asterisks for p-values. This
is equivalent to select = "{estimate}{stars} ({ci})"
.
"se_p"
: Estimates, standard errors and asterisks for p-values. This is
equivalent to select = "{estimate}{stars} ({se})"
..
"ci_p2"
: Estimates, confidence intervals and numeric p-values, in two
columns. This is equivalent to select = "{estimate} ({ci})|{p}"
.
"se_p2"
: Estimate, standard errors and numeric p-values, in two columns.
This is equivalent to select = "{estimate} ({se})|{p}"
.
For model_parameters()
, glue-like syntax is still experimental in the
case of more complex models (like mixed models) and may not return expected
results.
Character vector with strings that should be used as
column headers. Must be of same length as number of models in ...
.
Can be TRUE
, which will return "pretty" (i.e. more human
readable) parameter names. Or "labels"
, in which case value and variable
labels will be used as parameters names. The latter only works for "labelled"
data, i.e. if the data used to fit the model had "label"
and "labels"
attributes. See also section Global Options to Customize Messages when Printing.
Character vector with strings that should be used
as column headers for the coefficient column. Must be of same length as
number of models in ...
, or length 1. If length 1, this name will be
used for all coefficient columns. If NULL
, the name for the coefficient
column will detected automatically (as in model_parameters()
).
Character containing a regular expression pattern that
describes the parameters that should be included (for keep
) or excluded
(for drop
) in the returned data frame. keep
may also be a
named list of regular expressions. All non-matching parameters will be
removed from the output. If keep
is a character vector, every parameter
name in the "Parameter" column that matches the regular expression in
keep
will be selected from the returned data frame (and vice versa,
all parameter names matching drop
will be excluded). Furthermore, if
keep
has more than one element, these will be merged with an OR
operator into a regular expression pattern like this: "(one|two|three)"
.
If keep
is a named list of regular expression patterns, the names of the
list-element should equal the column name where selection should be
applied. This is useful for model objects where model_parameters()
returns multiple columns with parameter components, like in
model_parameters.lavaan()
. Note that the regular expression pattern
should match the parameter names as they are stored in the returned data
frame, which can be different from how they are printed. Inspect the
$Parameter
column of the parameters table to get the exact parameter
names.
See keep
.
Logical, if TRUE
, the reference level of factors will
be added to the parameters table. This is only relevant for models with
categorical predictors. The coefficient for the reference level is always
0
(except when exponentiate = TRUE
, then the coefficient will be 1
),
so this is just for completeness.
Named list, can be used to group parameters in the printed output.
List elements may either be character vectors that match the name of those
parameters that belong to one group, or list elements can be row numbers
of those parameter rows that should belong to one group. The names of the
list elements will be used as group names, which will be inserted as "header
row". A possible use case might be to emphasize focal predictors and control
variables, see 'Examples'. Parameters will be re-ordered according to the
order used in groups
, while all non-matching parameters will be added
to the end.
Toggle warnings and messages.
This function is in an early stage and does not yet cope with more complex models, and probably does not yet properly render all model components. It should also be noted that when including models with interaction terms, not only do the values of the parameters change, but so does their meaning (from main effects, to simple slopes), thereby making such comparisons hard. Therefore, you should not use this function to compare models with interaction terms with models without interaction terms.
if (FALSE) { # require("gt", quietly = TRUE)
data(iris)
lm1 <- lm(Sepal.Length ~ Species, data = iris)
lm2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)
compare_parameters(lm1, lm2)
# custom style
compare_parameters(lm1, lm2, select = "{estimate}{stars} ({se})")
# \donttest{
# custom style, in HTML
result <- compare_parameters(lm1, lm2, select = "{estimate}({se})|{p}")
print_html(result)
# }
data(mtcars)
m1 <- lm(mpg ~ wt, data = mtcars)
m2 <- glm(vs ~ wt + cyl, data = mtcars, family = "binomial")
compare_parameters(m1, m2)
# \donttest{
# exponentiate coefficients, but not for lm
compare_parameters(m1, m2, exponentiate = "nongaussian")
# change column names
compare_parameters("linear model" = m1, "logistic reg." = m2)
compare_parameters(m1, m2, column_names = c("linear model", "logistic reg."))
# or as list
compare_parameters(list(m1, m2))
compare_parameters(list("linear model" = m1, "logistic reg." = m2))
# }
}
Run the code above in your browser using DataLab