Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Between and Within Power Arguments #108

Merged
merged 34 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
61cf4a8
extract model fitting master function
lebebr01 Sep 27, 2023
804220b
between conditions parse_varyargs
lebebr01 Sep 27, 2023
2c9e2c7
write new within parse arguments function
lebebr01 Sep 27, 2023
d2fd3c0
first stab at within structure
lebebr01 Sep 27, 2023
921f9f5
only need model fit within parse
lebebr01 Sep 27, 2023
2b8a6bc
Add new parse function doc
lebebr01 Sep 27, 2023
ae3c315
add function to namespace
lebebr01 Sep 27, 2023
9d800d8
only include model_fit within
lebebr01 Sep 27, 2023
aee7044
restructure within ids
lebebr01 Sep 27, 2023
06fb133
nested simglm functions
lebebr01 Sep 27, 2023
b82c236
restructure replications
lebebr01 Sep 27, 2023
a1b233c
restructure replications
lebebr01 Sep 27, 2023
ecae764
lapply merge
lebebr01 Sep 27, 2023
f819f38
restructure within output
lebebr01 Sep 27, 2023
5e25b67
continued restructuring
lebebr01 Sep 27, 2023
fa38b76
use left_join dplyr
lebebr01 Sep 29, 2023
1585a77
add argument parse_varyarguments_w
lebebr01 Sep 29, 2023
f254e67
specify new arg name
lebebr01 Sep 29, 2023
35ab819
add argument to docs
lebebr01 Sep 29, 2023
94c6d9f
fix wrong function name
lebebr01 Sep 29, 2023
e2fed5a
update spacing
lebebr01 Oct 9, 2023
51bc311
structure to parse power list
lebebr01 Oct 9, 2023
39ca860
remove line
lebebr01 Oct 31, 2023
f2ea588
good start power within structure
lebebr01 Nov 17, 2023
9c2ff5a
add power names
lebebr01 Nov 17, 2023
470207a
add df name
lebebr01 Nov 17, 2023
6431955
one more name adjustment
lebebr01 Nov 17, 2023
630d6f2
remove row names cbind
lebebr01 Nov 17, 2023
fd4fd5e
adjust tests for now
lebebr01 Nov 17, 2023
3947c97
remove type I error argument for now
lebebr01 Nov 17, 2023
405bbdf
remove old simglm package doc
lebebr01 Nov 17, 2023
b1690a7
add new simglm package information
lebebr01 Nov 17, 2023
9b83f5c
update docs
lebebr01 Nov 17, 2023
058aeb6
adjust package doc alias
lebebr01 Nov 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(parse_multiplemember)
export(parse_power)
export(parse_randomeffect)
export(parse_varyarguments)
export(parse_varyarguments_w)
export(random_missing)
export(rbimod)
export(replicate_simulation)
Expand Down
1 change: 0 additions & 1 deletion R/fixef_sim.r
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ sim_factor2 <- function(n, levels, var_level = 1, replace = TRUE,
}
}


cat_var <- factor(cat_var, levels = levels)

cat_var
Expand Down
47 changes: 45 additions & 2 deletions R/parse_formula.r
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ parse_power <- function(sim_args, samp_size) {

}

