Skip to content

Commit

Permalink
Wrong header when using identity-link with GLMs (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Sep 11, 2023
1 parent 219e6bc commit 99a2993
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
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.1.4
Version: 0.21.1.5
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Fixed issue with wrong calculation of test-statistic and p-values in
`model_parameters()` for `fixest` models.

* Fixed issue with wrong column header for `glm` models with
`family = binomial("identiy")`.

* Minor fixes for `dominance_analysis()`.

# parameters 0.21.1
Expand Down
12 changes: 10 additions & 2 deletions R/utils_model_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@
} else if ((info$is_binomial && info$is_logit) || info$is_ordinal || info$is_multinomial || info$is_categorical) {
coef_col <- "Odds Ratio"
} else if (info$is_binomial && !info$is_logit) {
coef_col <- "Risk Ratio"
if (info$link_function == "identity") {
coef_col <- "Exp. Risk"
} else {
coef_col <- "Risk Ratio"
}
} else if (info$is_count) {
coef_col <- "IRR"
}
Expand All @@ -261,7 +265,11 @@
} else if ((info$is_binomial && info$is_logit) || info$is_ordinal || info$is_multinomial || info$is_categorical) {
coef_col <- "Log-Odds"
} else if (info$is_binomial && !info$is_logit) {
coef_col <- "Log-Risk"
if (info$link_function == "identity") {
coef_col <- "Risk"
} else {
coef_col <- "Log-Risk"
}
} else if (info$is_count) {
coef_col <- "Log-Mean"
}
Expand Down
23 changes: 15 additions & 8 deletions tests/testthat/test-model_parameters.glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ skip_if_not_installed("boot")
test_that("model_parameters.lm", {
model <- lm(mpg ~ wt, data = mtcars)
params <- model_parameters(model, verbose = FALSE)
expect_equal(c(nrow(params), ncol(params)), c(2, 9))
expect_identical(c(nrow(params), ncol(params)), c(2L, 9L))
expect_equal(params$CI_high, c(41.119752761418, -4.20263490802709), tolerance = 1e-3)
expect_equal(attributes(params)$sigma, 3.045882, tolerance = 1e-3)

params <- model_parameters(model, ci = c(0.8, 0.9), verbose = FALSE)
expect_equal(c(nrow(params), ncol(params)), c(2, 10))
expect_identical(c(nrow(params), ncol(params)), c(2L, 10L))

params <- model_parameters(model, dispersion = TRUE, bootstrap = TRUE, iterations = 500, verbose = FALSE)
expect_equal(c(nrow(params), ncol(params)), c(2, 7))
expect_identical(c(nrow(params), ncol(params)), c(2L, 7L))

model <- lm(mpg ~ wt + cyl, data = mtcars)
params <- model_parameters(model, verbose = FALSE)
expect_equal(c(nrow(params), ncol(params)), c(3, 9))
expect_identical(c(nrow(params), ncol(params)), c(3L, 9L))

model <- lm(mpg ~ wt * cyl, data = mtcars)
params <- model_parameters(model, verbose = FALSE)
expect_equal(c(nrow(params), ncol(params)), c(4, 9))
expect_identical(c(nrow(params), ncol(params)), c(4L, 9L))

params <- model_parameters(model, component = "conditional", effects = "fixed", verbose = FALSE)
})
Expand All @@ -28,7 +28,7 @@ test_that("print digits model_parameters.lm", {
model <- lm(mpg ~ wt, data = mtcars)
params <- model_parameters(model, digits = 4, ci_digits = 5, verbose = FALSE)
out <- capture.output(print(params))
expect_equal(out[3], "(Intercept) | 37.2851 | 1.8776 | [33.45050, 41.11975] | 19.8576 | < .001")
expect_identical(out[3], "(Intercept) | 37.2851 | 1.8776 | [33.45050, 41.11975] | 19.8576 | < .001")
})


Expand All @@ -48,10 +48,10 @@ test_that("model_parameters.glm - binomial", {
model <- glm(vs ~ wt + cyl, data = mtcars, family = "binomial")

params <- model_parameters(model, verbose = FALSE)
expect_equal(c(nrow(params), ncol(params)), c(3, 9))
expect_identical(c(nrow(params), ncol(params)), c(3L, 9L))

params <- suppressWarnings(model_parameters(model, bootstrap = TRUE, iterations = 500, verbose = FALSE))
expect_equal(c(nrow(params), ncol(params)), c(3, 6))
expect_identical(c(nrow(params), ncol(params)), c(3L, 6L))

params <- model_parameters(model, component = "conditional", effects = "fixed", verbose = FALSE)
})
Expand All @@ -67,3 +67,10 @@ test_that("model_parameters.glm - Gamma - print", {
mp <- model_parameters(m, exponentiate = TRUE)
expect_snapshot(mp)
})

test_that("model_parameters.glm - glm, identity link", {
data(mtcars)
m <- glm(am ~ vs, data = mtcars, family = binomial(link = "identity"))
p <- model_parameters(m)
expect_identical(attributes(p)$coefficient_name, "Risk")
})

0 comments on commit 99a2993

Please sign in to comment.