ggstatsplot icon indicating copy to clipboard operation
ggstatsplot copied to clipboard

How to export data from grouped extract_stats efficiently?

Open lxsteiner opened this issue 11 months ago • 0 comments

I'd like to export the individual statistics from a grouped_ggbetweenstats analysis as a .csv or .xslx file, or any other format really. Ideally you'd get the individual $caption_data, $subtitle_data, $pairwise_comparisons_data and other sections in a single file/sheet, but no matter what solution I come up with I'm hitting an issue with "expression" columns within the tibbles that contain a list of statistical expressions.

e.g.

library(PMCMRplus)
p <- grouped_ggbetweenstats(data = mtcars, x = gear, y = mpg, grouping.var = am)
p

image

all columns inside the invididual tibbles are fine except for "expression":

extract_stats(p[[1]])$caption_data
# A tibble: 1 × 16
  term       effectsize      estimate conf.level conf.low conf.high    pd prior.distribution prior.location
  <chr>      <chr>              <dbl>      <dbl>    <dbl>     <dbl> <dbl> <chr>                       <dbl>
1 Difference Bayesian t-test    -3.69       0.95    -7.63    0.0293 0.974 cauchy                          0
  prior.scale  bf10 method          conf.method log_e_bf10 n.obs expression
        <dbl> <dbl> <chr>           <chr>            <dbl> <int> <list>    
1       0.707  3.43 Bayesian t-test ETI               1.23    19 <language>

Trying to write to an .xlsx for example with single sheets containing all the statistics for a group:

library(do)
for (i in 1:length(p)) {
  subplot <- extract_stats(p[[i]])
  sheetname <- paste0("group", i)
  do::write_xlsx(subplot$subtitle_data, file = "stats.xlsx", sheet = sheetname)
  do::write_xlsx(subplot$caption_data, file = "stats.xlsx", sheet = sheetname, append = TRUE)
}

but there's always an error in most functions that export the data objects (also with e.g. openxlsx::writeData):

Error in FUN(X[[i]], ...) : 
  argument `...` should be a character vector (or an object coercible to)
In addition: Warning message:
In is.na(x) : is.na() applied to non-(list or vector) of type 'language'

Any suggestions or recommendations for better practices on getting all the statistics in some type of delimited file? I guess one option could be to omit the "expression" column before, but I don't think that all tibbles listed inside extract_stats have that column, if that could be an issue. Also, any way to easily access the label of the group to print that label along with all the exported data (in the above example comes from grouping.var = am which is just "0" or "1"), other than how it is indexed and ordered?

Any ideas or suggestions would really be welcome. Thank you.

lxsteiner avatar Mar 14 '24 13:03 lxsteiner