#' Parse varying arguments
#' Parse between varying arguments
#'
#' @param sim_args A named list with special model formula syntax. See details and examples
#' for more information. The named list may contain the following:
Expand All @@ -196,7 +196,50 @@ parse_power <- function(sim_args, samp_size) {
#' @export
parse_varyarguments <- function(sim_args) {

conditions <- expand.grid(sim_args[['vary_arguments']], KEEP.OUT.ATTRS = FALSE)
conditions <- expand.grid(list_select(sim_args[['vary_arguments']],
names = c('model_fit', 'power'),
exclude = TRUE),
KEEP.OUT.ATTRS = FALSE)
if(any(sapply(conditions, is.list))) {
loc <- sapply(conditions, is.list)
simp_conditions <- conditions[loc != TRUE]
list_conditions <- conditions[loc == TRUE]
list_conditions <- lapply(seq_along(list_conditions), function(xx)
unlist(list_conditions[xx], recursive = FALSE))
for(tt in seq_along(list_conditions)) {
names(list_conditions[[tt]]) <- gsub("[0-9]*", "", names(list_conditions[[tt]]))
}
lapply(1:nrow(conditions), function(xx) c(sim_args,
simp_conditions[xx, , drop = FALSE],
do.call('c', lapply(seq_along(list_conditions), function(tt)
list_conditions[[tt]][xx]))
))
} else {
lapply(1:nrow(conditions), function(xx) c(sim_args,
conditions[xx, , drop = FALSE]))
}

}

#' Parse within varying arguments
#'
#' @param sim_args A named list with special model formula syntax. See details and examples
#' for more information. The named list may contain the following:
#' \itemize{
#' \item fixed: This is the fixed portion of the model (i.e. covariates)
#' \item random: This is the random portion of the model (i.e. random effects)
#' \item error: This is the error (i.e. residual term).
#' }
#' @param name The name of the within simulation condition. This is primarily
#' an internal function.
#'
#' @export
parse_varyarguments_w <- function(sim_args, name) {

conditions <- expand.grid(list_select(sim_args[['vary_arguments']],
names = name,
exclude = FALSE),
KEEP.OUT.ATTRS = FALSE)
if(any(sapply(conditions, is.list))) {
loc <- sapply(conditions, is.list)
simp_conditions <- conditions[loc != TRUE]
Expand Down
124 changes: 108 additions & 16 deletions R/pow_sim.r
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ tidy_mixed <- function(model) {
#' \code{\link[future.apply:future_lapply]{future_replicate}}.
#' @param ... Currently not used.
#' @importFrom future.apply future_replicate
#' @importFrom dplyr left_join
#' @export
replicate_simulation <- function(sim_args, return_list = FALSE,
future.seed = TRUE, ...) {
Expand All @@ -123,7 +124,8 @@ replicate_simulation <- function(sim_args, return_list = FALSE,
sim_args[['replications']] <- 1
}
future.apply::future_replicate(sim_args[['replications']],
simglm(sim_args),
simglm_modelfit(simglm(sim_args),
sim_args),
simplify = FALSE,
future.seed = future.seed)
} else {
Expand All @@ -141,7 +143,7 @@ replicate_simulation_vary <- function(sim_args, return_list = FALSE,
}

within_conditions <- list_select(sim_args[['vary_arguments']],
names = c('model_fit', 'power'),
names = c('model_fit'),
exclude = FALSE)
between_conditions <- list_select(sim_args[['vary_arguments']],
names = c('model_fit', 'power'),
Expand All @@ -154,13 +156,30 @@ replicate_simulation_vary <- function(sim_args, return_list = FALSE,

sim_arguments <- parse_varyarguments(sim_args)

power_out <- future.apply::future_lapply(seq_along(sim_arguments), function(xx) {
simulation_out <- future.apply::future_lapply(seq_along(sim_arguments), function(xx) {
future.apply::future_replicate(sim_arguments[[xx]][['replications']],
simglm(sim_arguments[[xx]]),
simplify = FALSE,
future.seed = future.seed)
}, future.seed = future.seed)

if(length(within_conditions_name) > 0) {
sim_arguments_w <- parse_varyarguments_w(sim_args, name = c('model_fit'))

power_out <- future.apply::future_lapply(seq_along(simulation_out), function(xx) {
future.apply::future_lapply(seq_along(simulation_out[[xx]]), function(yy) {
future.apply::future_lapply(seq_along(sim_arguments_w), function(zz) {
simglm_modelfit(simulation_out[[xx]][[yy]],
sim_arguments_w[[zz]])
}, future.seed = future.seed)
}, future.seed = future.seed)
}, future.seed = future.seed)
}
if(length(within_conditions_name) == 0) {
power_out <- simulation_out
}


if(return_list) {
return(power_out)
} else {
Expand All @@ -172,14 +191,45 @@ replicate_simulation_vary <- function(sim_args, return_list = FALSE,
rep_id <- lapply(seq_along(num_rows), function(xx)
rep(1:sim_args[['replications']],
each = num_rows[xx]/sim_args[['replications']]))

power_list <- lapply(seq_along(sim_arguments), function(xx)
data.frame(between_conditions_name[xx, , drop = FALSE],
replication = rep_id[[xx]],
power_df[[xx]],
row.names = NULL

if(length(within_conditions_name) > 0) {
num_terms <- lapply(seq_along(power_out), function(xx)
lapply(seq_along(power_out[[xx]]), function(yy)
lapply(power_out[[xx]][[yy]], nrow))
)
)
within_id <- rep(rep(seq_along(sim_arguments_w),
unique(unlist(num_terms))),
sim_args[['replications']])

within_df <- data.frame(
within_id = unique(within_id),
within_names = within_conditions_name
)

power_list <- lapply(seq_along(sim_arguments), function(xx)
data.frame(between_conditions_name[xx, , drop = FALSE],
replication = rep_id[[xx]],
within_id = within_id,
power_df[[xx]],
row.names = NULL
)
)

power_list <- lapply(seq_along(power_list), function(xx)
dplyr::left_join(power_list[[xx]],
within_df,
by = 'within_id')
)
} else {
power_list <- lapply(seq_along(sim_arguments), function(xx)
data.frame(between_conditions_name[xx, , drop = FALSE],
replication = rep_id[[xx]],
power_df[[xx]],
row.names = NULL
)
)
}


power_list
}
Expand Down Expand Up @@ -226,7 +276,22 @@ compute_statistics <- function(data, sim_args, power = TRUE,
samp_size <- sim_args[['sample_size']]
}

power_args <- parse_power(sim_args, samp_size)
if(is.null(sim_args[['power']])) {
sim_arguments_w <- parse_varyarguments_w(sim_args, name = 'power')
within_conditions <- list_select(sim_args[['vary_arguments']],
names = c('power'),
exclude = FALSE)

within_conditions_name <- data.frame(sapply(expand.grid(within_conditions, KEEP.OUT.ATTRS = FALSE),
as.character))


power_args <- lapply(seq_along(sim_arguments_w), function(xx)
parse_power(sim_arguments_w[[xx]], samp_size)
)
} else {
power_args <- parse_power(sim_args, samp_size)
}

if(is.null(sim_args[['model_fit']][['reg_weights_model']])) {
reg_weights <- sim_args[['reg_weights']]
Expand All @@ -238,20 +303,47 @@ compute_statistics <- function(data, sim_args, power = TRUE,

data_list <- split(data_df, f = data_df['term'])

data_list <- lapply(seq_along(data_list), function(xx) {
compute_power(data_list[[xx]], power_args[[xx]])
if(is.null(sim_args[['power']])) {
data_list <- lapply(seq_along(sim_arguments_w), function(yy) {
lapply(seq_along(data_list), function(xx) {
cbind(compute_power(data_list[[xx]], power_args[[yy]][[xx]]),
power_arg = within_conditions_name[yy, ], row.names = NULL)
}
)
}
)

# data_list <- lapply(seq_along(sim_arguments_w), function(yy) {
# lapply(seq_along(data_list), function(xx) {
# compute_t1e(data_list[[xx]], power_args[[yy]][[xx]], reg_weights = reg_weights[xx])
# }
# )
# }
# )
} else {
data_list <- lapply(seq_along(data_list), function(xx) {
compute_power(data_list[[xx]], power_args[[xx]])
})
data_list <- lapply(seq_along(data_list), function(xx) {
compute_t1e(data_list[[xx]], power_args[[xx]], reg_weights = reg_weights[xx])
})
data_list <- lapply(seq_along(data_list), function(xx) {
compute_t1e(data_list[[xx]], power_args[[xx]], reg_weights = reg_weights[xx])
})
}


data_df <- do.call("rbind", data_list)

if(!is.data.frame(data_df)) {
data_df <- do.call("rbind", data_df)
}

if(is.null(sim_args['vary_arguments'])) {
group_vars <- c('term')
} else {
group_vars <- c(names(expand.grid(sim_args[['vary_arguments']], KEEP.OUT.ATTRS = FALSE)),
'term')
if(any(group_vars %in% 'power')) {
group_vars <- gsub("power", "power_arg", group_vars)
}
}

avg_estimates <- aggregate_estimate(data_df,
Expand Down
7 changes: 7 additions & 0 deletions R/simglm-package.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
#' @aliases simglm-package
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL
11 changes: 0 additions & 11 deletions R/simglm.r

This file was deleted.

14 changes: 11 additions & 3 deletions R/simglm_master_function.r
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ simglm <- function(sim_args) {
data <- generate_missing(data, sim_args = sim_args)
}

data

}

simglm_modelfit <- function(data, sim_args) {
if(is.null(data)) {
stop('Must pass a valid data object')
}

if(!is.null(sim_args[['model_fit']])) {
data <- model_fit(data, sim_args = sim_args)
}

if(!is.null(sim_args[['extract_coefficients']])) {
data <- extract_coefficients(data)
}

data


data
}
4 changes: 2 additions & 2 deletions man/parse_varyarguments.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/parse_varyarguments_w.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/simglm-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading