Writing new `tidy()` method
The art of extensibility 2
Source:vignettes/extend/new-tidy.Rmd
new-tidy.RmdRationale
statim is so extendable, you can extend the existing methods or write your own and make it extensive.
Here, I plan to extend TTEST() with another
bootstrapping by simply adding another variation:
add_variant(TTEST, x_by, "another_boot") %<-% variant(
fn = function(.proc, .n = 1000L) {
x = .proc$x_data[[1]]
group_data = .proc$group_data
grp = as.character(group_data[[1]])
lvls = unique(grp)
x1 = x[grp == lvls[[1]]]
x2 = x[grp == lvls[[2]]]
boot_fn = function(d, i) mean(d[i, 1]) - mean(d[i, 2])
b = boot::boot(data.frame(x1, x2), boot_fn, R = .n)
boot::boot.ci(b, type = "perc")
}
)
another_boot_tt =
sleep |>
define_model(extra %by% group) |>
prepare_test(TTEST) |>
via("another_boot") |>
conclude()
another_boot_tt
#>
#> == Model =======================================================================
#>
#> Variable Mapper : x_by
#> Args : extra | group
#> x_vars : 1
#> by_vars : 1
#>
#> == T-Test · another_boot =======================================================
#>
#> BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
#> Based on 1000 bootstrap replicates
#>
#> CALL :
#> boot::boot.ci(boot.out = b, type = "perc")
#>
#> Intervals :
#> Level Percentile
#> 95% (-2.44, -0.95 )
#> Calculations and Intervals on Original Scalestatim has built-in output S7
classes support, which you can use them to register your own method
into that specific class in order to be automatically processed into
statim internals, like extracting your output in tibble
format with tidy(). Otherwise, you must register them by
your own.
An example above doesn’t register with one of built-in output S7
classes, so tidy() will fail. Fortunately, you can bring it
down by using
making_tidy(<stat_fn>, <model_id / S7::class_formula>) %<-% method_tidy(...)type
of registry:
making_tidy(TTEST, x_by) %<-% method_tidy(
another_boot = function(.x, ...) {
dat = .x@data
tibble::tibble(
R = dat$R,
t0 = dat$t0
)
}
)
another_boot_tt |> tidy()
#> # A tibble: 1 × 2
#> R t0
#> <int> <dbl>
#> 1 1000 -1.58You can clean the TTEST() in x_by model
pipeline slate by simply using remove_variant().
remove_variant(TTEST, x_by, "another_boot")