File: sort_parameters.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,518 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#' Sort parameters by coefficient values
#'
#' @param x A data frame or a `parameters_model` object.
#' @param ... Arguments passed to or from other methods.
#'
#' @examples
#' # creating object to sort (can also be a regular data frame)
#' mod <- model_parameters(stats::lm(wt ~ am * cyl, data = mtcars))
#'
#' # original output
#' mod
#'
#' # sorted outputs
#' sort_parameters(mod, sort = "ascending")
#' sort_parameters(mod, sort = "descending")
#'
#' @return A sorted data frame or original object.
#'
#' @export
sort_parameters <- function(x, ...) {
  UseMethod("sort_parameters")
}

#' @rdname sort_parameters
#'
#' @param sort If `"none"` (default) do not sort, `"ascending"` sort by
#'   increasing coefficient value, or `"descending"` sort by decreasing
#'   coefficient value.
#' @param column The column containing model parameter estimates. This will be
#'   `"Coefficient"` (default) in *easystats* packages, `"estimate"` in *broom*
#'   package, etc.
#'
#' @export
sort_parameters.default <- function(x, sort = "none", column = "Coefficient", ...) {
  sort <- match.arg(tolower(sort), choices = c("none", "ascending", "descending"))

  if (sort == "none") {
    return(x)
  }

  # new row indices to use for sorting
  new_row_order <- switch(sort,
    ascending = order(x[[column]], decreasing = FALSE),
    descending = order(x[[column]], decreasing = TRUE)
  )

  x[new_row_order, ]
}

#' @export
sort_parameters.data.frame <- sort_parameters.default