-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_distribution.R
274 lines (210 loc) · 10.2 KB
/
random_distribution.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#' Helper function: distributon repeater
#' @param number_of_repeatings how often should the random distribution with the
#' same parameters be generated (default: 1)
#' @param number_of_events number of events
#' @param func distribution function to be repeated (e.g. runif, rlnorm, rnorm)
#' @param ... further parameters passed to func
#' @return data.frame with columns repeatID, eventID and values
#' @export
#' @examples
#' distribution_repeater(number_of_repeatings = 2,
#' number_of_events = 10,
#' func = runif,
#' min = 1,
#' max = 10)
distribution_repeater <- function(number_of_repeatings = 10,
number_of_events = 365,
func,
... ) {
repl_tmp <- vapply(seq_len(number_of_repeatings),
function(x) {func(n = number_of_events,
...)},FUN.VALUE = numeric(number_of_events))
if(number_of_events == 1) repl_tmp <- t(repl_tmp)
repl <- repl_tmp %>%
as.data.frame() %>%
cbind(eventID = seq_len(number_of_events))
colnames(repl) <- c(seq_len(number_of_repeatings), "eventID")
repl_list <- tidyr::gather_(data = repl,
key_col = "repeatID",
value_col = "values",
gather_cols = as.character(seq_len(number_of_repeatings)))
repl_list$repeatID <- as.integer(repl_list$repeatID)
return(repl_list[,c("repeatID","eventID", "values")])
}
#' Create random distribution
#' @param type "uniform" calls runif(), "triangle" calls EnvStats::rtri(), "lognorm"
#' calls rlnorm() and "norm" calls rnorm(), (default: "uniform")
#' @param number_of_repeatings how often should the random distribution with the
#' same parameters be generated (default: 1)
#' @param number_of_events number of events
#' @param value constant value (no random number), gets repeated number_of_events
#' times (if 'type' = 'value')
#' @param min minimum value (default: 10), only used if 'type' is "runif" or
#' "triangle"
#' @param max maximum value (default: 1000), only used if 'type' is "runif" or
#' "triangle"
#' @param mean mean value (default: mean of min & max value), only used if 'type'
#' is "norm"
#' @param sdev standard deviation (default: (max-mean) / qnorm(0.975)),
#' only used if 'type' is "norm"
#' @param meanlog log mean value (default: mean of log min & max value), only
#' used if 'type' is "lognorm"
#' @param sdlog standard deviation (default: sd of log min & max value), only
#' used if 'type' is "lognorm"
#' @param mode (default: mean of min & max), only used if 'type' is "triangle"
#' @param debug print debug information (default: TRUE)
#' @return list with parameters of user defined random distribution and
#' corresponding values
#' @export
#' @importFrom stats sd qnorm runif rnorm rlnorm
#' @seealso for random triangle see \code{\link{rtri}}
create_random_distribution <- function(type = "uniform",
number_of_repeatings = 1,
number_of_events = 365,
value = 10,
min = 10,
max = 1000,
mean = (min + max)/2,
sdev = (max - mean) / qnorm(0.975),
meanlog = mean(log((min + max)/2)),
sdlog = sd(log((c(min,max)))),
mode = (min + max)/2,
debug = TRUE) {
if (type == "value") {
if (debug) {
cat(sprintf("Replicate %d times constant value %f\n",
number_of_events * number_of_repeatings,
value)) }
values <- rep(value,
times = number_of_events * number_of_repeatings)
events <- data.frame(repeatID = lapply(1:number_of_repeatings,
function(x) { rep(x, number_of_events)}) %>%
unlist(),
eventID = rep(1:number_of_events,number_of_repeatings),
values = rep(value, number_of_events * number_of_repeatings))
paras <- data.frame(repeatings = number_of_repeatings,
events = number_of_events,
value = value)
}
else if (type == "uniform") {
if (debug) {
cat(sprintf("Create %d random distribution(s): uniform (with parameters n: %d, min: %f, max: %f)\n",
number_of_repeatings,
number_of_events,
min,
max)) }
events <- distribution_repeater(number_of_repeatings = number_of_repeatings,
number_of_events = number_of_events,
func = runif,
min = min,
max = max)
paras <- data.frame(repeatings = number_of_repeatings,
events = number_of_events,
min = min,
max = max)
}
else if (type == "norm") {
if (debug) {
cat(sprintf("Create %d random distribution(s): norm (with parameters n: %d, mean: %f, sd: %f)\n",
number_of_repeatings,
number_of_events,
mean,
sdev)) }
events <- distribution_repeater(number_of_repeatings = number_of_repeatings,
number_of_events = number_of_events,
func = rnorm,
mean = mean,
sd = sdev)
paras <- data.frame(repeatings = number_of_repeatings,
events = number_of_events,
mean = mean,
sd = sdev)
}
else if (type == "lognorm") {
if (debug) {
cat(sprintf("Create %d random distribution(s): lognorm (with parameters n: %d, meanlog: %f, sdlog: %f)\n",
number_of_repeatings,
number_of_events,
meanlog,
sdlog)) }
events <- distribution_repeater(number_of_repeatings = number_of_repeatings,
number_of_events = number_of_events,
func = rlnorm,
meanlog = meanlog,
sdlog = sdlog)
paras <- data.frame(repeatings = number_of_repeatings,
events = number_of_events,
meanlog = meanlog,
sdlog = sdlog)
} else if (type == "triangle") {
if (debug) {
cat(sprintf("Create %d random distribution(s): triangle (with parameters n: %d, min: %f, max: %f, mode = %f)\n",
number_of_repeatings,
number_of_events,
min,
max,
mode)) }
events <- distribution_repeater(number_of_repeatings = number_of_repeatings,
number_of_events = number_of_events,
func = EnvStats::rtri,
min = min,
max = max,
mode = mode)
paras <- data.frame(repeatings = number_of_repeatings,
events = number_of_events,
min = min,
max = max,
mode = mode)
} else {
stop(sprintf("Your value for parameter 'type' = %s is not an implemented distribution.
Valid values of 'type' are: 'uniform', 'triangle', 'norm' or 'lognorm'", type))
}
paras <- data.frame(type = type, paras)
dist <- list(events = events,
paras = paras)
return(dist)
}
#' Create random distribution based on configuration file
#' @param config as retrieved by config_read()
#' @param number_of_repeatings how often should the random distribution with the
#' same parameters be generated (default: 1)
#' @param number_of_events number of events
#' @param debug print debug information (default: TRUE)
#' @return list random distributions based on configuration file
#' @export
generate_random_values <- function(config,
number_of_repeatings = 1,
number_of_events,
debug = TRUE) {
#### Set defaults based on min/max for function create_random_distribution() if
#### values are missing
if (config$type == "norm") {
if (is.na(config$mean)) config$mean <- (config$max + config$min)/2
if (is.na(config$sd)) config$sd <- sd(c(config$mean, config$max))/2
}
if (config$type == "lnorm") {
if (is.na(config$meanlog)) config$meanlog <- mean(log((config$min + config$max)/2))
if (is.na(config$sdlog)) config$sdlog <- sd(log((c(config$min,config$max))))
}
if (config$type == "triangle") {
if (is.na(config$mode)) {
if(config$min == config$max) {
cat("Distribution set from 'triangle' to 'uniform' because 'min' equals 'max'\n")
config$type <- "uniform"
} else {
config$mode <- (config$min + config$max)/2
}}}
random <- create_random_distribution(type = config$type,
number_of_repeatings = number_of_repeatings,
number_of_events = number_of_events,
value = config$value,
min = config$min,
max = config$max,
mean = config$mean,
sdev = config$sd,
meanlog = config$meanlog,
sdlog = config$sdlog,
mode = config$mode,
debug = debug)
return(random)
}