Skip to content

Commit 503e983

Browse files
committed
parameters.fixest: Uninformative error
Fixes #1054
1 parent 1fb5413 commit 503e983

File tree

5 files changed

+143
-160
lines changed

5 files changed

+143
-160
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.24.0.4
4+
Version: 0.24.0.5
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# parameters 0.24.1
22

3+
## Changes
4+
5+
* `model_parameters()` now gives informative error messages for more model
6+
classes than before when the function fails to extract model parameters.
7+
38
## Bug fixes
49

510
* Fixed issue when printing `model_parameters()` with models from `mgcv::gam()`.

R/1_model_parameters.R

Lines changed: 103 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -605,32 +605,31 @@ model_parameters.default <- function(model,
605605
)
606606

607607
# extract model parameters table, as data frame
608-
out <- tryCatch(
609-
.model_parameters_generic(
610-
model = model,
611-
ci = ci,
612-
ci_method = ci_method,
613-
bootstrap = bootstrap,
614-
iterations = iterations,
615-
merge_by = "Parameter",
616-
standardize = standardize,
617-
exponentiate = exponentiate,
618-
p_adjust = p_adjust,
619-
include_info = include_info,
620-
keep_parameters = keep,
621-
drop_parameters = drop,
622-
vcov = vcov,
623-
vcov_args = vcov_args,
624-
verbose = verbose,
625-
...
626-
),
627-
error = function(e) {
628-
fail <- NA
629-
attr(fail, "error") <- gsub(" ", " ", gsub("\\n", "", e$message), fixed = TRUE)
630-
fail
631-
}
608+
out <- .model_parameters_generic(
609+
model = model,
610+
ci = ci,
611+
ci_method = ci_method,
612+
bootstrap = bootstrap,
613+
iterations = iterations,
614+
merge_by = "Parameter",
615+
standardize = standardize,
616+
exponentiate = exponentiate,
617+
p_adjust = p_adjust,
618+
include_info = include_info,
619+
keep_parameters = keep,
620+
drop_parameters = drop,
621+
vcov = vcov,
622+
vcov_args = vcov_args,
623+
verbose = verbose,
624+
...
632625
)
633626

627+
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
628+
out
629+
}
630+
631+
632+
.fail_error_message <- function(out, model) {
634633
# tell user if something went wrong...
635634
if (length(out) == 1 && isTRUE(is.na(out))) {
636635
insight::format_error(
@@ -650,14 +649,9 @@ model_parameters.default <- function(model,
650649
)
651650
)
652651
}
653-
654-
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
655-
out
656652
}
657653

658654

