R/anova_sp.R

Defines functions anova_sp

Documented in anova_sp

#' Tabla de ANOVA
#'
#' @description
#'    Tabla de Analisis de varianza - EspaƱol.
#'    Modificada de broom:tidy()
#' @usage anova_sp(object, ...)
#'
#' @param anova
#'
#' @return
#' @export
#' @importFrom broom fix_data_frame
#' @importFrom plyr rename
#' @importFrom dplyr setdiff as_tibble
#' @importFrom stringr str_trim
#' @examples
anova_sp <- function(x, ...) {
  x <- summary(x)
  x <- x[[1]]
  # there are many possible column names that need to be transformed
  renamers <- c(
    "Df" = "gl",
    "Sum Sq" = "sum_cuad",
    "Mean Sq" = "cuad_med",
    "F value" = "f_",
    "Pr(>F)" = "p.valor",
    "Res.Df" = "res.df",
    "RSS" = "rss",
    "Sum of Sq" = "sumsq",
    "F" = "statistic",
    "Chisq" = "statistic",
    "P(>|Chi|)" = "p.value",
    "Pr(>Chi)" = "p.value",
    "Pr..Chisq." = "p.value",
    "Pr..Chi." = "p.value",
    "p.value" = "p.value",
    "Chi.sq" = "statistic",
    "LR.Chisq" = "statistic",
    "LR Chisq" = "statistic",
    "edf" = "edf",
    "Ref.df" = "ref.df"
  )

  names(renamers) <- make.names(names(renamers))

  x <- broom::fix_data_frame(x, newcol = "fuente")

  unknown_cols <- dplyr::setdiff(colnames(x),
                                 c("fuente", names(renamers)))
  if (length(unknown_cols) > 0) {
    warning(
      "The following column names in ANOVA output were not ",
      "recognized or transformed: ",
      paste(unknown_cols, collapse = ", ")
    )
  }
  ret <- plyr::rename(x, renamers, warn_missing = FALSE)
  if (!is.null(ret$fuente)) {
    # if rows had names, strip whitespace in them
    ret <- mutate(ret, fuente = stringr::str_trim(fuente))
  }
  dplyr::as_tibble(ret)
}
PaulESantos/paulrpkg documentation built on July 20, 2019, 11:01 p.m.