Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exponentiation of clmm coefficients in the ordinal package #954

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.21.5.5
Version: 0.21.5.6
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* Fixed issue with parameter names for `model_parameters()` and objects from
package *epiR*.

* Fixed issue with `exponentiate = TRUE` for `model_parameters()` with models
of class `clmm` (package *ordinal*), when model had no `component` column
(e.g., no scale or location parameters were returned).

# parameters 0.21.5

## Bug fixes
Expand Down
30 changes: 21 additions & 9 deletions R/p_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#' (i.e. assumptions are taken as given). The unconditional interpretation (B),
#' however, questions all these assumptions.
#'
#' \if{html}{\cr \figure{unconditional_interpretation.png}{options: alt="Conditional versus unconditional interpretations of P-values"} \cr}

Check warning on line 61 in R/p_function.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/p_function.R,line=61,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 140 characters.
#'
#' "Emphasizing unconditional interpretations helps avoid overconfident and
#' misleading inferences in light of uncertainties about the assumptions used
Expand Down Expand Up @@ -92,6 +92,17 @@
#' (which is in line with the "unconditional" interpretation in the sense of
#' Rafi and Greenland).
#'
#' Ascribing a probabilistic interpretation to one realized confidence interval
#' is possible without repeated sampling of the specific experiment. Important
#' is the assumption that a _sampling distribution_ is a good description of the
#' variability of the parameter (_Vos and Holbert 2022_). At the core, the
#' interpretation of a confidence interval is "I assume that this sampling
#' distribution is a good description of the uncertainty of the parameter. If
#' that's a good assumption, then the values in this interval are the most
#' plausible or compatible with the data". The source of confidence in
#' probability statements is the assumption that the selected sampling
#' distribution is appropriate.
#'
#' "The realized confidence distribution is clearly an epistemic probability
#' distribution" (_Schweder 2018_). In Bayesian words, compatibility intervals
#' (or confidence distributons, or consonance curves) are "posteriors without
Expand Down Expand Up @@ -156,20 +167,21 @@
#' - Schweder T, Hjort NL. Confidence, Likelihood, Probability: Statistical
#' inference with confidence distributions. Cambridge University Press, 2016.
#'
#' @examples
#' - Vos P, Holbert D. Frequentist statistical inference without repeated sampling.
#' Synthese 200, 89 (2022). \doi{10.1007/s11229-022-03560-x}
#'
#' @examplesIf requireNamespace("see")
#' model <- lm(Sepal.Length ~ Species, data = iris)
#' p_function(model)
#'
#' if (requireNamespace("see") && packageVersion("see") > "0.7.3") {
#' model <- lm(mpg ~ wt + as.factor(gear) + am, data = mtcars)
#' result <- p_function(model)
#' model <- lm(mpg ~ wt + as.factor(gear) + am, data = mtcars)
#' result <- p_function(model)
#'
#' # single panels
#' plot(result, n_columns = 2)
#' # single panels
#' plot(result, n_columns = 2)
#'
#' # integrated plot, the default
#' plot(result)
#' }
#' # integrated plot, the default
#' plot(result)
#' @export
p_function <- function(model,
ci_levels = c(0.25, 0.5, 0.75, emph = 0.95),
Expand Down
6 changes: 3 additions & 3 deletions R/utils_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# usually used for printing etc.

#' @keywords internal
.add_model_parameters_attributes <- function(params,

Check warning on line 5 in R/utils_model_parameters.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_model_parameters.R,line=5,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 41 to at most 40.
model,
ci,
exponentiate = FALSE,
Expand Down Expand Up @@ -239,7 +239,7 @@
)
}

