easystats/parameters

Feature request - ability to save printed output of model_parameters to dataframe

rbcavanaugh opened this issue · 9 comments

Hi -

Thanks for a great package. I would love to be able to save the (printed) output of functions like model_parameters() to a tibble/dataframe so I can do things like put it right in a word document using flexible and never have to manually touch my tables before manuscript submission. At the moment, I manually format each table, but the print method of model_parameters is nearly perfect. Is there a way to save the output to a dataframe? Apologies if this is the wrong place for this sort of question/request. Example:

image

Have you tried this?

as.data.frame(model_parameters(model))

Yup - that nicely organizes the information, but still requires formatting (pretty parameter names, combine the CIs, format significant digits etc.). I just realized that insight::format_table() gets most of the way there, but doesn't format the parameter names in quite the same nice fashion (but is so close that the final touches are probably more my preference than anything else). thanks!

Yeah, format_table() is very cool, but you might be better off with a dedicated package for that. Maybe modelsummary or sjPlot:

https://vincentarelbundock.github.io/modelsummary/

https://strengejacke.github.io/sjPlot/

Both use parameters under the hood to extract information from model objects, but they offer a ton more options to customize the look and structure, and can also save tables directly to Microsoft Word files if that is your ultimate target.

Take a look at export_table() as well to export it in markdown

The format() function does just this:

library(parameters)
#> Warning: package 'parameters' was built under R version 4.2.2

m <- lm(mpg ~ factor(cyl), mtcars)

(pars <- model_parameters(m))
#> Parameter   | Coefficient |   SE |          95% CI | t(29) |      p
#> -------------------------------------------------------------------
#> (Intercept) |       26.66 | 0.97 | [ 24.68, 28.65] | 27.44 | < .001
#> cyl [6]     |       -6.92 | 1.56 | [-10.11, -3.73] | -4.44 | < .001
#> cyl [8]     |      -11.56 | 1.30 | [-14.22, -8.91] | -8.90 | < .001
#> 
#> Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
#>   using a Wald t-distribution approximation.

format(pars)
#>     Parameter Coefficient   SE        95% CI t(29)      p
#> 1 (Intercept)       26.66 0.97  24.68, 28.65 27.44 < .001
#> 2     cyl [6]       -6.92 1.56 -10.11, -3.73 -4.44 < .001
#> 3     cyl [8]      -11.56 1.30 -14.22, -8.91 -8.90 < .001

Created on 2023-01-12 with reprex v2.0.2

There are two vignettes for formatting (https://easystats.github.io/parameters/articles/model_parameters_formatting.html) and printing (https://easystats.github.io/parameters/articles/model_parameters_print.html). If you don't need automated reports (rmarkdown), I usually export to HTML and then copy/paste to Word. You can do this with:

model_parameters() |> print_html()

This also works for those tables described in the vignette and is the easiest way to get formatted tables. More flexible and comprehensive in terms of summary tables is the modelsummary package, which, afaik, also directly exports tables to word documents. I bet if you try out modelsummary or some of parameter's HTML printing options, you'll get what you want.

this vignette also mentions some more packages at the bottom.

Hugely helpful thanks. I'm really trying to avoid any manual copy-paste so that my research pipeline is fully reproducible (the journals I typically submit to want APA-formatted word docs). I missed some of the other functions in the parameters vignette that I think complete the pipeline for me - creating an empty word doc with the correct styles and adding tables via flextable from a correctly formatted tibble does the trick.

@rbcavanaugh sorry I'm a bit late to the party. But since you use APA style, would the option below be useful at all?

library(report)
library(rempsyc)

m <- lm(mpg ~ cyl + wt * hp, mtcars)
pars <- report_table(m)

x <- nice_table(pars, 
                title = c("Table 1", "A Very Surprising Linear Model"),
                note = "This is an APA note.")

flextable::save_as_docx(x, path = "my_table.docx")
# word output:

nice_table

Created on 2023-02-02 with reprex v2.0.2

As you can see, the function took automatically care of formatting the p-values, removing the leading zeros, collapsing the 95% CI, italicizing proper symbols, and using Greek symbols when relevant. Full tutorial here: https://rempsyc.remi-theriault.com/articles/table.html

The format() function does just this:

library(parameters)
#> Warning: package 'parameters' was built under R version 4.2.2

m <- lm(mpg ~ factor(cyl), mtcars)

(pars <- model_parameters(m))
#> Parameter   | Coefficient |   SE |          95% CI | t(29) |      p
#> -------------------------------------------------------------------
#> (Intercept) |       26.66 | 0.97 | [ 24.68, 28.65] | 27.44 | < .001
#> cyl [6]     |       -6.92 | 1.56 | [-10.11, -3.73] | -4.44 | < .001
#> cyl [8]     |      -11.56 | 1.30 | [-14.22, -8.91] | -8.90 | < .001
#> 
#> Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
#>   using a Wald t-distribution approximation.

format(pars)
#>     Parameter Coefficient   SE        95% CI t(29)      p
#> 1 (Intercept)       26.66 0.97  24.68, 28.65 27.44 < .001
#> 2     cyl [6]       -6.92 1.56 -10.11, -3.73 -4.44 < .001
#> 3     cyl [8]      -11.56 1.30 -14.22, -8.91 -8.90 < .001

Created on 2023-01-12 with reprex v2.0.2

Thank you this is just what I was looking for! Thanks everyone else for the suggestions as well.