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
tidy_bernoulli<-function(.n=50, .prob=0.1, .num_sims=1, .return_tibble=TRUE) {
# Argumentsn<- as.integer(.n)
num_sims<- as.integer(.num_sims)
pr<- as.numeric(.prob)
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 (pr<0|pr>1) {
rlang::abort(
message="The '.prob' parameter must have an argument between 0 and 1 inclusive",
use_cli_format=TRUE
)
}
# Create a data.table with one row per simulationdf<-data.table::CJ(sim_number=factor(1:num_sims), x=1:n)
# Group the data by sim_number and add columns for x and ydf[, y:=stats::rbinom(n=.N, size=1, prob=pr)]
# Compute the density of the y values and add columns for dx and dydf[, 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 pdf[, p:=stats::pbinom(y, size=1, prob=pr)]
# Compute the q-values for the p-values and add a column for qdf[, q:=stats::qbinom(p, size=1, prob=pr)]
if(.return_tibble){
df<-dplyr::as_tibble(df)
} else {
data.table::setkey(df, NULL)
}
param_grid<-dplyr::tibble(pr)
# Attach descriptive attributes to tibble
attr(df, "distribution_family_type") <-"discrete"
attr(df, ".prob") <-.prob
attr(df, ".n") <-.n
attr(df, ".num_sims") <-.num_sims
attr(df, ".ret_tbl") <-.return_tibble
attr(df, "tibble_type") <-"tidy_bernoulli"
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(
"Bernoulli",
"",
paste0(
"c(",
paste(param_grid[, names(param_grid)], collapse=", "),
")"
)
)
return(df)
}
Function Example:
Example Output
The text was updated successfully, but these errors were encountered: