Skip to content

Commit

Permalink
lintr
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 21, 2023
1 parent 8043409 commit c4465c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
11 changes: 4 additions & 7 deletions R/check_clusterstructure.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,25 @@ plot.check_clusterstructure <- function(x, ...) {
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" &&
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

0 comments on commit c4465c1

Please sign in to comment.