Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 22, 2024
1 parent 90d0008 commit c8c8120
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 62 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: performance
Title: Assessment of Regression Models Performance
Version: 0.12.4
Version: 0.12.4.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -160,3 +160,4 @@ Config/Needs/website:
r-lib/pkgdown,
easystats/easystatstemplate
Config/rcmdcheck/ignore-inconsequential-notes: true
Remotes: easystats/insight
65 changes: 4 additions & 61 deletions R/performance_aicc.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,77 +266,20 @@ performance_aicc.rma <- function(x, ...) {
}




# jacobian / derivate for log models and other transformations ----------------


# this function adjusts any IC for models with transformed response variables
.adjust_ic_jacobian <- function(model, ic) {
response_transform <- insight::find_transformation(model)
if (!is.null(ic) && !is.null(response_transform) && !identical(response_transform, "identity")) {
adjustment <- .safe(.ll_analytic_adjustment(model, insight::get_weights(model, remove_na = TRUE)))
adjustment <- .safe(insight::get_loglikelihood_adjustment(
model,
insight::get_weights(model, remove_na = TRUE)
))
if (!is.null(adjustment)) {
ic <- ic - 2 * adjustment
}
}
ic
}


# copied from `insight`
.ll_analytic_adjustment <- function(x, model_weights = NULL) {
tryCatch(
{
trans <- insight::find_transformation(x)
switch(trans,
identity = .weighted_sum(log(insight::get_response(x)), w = model_weights),
log = .weighted_sum(log(1 / insight::get_response(x)), w = model_weights),
log1p = .weighted_sum(log(1 / (insight::get_response(x) + 1)), w = model_weights),
log2 = .weighted_sum(log(1 / (insight::get_response(x) * log(2))), w = model_weights),
log10 = .weighted_sum(log(1 / (insight::get_response(x) * log(10))), w = model_weights),
exp = .weighted_sum(insight::get_response(x), w = model_weights),
expm1 = .weighted_sum((insight::get_response(x) - 1), w = model_weights),
sqrt = .weighted_sum(log(0.5 / sqrt(insight::get_response(x))), w = model_weights),
.ll_jacobian_adjustment(x, model_weights)
)
},
error = function(e) {
NULL
}
)
}


# this function calculates the adjustment for the log-likelihood of a model
# with transformed response
.ll_jacobian_adjustment <- function(model, weights = NULL) {
tryCatch(
{
trans <- insight::get_transformation(model)$transformation
.weighted_sum(log(
diag(attr(with(
insight::get_data(model, verbose = FALSE),
stats::numericDeriv(
expr = quote(trans(
get(insight::find_response(model))
)),
theta = insight::find_response(model)
)
), "gradient"))
), weights)
},
error = function(e) {
NULL
}
)
}


.weighted_sum <- function(x, w = NULL, ...) {
if (is.null(w)) {
mean(x) * length(x)
} else {
stats::weighted.mean(x, w) * length(x)
}
}

0 comments on commit c8c8120

Please sign in to comment.