| 1 |
#' @templateVar class rqs |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `rqs` object returned from [quantreg::rq()]. |
| 5 |
#' @param se.type Character specifying the method to use to calculate |
| 6 |
#' standard errors. Passed to [quantreg::summary.rq()] `se` argument. |
| 7 |
#' Defaults to `"rank"`. |
| 8 |
#' @template param_confint |
| 9 |
#' @param ... Additional arguments passed to [quantreg::summary.rqs()] |
| 10 |
#' |
| 11 |
#' @evalRd return_tidy(regression = TRUE, |
| 12 |
#' quantile = "Linear conditional quantile.") |
| 13 |
#' |
| 14 |
#' @details If `se.type = "rank"` confidence intervals are calculated by |
| 15 |
#' `summary.rq`. When only a single predictor is included in the model, |
| 16 |
#' no confidence intervals are calculated and the confidence limits are |
| 17 |
#' set to NA. |
| 18 |
#' |
| 19 |
#' @aliases rqs_tidiers |
| 20 |
#' @export |
| 21 |
#' @seealso [tidy()], [quantreg::rq()] |
| 22 |
#' @family quantreg tidiers |
| 23 |
#' @inherit tidy.rq examples |
| 24 |
#' |
| 25 |
tidy.rqs <- function( |
| 26 |
x, |
| 27 |
se.type = "rank", |
| 28 |
conf.int = FALSE, |
| 29 |
conf.level = 0.95, |
| 30 |
... |
| 31 |
) { |
| 32 |
check_ellipses("exponentiate", "tidy", "rqs", ...) |
| 33 |
|
| 34 |
rq_summary <- suppressWarnings( |
| 35 |
quantreg::summary.rqs(x, se = se.type, alpha = 1 - conf.level, ...) |
| 36 |
) |
| 37 |
|
| 38 |
purrr::map_df( |
| 39 |
rq_summary, |
| 40 |
process_rq, |
| 41 |
se.type = se.type, |
| 42 |
conf.int = conf.int, |
| 43 |
conf.level = conf.level |
| 44 |
) |
| 45 |
} |
| 46 |
|
| 47 |
#' @export |
| 48 |
glance.rqs <- function(x, ...) { |
| 49 |
cli::cli_abort( |
| 50 |
c( |
| 51 |
"{.fn glance} cannot handle objects of class {.cls rqs}, |
| 52 |
i.e. models with more than one tau value.", |
| 53 |
"i" = "Please use a {.pkg purrr} {.fn map}-based workflow with |
| 54 |
{.cls rq} models instead." |
| 55 |
) |
| 56 |
) |
| 57 |
} |
| 58 |
|
| 59 |
#' @templateVar class rqs |
| 60 |
#' @template title_desc_augment |
| 61 |
#' |
| 62 |
#' @inherit tidy.rqs examples |
| 63 |
#' @inherit augment.rq return details |
| 64 |
#' |
| 65 |
#' @param x An `rqs` object returned from [quantreg::rq()]. |
| 66 |
#' @template param_data |
| 67 |
#' @template param_newdata |
| 68 |
#' @inheritDotParams quantreg::predict.rq |
| 69 |
#' |
| 70 |
#' @export |
| 71 |
#' @seealso [augment], [quantreg::rq()], [quantreg::predict.rqs()] |
| 72 |
#' @family quantreg tidiers |
| 73 |
#' @inherit tidy.rq examples |
| 74 |
augment.rqs <- function(x, data = model.frame(x), newdata, ...) { |
| 75 |
n_tau <- length(x[["tau"]]) |
| 76 |
if (missing(newdata) || is.null(newdata)) { |
| 77 |
original <- data[rep(seq_len(nrow(data)), each = n_tau), , drop = FALSE] |
| 78 |
pred <- predict(x, stepfun = FALSE, ...) |
| 79 |
resid <- residuals(x) |
| 80 |
resid <- setNames(as.data.frame(resid), x[["tau"]]) |
| 81 |
resid <- pivot_longer( |
| 82 |
resid, |
| 83 |
cols = dplyr::everything(), |
| 84 |
names_to = ".tau", |
| 85 |
values_to = ".resid" |
| 86 |
) |> |
| 87 |
as.data.frame() |
| 88 |
original <- cbind(original, resid) |
| 89 |
pred <- setNames(as.data.frame(pred), x[["tau"]]) |
| 90 |
pred <- pivot_longer( |
| 91 |
pred, |
| 92 |
cols = dplyr::everything(), |
| 93 |
names_to = ".tau", |
| 94 |
values_to = ".fitted" |
| 95 |
) |> |
| 96 |
as.data.frame() |
| 97 |
ret <- unrowname(cbind(original, pred[, -1, drop = FALSE])) |
| 98 |
} else { |
| 99 |
original <- newdata[ |
| 100 |
rep(seq_len(nrow(newdata)), each = n_tau), |
| 101 |
, |
| 102 |
drop = FALSE |
| 103 |
] |
| 104 |
pred <- predict(x, newdata = newdata, stepfun = FALSE, ...) |
| 105 |
pred <- setNames(as.data.frame(pred), x[["tau"]]) |
| 106 |
pred <- pivot_longer( |
| 107 |
pred, |
| 108 |
cols = dplyr::everything(), |
| 109 |
names_to = ".tau", |
| 110 |
values_to = ".fitted" |
| 111 |
) |> |
| 112 |
as.data.frame() |
| 113 |
ret <- unrowname(cbind(original, pred)) |
| 114 |
} |
| 115 |
as_tibble(ret) |
| 116 |
} |