Skip to content

Commit

Permalink
Fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Jan 18, 2022
1 parent 0af22db commit 93097ea
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions R/random-tidy-rnorm.R → R/random-tidy-normal.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#' @seealso \url{https://data-flair.training/blogs/normal-distribution-in-r/}
#'
#' @details This function uses the underlying `stats::rnorm()`, `stats::pnorm()`,
#' and `stats::qnorm()` functions to generate data from the given parameters.
#' and `stats::qnorm()` functions to generate data from the given parameters. For
#' more information please see [stats::rnorm()]
#'
#' @description This function will generate `n` random points from a Gaussian
#' distribution with a user provided, mean, standard deviation and number of
Expand All @@ -17,6 +18,16 @@
#'
#' The data is returned un-grouped.
#'
#' The columns that are output are:
#'
#' - `sim_number` The current simulation number.
#' - `x` The current value of `n` for the current simulation.
#' - `y` The randomly generated data point.
#' - `dx` The `x` value from the [stats::density()] function.
#' - `dy` The `y` value from the [stats::density()] function.
#' - `p` The values from the resulting p_ function of the distribution family.
#' - `q` The values from the resulting q_ function of the distribution family.
#'
#' @param .n The number of randomly generated points you want.
#' @param .mean The mean of the randomly generated data.
#' @param .sd The standard deviation of the randomly generated data.
Expand Down Expand Up @@ -77,10 +88,12 @@ tidy_normal <- function(.n = 50, .mean = 0, .sd = 1, .num_sims = 1){
dplyr::group_by(sim_number) %>%
dplyr::mutate(x = list(1:n)) %>%
dplyr::mutate(y = list(stats::rnorm(n, mu, std))) %>%
dplyr::mutate(d_norm = list(stats::dnorm(unlist(y), mu, std))) %>%
dplyr::mutate(p_norm = list(stats::pnorm(ps, mu, std))) %>%
dplyr::mutate(q_norm = list(stats::qnorm(qs, mu, std))) %>%
tidyr::unnest(cols = c(x, y, d_norm, p_norm, q_norm)) %>%
dplyr::mutate(d = list(density(unlist(y), n = n)[c("x","y")] %>%
purrr::set_names("dx","dy") %>%
dplyr::as_tibble())) %>%
dplyr::mutate(p = list(stats::pnorm(ps, mu, std))) %>%
dplyr::mutate(q = list(stats::qnorm(qs, mu, std))) %>%
tidyr::unnest(cols = c(x, y, d, p, q)) %>%
dplyr::ungroup()


Expand Down

0 comments on commit 93097ea

Please sign in to comment.