-
Notifications
You must be signed in to change notification settings - Fork 12
/
redesign.R
74 lines (72 loc) · 2.57 KB
/
redesign.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#' Redesign
#'
#' \code{redesign} quickly generates a design from an existing one by resetting symbols used in design handler parameters in a step's environment (Advanced).
#'
#' Warning: \code{redesign} will edit any symbol in your design, but if the symbol you attempt to change does not exist in a step's environment no changes will be made and no error or warning will be issued.
#'
#' Please note that \code{redesign} functionality is experimental and may be changed in future versions.
#'
#' @param design An object of class design.
#' @param ... Arguments to redesign e.g., \code{n = 100.} If redesigning multiple arguments, they must be specified as a named list.
#' @param expand If TRUE, redesign using the crossproduct of \code{...}, otherwise recycle them.
#' @return A design, or, in the case of multiple values being passed onto \code{...}, a `by`-list of designs.
#' @examples
#'
#' n <- 500
#' population <- declare_population(N = 1000)
#' sampling <- declare_sampling(n = n)
#' design <- population + sampling
#'
#' # returns a single, modified design
#' modified_design <- redesign(design, n = 200)
#'
#' # returns a list of six modified designs
#' design_vary_N <- redesign(design, n = seq(400, 900, 100))
#'
#' # When redesigning with arguments that are vectors,
#' # use list() in redesign, with each list item
#' # representing a design you wish to create
#'
#' prob_each <- c(.1, .5, .4)
#'
#' assignment <- declare_assignment(prob_each = prob_each)
#'
#' design <- population + assignment
#'
#' # returns two designs
#'
#' designs_vary_prob_each <- redesign(
#' design,
#' prob_each = list(c(.2, .5, .3), c(0, .5, .5)))
#'
#'
#' # To illustrate what does and does not get edited by redesign,
#' # consider the following three designs. In the first two, argument
#' # X is called from the step's environment; in the third it is not.
#' # Using redesign will alter the role of X in the first two designs
#' # but not the third one.
#'
#'X <- 3
#'f <- function(b, X) b*X
#'g <- function(b) b*X
#'
#'design1 <- declare_population(N = 1, A = X) + NULL
#'design2 <- declare_population(N = 1, A = f(2, X)) + NULL
#'design3 <- declare_population(N = 1, A = g(2)) + NULL
#'
#'draw_data(design1)
#'draw_data(design2)
#'draw_data(design3)
#'
#'draw_data(redesign(design1, X=0))
#'draw_data(redesign(design2, X=0))
#'draw_data(redesign(design3, X=0))
#' @export
redesign <- function(design, ..., expand = TRUE) {
check_design_class_single(design)
f <- function(...) {
clone_design_edit(design, ...)
}
design <- expand_design(f, ..., expand = expand)
structure(design, code = NULL)
}