-
Notifications
You must be signed in to change notification settings - Fork 1
/
empirical-tidy.R
139 lines (129 loc) · 3.84 KB
/
empirical-tidy.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
#' Tidy Empirical
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details This function takes in a single argument of .x a vector
#'
#' @description This function takes in a single argument of .x a vector and will
#' return a tibble of information similar to the `tidy_` distribution functions.
#' The `y` column is set equal to `dy` from the density function.
#'
#' @param .x A vector of numbers
#' @param .num_sims How many simulations should be run, defaults to 1.
#' @param .distribution_type A string of either "continuous" or "discrete". The
#' function will default to "continuous"
#'
#' @examples
#' x <- mtcars$mpg
#' tidy_empirical(.x = x, .distribution_type = "continuous")
#' tidy_empirical(.x = x, .num_sims = 10, .distribution_type = "continuous")
#'
#' @return
#' A tibble
#'
#' @export
#'
tidy_empirical <- function(.x, .num_sims = 1, .distribution_type = "continuous") {
x_term <- .x
n <- length(x_term)
dist_type <- tolower(as.character(.distribution_type))
num_sims <- as.integer(.num_sims)
if (!is.vector(x_term)) {
rlang::abort("You must pass a vector as the .x argument to this function.")
}
if (!dist_type %in% c("continuous", "discrete")) {
rlang::abort("You must choose either 'continuous' or 'discrete'.")
}
## New P
e <- stats::ecdf(x_term)
df <- dplyr::tibble(sim_number = as.factor(1:num_sims)) |>
dplyr::group_by(sim_number) |>
dplyr::mutate(x = list(1:n)) |>
dplyr::mutate(y = ifelse(
num_sims == 1,
list(x_term),
list(sample(x_term, replace = TRUE))
)) |>
dplyr::mutate(d = list(density(unlist(y), n = n)[c("x", "y")] |>
purrr::set_names("dx", "dy") |>
dplyr::as_tibble())) |>
dplyr::mutate(p = list(e(unlist(y)))) |>
dplyr::mutate(q = NA) |>
tidyr::unnest(cols = c(x, y, d, p, q)) |>
dplyr::ungroup()
q_vec <- df |>
dplyr::select(sim_number, y) |>
dplyr::group_by(sim_number) |>
dplyr::mutate(
q = rep(
stats::quantile(y, probs = seq(0, 1, 1 / (n - 1)), type = 1),
1
)
) |>
dplyr::ungroup() |>
dplyr::select(q)
df <- df |>
dplyr::mutate(q = q_vec$q)
# Attach descriptive attributes to tibble
attr(df, "distribution_family_type") <- dist_type
attr(df, ".x") <- .x
attr(df, ".n") <- n
attr(df, ".num_sims") <- num_sims
attr(df, "tibble_type") <- "tidy_empirical"
attr(df, "dist_with_params") <- "Empirical"
# Return ----
return(df)
}
# tidy_empirical <- function(.x) {
# x_term <- .x
# n <- length(x_term)
#
# if (!is.vector(x_term)) {
# rlang::abort("You must pass a vector as the .x arguemtn to this function.")
# }
#
# dens_obj <- stats::density(1:n, n = n)
# dft <- data.frame(x = double(), y = double())
# for (i in 1:n) {
# lower <- i - 1
# upper <- i
# y <- stats::integrate(
# stats::approxfun(dens_obj),
# lower = lower,
# upper = upper
# )$value
# tmp <- data.frame(x = i, y = y)
# dft <- rbind(dft, tmp)
# }
#
# p_vec <- dft$y
#
# df <- dplyr::tibble(sim_number = as.factor(1)) |>
# dplyr::group_by(sim_number) |>
# dplyr::mutate(x = list(1:n)) |>
# dplyr::mutate(y = NA) |>
# dplyr::mutate(d = list(density(unlist(x), n = n)[c("x", "y")] |>
# purrr::set_names("dx", "dy") |>
# dplyr::as_tibble())) |>
# dplyr::mutate(p = list(p_vec)) |>
# dplyr::mutate(q = NA) |>
# dplyr::mutate(y = list(d[[1]][["dy"]])) |>
# tidyr::unnest(cols = c(x, y, d, p, q)) |>
# dplyr::ungroup()
#
# q_vec <- stats::quantile(df$y, probs = seq(0, 1, 1 / (n - 1)), type = 1) |>
# dplyr::as_tibble() |>
# dplyr::rename("q" = "value")
#
# df <- df |>
# dplyr::mutate(q = q_vec$q)
#
# attr(df, ".x") <- .x
# attr(df, ".n") <- n
# attr(df, ".num_sims") <- 1L
# attr(df, "tibble_type") <- "tidy_empirical"
# attr(df, "dist_with_params") <- "Empirical"
#
# # Return ----
# return(df)
# }