Skip to content

Commit

Permalink
lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 6, 2024
1 parent a14f650 commit 036d4ec
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 44 deletions.
3 changes: 2 additions & 1 deletion R/bci.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ bci.numeric <- function(x, ci = 0.95, verbose = TRUE, ...) {
bci.data.frame <- function(x, ci = 0.95, rvar_col = NULL, verbose = TRUE, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::bci
cl$x <- x_rvar
Expand Down
3 changes: 2 additions & 1 deletion R/ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ ci.numeric <- function(x, ci = 0.95, method = "ETI", verbose = TRUE, BF = 1, ...
#' @inheritParams p_direction
#' @export
ci.data.frame <- function(x, ci = 0.95, method = "ETI", BF = 1, rvar_col = NULL, verbose = TRUE, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::ci
cl$x <- x_rvar
Expand Down
8 changes: 5 additions & 3 deletions R/describe_posterior.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ describe_posterior.default <- function(posterior, ...) {
test_psig$.rowid <- seq_len(nrow(test_psig))
} else if (!all(is.na(test_rope$Parameter))) {
test_rope$.rowid <- seq_len(nrow(test_rope))
} else if (!all(is.na(test_bf$Parameter))) {
} else if (!all(is.na(test_bf$Parameter))) { # nolint
test_bf$.rowid <- seq_len(nrow(test_bf))
} else {
estimates$.rowid <- seq_len(nrow(estimates))
Expand Down Expand Up @@ -590,12 +590,14 @@ describe_posterior.data.frame <- function(posterior,
rvar_col = NULL,
verbose = TRUE,
...) {
if (length(x_rvar <- .possibly_extract_rvar_col(posterior, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(posterior, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::describe_posterior
cl$posterior <- x_rvar
cl$rvar_col <- NULL
if (length(prior_rvar <- .possibly_extract_rvar_col(posterior, bf_prior)) > 0L) {
prior_rvar <- .possibly_extract_rvar_col(posterior, bf_prior)
if (length(prior_var) > 0L) {
cl$bf_prior <- prior_rvar
}
out <- eval.parent(cl)
Expand Down
3 changes: 2 additions & 1 deletion R/equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ equivalence_test.numeric <- function(x, range = "default", ci = 0.95, verbose =
equivalence_test.data.frame <- function(x, range = "default", ci = 0.95, rvar_col = NULL, verbose = TRUE, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::equivalence_test
cl$x <- x_rvar
Expand Down
3 changes: 2 additions & 1 deletion R/estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ estimate_density.data.frame <- function(x,
at = NULL,
rvar_col = NULL,
...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::estimate_density
cl$x <- x_rvar
Expand Down
3 changes: 2 additions & 1 deletion R/eti.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ eti.numeric <- function(x, ci = 0.95, verbose = TRUE, ...) {
eti.data.frame <- function(x, ci = 0.95, rvar_col = NULL, verbose = TRUE, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::eti
cl$x <- x_rvar
Expand Down
3 changes: 2 additions & 1 deletion R/hdi.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ hdi.numeric <- function(x, ci = 0.95, verbose = TRUE, ...) {
hdi.data.frame <- function(x, ci = 0.95, rvar_col = NULL, verbose = TRUE, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::hdi
cl$x <- x_rvar
Expand Down
3 changes: 2 additions & 1 deletion R/map_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ map_estimate.brmsfit <- function(x, precision = 2^10, method = "kernel", effects
#' @inheritParams p_direction
#' @export
map_estimate.data.frame <- function(x, precision = 2^10, method = "kernel", rvar_col = NULL, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::map_estimate
cl$x <- x_rvar
Expand Down
3 changes: 2 additions & 1 deletion R/p_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ p_map.get_predicted <- function(x,
#' @rdname p_map
#' @inheritParams p_direction
p_map.data.frame <- function(x, null = 0, precision = 2^10, method = "kernel", rvar_col = NULL, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::p_map
cl$x <- x_rvar
Expand Down
15 changes: 8 additions & 7 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ p_significance.get_predicted <- function(x,
p_significance.data.frame <- function(x, threshold = "default", rvar_col = NULL, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::p_significance
cl$x <- x_rvar
Expand Down Expand Up @@ -269,18 +270,18 @@ p_significance.stanreg <- function(x,
component <- match.arg(component)
threshold <- .select_threshold_ps(model = x, threshold = threshold, verbose = verbose)

data <- p_significance(
result <- p_significance(
insight::get_parameters(x, effects = effects, component = component, parameters = parameters),
threshold = threshold
)

cleaned_parameters <- insight::clean_parameters(x)
out <- .prepare_output(data, cleaned_parameters, inherits(x, "stanmvreg"))
out <- .prepare_output(result, cleaned_parameters, inherits(x, "stanmvreg"))

attr(out, "clean_parameters") <- cleaned_parameters
attr(out, "threshold") <- threshold
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
class(out) <- class(data)
class(out) <- class(result)

out
}
Expand All @@ -305,18 +306,18 @@ p_significance.brmsfit <- function(x,
component <- match.arg(component)
threshold <- .select_threshold_ps(model = x, threshold = threshold, verbose = verbose)

data <- p_significance(
result <- p_significance(
insight::get_parameters(x, effects = effects, component = component, parameters = parameters),
threshold = threshold
)

cleaned_parameters <- insight::clean_parameters(x)
out <- .prepare_output(data, cleaned_parameters)
out <- .prepare_output(result, cleaned_parameters)

attr(out, "clean_parameters") <- cleaned_parameters
attr(out, "threshold") <- threshold
attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(x))
class(out) <- class(data)
class(out) <- class(result)

out
}
Expand Down
20 changes: 15 additions & 5 deletions R/point_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ point_estimate.numeric <- function(x, centrality = "all", dispersion = FALSE, th
estimate_list <- centrality
}

out <- data.frame(".temp" = 0)
out <- data.frame(.temp = 0)

# Median
if ("median" %in% estimate_list) {
Expand Down Expand Up @@ -132,8 +132,14 @@ point_estimate.numeric <- function(x, centrality = "all", dispersion = FALSE, th
#' @export
#' @rdname point_estimate
#' @inheritParams p_direction
point_estimate.data.frame <- function(x, centrality = "all", dispersion = FALSE, threshold = 0.1, rvar_col = NULL, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
point_estimate.data.frame <- function(x,
centrality = "all",
dispersion = FALSE,
threshold = 0.1,
rvar_col = NULL,
...) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::point_estimate
cl$x <- x_rvar
Expand All @@ -156,7 +162,7 @@ point_estimate.data.frame <- function(x, centrality = "all", dispersion = FALSE,
estimates <- do.call(rbind, estimates)
}

out <- cbind(data.frame("Parameter" = names(x), stringsAsFactors = FALSE), estimates)
out <- cbind(data.frame(Parameter = names(x), stringsAsFactors = FALSE), estimates)
rownames(out) <- NULL
attr(out, "data") <- x
attr(out, "centrality") <- centrality
Expand Down Expand Up @@ -209,7 +215,11 @@ point_estimate.BGGM <- point_estimate.bcplm


#' @export
point_estimate.bamlss <- function(x, centrality = "all", dispersion = FALSE, component = c("conditional", "location", "all"), ...) {
point_estimate.bamlss <- function(x,
centrality = "all",
dispersion = FALSE,
component = c("conditional", "location", "all"),
...) {
component <- match.arg(component)
out <- point_estimate(
insight::get_parameters(x, component = component),
Expand Down
2 changes: 1 addition & 1 deletion R/print.equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ print.equivalence_test <- function(x, digits = 2, ...) {


.dynGet <- function(x,
ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA),
ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE),
minframe = 1L,
inherits = FALSE) {
x <- insight::safe_deparse(x)
Expand Down
3 changes: 2 additions & 1 deletion R/rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ rope.get_predicted <- function(x,
rope.data.frame <- function(x, range = "default", ci = 0.95, ci_method = "ETI", rvar_col = NULL, verbose = TRUE, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::rope
cl$x <- x_rvar
Expand Down
6 changes: 4 additions & 2 deletions R/si.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ si.get_predicted <- function(posterior, prior = NULL, BF = 1, use_iterations = F
#' @inheritParams p_direction
#' @export
si.data.frame <- function(posterior, prior = NULL, BF = 1, rvar_col = NULL, verbose = TRUE, ...) {
if (length(x_rvar <- .possibly_extract_rvar_col(posterior, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(posterior, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::si
cl$posterior <- x_rvar
cl$rvar_col <- NULL
if (length(prior_rvar <- .possibly_extract_rvar_col(posterior, prior)) > 0L) {
prior_rvar <- .possibly_extract_rvar_col(posterior, prior)
if (length(prior_rvar) > 0L) {
cl$prior <- prior_rvar
}
out <- eval.parent(cl)
Expand Down
11 changes: 4 additions & 7 deletions R/spi.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ spi.numeric <- function(x, ci = 0.95, verbose = TRUE, ...) {
spi.data.frame <- function(x, ci = 0.95, rvar_col = NULL, verbose = TRUE, ...) {
obj_name <- insight::safe_deparse_symbol(substitute(x))

if (length(x_rvar <- .possibly_extract_rvar_col(x, rvar_col)) > 0L) {
x_rvar <- .possibly_extract_rvar_col(x, rvar_col)
if (length(x_rvar) > 0L) {
cl <- match.call()
cl[[1]] <- bayestestR::spi
cl$x <- x_rvar
Expand Down Expand Up @@ -351,11 +352,7 @@ spi.get_predicted <- function(x, ci = 0.95, use_iterations = FALSE, verbose = TR
}

# output
data.frame(
"CI" = ci,
"CI_low" = x.l,
"CI_high" = x.u
)
data.frame(CI = ci, CI_low = x.l,CI_high = x.u)
}

.spi_lower <- function(bw, n.sims, k, l, dens, x) {
Expand Down Expand Up @@ -432,7 +429,7 @@ spi.get_predicted <- function(x, ci = 0.95, use_iterations = FALSE, verbose = TR
w.l <- quadprog::solve.QP(D.l, d.l, A.l, c(1, rep(0, range_ll_lu + 2)), range_ll_lu)
x.l <- w.l$solution %*% x[l.l:l.u]

return(x.l)
x.l
}

.spi_upper <- function(bw, n.sims, ui, u, dens, x) {
Expand Down
20 changes: 10 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,21 @@
all_attrs <- attributes(results) # save attributes for later
all_class <- class(results)

grid <- insight::get_datagrid(object)
grid_names <- colnames(grid)
datagrid <- insight::get_datagrid(object)
grid_names <- colnames(datagrid)

if (long) {
grid$Parameter <- unique(results$Parameter)
results <- datawizard::data_merge(grid, results, by = "Parameter")
datagrid$Parameter <- unique(results$Parameter)
results <- datawizard::data_merge(datagrid, results, by = "Parameter")
results$Parameter <- NULL
class(results) <- all_class
} else {
results[colnames(grid)] <- grid
results[colnames(datagrid)] <- datagrid
results$Parameter <- NULL
results <- results[, c(grid_names, setdiff(colnames(results), grid_names)), drop = FALSE]

# add back attributes
most_attrs <- all_attrs[setdiff(names(all_attrs), names(attributes(grid)))]
most_attrs <- all_attrs[setdiff(names(all_attrs), names(attributes(datagrid)))]
attributes(results)[names(most_attrs)] <- most_attrs
}

Expand All @@ -225,13 +225,13 @@
all_attrs <- attributes(results) # save attributes for later
all_class <- class(results)

is_rvar <- vapply(object, function(col) inherits(col, "rvar"), FUN.VALUE = logical(1))
is_rvar <- vapply(object, inherits, FUN.VALUE = logical(1), "rvar")
grid_names <- colnames(object)[!is_rvar]
grid <- data.frame(object[, grid_names, drop = FALSE])
datagrid <- data.frame(object[, grid_names, drop = FALSE])

if (long) {
grid$Parameter <- unique(results$Parameter)
results <- datawizard::data_merge(grid, results, by = "Parameter")
datagrid$Parameter <- unique(results$Parameter)
results <- datawizard::data_merge(datagrid, results, by = "Parameter")
results$Parameter <- NULL
class(results) <- all_class
} else {
Expand Down

0 comments on commit 036d4ec

Please sign in to comment.