Skip to content

Commit 7cd9d0f

Browse files
authored
equivalence_test() for modelbased-objects (#1171)
* `equivalence_test()` for modelbased-objects * ... * own function * final * news * fix * wordlist * fix
1 parent 9c4bc23 commit 7cd9d0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+240
-53
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
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.28.2
4+
Version: 0.28.2.3
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",
@@ -224,10 +224,11 @@ VignetteBuilder:
224224
knitr
225225
Encoding: UTF-8
226226
Language: en-US
227-
RoxygenNote: 7.3.2
227+
RoxygenNote: 7.3.3
228228
Roxygen: list(markdown = TRUE)
229229
Config/testthat/edition: 3
230230
Config/testthat/parallel: true
231231
Config/Needs/website: easystats/easystatstemplate
232232
Config/Needs/check: stan-dev/cmdstanr
233233
Config/rcmdcheck/ignore-inconsequential-notes: true
234+
Remotes: easystats/insight, easystats/modelbased

NAMESPACE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ S3method(ci,crq)
3434
S3method(ci,default)
3535
S3method(ci,deltaMethod)
3636
S3method(ci,effectsize_table)
37+
S3method(ci,estimate_contrasts)
38+
S3method(ci,estimate_means)
39+
S3method(ci,estimate_slopes)
3740
S3method(ci,fixest_multi)
3841
S3method(ci,flac)
3942
S3method(ci,flic)
@@ -137,6 +140,9 @@ S3method(display,parameters_simulate)
137140
S3method(display,parameters_standardized)
138141
S3method(dof_satterthwaite,lmerMod)
139142
S3method(equivalence_test,MixMod)
143+
S3method(equivalence_test,estimate_contrasts)
144+
S3method(equivalence_test,estimate_means)
145+
S3method(equivalence_test,estimate_slopes)
140146
S3method(equivalence_test,feis)
141147
S3method(equivalence_test,felm)
142148
S3method(equivalence_test,gee)
@@ -237,6 +243,9 @@ S3method(model_parameters,draws)
237243
S3method(model_parameters,emmGrid)
238244
S3method(model_parameters,emm_list)
239245
S3method(model_parameters,epi.2by2)
246+
S3method(model_parameters,estimate_contrasts)
247+
S3method(model_parameters,estimate_means)
248+
S3method(model_parameters,estimate_slopes)
240249
S3method(model_parameters,fa)
241250
S3method(model_parameters,fa.ci)
242251
S3method(model_parameters,feglm)
@@ -788,6 +797,9 @@ S3method(standard_error,draws)
788797
S3method(standard_error,effectsize_table)
789798
S3method(standard_error,emmGrid)
790799
S3method(standard_error,emm_list)
800+
S3method(standard_error,estimate_contrasts)
801+
S3method(standard_error,estimate_means)
802+
S3method(standard_error,estimate_slopes)
791803
S3method(standard_error,factor)
792804
S3method(standard_error,feglm)
793805
S3method(standard_error,fitdistr)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# parameters 0.28.2.1
2+
3+
* `equivalence_test()` gets methods for objects from the *modelbased* package.
4+
15
# parameters 0.28.2
26

37
## Bug fixes

R/equivalence_test.R

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,98 @@ equivalence_test.glmmTMB <- equivalence_test.merMod
355355
equivalence_test.MixMod <- equivalence_test.merMod
356356

357357

358+
# modelbased ------------------------------
359+
360+
#' @export
361+
equivalence_test.estimate_means <- function(
362+
x,
363+
range = "default",
364+
ci = 0.95,
365+
rule = "classic",
366+
vcov = NULL,
367+
vcov_args = NULL,
368+
verbose = TRUE,
369+
...
370+
) {
371+
# ==== define rope range ====
372+
373+
range <- .check_rope_range(x, range, verbose)
374+
375+
if (length(ci) > 1) {
376+
insight::format_alert("`ci` may only be of length 1. Using first ci-value now.")
377+
ci <- ci[1]
378+
}
379+
380+
# ==== check degrees of freedom ====
381+
382+
dof <- unique(insight::get_df(x))
383+
if (length(dof) > 1) {
384+
dof <- Inf
385+
}
386+
387+
# ==== requested confidence intervals ====
388+
389+
conf_int <- as.data.frame(t(x[c("CI_low", "CI_high")]))
390+
391+
# ==== the "narrower" intervals (1-2*alpha) for CET-rules. ====
392+
393+
alpha <- 1 - ci
394+
insight::check_if_installed("modelbased")
395+
396+
# we need to call the modelbased function again, so get the call
397+
# modify CI and evaluate that call
398+
cl <- insight::get_call(x)
399+
cl$ci <- ci - alpha
400+
x2 <- eval(cl)
401+
conf_int2 <- as.data.frame(t(x2[c("CI_low", "CI_high")]))
402+
403+
# ==== equivalence test for each parameter ====
404+
405+
l <- Map(
406+
function(ci_wide, ci_narrow) {
407+
.equivalence_test_numeric(
408+
ci = ci,
409+
ci_wide,
410+
ci_narrow,
411+
range_rope = range,
412+
rule = rule,
413+
dof = dof,
414+
verbose = verbose
415+
)
416+
},
417+
conf_int,
418+
conf_int2
419+
)
420+
421+
dat <- do.call(rbind, l)
422+
params <- insight::get_parameters(x)
423+
424+
out <- data.frame(
425+
Parameter = params$Parameter,
426+
CI = ifelse(rule == "bayes", ci, ci - alpha),
427+
dat,
428+
stringsAsFactors = FALSE
429+
)
430+
431+
# ==== (adjusted) p-values for tests ====
432+
433+
out$p <- .add_p_to_equitest(x, ci, range, vcov = vcov, vcov_args = vcov_args, ...)
434+
435+
attr(out, "rope") <- range
436+
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
437+
attr(out, "rule") <- rule
438+
class(out) <- c("equivalence_test_lm", "see_equivalence_test_lm", class(out))
439+
440+
out
441+
}
442+
443+
#' @export
444+
equivalence_test.estimate_contrasts <- equivalence_test.estimate_means
445+
446+
#' @export
447+
equivalence_test.estimate_slopes <- equivalence_test.estimate_means
448+
449+
358450
# Special classes -------------------------
359451

360452
#' @export
@@ -407,6 +499,14 @@ equivalence_test.parameters_model <- function(x,
407499

408500
#' @keywords internal
409501
.check_rope_range <- function(x, range, verbose) {
502+
# for modelbased-objects, we extract the model to define the rope range
503+
if (inherits(x, c("estimate_means", "estimate_contrasts", "estimate_slopes"))) {
504+
x <- .safe(insight::get_model(x))
505+
# if not successful, return defaults
506+
if (is.null(x)) {
507+
return(c(-1, 1))
508+
}
509+
}
410510
if (all(range == "default")) {
411511
range <- bayestestR::rope_range(x, verbose = verbose)
412512
if (is.list(range)) {
@@ -439,7 +539,6 @@ equivalence_test.parameters_model <- function(x,
439539
ci <- ci[1]
440540
}
441541

442-
443542
# ==== check degrees of freedom ====
444543

445544
df_column <- grep("(df|df_error)", colnames(x))

R/methods_modelbased.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# model_parameters ----------------
2+
3+
#' @export
4+
model_parameters.estimate_means <- function(model, ...) {
5+
out <- model
6+
class(out) <- c("parameters_model", "see_parameters_model", class(out))
7+
out
8+
}
9+
10+
#' @export
11+
model_parameters.estimate_slopes <- model_parameters.estimate_means
12+
13+
#' @export
14+
model_parameters.estimate_contrasts <- model_parameters.estimate_means
15+
16+
17+
# standard_error ----------------
18+
19+
#' @export
20+
standard_error.estimate_means <- function(model, ...) {
21+
params <- insight::get_parameters(model)
22+
data.frame(Parameter = params$Parameter, SE = model$SE, stringsAsFactors = FALSE)
23+
}
24+
25+
#' @export
26+
standard_error.estimate_slopes <- standard_error.estimate_means
27+
28+
#' @export
29+
standard_error.estimate_contrasts <- standard_error.estimate_means
30+
31+
32+
# ci ----------------
33+
34+
#' @export
35+
ci.estimate_means <- function(x, ...) {
36+
params <- insight::get_parameters(x)
37+
38+
ci_value <- attributes(x)$ci
39+
if (is.null(ci_value)) {
40+
ci_value <- 0.95
41+
}
42+
43+
data.frame(
44+
Parameter = params$Parameter,
45+
CI = ci_value,
46+
CI_low = x$CI_low,
47+
CI_high = x$CI_high,
48+
stringsAsFactors = FALSE
49+
)
50+
}
51+
52+
#' @export
53+
ci.estimate_slopes <- ci.estimate_means
54+
55+
#' @export
56+
ci.estimate_contrasts <- ci.estimate_means

R/n_clusters_easystats.R

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,34 @@ n_clusters_hclust <- function(x,
244244

245245
# Utils -------------------------------------------------------------------
246246

247-
248247
#' @keywords internal
249-
.n_clusters_factoextra <- function(x,
250-
method = "wss",
251-
standardize = TRUE,
252-
include_factors = FALSE,
253-
clustering_function = stats::kmeans,
254-
n_max = 10,
255-
...) {
256-
x <- .prepare_data_clustering(x, include_factors = include_factors, standardize = standardize, ...)
248+
.n_clusters_factoextra <- function(
249+
x,
250+
method = "wss",
251+
standardize = TRUE,
252+
include_factors = FALSE,
253+
clustering_function = stats::kmeans,
254+
n_max = 10,
255+
...
256+
) {
257+
x <- .prepare_data_clustering(
258+
x,
259+
include_factors = include_factors,
260+
standardize = standardize,
261+
...
262+
)
257263

258264
insight::check_if_installed("factoextra")
259265

260-
factoextra::fviz_nbclust(x, clustering_function, method = method, k.max = n_max, verbose = FALSE)$data
266+
suppressWarnings(
267+
factoextra::fviz_nbclust(
268+
x,
269+
clustering_function,
270+
method = method,
271+
k.max = n_max,
272+
verbose = FALSE
273+
)$data
274+
)
261275
}
262276

263277

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ mgcv
319319
mhurdle
320320
mlogit
321321
mmrm
322+
modelbased
322323
modelsummary
323324
multcomp
324325
multicollinearity

man/bootstrap_model.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bootstrap_parameters.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ci.default.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)