R/tidy_gam.R

Defines functions tidy_gam

Documented in tidy_gam

#' tidy_gam
#'
#' Wrapped for 2x broom::tidy as applied to a GAM object, pulling both parametric and smooth terms.
#'
#' @param x GAM model object
#' @param conf.level Confidence level for pulling confidence intervals from parametric terms.
#'
#' @return Dataframe with model/term metrics.
#' @export
#'
#' @examples
#' \dontrun{
#' }
#'
#'


tidy_gam <- function(x,
                     conf.level = 0.95) {
  .model <-
    x
  .conf.level <-
    conf.level

  .parametric <-
    .model |>
    broom::tidy(parametric = TRUE,
                conf.int = TRUE,
                conf.level = .conf.level) |>
    dplyr::mutate(term.type = "Parametric")

  .nonparametric <-
    .model |>
    broom::tidy(parametric = FALSE) |>
    dplyr::mutate(term.type = "Smooth")

  output <-
    .parametric |>
    dplyr::bind_rows(.nonparametric) |>
    dplyr::select(term,
                  term.type,
                  estimate,
                  std.error,
                  edf,
                  ref.df,
                  statistic,
                  p.value,
                  conf.low,
                  conf.high)

  return(output)

}
toddellis/miao documentation built on June 1, 2025, 10:11 p.m.