Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 22, 2023
2 parents 56cb24a + 750b687 commit 281d4fd
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 71 deletions.
2 changes: 1 addition & 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.10.8.1
Version: 0.10.8.2
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ S3method(r2_coxsnell,coxph)
S3method(r2_coxsnell,cpglm)
S3method(r2_coxsnell,crch)
S3method(r2_coxsnell,glm)
S3method(r2_coxsnell,glmmTMB)
S3method(r2_coxsnell,glmx)
S3method(r2_coxsnell,logitmfx)
S3method(r2_coxsnell,logitor)
Expand Down Expand Up @@ -463,6 +464,7 @@ S3method(r2_nagelkerke,coxph)
S3method(r2_nagelkerke,cpglm)
S3method(r2_nagelkerke,crch)
S3method(r2_nagelkerke,glm)
S3method(r2_nagelkerke,glmmTMB)
S3method(r2_nagelkerke,glmx)
S3method(r2_nagelkerke,logitmfx)
S3method(r2_nagelkerke,logitor)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# performance 0.10.9

* `r2()` for models of class `glmmTMB` without random effects now returns the
correct r-squared value for non-mixed models.

# performance 0.10.8

## Changes
Expand Down
21 changes: 9 additions & 12 deletions R/check_clusterstructure.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ check_clusterstructure <- function(x,

H <- .clusterstructure_hopkins(x, distance = distance)
if (H < 0.5) {
text <- paste0(
res_text <- paste0(
"The dataset is suitable for clustering (Hopkins' H = ",
insight::format_value(H),
").\n"
)
color <- "green"
} else {
text <- paste0(
res_text <- paste0(
"The dataset is not suitable for clustering (Hopkins' H = ",
insight::format_value(H),
").\n"
Expand All @@ -67,7 +67,7 @@ check_clusterstructure <- function(x,
dissimilarity_matrix = .clusterstructure_dm(x, distance = distance, method = "ward.D2")
)

attr(out, "text") <- text
attr(out, "text") <- res_text
attr(out, "color") <- color
attr(out, "title") <- "Clustering tendency"
class(out) <- c("see_check_clusterstructure", "check_clusterstructure", "easystats_check", class(out))
Expand Down Expand Up @@ -107,35 +107,32 @@ plot.check_clusterstructure <- function(x, ...) {

n <- nrow(x) - 1

c <- apply(x, 2, min) # minimum value per column
cc <- apply(x, 2, min) # minimum value per column
d <- apply(x, 2, max)
p <- matrix(0, ncol = ncol(x), nrow = n) # n vectors of space
for (i in seq_len(ncol(x))) {
p[, i] <- stats::runif(n, min = c[i], max = d[i])
p[, i] <- stats::runif(n, min = cc[i], max = d[i])
}
k <- round(stats::runif(n, 1, nrow(x)))
q <- as.matrix(x[k, ])
qq <- as.matrix(x[k, ])
distp <- rep(0, nrow(x))
# distq=rep(0,nrow(x)-1)
distq <- 0
minp <- rep(0, n)
minq <- rep(0, n)
for (i in 1:n) {
distp[1] <- stats::dist(rbind(p[i, ], x[1, ]), method = distance)
minqi <- stats::dist(rbind(q[i, ], x[1, ]), method = distance)
minqi <- stats::dist(rbind(qq[i, ], x[1, ]), method = distance)
for (j in 2:nrow(x)) {
distp[j] <- stats::dist(rbind(p[i, ], x[j, ]), method = distance)
error <- q[i, ] - x[j, ]
error <- qq[i, ] - x[j, ]
if (sum(abs(error)) != 0) {
# distq[j]<-stats::dist(rbind(q[i,],x[j,]))
distq <- stats::dist(rbind(q[i, ], x[j, ]), method = distance)
distq <- stats::dist(rbind(qq[i, ], x[j, ]), method = distance)
if (distq < minqi) {
minqi <- distq
}
}
}
minp[i] <- min(distp)
# minq[i]<-apply(distq,1,min)
minq[i] <- minqi
}
sum(minq) / (sum(minp) + sum(minq))
Expand Down
42 changes: 21 additions & 21 deletions R/check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,10 @@ check_collinearity.zerocount <- function(x,

.check_collinearity <- function(x, component, ci = 0.95, verbose = TRUE) {
v <- insight::get_varcov(x, component = component, verbose = FALSE)
assign <- .term_assignments(x, component, verbose = verbose)
term_assign <- .term_assignments(x, component, verbose = verbose)

# any assignment found?
if (is.null(assign) || all(is.na(assign))) {
if (is.null(term_assign) || all(is.na(term_assign))) {
if (verbose) {
insight::format_alert(
sprintf("Could not extract model terms for the %s component of the model.", component)
Expand All @@ -420,7 +420,7 @@ check_collinearity.zerocount <- function(x,

# we have rank-deficiency here. remove NA columns from assignment
if (isTRUE(attributes(v)$rank_deficient) && !is.null(attributes(v)$na_columns_index)) {
assign <- assign[-attributes(v)$na_columns_index]
term_assign <- term_assign[-attributes(v)$na_columns_index]
if (isTRUE(verbose)) {
insight::format_alert(
"Model matrix is rank deficient. VIFs may not be sensible."
Expand All @@ -431,7 +431,7 @@ check_collinearity.zerocount <- function(x,
# check for missing intercept
if (insight::has_intercept(x)) {
v <- v[-1, -1]
assign <- assign[-1]
term_assign <- term_assign[-1]
} else {
if (isTRUE(verbose)) {
insight::format_alert("Model has no intercept. VIFs may not be sensible.")
Expand All @@ -443,22 +443,22 @@ check_collinearity.zerocount <- function(x,
# hurdle or zeroinfl model can have no zero-inflation formula, in which case
# we have the same formula as for conditional formula part
if (inherits(x, c("hurdle", "zeroinfl", "zerocount")) &&
component == "zero_inflated" &&
is.null(f[["zero_inflated"]])) {
component == "zero_inflated" &&

Check warning on line 446 in R/check_collinearity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/check_collinearity.R,line=446,col=6,[indentation_linter] Indentation should be 8 spaces but is 6 spaces.
is.null(f[["zero_inflated"]])) {
f$zero_inflated <- f$conditional
}

if (inherits(x, "mixor")) {
terms <- labels(x$terms)
model_terms <- labels(x$terms)
} else {
terms <- labels(stats::terms(f[[component]]))
model_terms <- labels(stats::terms(f[[component]]))
}

if ("instruments" %in% names(f)) {
terms <- unique(c(terms, labels(stats::terms(f[["instruments"]]))))
model_terms <- unique(c(model_terms, labels(stats::terms(f[["instruments"]]))))
}

n.terms <- length(terms)
n.terms <- length(model_terms)

if (n.terms < 2) {
if (isTRUE(verbose)) {
Expand All @@ -476,7 +476,7 @@ check_collinearity.zerocount <- function(x,
na_terms <- vector("numeric")

for (term in 1:n.terms) {
subs <- which(assign == term)
subs <- which(term_assign == term)
if (length(subs)) {
result <- c(
result,
Expand All @@ -489,7 +489,7 @@ check_collinearity.zerocount <- function(x,

# any terms to remove, due to rank deficiency?
if (length(na_terms)) {
terms <- terms[-na_terms]
model_terms <- model_terms[-na_terms]
}

# check for interactions, VIF might be inflated...
Expand Down Expand Up @@ -522,7 +522,7 @@ check_collinearity.zerocount <- function(x,

out <- insight::text_remove_backticks(
data.frame(
Term = terms,
Term = model_terms,
VIF = result,
VIF_CI_low = 1 / (1 - ci_lo),
VIF_CI_high = 1 / (1 - ci_up),
Expand All @@ -538,7 +538,7 @@ check_collinearity.zerocount <- function(x,

attr(out, "data") <- insight::text_remove_backticks(
data.frame(
Term = terms,
Term = model_terms,
VIF = result,
SE_factor = sqrt(result),
stringsAsFactors = FALSE
Expand All @@ -564,29 +564,29 @@ check_collinearity.zerocount <- function(x,
tryCatch(
{
if (inherits(x, c("hurdle", "zeroinfl", "zerocount"))) {
assign <- switch(component,
term_assign <- switch(component,
conditional = attr(insight::get_modelmatrix(x, model = "count"), "assign"),
zero_inflated = attr(insight::get_modelmatrix(x, model = "zero"), "assign")
)
} else if (inherits(x, "glmmTMB")) {
assign <- switch(component,
term_assign <- switch(component,
conditional = attr(insight::get_modelmatrix(x), "assign"),
zero_inflated = .zi_term_assignment(x, component, verbose = verbose)
)
} else if (inherits(x, "MixMod")) {
assign <- switch(component,
term_assign <- switch(component,
conditional = attr(insight::get_modelmatrix(x, type = "fixed"), "assign"),
zero_inflated = attr(insight::get_modelmatrix(x, type = "zi_fixed"), "assign")
)
} else {
assign <- attr(insight::get_modelmatrix(x), "assign")
term_assign <- attr(insight::get_modelmatrix(x), "assign")
}

if (is.null(assign)) {
assign <- .find_term_assignment(x, component, verbose = verbose)
if (is.null(term_assign)) {
term_assign <- .find_term_assignment(x, component, verbose = verbose)
}

assign
term_assign
},
error = function(e) {
.find_term_assignment(x, component, verbose = verbose)
Expand Down
7 changes: 3 additions & 4 deletions R/check_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ check_distribution.default <- function(model) {
} else {
x <- stats::residuals(model)
}
# x_scaled <- .normalize(x)
dat <- .extract_features(x)

dist_residuals <- as.data.frame(t(stats::predict(classify_distribution, dat, type = "prob")))
Expand Down Expand Up @@ -171,11 +170,11 @@ check_distribution.numeric <- function(model) {
insight::check_if_installed("randomForest")

dat <- .extract_features(model)
dist <- as.data.frame(t(stats::predict(classify_distribution, dat, type = "prob")))
distance <- as.data.frame(t(stats::predict(classify_distribution, dat, type = "prob")))

out <- data.frame(
Distribution = rownames(dist),
p_Vector = dist[[1]],
Distribution = rownames(distance),
p_Vector = distance[[1]],
stringsAsFactors = FALSE,
row.names = NULL
)
Expand Down
28 changes: 13 additions & 15 deletions R/check_factorstructure.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ check_factorstructure <- function(x, n = NULL, ...) {
kmo <- check_kmo(x, n, ...)
sphericity <- check_sphericity_bartlett(x, n, ...)

text <- paste0("\n - Sphericity: ", attributes(sphericity)$text, "\n - KMO: ", attributes(kmo)$text)
res_text <- paste0("\n - Sphericity: ", attributes(sphericity)$text, "\n - KMO: ", attributes(kmo)$text)

if (attributes(kmo)$color == "red" || attributes(sphericity)$color == "red") {
color <- "red"
Expand All @@ -105,7 +105,7 @@ check_factorstructure <- function(x, n = NULL, ...) {

out <- list(KMO = kmo, sphericity = sphericity)

attr(out, "text") <- text
attr(out, "text") <- res_text
attr(out, "color") <- color
attr(out, "title") <- "Is the data suitable for Factor Analysis?"
class(out) <- c("easystats_check", class(out))
Expand Down Expand Up @@ -136,18 +136,16 @@ check_kmo <- function(x, n = NULL, ...) {

# TODO: add interpret_kmo in effectsize and use that here for more fine-grained interpretation
if (MSA < 0.5) {
text <-
sprintf(
"The Kaiser, Meyer, Olkin (KMO) overall measure of sampling adequacy suggests that factor analysis is likely to be inappropriate (KMO = %.2f).",
MSA
)
msg_text <- sprintf(
"The Kaiser, Meyer, Olkin (KMO) overall measure of sampling adequacy suggests that factor analysis is likely to be inappropriate (KMO = %.2f).", # nolint
MSA
)
color <- "red"
} else {
text <-
sprintf(
"The Kaiser, Meyer, Olkin (KMO) overall measure of sampling adequacy suggests that data seems appropriate for factor analysis (KMO = %.2f).",
MSA
)
msg_text <- sprintf(
"The Kaiser, Meyer, Olkin (KMO) overall measure of sampling adequacy suggests that data seems appropriate for factor analysis (KMO = %.2f).", # nolint
MSA
)
color <- "green"
}

Expand All @@ -160,9 +158,9 @@ check_kmo <- function(x, n = NULL, ...) {
")"
))

text <- paste0(text, " The individual KMO scores are: ", text_ind, ".")
msg_text <- paste0(msg_text, " The individual KMO scores are: ", text_ind, ".")

attr(out, "text") <- text
attr(out, "text") <- msg_text
attr(out, "color") <- color
attr(out, "title") <- "KMO Measure of Sampling Adequacy"
class(out) <- c("easystats_check", class(out))
Expand Down Expand Up @@ -206,7 +204,7 @@ check_sphericity_bartlett <- function(x, n = NULL, ...) {
color <- "red"
}

attr(out, "text") <- text
attr(out, "text") <- msg_text
attr(out, "color") <- color
attr(out, "title") <- "Test of Sphericity"
class(out) <- c("easystats_check", class(out))
Expand Down
Loading

0 comments on commit 281d4fd

Please sign in to comment.