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
|
backtransform <- function(x, transform, draws = NULL) {
# transform can be a function or a named list of length 1 with a function, but could be NULL
if (!is.function(transform)) {
if (is.null(transform[[1]])) {
return(x)
}
transform <- transform[[1]]
}
checkmate::assert_data_frame(x)
checkmate::assert_function(transform)
cols <- intersect(colnames(x), c("estimate", "conf.low", "conf.high"))
if (!is.null(draws)) {
dim_pre <- dim(draws)
draws <- transform(draws)
dim_post <- dim(draws)
if (!identical(dim_pre, dim_post)) {
stop_sprintf(
"The `transform` function must return an object of the same class as its input: a matrix input must return a matrix of the same size, and a vector input must return a vector of the same length."
)
}
}
for (col in cols) {
x[[col]] <- transform(x[[col]])
}
# Issue #1204: Some inverse link functions swap the order of low and high
if (all(c("conf.low", "conf.high") %in% colnames(x))) {
if (all(x$conf.high < x$conf.low)) {
lo <- x[["conf.low"]]
hi <- x[["conf.high"]]
x[["conf.low"]] <- hi
x[["conf.high"]] <- lo
}
}
for (col in c("std.error", "statistic")) {
x[[col]] <- NULL
}
attr(x, "posterior_draws") <- draws
return(x)
}
|