Best way to save output/results of multiple models?

Hello all,
I’m trying to figure out how to better save and organize the info on my models, both to make my scripts more efficient and make finding what I need later less painful. In one of my current studies, I have a whole slew of planned comparisons that I have to run, which means I have a lot of models. The current way I’m saving all this information is by creating a table for each rstan model, binding them together, and then writing all of my tables to an Excel sheet. My code currently looks like this:

note:describe_posterior_fancy is a custom version of bayestestR::describe_posterior that simply gives model information; the models (e.g. Aconditions, Bconditions) are stan_glm objects.

# Part 1

hyp_table1=describe_posterior_fancy(Aconditions)
hyp_table1=add_row(hyp_table1, Parameter="A conditions", .before = 1) # separate and distinguish models by name with a blank row

hyp_table2=describe_posterior_fancy(Bconditions)
hyp_table2=add_row(hyp_table2, Parameter="B conditions", .before = 1)

hyp_table3=describe_posterior_fancy(Cconditions)
hyp_table3=add_row(hyp_table3, Parameter="C conditions", .before = 1)

hypothesis_table=rbind(hyp_table1,hyp_table2,hyp_table3)
print(hypothesis_table)
rm(hyp_table1,hyp_table2,hyp_table3)

# part 2
hyp_table4=describe_posterior_fancy(discountconditions1)
hyp_table4=add_row(hyp_table4, Parameter="50% Discount", .before = 1)

hyp_table5=describe_posterior_fancy(discountconditions2)
hyp_table5=add_row(hyp_table5, Parameter="60% Discount", .before = 1)

hypothesis_table2=rbind(hyp_table4,hyp_table5)
rm(hyp_table4,hyp_table5)
print(hypothesis_table2)

library(openxlsx)

wb <- createWorkbook("Ryan")

addWorksheet(wb, "plannedComparisons")

writeDataTable(wb, "plannedComparisons", hypothesis_table, startCol = 1, startRow = 1, colNames = TRUE, rowNames = FALSE,
          tableStyle="TableStyleLight2",tableName=NULL, headerStyle = NULL,withFilter=FALSE,keepNA=TRUE,sep=", ",
          stack = FALSE, firstColumn = FALSE, lastColumn = FALSE,bandedRows = TRUE,bandedCols = FALSE)


writeDataTable(wb, "plannedComparisons", hypothesis_table2, startCol = 1, startRow = 15, colNames = TRUE, rowNames = FALSE,
          tableStyle="TableStyleLight2",tableName=NULL, headerStyle = NULL,withFilter=FALSE,keepNA=TRUE,sep=", ",
          stack = FALSE, firstColumn = FALSE, lastColumn = FALSE,bandedRows = TRUE,bandedCols = FALSE)

writeDataTable(wb, "plannedComparisons", main_model, startCol = 1, startRow = 25, colNames = TRUE, rowNames = FALSE,
          tableStyle="TableStyleLight2",tableName=NULL, headerStyle = NULL,withFilter=FALSE,keepNA=TRUE,sep=", ",
          stack = FALSE, firstColumn = FALSE, lastColumn = FALSE,bandedRows = TRUE,bandedCols = FALSE)


saveWorkbook(wb, "filenamehere.xlsx", overwrite =TRUE)

Obviously this is extremely inefficient; it takes a lot of space and time to write, and even more annoying yet is how the “startRow” option needs to be modified in each writeDataTable command, depending on how many models I have and how big they are, so they don’t overlap. Since this is my first full study with R though I’m not too well versed in other possible solutions. Any suggestions?

I don’t think save your data with Excel files. If you get a problem with that format, I don’t know how easy it is to recover your data (or if you want to read it with another piece of software, etc). If it’s dataframes, the basic way to save those in Tidyverse is write_delim.

Thanks! I’ll give that a shot