659-
660-
661655
# helper function for the composition of the parameters table,
662656
# including a bunch of attributes required for further processing
663657
# (like printing etc.)
@@ -682,76 +676,90 @@ model_parameters.default <- function(model,
682676
...) {
683677
dots <- list(...)
684678

685-
# ==== 1. first step, extracting (bootstrapped) model parameters -------
686-
687-
# Processing, bootstrapped parameters
688-
if (bootstrap) {
689-
# set default method for bootstrapped CI
690-
if (is.null(ci_method) || missing(ci_method)) {
691-
ci_method <- "quantile"
692-
}
693-
694-
fun_args <- list(
695-
model,
696-
iterations = iterations,
697-
ci = ci,
698-
ci_method = ci_method
699-
)
700-
fun_args <- c(fun_args, dots)
701-
params <- do.call("bootstrap_parameters", fun_args)
679+
out <- tryCatch(
680+
{
681+
# ==== 1. first step, extracting (bootstrapped) model parameters -------
682+
683+
# Processing, bootstrapped parameters
684+
if (bootstrap) {
685+
# set default method for bootstrapped CI
686+
if (is.null(ci_method) || missing(ci_method)) {
687+
ci_method <- "quantile"
688+
}
689+
690+
fun_args <- list(
691+
model,
692+
iterations = iterations,
693+
ci = ci,
694+
ci_method = ci_method
695+
)
696+
fun_args <- c(fun_args, dots)
697+
params <- do.call("bootstrap_parameters", fun_args)
698+
699+
# Processing, non-bootstrapped parameters
700+
} else {
701+
# set default method for CI
702+
if (is.null(ci_method) || missing(ci_method)) {
703+
ci_method <- "wald"
704+
}
705+
706+
fun_args <- list(
707+
model,
708+
ci = ci,
709+
component = component,
710+
merge_by = merge_by,
711+
standardize = standardize,
712+
effects = effects,
713+
ci_method = ci_method,
714+
p_adjust = p_adjust,
715+
keep_parameters = keep_parameters,
716+
drop_parameters = drop_parameters,
717+
verbose = verbose,
718+
vcov = vcov,
719+
vcov_args = vcov_args
720+
)
721+
fun_args <- c(fun_args, dots)
722+
params <- do.call(".extract_parameters_generic", fun_args)
723+
}
724+
725+
726+
# ==== 2. second step, exponentiate -------
727+
728+
# exponentiate coefficients and SE/CI, if requested
729+
params <- .exponentiate_parameters(params, model, exponentiate)
730+
731+
732+
# ==== 3. third step, add information as attributes -------
733+
734+
# add further information as attributes
735+
params <- .add_model_parameters_attributes(
736+
params,
737+
model,
738+
ci,
739+
exponentiate,
740+
bootstrap,
741+
iterations,
742+
ci_method = ci_method,
743+
p_adjust = p_adjust,
744+
include_info = include_info,
745+
verbose = verbose,
746+
...
747+
)
702748

703-
# Processing, non-bootstrapped parameters
704-
} else {
705-
# set default method for CI
706-
if (is.null(ci_method) || missing(ci_method)) {
707-
ci_method <- "wald"
749+
class(params) <- c("parameters_model", "see_parameters_model", class(params))
750+
params
751+
},
752+
error = function(e) {
753+
fail <- NA
754+
attr(fail, "error") <- gsub(" ", " ", gsub("\\n", "", e$message), fixed = TRUE)
755+
fail
708756
}
709-
710-
fun_args <- list(
711-
model,
712-
ci = ci,
713-
component = component,
714-
merge_by = merge_by,
715-
standardize = standardize,
716-
effects = effects,
717-
ci_method = ci_method,
718-
p_adjust = p_adjust,
719-
keep_parameters = keep_parameters,
720-
drop_parameters = drop_parameters,
721-
verbose = verbose,
722-
vcov = vcov,
723-
vcov_args = vcov_args
724-
)
725-
fun_args <- c(fun_args, dots)
726-
params <- do.call(".extract_parameters_generic", fun_args)
727-
}
728-
729-
730-
# ==== 2. second step, exponentiate -------
731-
732-
# exponentiate coefficients and SE/CI, if requested
733-
params <- .exponentiate_parameters(params, model, exponentiate)
734-
735-
736-
# ==== 3. third step, add information as attributes -------
737-
738-
# add further information as attributes
739-
params <- .add_model_parameters_attributes(
740-
params,
741-
model,
742-
ci,
743-
exponentiate,
744-
bootstrap,
745-
iterations,
746-
ci_method = ci_method,
747-
p_adjust = p_adjust,
748-
include_info = include_info,
749-
verbose = verbose,
750-
...
751757
)
752758

753-
class(params) <- c("parameters_model", "see_parameters_model", class(params))
754-
params
759+
# check if everything is ok
760+
.fail_error_message(out, model)
761+
762+
out
755763
}
756764

757765

R/methods_fixest.R

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,25 @@ model_parameters.fixest <- function(model,
3333
}
3434

3535
# extract model parameters table, as data frame
36-
out <- tryCatch(
37-
{
38-
.model_parameters_generic(
39-
model = model,
40-
ci = ci,
41-
ci_method = ci_method,
42-
bootstrap = bootstrap,
43-
iterations = iterations,
44-
merge_by = "Parameter",
45-
standardize = standardize,
46-
exponentiate = exponentiate,
47-
p_adjust = p_adjust,
48-
include_info = include_info,
49-
keep_parameters = keep,
50-
drop_parameters = drop,
51-
vcov = vcov,
52-
vcov_args = vcov_args,
53-
verbose = verbose,
54-
...
55-
)
56-
},
57-
error = function(e) {
58-
NULL
59-
}
36+
out <- .model_parameters_generic(
37+
model = model,
38+
ci = ci,
39+
ci_method = ci_method,
40+
bootstrap = bootstrap,
41+
iterations = iterations,
42+
merge_by = "Parameter",
43+
standardize = standardize,
44+
exponentiate = exponentiate,
45+
p_adjust = p_adjust,
46+
include_info = include_info,
47+
keep_parameters = keep,
48+
drop_parameters = drop,
49+
vcov = vcov,
50+
vcov_args = vcov_args,
51+
verbose = verbose,
52+
...
6053
)
6154

62-
if (is.null(out)) {
63-
insight::format_error("Something went wrong... :-/")
64-
}
65-
6655
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
6756
out
6857
}

R/methods_mmrm.R

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,25 @@ model_parameters.mmrm <- function(model,
2727
}
2828

2929
# extract model parameters table, as data frame
30-
out <- tryCatch(
31-
.model_parameters_generic(
32-
model = model,
33-
ci = ci,
34-
ci_method = ci_method,
35-
bootstrap = bootstrap,
36-
iterations = iterations,
37-
merge_by = "Parameter",
38-
standardize = standardize,
39-
exponentiate = exponentiate,
40-
p_adjust = p_adjust,
41-
include_info = include_info,
42-
keep_parameters = keep,
43-
drop_parameters = drop,
44-
vcov = NULL,
45-
vcov_args = NULL,
46-
verbose = verbose,
47-
...
48-
),
49-
error = function(e) {
50-
fail <- NA
51-
attr(fail, "error") <- gsub(" ", " ", gsub("\\n", "", e$message), fixed = TRUE)
52-
fail
53-
}
30+
out <- .model_parameters_generic(
31+
model = model,
32+
ci = ci,
33+
ci_method = ci_method,
34+
bootstrap = bootstrap,
35+
iterations = iterations,
36+
merge_by = "Parameter",
37+
standardize = standardize,
38+
exponentiate = exponentiate,
39+
p_adjust = p_adjust,
40+
include_info = include_info,
41+
keep_parameters = keep,
42+
drop_parameters = drop,
43+
vcov = NULL,
44+
vcov_args = NULL,
45+
verbose = verbose,
46+
...
5447
)
5548

56-
# tell user if something went wrong...
57-
if (length(out) == 1 && isTRUE(is.na(out))) {
58-
insight::format_error(
59-
paste0(
60-
"Sorry, `model_parameters()` failed with the following error (possible class `",
61-
class(model)[1],
62-
"` not supported):\n"
63-
),
64-
attr(out, "error")
65-
)
66-
}
67-
6849
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
6950
out
7051
}

0 commit comments

Comments
 (0)