You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know that custom summaries can be provided on the descriptive layer.
How can I do the same on the counting one?
I need a Wilson confidence interval (binom::binom.wilson()) for each %. To access it I need the current n (events or subjects, depending on scenario) and corresponding denominator. I need it both at the items and total level.
Ideally, it should also support nested (hierarchical) counting, so the most nested items sum up to 100% with respect to its parent total, and each subsequent group parent total % is calculated - again - with respect to its parent group. The top most % is 100% then.
Where should I put the procedure calculating Wilson score CI? Ideally I want to merge it with the current cell content after the "\n" character (will be formatted later via flextable package).
Another option is to obtain a 2-row layout (it's quite common) like below:
OK, let's start from something simple and obtain the CIs. Then I will think how to merge it.
I found that I can access all the components used for the calculation of % (nominator and denominator).
Let's use it:
> data %>%
+ unite("ARM_SIDE", c(ARM, SIDE)) %>%
+ tplyr_table(ARM_SIDE) %>%
+ add_total_group() %>%
+ add_layer(
+ layer = group_count(SOC) %>%
+ set_distinct_by(PatID) %>%
+ set_format_strings(f_str("xx.x ; xx.x", binom::binom.wilson(x = distinct, n=distinct_total)$lower,
+ binom::binom.wilson(n = distinct, x=distinct_total)$upper))) %>%
+ build()
Error: In `f_str` all values submitted via `...` must be variable names.
Failed. I cannot provide just the results I want. It must be the closed set of recognizable variable names.
It's also highly inefficient to call the same function multiple times, so let's try with(), just to check:
> data %>%
+ unite("ARM_SIDE", c(ARM, SIDE)) %>%
+ tplyr_table(ARM_SIDE) %>%
+ add_total_group() %>%
+ add_layer(
+ layer = group_count(SOC) %>%
+ set_distinct_by(PatID) %>%
+ set_format_strings( with(binom::binom.wilson(x = distinct, n=distinct_total), f_str("xx.x ; xx.x", lower, upper)))) %>%
+ build()
Error: In `set_format_string` entry 1 is not an `f_str` object. All assignmentes made within `set_format_string` must be made using the function `f_str`. See the `f_str` documentation.
OK, this won't pass me further.
EDIT: I tried via set_custom_summaries (inefficient as previously), but didn't succeed. It works only with descriptive summaries.
> data %>%
+ unite("ARM_SIDE", c(ARM, SIDE)) %>%
+ tplyr_table(ARM_SIDE) %>%
+ add_total_group() %>%
+ add_layer(
+ layer = group_count(SOC) %>%
+ set_distinct_by(PatID) %>%
+ set_custom_summaries(
+ Wilson_LO = binom::binom.wilson(x = distinct, n=distinct_total)$lower,
+ Wilson_HI = binom::binom.wilson(x = distinct, n=distinct_total)$upper) %>%
+ set_format_strings(f_str("xx.x ; xx.x", Wilson_LO, Wilson_HI))) %>%
+ build()
Error in `assert_inherits_class()`:
! Argument `e` does not inherit "desc_layer". Classes: tplyr_layer, count_layer, environment
Run `rlang::last_error()` to see where the error occurred.
Warning message:
In max(trc$indices) : no non-missing arguments to max; returning -Inf
Is there any other place where I can put the calculation of the CI and re-use the returned lower/upper bounds of the CI?
EDIT: I tried defining my own "add-in" that provides the environment with necessary variables (understandable by f_str), but the package is well protected from using external functions, and also the environment is locked from adding them.
> f <- function (e, x)
{
env_bind(e, xx = 2*x)
e
}
> data %>%
unite("ARM_SIDE", c(ARM, SIDE)) %>%
tplyr_table(ARM_SIDE) %>%
add_total_group() %>%
add_layer(
layer = group_count(SOC) %>%
set_distinct_by(PatID) %>%
f(2) %>%
set_format_strings(f_str("xx.x", xx))) %>%
build()
Error: Functions called within `add_layer` must be part of `Tplyr`
OK, so I tried to "hack" the Tplyr and add my own function, so it's recognized by the engine, but it turned out the protection goes farther:
# A little dummy function, only to check if I can provide my own new variable (e.g. holding the lower or upper CI bound).
# If I could do this, then I could generate any kind of summary I need and merge it with the rest of the layer.
f <- function (e, x)
{
env_bind(e, xx = 2*x)
e
}
rlang::env_unlock(environment(Tplyr::add_layer)) #unlocking the Tplyr environment
ns <- environment(Tplyr::add_layer)
assign("f", f, envir = ns)
environment(f) <- ns # some of these calls may be redundant, just for "safety"
assignInNamespace("f", f, ns = ns)
namespaceExport(ns, "f")
# Now let's try using it with Tplyr
> data %>%
unite("ARM_SIDE", c(ARM, SIDE)) %>%
tplyr_table(ARM_SIDE) %>%
add_total_group() %>%
add_layer(
layer = group_count(SOC) %>%
set_distinct_by(PatID) %>%
f(2) %>% # some number just to trace the result
set_format_strings(f_str("xx.x", xx))) %>%
build()
Error: f_str for n_counts in a count_layer can only be n, pct, distinct, distinct_pct, total, or distinct_total
Good! Now f() is part of Tplyr and passes the getNamespaceExports("Tplyr") checking.
Bad! The names of variables that can be used with this layer are fixed.
And also putting it here seems to be nonsensical, as the nominators and denominators aren't known at this level.
It could be on the level of f_str(), combined via paste() or sprintf():
But the distinct_n and distinct_total isn't known in the paste function...
f_str won't allow for other variables, set_format_strings won't allow for anything else than f_str.
And here my "creativity" ends. Briefly - while the numerical summary is extensible, the categorical one is not. Would you consider enabling this as well?
This should be done in a more general way - to allow one for using any custom functions taking the n, distinct n, and appropriate denominators, producing custom columns. It's very common to add various CIs (for %, for their differences, like Miettinen-Nurminen), p-values of various tests, effect size of various kinds (for difference in proportions) along with their own bootstrapped CI and so on. So if there is any common mechanism enabling the user to use own, custom functions returning custom columns, that can be reused within the counting layer (and maybe also the shift layer), it would be really great!
To make it absolutely clear - I am not asking for making Tplyr a statistical suite. This would be a nonsense, as it's just for tabular summaries. I rather mean enabling the user to provide own summaries instead.
The text was updated successfully, but these errors were encountered:
Dear Authors,
I know that custom summaries can be provided on the descriptive layer.
How can I do the same on the counting one?
I need a Wilson confidence interval (
binom::binom.wilson()
) for each %. To access it I need the current n (events or subjects, depending on scenario) and corresponding denominator. I need it both at the items and total level.Ideally, it should also support nested (hierarchical) counting, so the most nested items sum up to 100% with respect to its parent total, and each subsequent group parent total % is calculated - again - with respect to its parent group. The top most % is 100% then.
My data:
Where should I put the procedure calculating Wilson score CI? Ideally I want to merge it with the current cell content after the "\n" character (will be formatted later via flextable package).
Another option is to obtain a 2-row layout (it's quite common) like below:
OK, let's start from something simple and obtain the CIs. Then I will think how to merge it.
I found that I can access all the components used for the calculation of % (nominator and denominator).
Let's use it:
Failed. I cannot provide just the results I want. It must be the closed set of recognizable variable names.
It's also highly inefficient to call the same function multiple times, so let's try with(), just to check:
OK, this won't pass me further.
EDIT: I tried via set_custom_summaries (inefficient as previously), but didn't succeed. It works only with descriptive summaries.
Is there any other place where I can put the calculation of the CI and re-use the returned lower/upper bounds of the CI?
EDIT: I tried defining my own "add-in" that provides the environment with necessary variables (understandable by f_str), but the package is well protected from using external functions, and also the environment is locked from adding them.
OK, so I tried to "hack" the Tplyr and add my own function, so it's recognized by the engine, but it turned out the protection goes farther:
Good! Now f() is part of Tplyr and passes the
getNamespaceExports("Tplyr")
checking.Bad! The names of variables that can be used with this layer are fixed.
And also putting it here seems to be nonsensical, as the nominators and denominators aren't known at this level.
It could be on the level of f_str(), combined via paste() or sprintf():
But the distinct_n and distinct_total isn't known in the paste function...
f_str won't allow for other variables, set_format_strings won't allow for anything else than f_str.
And here my "creativity" ends. Briefly - while the numerical summary is extensible, the categorical one is not. Would you consider enabling this as well?
This should be done in a more general way - to allow one for using any custom functions taking the n, distinct n, and appropriate denominators, producing custom columns. It's very common to add various CIs (for %, for their differences, like Miettinen-Nurminen), p-values of various tests, effect size of various kinds (for difference in proportions) along with their own bootstrapped CI and so on. So if there is any common mechanism enabling the user to use own, custom functions returning custom columns, that can be reused within the counting layer (and maybe also the shift layer), it would be really great!
To make it absolutely clear - I am not asking for making Tplyr a statistical suite. This would be a nonsense, as it's just for tabular summaries. I rather mean enabling the user to provide own summaries instead.
The text was updated successfully, but these errors were encountered: