You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adding the .num_sims parameter to the tidy_empirical() will allow bootstrapping of the values, this keeps it inline with the rest of the tidy_ distribution functions.
Check for downstream effects, as they are yet unknown.
Function:
tidy_empirical<-function(.x, .num_sims=1, .distribution_type="continuous") {
x_term<-.xn<- 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 Pe<-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)
}
Example:
>x<-mtcars$mpg> tidy_empirical(x, .num_sims=1)
# A tibble: 32 x 7sim_numberxydxdypq<fct><int><dbl><dbl><dbl><dbl><dbl>111212.970.0001140.62510.4212214.210.0004550.62510.431322.85.440.001420.78113.341421.46.680.003550.68814.351518.77.920.007210.46914.761618.19.160.01240.4381571714.310.40.01920.12515.281824.411.60.02810.81215.291922.812.90.03950.78115.51011019.214.10.05160.53115.8# ... with 22 more rows>> tidy_empirical(x, .num_sims=10)
# A tibble: 320 x 7sim_numberxydxdypq<fct><int><dbl><dbl><dbl><dbl><dbl>11119.211.20.0002830.5311521218.112.10.001870.4381531333.913.00.00801115.54142113.80.02240.62515.851521.414.70.04160.68815.861633.915.50.0531117.371718.716.40.05160.46918.18182117.20.04760.62518.791915.818.10.05080.31219.21011021.418.90.06270.68819.2# ... with 310 more rows>> tidy_empirical(x, .num_sims=10) %>%
+ tidy_distribution_summary_tbl(sim_number)
# A tibble: 10 x 11sim_numbermean_valmedian_valstd_valmin_valmax_valskewnesskurtosisrangeiqr<fct><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>1120.220.45.0910.432.40.4232.842272220.419.46.3210.433.90.7432.7223.57.63319.818.16.1510.432.40.8762.86226.024420.918.46.6614.333.91.002.5519.65.755519.118.05.8210.433.90.6642.9023.57.66618.016.83.9810.430.40.8894.26205.87719.618.45.7410.432.40.8683.02226.328818.816.85.9910.433.90.7382.8923.58.19918.218.44.4310.430.40.4483.65205.8101017.916.86.2610.432.40.9733.48225.3# ... with 1 more variable: variance <dbl>
The text was updated successfully, but these errors were encountered:
Adding the
.num_sims
parameter to thetidy_empirical()
will allow bootstrapping of the values, this keeps it inline with the rest of thetidy_
distribution functions.Check for downstream effects, as they are yet unknown.
Function:
Example:
The text was updated successfully, but these errors were encountered: