Skip to content

Commit

Permalink
fix issues in find_statistic for fixest (#802)
Browse files Browse the repository at this point in the history
* fix issues in  find_statistic for fixest

* news, version

* typo
  • Loading branch information
strengejacke authored Sep 9, 2023
1 parent 842c6e2 commit f12c0cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 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: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.19.3.3
Version: 0.19.3.4
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

* Fixed issue with invalid multibyte strings in `trim_ws()`.

* Fixed issue in `find_statistic()` for models from package *fixest*.

# insight 0.19.3

## Breaking changes
Expand Down
9 changes: 6 additions & 3 deletions R/find_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ find_statistic <- function(x, ...) {
"bam", "bigglm",
"cgam", "cgamm",
"eglm", "emmGrid", "emm_list",
"fixest",
"gam", "glm", "Glm", "glmc", "glmerMod", "glmRob", "glmrob",
"pseudoglm",
"scam",
Expand All @@ -191,7 +190,7 @@ find_statistic <- function(x, ...) {

# pattern finding ----------------------------------------------------------

unclear.mods <- "plm"
unclear.mods <- c("plm", "fixest")

if (inherits(x, "glht")) {
if (x$df == 0) {
Expand Down Expand Up @@ -290,7 +289,11 @@ find_statistic <- function(x, ...) {
# ambiguous cases -----------------------------------------------------------

if (model_class %in% unclear.mods) {
col_names <- colnames(as.data.frame(summary(x)$coefficients))
if (model_class == "fixest") {
col_names <- colnames(as.data.frame(summary(x)$coeftable))
} else {
col_names <- colnames(as.data.frame(summary(x)$coefficients))
}

t_names <- c(
"t",
Expand Down
43 changes: 34 additions & 9 deletions tests/testthat/test-fixest.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
skip_on_os("mac")
skip_if(getRversion() < "3.6.0")
skip_if_not_installed("fixest")
skip_if_not_installed("fixest")

# avoid warnings
fixest::setFixest_nthreads(1)
Expand Down Expand Up @@ -147,13 +146,12 @@ test_that("get_data", {

skip_if_not_installed("parameters")
test_that("get_df", {
expect_equal(get_df(m1, type = "residual"), 38290, ignore_attr = TRUE)
expect_equal(get_df(m1, type = "residual"), fixest::degrees_freedom(m1, type = "resid"), ignore_attr = TRUE)
expect_equal(get_df(m1, type = "normal"), Inf, ignore_attr = TRUE)
## TODO: check if statistic is z or t for this model
expect_equal(get_df(m1, type = "wald"), 14, ignore_attr = TRUE)
## statistic is t for this model
expect_equal(get_df(m1, type = "wald"), fixest::degrees_freedom(m1, type = "t"), ignore_attr = TRUE)
})


test_that("find_formula", {
expect_length(find_formula(m1), 2)
expect_equal(
Expand Down Expand Up @@ -256,15 +254,42 @@ test_that("is_multivariate", {
})

test_that("find_statistic", {
expect_identical(find_statistic(m1), "z-statistic")
expect_identical(find_statistic(m2), "t-statistic")
out <- capture.output(summary(m1))
if (any(grepl("t value", out, fixed = TRUE))) {
expect_identical(find_statistic(m1), "t-statistic")
} else {
expect_identical(find_statistic(m1), "z-statistic")
}
out <- capture.output(summary(m2))
if (any(grepl("t value", out, fixed = TRUE))) {
expect_identical(find_statistic(m2), "t-statistic")
} else {
expect_identical(find_statistic(m2), "z-statistic")
}
out <- capture.output(summary(m3))
if (any(grepl("t value", out, fixed = TRUE))) {
expect_identical(find_statistic(m3), "t-statistic")
} else {
expect_identical(find_statistic(m3), "z-statistic")
}
out <- capture.output(summary(m4))
if (any(grepl("t value", out, fixed = TRUE))) {
expect_identical(find_statistic(m4), "t-statistic")
} else {
expect_identical(find_statistic(m4), "z-statistic")
}
})

test_that("get_statistic", {
stat <- get_statistic(m1)
expect_equal(stat$Statistic, -13.212695, tolerance = 1e-3)
out <- as.data.frame(summary(m1)$coeftable)
expect_equal(stat$Statistic, out[, "t value"], tolerance = 1e-3, ignore_attr = TRUE)
stat <- get_statistic(m2)
expect_equal(stat$Statistic, -14.065336, tolerance = 1e-3)
out <- as.data.frame(summary(m2)$coeftable)
expect_equal(stat$Statistic, out[, "t value"], tolerance = 1e-3, ignore_attr = TRUE)
stat <- get_statistic(m3)
out <- as.data.frame(summary(m3)$coeftable)
expect_equal(stat$Statistic, out[, "t value"], tolerance = 1e-3, ignore_attr = TRUE)
})

test_that("get_predicted", {
Expand Down

0 comments on commit f12c0cf

Please sign in to comment.