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

Wrong header when using identity-link with GLMs #900

Merged
merged 3 commits into from
Sep 11, 2023
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.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 @@ -16,7 +16,7 @@
wb_component = FALSE,
...) {
# capture additional arguments
dot.arguments <- list(...)

Check warning on line 19 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=19,col=3,[object_name_linter] Variable and function name style should match snake_case or symbols.

# model info
info <- .safe(suppressWarnings(insight::model_info(model, verbose = FALSE)))
Expand Down Expand Up @@ -63,7 +63,7 @@
# use tryCatch, these might fail...
attr(params, "test_statistic") <- .safe(insight::find_statistic(model))
attr(params, "log_response") <- .safe(isTRUE(grepl("log", insight::find_transformation(model), fixed = TRUE)))
attr(params, "log_predictors") <- .safe(any(grepl("log", unlist(insight::find_terms(model)[c("conditional", "zero_inflated", "instruments")]), fixed = TRUE)))

Check warning on line 66 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=66,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 160 characters.

# save if model is multivariate response model
if (isTRUE(info$is_multivariate)) {
Expand Down Expand Up @@ -213,16 +213,16 @@

switch(tolower(ci_method),
# abbreviations
"eti" = ,

Check warning on line 216 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=216,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"hdi" = ,

Check warning on line 217 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=217,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"si" = toupper(ci_method),

Check warning on line 218 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=218,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
# named after people
"satterthwaite" = ,

Check warning on line 220 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=220,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"kenward" = ,

Check warning on line 221 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=221,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"wald" = insight::format_capitalize(ci_method),

Check warning on line 222 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=222,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
# special cases
"bci" = ,

Check warning on line 224 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=224,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"bcai" = "BCa",

Check warning on line 225 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=225,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
# no change otherwise
ci_method
)
Expand Down 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")
})
Loading