From 10dea9e8837fa051c39f0dbef2129ef60e96c6c8 Mon Sep 17 00:00:00 2001 From: philchalmers Date: Mon, 2 Dec 2024 11:59:20 -0500 Subject: [PATCH] make SimResults output consistent --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/SimResults.R | 17 +++++++++++++---- man/SimSolve.Rd | 14 ++++++-------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d76cead9..28e1ccb3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: SimDesign Title: Structure for Organizing Monte Carlo Simulation Designs -Version: 2.17.4 +Version: 2.17.5 Authors@R: c(person("Phil", "Chalmers", email = "rphilip.chalmers@gmail.com", role = c("aut", "cre"), comment = c(ORCID="0000-0001-5332-2810")), person("Matthew", "Sigal", role = c("ctb")), diff --git a/NEWS.md b/NEWS.md index c8494d21..6c484169 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,9 @@ ## Changes in SimDesign 2.18 +- `SimResults()` now gives the same output behavior when `store_results` or + `save_results` are used (see issue #45) + - Use of `SimSolve(..., wait.time)` now automatically sets the `maxiter` to 3000 to avoid early terminations diff --git a/R/SimResults.R b/R/SimResults.R index 93a461db..bbb34b24 100644 --- a/R/SimResults.R +++ b/R/SimResults.R @@ -86,11 +86,20 @@ SimResults <- function(obj, which, prefix = "results-row", wd = getwd()){ file_nums <- gsub(paste0(prefix, '-'), '', file_nums) file_nums <- as.numeric(gsub('.rds', '', file_nums)) files <- data.frame(file_nums, files, stringsAsFactors = FALSE) - ret <- vector('list', length(which)) + stored_Results_list <- vector('list', length(which)) for(i in seq_len(length(which))){ pick <- which(files$file_num == which[i]) - ret[[i]] <- readRDS(files$files[pick]) + stored_Results_list[[i]] <- readRDS(files$files[pick]) } - if(length(which) == 1L) ret <- ret[[1]] - ret + design <- SimExtract(obj, 'design') + if(is(stored_Results_list[[1L]]$results, 'data.frame') || + is(stored_Results_list[[1L]]$results, 'matrix')){ + for(i in seq_len(length(stored_Results_list))) + stored_Results_list[[i]] <- cbind(design[i,], + stored_Results_list[[i]]$results, row.names=NULL) + stored_Results_list <- dplyr::bind_rows(stored_Results_list) + stored_Results_list$ID <- NULL + stored_Results_list <- dplyr::as_tibble(stored_Results_list) + } + stored_Results_list } diff --git a/man/SimSolve.Rd b/man/SimSolve.Rd index 7dcb780b..c71d8274 100644 --- a/man/SimSolve.Rd +++ b/man/SimSolve.Rd @@ -370,19 +370,17 @@ pwr.t.test(d=.8, n=N[3]) # Solutions involving more iterations will be more accurate, # and therefore it is recommended to run the ProBABLI root-solver as long # the analyst can tolerate if the most accurate estimates are desired. -# Below executes the simulation for 5 minutes for each condition +# Below executes the simulation for 2 minutes per condition -solved_5min <- SimSolve(design=Design, b=.8, interval=c(10, 500), +solved_2min <- SimSolve(design=Design[1, ], b=.8, interval=c(10, 500), generate=Generate, analyse=Analyse, summarise=Summarise, - wait.time="5") -solved_5min -summary(solved_5min) + wait.time="2") +solved_2min +summary(solved_2min) # use estimated N results to see how close power was -N <- solved_5min$N +N <- solved_2min$N pwr.t.test(d=.2, n=N[1]) -pwr.t.test(d=.5, n=N[2]) -pwr.t.test(d=.8, n=N[3]) #------------------------------------------------