-
Notifications
You must be signed in to change notification settings - Fork 1
/
random-tidy-triangular.R
130 lines (113 loc) · 4.03 KB
/
random-tidy-triangular.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
#' Generate Tidy Data from Triangular Distribution
#'
#' @family Continuous Distribution
#' @family Triangular
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description This function generates tidy data from the triangular distribution.
#'
#' @details The function takes parameters for the triangular distribution
#' (minimum, maximum, mode), the number of x values (`n`), the number of
#' simulations (`num_sims`), and an option to return the result as a tibble
#' (`return_tibble`). It performs various checks on the input parameters to ensure
#' validity. The result is a data frame or tibble with tidy data for
#' further analysis.
#'
#' @param .n The number of x values for each simulation.
#' @param .min The minimum value of the triangular distribution.
#' @param .max The maximum value of the triangular distribution.
#' @param .mode The mode (peak) value of the triangular distribution.
#' @param .num_sims The number of simulations to perform.
#' @param .return_tibble A logical value indicating whether to return the result
#' as a tibble. Default is TRUE.
#'
#' @examples
#' tidy_triangular(.return_tibble = TRUE)
#'
#' @return
#' A tibble of randomly generated data.
#'
#' @name tidy_triangular
NULL
#' @export
#' @rdname tidy_triangular
tidy_triangular <- function(.n = 50, .min = 0, .max = 1,
.mode = 1/2, .num_sims = 1, .return_tibble = TRUE){
# Arguments
n <- as.integer(.n)
num_sims <- as.integer(.num_sims)
mn <- as.numeric(.min)
mx <- as.numeric(.max)
md <- as.numeric(.mode)
ret_tbl <- as.logical(.return_tibble)
# Checks ----
if (!is.integer(n) | n < 0) {
rlang::abort(
message = "The parameters '.n' must be of class integer. Please pass a whole
number like 50 or 100. It must be greater than 0.",
use_cli_format = TRUE
)
}
if (!is.integer(num_sims) | num_sims < 0) {
rlang::abort(
message = "The parameter `.num_sims' must be of class integer. Please pass a
whole number like 50 or 100. It must be greater than 0.",
use_cli_format = TRUE
)
}
if (mn > mx){
rlang::abort(
message = "The parameters .min and .max must satisfy .min < .max",
use_cli_format = TRUE
)
}
if (md < mn || md > mx){
rlang::abort(
message = "The parameters must follow .min <= .mode <= .max",
use_cli_format = TRUE
)
}
# Create a data.table with one row per simulation
df <- data.table::CJ(sim_number = factor(1:num_sims), x = 1:n)
# Group the data by sim_number and add columns for x and y
df[, y := EnvStats::rtri(n = .N, min = mn, max = mx, mode = md)]
# Compute the density of the y values and add columns for dx and dy
df[, c("dx", "dy") := density(y, n = n)[c("x", "y")], by = sim_number]
# Compute the p-values for the y values and add a column for p
df[, p := EnvStats::ptri(y, min = mn, max = mx, mode = md)]
# Compute the q-values for the p-values and add a column for q
df[, q := EnvStats::qtri(p, min = mn, max = mx, mode = md)]
if(.return_tibble){
df <- dplyr::as_tibble(df)
} else {
data.table::setkey(df, NULL)
}
# Create tibble of parameter grid
param_grid <- dplyr::tibble(mn, mx, md)
# Attach descriptive attributes to tibble
attr(df, "distribution_family_type") <- "continuous"
attr(df, ".min") <- .min
attr(df, ".max") <- .max
attr(df, ".mode") <- .mode
attr(df, ".n") <- .n
attr(df, ".num_sims") <- .num_sims
attr(df, ".ret_tbl") <- .return_tibble
attr(df, "tibble_type") <- "tidy_triangular"
attr(df, "param_grid") <- param_grid
attr(df, "param_grid_txt") <- paste0(
"c(",
paste(param_grid[, names(param_grid)], collapse = ", "),
")"
)
attr(df, "dist_with_params") <- paste0(
"Triangular",
" ",
paste0(
"c(",
paste(param_grid[, names(param_grid)], collapse = ", "),
")"
)
)
return(df)
}