| 1 |
#' @templateVar class drc |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `drc` object produced by a call to [drc::drm()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @template param_unused_dots |
| 7 |
#' |
| 8 |
#' @evalRd return_tidy( |
| 9 |
#' curve = "Index identifying the curve.", |
| 10 |
#' regression = TRUE |
| 11 |
#' ) |
| 12 |
#' |
| 13 |
#' @details The tibble has one row for each curve and term in the regression. |
| 14 |
#' The `curveid` column indicates the curve. |
| 15 |
#' |
| 16 |
#' @examplesIf rlang::is_installed("drc") |
| 17 |
#' |
| 18 |
#' # load libraries for models and data |
| 19 |
#' library(drc) |
| 20 |
#' |
| 21 |
#' # fit model |
| 22 |
#' mod <- drm(dead / total ~ conc, type, |
| 23 |
#' weights = total, data = selenium, fct = LL.2(), type = "binomial" |
| 24 |
#' ) |
| 25 |
#' |
| 26 |
#' # summarize model fit with tidiers |
| 27 |
#' tidy(mod) |
| 28 |
#' tidy(mod, conf.int = TRUE) |
| 29 |
#' |
| 30 |
#' glance(mod) |
| 31 |
#' |
| 32 |
#' augment(mod, selenium) |
| 33 |
#' |
| 34 |
#' @export |
| 35 |
#' @seealso [tidy()], [drc::drm()] |
| 36 |
#' @family drc tidiers |
| 37 |
#' @aliases drc_tidiers |
| 38 |
tidy.drc <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { |
| 39 |
check_ellipses("exponentiate", "tidy", "drc", ...) |
| 40 |
|
| 41 |
ret <- coef(summary(x)) |
| 42 |
ret <- as_tibble(ret, rownames = "term") |
| 43 |
names(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") |
| 44 |
|
| 45 |
if (conf.int) { |
| 46 |
ci <- broom_confint_terms(x, level = conf.level) |
| 47 |
ret <- dplyr::left_join(ret, ci, by = "term") |
| 48 |
} |
| 49 |
|
| 50 |
tidyr::separate(ret, term, c("term", "curve"), sep = ":") |
| 51 |
} |
| 52 |
|
| 53 |
#' @templateVar class drc |
| 54 |
#' @template title_desc_glance |
| 55 |
#' |
| 56 |
#' @inherit tidy.drc params examples |
| 57 |
#' @template param_unused_dots |
| 58 |
#' |
| 59 |
#' @evalRd return_glance( |
| 60 |
#' "logLik", |
| 61 |
#' "AIC", |
| 62 |
#' "AICc" = "AIC corrected for small samples", |
| 63 |
#' "BIC", |
| 64 |
#' "df.residual" |
| 65 |
#' ) |
| 66 |
#' @seealso [glance()], [drc::drm()] |
| 67 |
#' @export |
| 68 |
#' @family drc tidiers |
| 69 |
glance.drc <- function(x, ...) { |
| 70 |
as_glance_tibble( |
| 71 |
AIC = stats::AIC(x), |
| 72 |
BIC = stats::BIC(x), |
| 73 |
logLik = stats::logLik(x), |
| 74 |
df.residual = x$df.residual, |
| 75 |
na_types = "rrri" |
| 76 |
) |
| 77 |
} |
| 78 |
|
| 79 |
#' @templateVar class drc |
| 80 |
#' @template title_desc_augment |
| 81 |
|
| 82 |
#' @inherit tidy.drc params examples |
| 83 |
#' @template param_data |
| 84 |
#' @template param_newdata |
| 85 |
#' @template param_confint |
| 86 |
#' @template param_se_fit |
| 87 |
#' @template param_unused_dots |
| 88 |
#' |
| 89 |
#' @evalRd return_augment( |
| 90 |
#' ".lower", |
| 91 |
#' ".upper", |
| 92 |
#' ".se.fit", |
| 93 |
#' ".fitted", |
| 94 |
#' ".resid", |
| 95 |
#' ".cooksd" |
| 96 |
#' ) |
| 97 |
#' |
| 98 |
#' @seealso [augment()], [drc::drm()] |
| 99 |
#' @export |
| 100 |
#' |
| 101 |
#' @family drc tidiers |
| 102 |
augment.drc <- function( |
| 103 |
x, |
| 104 |
data = NULL, |
| 105 |
newdata = NULL, |
| 106 |
se_fit = FALSE, |
| 107 |
conf.int = FALSE, |
| 108 |
conf.level = 0.95, |
| 109 |
... |
| 110 |
) { |
| 111 |
if (is.null(data) && is.null(newdata)) { |
| 112 |
cli::cli_abort( |
| 113 |
"Must specify either {.arg data} or {.arg newdata} argument." |
| 114 |
) |
| 115 |
} |
| 116 |
|
| 117 |
# drc doesn't like tibbles |
| 118 |
if (inherits(newdata, "tbl")) { |
| 119 |
newdata <- data.frame(newdata) |
| 120 |
} |
| 121 |
|
| 122 |
# drc doesn't like NA in the type |
| 123 |
if (!missing(newdata) || is.null(newdata)) { |
| 124 |
original <- newdata |
| 125 |
original$.rownames <- rownames(original) |
| 126 |
} |
| 127 |
|
| 128 |
if ( |
| 129 |
!missing(newdata) && |
| 130 |
x$curveVarNam %in% names(newdata) && |
| 131 |
any(is.na(newdata[[x$curveVarNam]])) |
| 132 |
) { |
| 133 |
newdata <- newdata[!is.na(newdata[[x$curveVarNam]]), ] |
| 134 |
} |
| 135 |
|
| 136 |
ret <- augment_columns(x, data, newdata, se.fit = FALSE) |
| 137 |
|
| 138 |
if (!is.null(newdata)) { |
| 139 |
if (conf.int) { |
| 140 |
preds <- data.frame(predict( |
| 141 |
x, |
| 142 |
newdata = newdata, |
| 143 |
interval = "confidence", |
| 144 |
level = conf.level |
| 145 |
)) |
| 146 |
ret[[".lower"]] <- preds[["Lower"]] |
| 147 |
ret[[".upper"]] <- preds[["Upper"]] |
| 148 |
} |
| 149 |
if (se_fit) { |
| 150 |
preds <- data.frame(predict(x, newdata = newdata, se.fit = TRUE)) |
| 151 |
ret[[".se.fit"]] <- preds[["SE"]] |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
# join back removed rows |
| 156 |
if (!".rownames" %in% names(ret)) { |
| 157 |
ret$.rownames <- rownames(ret) |
| 158 |
} |
| 159 |
|
| 160 |
if (!is.null(original)) { |
| 161 |
reto <- ret |> select(starts_with(".")) |
| 162 |
ret <- merge(reto, original, by = ".rownames", all.y = TRUE) |
| 163 |
} |
| 164 |
|
| 165 |
# reorder to line up with original |
| 166 |
ret <- ret[order(match(ret$.rownames, rownames(original))), ] |
| 167 |
rownames(ret) <- NULL |
| 168 |
|
| 169 |
# if rownames are just the original 1...n, they can be removed |
| 170 |
if (all(ret$.rownames == seq_along(ret$.rownames))) { |
| 171 |
ret$.rownames <- NULL |
| 172 |
} |
| 173 |
|
| 174 |
as_tibble(ret) |
| 175 |
} |