R/broom.R

Defines functions add_signif_col tidy_fit_pretty glance_fit_pretty

Documented in add_signif_col glance_fit_pretty tidy_fit_pretty

#' Add a significance column
#'
#' @description Add a significance column to the output of \code{broom::tidy()}.
#' @details This function adds the stars column seen in the output of a call
#' to \code{stats::summary()} for a fitted model object. The input to this function
#' should be the output from \code{broom::tidy()}. As an alternative, one may
#' simply call \code{teml::tidy_fit_pretty()}, which calls \code{broom::tidy()}.
#' @param data data.frame.
#' @param col_out character for SE version; symbol for NSE version. Name of significance column in \code{data} added to output.
#' @return data.frame.
#' @export
add_signif_col <-
  function(data = NULL,
           col_out = "signif") {
    stopifnot(!is.null(data), is.data.frame(data))
    stopifnot(is.character(col_out), length(col_out) == 1)
    col_out <- rlang::sym(col_out)
    p.value <- NULL
    ret <-
      dplyr::mutate(
        data,
        !!col_out :=
          dplyr::case_when(
            p.value <= 0.001 ~ "***",
            p.value <= 0.001 ~ "**",
            p.value <= 0.06 ~ "*",
            p.value <= 0.01 ~ ".",
            TRUE ~ " "
          )
      )
    ret
  }

#' Custom wrapper for \code{broom::tidy()}
#'
#' @description Calls  \code{broom::tidy()} and modifies the output.
#' @details This function calls \code{broom::tidy()} then modifies the output of
#' by adding a significance column (with \code{teml::add_signif_col()},
#' truncating numeric output columns,
#' coercing to a \code{tibble}, and arranging
#' by ascending p.value.
#' @inheritParams add_signif_col
#' @param n_digits integer. Number of columns to round numeric columns. This is
#' useful because the values in the \code{p.value} column may be very small,
#' leading to extremely \"wide\" output.
#' @return tibble.
#' @export
#' @importFrom broom tidy
tidy_fit_pretty <-
  function(data = NULL, n_digits = getOption("digits")) {
    . <- p.value <- NULL
    ret <- broom::tidy(data)
    ret <- dplyr::as_tibble(ret)
    ret <- dplyr::mutate_if(ret, is.numeric, dplyr::funs(round(., n_digits)))
    ret <- add_signif_col(ret)
    ret <- dplyr::arrange(ret, p.value)
    ret
  }

#' Custom wrapper for \code{broom::glance()}
#'
#' @description Calls  \code{broom::glance()} and modifies the output.
#' @details This function calls \code{broom::glance()} then modifies the output of
#' by truncating numeric output columns,
#' coercing to a \code{tibble}, and, optionally, formatting in long (i.e. tidy) format.
#' @inheritParams tidy_fit_pretty
#' @param tidy logical. Whether to put output in long (i.e. tidy) format.
#' @return tibble.
#' @export
#' @importFrom broom glance
#' @importFrom tidyr gather
glance_fit_pretty <-
  function(data = NULL, n_digits  = getOption("digits"), tidy = FALSE) {
    . <- metric <- value <- NULL
    ret <- broom::glance(data)
    ret <- dplyr::as_tibble(ret)
    ret <- dplyr::mutate_if(ret, is.numeric, dplyr::funs(round(., n_digits)))
    if(tidy) {
      ret <- tidyr::gather(ret, metric, value)
    }
    ret
  }
tonyelhabr/teml documentation built on May 4, 2019, 12:57 a.m.