Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 28, 2022
1 parent 9178b13 commit 4007f11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
9 changes: 1 addition & 8 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
} else if (inherits(x, "bayesx")) {
out <- stats::BIC(x)[["BIC"]]
} else {
out <- tryCatch(
{
stats::BIC(x)
},
error = function(e) {
NULL
}
)
out <- tryCatch(stats::BIC(x), error = function(e) NULL)
}
.adjust_ic_jacobian(x, out)
}
Expand Down
24 changes: 11 additions & 13 deletions R/performance_aicc.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,8 @@ performance_aic.vglm <- performance_aic.vgam

#' @export
performance_aic.svyglm <- function(x, ...) {
aic <- tryCatch(
{
stats::AIC(x)[["AIC"]]
},
error = function(e) {
NULL
}
)
.adjust_ic_jacobian(aic)
aic <- tryCatch(stats::AIC(x)[["AIC"]], error = function(e) NULL)
.adjust_ic_jacobian(x, aic)
}

#' @export
Expand Down Expand Up @@ -173,15 +166,15 @@ performance_aic.poissonmfx <- performance_aic.logitor
#' @export
performance_aic.bayesx <- function(x, ...) {
out <- stats::AIC(x)[["AIC"]]
.adjust_ic_jacobian(out)
.adjust_ic_jacobian(x, out)
}

# methods ------------------------------------------

#' @export
AIC.bife <- function(object, ..., k = 2) {
out <- -2 * as.numeric(insight::get_loglikelihood(object)) + k * insight::get_df(object, type = "model")
.adjust_ic_jacobian(out)
.adjust_ic_jacobian(object, out)
}


Expand All @@ -195,7 +188,7 @@ performance_aicc.default <- function(x, ...) {
k <- attr(ll, "df")

aicc <- -2 * as.vector(ll) + 2 * k * (n / (n - k - 1))
.adjust_ic_jacobian(aicc)
.adjust_ic_jacobian(x, aicc)
}


Expand All @@ -205,8 +198,9 @@ performance_aicc.bife <- function(x, ...) {
ll <- insight::get_loglikelihood(x)
nparam <- length(insight::find_parameters(x, effects = "fixed", flatten = TRUE))
k <- n - nparam

aicc <- -2 * as.vector(ll) + 2 * k * (n / (n - k - 1))
.adjust_ic_jacobian(aicc)
.adjust_ic_jacobian(x, aicc)
}

#' @export
Expand All @@ -231,6 +225,7 @@ 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")) {
Expand All @@ -243,6 +238,7 @@ performance_aicc.rma <- function(x, ...) {
}


# this function adjusts the AIC, either dlnorm for log, or Jacobian for other transformations
.adjust_aic <- function(x, response_transform, aic = NULL, ...) {
if (response_transform == "log") {
aic <- .adjust_aic_dlnorm(x)
Expand All @@ -253,6 +249,7 @@ performance_aicc.rma <- function(x, ...) {
}


# this function adjusts the AIC, based on dlnorm() log-likelihood adjustment
.adjust_aic_dlnorm <- function(model) {
# loglik-transformation. first try, we use dlnorm()
aic <- tryCatch(
Expand Down Expand Up @@ -280,6 +277,7 @@ performance_aicc.rma <- function(x, ...) {
}


# this function just adjusts the log-likelihood of a model
.adjust_loglik_jacobian <- function(model) {
trans <- insight::get_transformation(model)$transformation
sum(log(
Expand Down

0 comments on commit 4007f11

Please sign in to comment.