Skip to content

Commit

Permalink
change autofill defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
emitanaka committed Nov 28, 2023
1 parent 87a70e5 commit 5f2e323
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions R/simulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ autofill_rcrds <- function(.data, ..., .seed = NULL, .nsim = 1L) {
default_opts <- with_variables()
list(type = ifelse(rname %in% names(vrcrds), vrcrds[[rname]]$record, NA),
rcrd_levels = if(rname %in% names(vrcrds)) vrcrds[[rname]]$values,
unit = uname,
unit = c(uname, sample(setdiff(unames, uname), size = sample(0:(length(unames) - 1), 1))),
trts = sample(tnames, size = sample(0:length(tnames), 1)),
rcrds = character(),
missing = default_opts$missing,
Expand Down Expand Up @@ -419,7 +419,7 @@ autofill_rcrds <- function(.data, ..., .seed = NULL, .nsim = 1L) {
"lessThanOrEqual" = vrcrds[[rname]]$value,
"lessThan" = vrcrds[[rname]]$value)
code_list <- effects_code(dep_fcts, .data)
code_adjust_y <- sprintf("edibble::rescale_values(y, lower = %f, upper = %f)", valid_lower, valid_upper)
code_adjust_y <- sprintf("rescale_values(y, lower = %f, upper = %f)", valid_lower, valid_upper)
}
} else if(deps[[rname]]$type=="factor") {
nlvls <- length(deps[[rname]]$rcrd_levels)
Expand All @@ -443,11 +443,10 @@ autofill_rcrds <- function(.data, ..., .seed = NULL, .nsim = 1L) {

process_functions <- c(process_functions, sprintf('
%s = function() {
set.seed(%d)\n%s
%s
%s
%s
}', rname,
random_seed_number(),
paste0(code_list$process_code, collapse = "\n"),
paste0(unlist(code_list$model_code), collapse = "\n "),
paste0(code_adjust_y, collapse = "\n")))
Expand Down Expand Up @@ -476,14 +475,14 @@ effects_code <- function(dep_fcts, .data, nlevels = 1) {
sprintf('%s_degree <- sample(1:%d, 1)', fct, ifelse(nfct > 5, 5, nfct - 1)),
paste(sprintf('%s_effects <- as.vector(poly(%s, %s_degree)', fct, fct, fct),
"%*%",
sprintf('rnorm(%s_degree, 0, 10))', fct)))
sprintf('rnorm(%s_degree, 0, %.1f))', fct, runif(1, 1, 10))))
code_list$model_code <- c(code_list$model_code, sprintf("%s_effects", fct))

# logical not accounted for
} else if(any(c("factor", "character") %in% fct_class)) {
code_list$process_code <- c(code_list$process_code,
sprintf(' %s_effects <- rnorm(%d, 0, 10)',
fct, nfct))
sprintf(' %s_effects <- rnorm(%d, 0, %.1f)',
fct, nfct, runif(1, 1, 10)))
code_list$model_code <- c(code_list$model_code, sprintf("%s_effects[index_levels(%s)]", fct, fct))
}
}
Expand All @@ -496,13 +495,13 @@ effects_code <- function(dep_fcts, .data, nlevels = 1) {
if("numeric" %in% fct_class) {
code_list$process_code <- c(code_list$process_code,
sprintf(' %s_degree <- lapply(1:%d, function(i) sample(1:%d, 1))', fct, nlevels, nfct),
sprintf(' %s_effects <- lapply(1:%d, function(i) as.vector(poly(%s, %s_degree[[i]]) %*% rnorm(%s_degree[[i]], 0, 10)))', fct, nlevels, fct, fct, fct))
sprintf(' %s_effects <- lapply(1:%d, function(i) as.vector(poly(%s, %s_degree[[i]]) %*% rnorm(%s_degree[[i]], 0, %.1f)))', fct, nlevels, fct, fct, fct, runif(1, 1, 10)))

code_list$model_code <- c(code_list$model_code, sprintf("%s_effects[[i]]", fct))
} else if(any(c("factor", "character") %in% fct_class)) {
code_list$process_code <- c(code_list$process_code,
sprintf(' %s_effects <- lapply(1:%d, function(i) rnorm(%d, 0, 10))',
fct, nlevels, nfct))
sprintf(' %s_effects <- lapply(1:%d, function(i) rnorm(%d, 0, %.1f))',
fct, nlevels, nfct, runif(1, 1, 10)))
code_list$model_code <- c(code_list$model_code, sprintf("%s_effects[[i]][index_levels(%s)]", fct, fct))
}
}
Expand Down

0 comments on commit 5f2e323

Please sign in to comment.