Skip to content

Commit

Permalink
lintr, silent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 21, 2023
1 parent 54f64e2 commit cf72bb5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
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
54 changes: 25 additions & 29 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 All @@ -189,26 +187,24 @@ check_sphericity_bartlett <- function(x, n = NULL, ...) {
out <- list(chisq = statistic, p = pval, dof = df)

if (pval < 0.001) {
text <-
sprintf(
"Bartlett's test of sphericity suggests that there is sufficient significant correlation in the data for factor analysis (Chisq(%i) = %.2f, %s).",
df,
statistic,
insight::format_p(pval)
)
msg_text <- sprintf(
"Bartlett's test of sphericity suggests that there is sufficient significant correlation in the data for factor analysis (Chisq(%i) = %.2f, %s).", # nolint
df,
statistic,
insight::format_p(pval)
)
color <- "green"
} else {
text <-
sprintf(
"Bartlett's test of sphericity suggests that there is not enough significant correlation in the data for factor analysis (Chisq(%i) = %.2f, %s).",
df,
statistic,
insight::format_p(pval)
)
msg_text <- sprintf(
"Bartlett's test of sphericity suggests that there is not enough significant correlation in the data for factor analysis (Chisq(%i) = %.2f, %s).", # nolint
df,
statistic,
insight::format_p(pval)
)
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
38 changes: 23 additions & 15 deletions tests/testthat/test-pkg-fixest.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
base <- iris
names(base) <- c("y1", "y2", "x1", "x2", "species")
base_iris <- iris
names(base_iris) <- c("y1", "y2", "x1", "x2", "species")

test_that("fixest: r2", {
skip_if_not_installed("fixest")
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base_iris)
r2_res <- performance::r2(res)

expect_equal(r2_res$R2, fixest::fitstat(res, "r2")[[1]])
expect_equal(r2_res$R2_adjusted, fixest::fitstat(res, "ar2")[[1]])
expect_equal(r2_res$R2_within, fixest::fitstat(res, "wr2")[[1]])
expect_equal(r2_res$R2_within_adjusted, fixest::fitstat(res, "war2")[[1]])
expect_equal(r2_res$R2, fixest::fitstat(res, "r2")[[1]], ignore_attr = TRUE)
expect_equal(r2_res$R2_adjusted, fixest::fitstat(res, "ar2")[[1]], ignore_attr = TRUE)
expect_equal(r2_res$R2_within, fixest::fitstat(res, "wr2")[[1]], ignore_attr = TRUE)
expect_equal(r2_res$R2_within_adjusted, fixest::fitstat(res, "war2")[[1]], ignore_attr = TRUE)
})


test_that("fixest: overdispersion", {
skip_if_not_installed("fixest")
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base_iris)
expect_error(
check_overdispersion(res),
"can only be used for models from Poisson"
Expand All @@ -24,14 +26,16 @@ test_that("fixest: overdispersion", {

test_that("fixest: outliers", {
skip_if_not_installed("fixest")
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base_iris)
outliers_list <- suppressMessages(check_outliers(res))
expect_identical(attr(outliers_list, "outlier_count"), list())
})

test_that("fixest: model_performance", {
skip_if_not_installed("fixest")
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base_iris)
perf <- model_performance(res)
expect_equal(perf$AIC, 107.743, tolerance = 1e-3)
expect_equal(perf$BIC, 125.807, tolerance = 1e-3)
Expand All @@ -47,15 +51,17 @@ test_that("fixest: model_performance", {

test_that("fixest_multi: r2", {
skip_if_not_installed("fixest")
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base_iris)
r2_res <- performance::r2(res)

expect_equal(unname(r2_res[[1]]$R2), 0.837, tolerance = 1e-3)
})

test_that("fixest_multi: overdispersion", {
skip_if_not_installed("fixest")
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base_iris)
expect_error(
check_overdispersion(res),
"can only be used for models from Poisson"
Expand All @@ -64,15 +70,17 @@ test_that("fixest_multi: overdispersion", {

test_that("fixest_multi: outliers", {
skip_if_not_installed("fixest")
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base_iris)
outliers_list <- suppressMessages(check_outliers(res)[[1]])
expect_identical(attr(outliers_list, "outlier_count"), list())
})

test_that("fixest_multi: model_performance", {
skip_if_not_installed("fixest")
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base)
res2 <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base)
fixest::setFixest_nthreads(1)
res <- fixest::feols(c(y1, y2) ~ x1 + csw(x2, x2^2) | species, base_iris)
res2 <- fixest::feols(y1 ~ x1 + x2 + x2^2 | species, base_iris)
perf <- model_performance(res)
perf2 <- model_performance(res2)
expect_identical(perf[[2]], perf2)
Expand Down

0 comments on commit cf72bb5

Please sign in to comment.