.find_coefficient_type <- function(info, exponentiate, model = NULL) {

Check warning on line 242 in R/utils_model_parameters.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_model_parameters.R,line=242,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 55 to at most 40.
# column name for coefficients
coef_col <- "Coefficient"
if (!is.null(model) && inherits(model, "emmGrid")) {
Expand Down Expand Up @@ -323,12 +323,12 @@
if (any(columns)) {
if (inherits(model, "mvord")) {
rows <- params$Component != "correlation"
} else if (inherits(model, c("clm", "clm2", "clmm"))) {
## TODO: make sure we catch all ordinal models properly here
rows <- !tolower(params$Component) %in% c("location", "scale")
} else if (is.null(params$Component)) {
# don't exponentiate dispersion
rows <- seq_len(nrow(params))
} else if (inherits(model, c("clm", "clm2", "clmm"))) {
## TODO: make sure we catch all ordinal models properly here
rows <- !tolower(params$Component) %in% c("location", "scale")
} else {
rows <- !tolower(params$Component) %in% c("dispersion", "residual")
}
Expand Down
29 changes: 21 additions & 8 deletions man/p_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test-marginaleffects.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
skip_if_not_installed("marginaleffects", minimum_version = "0.18.0")
skip_if_not_installed("insight", minimum_version = "0.19.8.8")
skip_if_not_installed("rstanarm")

test_that("marginaleffects()", {
# Frequentist
x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
model <- marginaleffects::avg_slopes(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length")

Check warning on line 8 in tests/testthat/test-marginaleffects.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-marginaleffects.R,line=8,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
out <- parameters(model)
expect_identical(nrow(out), 1L)
expect_named(out, c(
"Parameter", "Coefficient", "SE", "Statistic",
"p", "s.value", "CI", "CI_low", "CI_high"
"p", "S", "CI", "CI_low", "CI_high"
))
out <- model_parameters(model, exponentiate = TRUE)
expect_equal(out$Coefficient, 1.394, tolerance = 1e-3)
Expand All @@ -24,7 +25,7 @@
chains = 1
)
)
model <- marginaleffects::avg_slopes(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length")

Check warning on line 28 in tests/testthat/test-marginaleffects.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-marginaleffects.R,line=28,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
expect_identical(nrow(parameters(model)), 1L)
})

Expand All @@ -35,8 +36,8 @@
out <- parameters(p)
expect_identical(nrow(out), 3L)
expect_named(out, c(
"Predicted", "SE", "CI", "CI_low", "CI_high", "Statistic",
"p", "Species", "s.value"
"Predicted", "SE", "CI", "CI_low", "CI_high", "S", "Statistic",
"p", "Species"
))
out <- parameters(p, exponentiate = TRUE)
expect_equal(out$Predicted, c(30.81495, 15.95863, 19.57004), tolerance = 1e-4)
Expand All @@ -47,7 +48,7 @@
data(iris)
# Frequentist
x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
m <- marginaleffects::avg_comparisons(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length")

Check warning on line 51 in tests/testthat/test-marginaleffects.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-marginaleffects.R,line=51,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
expect_identical(nrow(parameters(m)), 1L)
out <- parameters(m, exponentiate = TRUE)
expect_equal(out$Coefficient, 1.393999, tolerance = 1e-4)
Expand All @@ -71,7 +72,6 @@
})



test_that("hypotheses()", {
data(mtcars)
x <- lm(mpg ~ hp + wt, data = mtcars)
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-model_parameters_ordinal.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ test_that("model_parameters.clm2", {

expect_snapshot(print(mp))
})

test_that("model_parameters.clmm, exponentiate works w/o component column", {
data(wine, package = "ordinal")
mox <- ordinal::clmm(rating ~ temp + contact + (1 | judge), data = wine)
out1 <- model_parameters(mox, exponentiate = FALSE)
out2 <- model_parameters(mox, exponentiate = TRUE)
expect_equal(out1$Coefficient, c(-1.62367, 1.51337, 4.22853, 6.08877, 3.063, 1.83488, 1.13113), tolerance = 1e-4)
expect_equal(out2$Coefficient, c(0.19717, 4.54199, 68.61606, 440.87991, 21.39156, 6.26441, 1.13113), tolerance = 1e-4)
expect_identical(attributes(out1)$coefficient_name, "Log-Odds")
expect_identical(attributes(out2)$coefficient_name, "Odds Ratio")
})
Loading