Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function tidy_rgamma() #4

Closed
spsanderson opened this issue Jan 17, 2022 · 0 comments
Closed

Add function tidy_rgamma() #4

spsanderson opened this issue Jan 17, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Jan 17, 2022

tidy_rgamma <- function(.n = 50, .shape = 1, .rate = 1, .num_sims = 1){
  
  # Tidyeval ----
  n        <- as.integer(.n)
  num_sims <- as.integer(.num_sims)
  shp <- .shape
  rte <- .rate
  
  # Checks ----
  if(!is.integer(n) | n < 0){
    rlang::abort(
      "The parameters '.n' must be of class integer. Please pass a whole
            number like 50 or 100. It must be greater than 0."
    )
  }
  
  if(!is.integer(num_sims) | num_sims < 0){
    rlang::abort(
      "The parameter `.num_sims' must be of class integer. Please pass a
            whole number like 50 or 100. It must be greater than 0."
    )
  }
  
  if(!is.numeric(shp) | shp < 0){
    rlang::abort(
      "The parameters of '.shapte' and '.rate' must be of class numeric.
            Please pass a numer like 1 or 1.1 etc. and must be greater than 0."
    )
  }
  
  if(!is.numeric(rte)){
    rlang::abort(
      "The parameters of '.shape' and '.rate' must be of class numeric.
            Please pass a numer like 1 or 1.1 etc."
    )
  }
  
  x <- seq(1, num_sims, 1)
  
  ps <- seq(-n, n-1, 2)
  qs <- seq(0, 1, (1/(n-1)))
  
  df <- dplyr::tibble(sim_number = as.factor(x)) %>%
    dplyr::group_by(sim_number) %>%
    dplyr::mutate(x = list(1:n)) %>%
    dplyr::mutate(y = list(stats::rgamma(n = n, shape = shp, rate = rte))) %>%
    dplyr::mutate(d_gamma = list(stats::dgamma(unlist(y), shape = shp, rate = rte))) %>%
    dplyr::mutate(p_gamma = list(stats::pgamma(ps, shape = shp, rate = rte))) %>%
    dplyr::mutate(q_gamma = list(stats::qgamma(qs, shape = shp, rate = rte))) %>%
    tidyr::unnest(cols = c(x, y, d_gamma, p_gamma, q_gamma)) %>%
    dplyr::ungroup()
  
  
  # Attach descriptive attributes to tibble
  attr(df, ".shape") <- .shape
  attr(df, ".rate") <- .rate
  attr(df, ".n") <- .n
  attr(df, ".num_sims") <- .num_sims
  attr(df, "tibble_type") <- "tidy_gamma"
  attr(df, "ps") <- ps
  attr(df, "qs") <- qs
  
  # Return final result as function output
  return(df)
  
}

tidy_rgamma()
@spsanderson spsanderson self-assigned this Jan 17, 2022
@spsanderson spsanderson added the enhancement New feature or request label Jan 17, 2022
@spsanderson spsanderson added this to the TidyDensity 0.0.1 milestone Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant