Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 10, 2023
1 parent d4c4b5a commit 219e6bc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 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: parameters
Title: Processing of Model Parameters
Version: 0.21.1.3
Version: 0.21.1.4
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 1 addition & 1 deletion R/methods_fixest.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ model_parameters.fixest <- function(model,
...) {
# default ci-method, based on statistic
if (is.null(ci_method)) {
if (identical(insight::find_statistic(model), "t-statistic") && identical(model$method, "feols")) {
if (identical(insight::find_statistic(model), "t-statistic")) {
ci_method <- "wald"
} else {
ci_method <- "normal"
Expand Down
65 changes: 56 additions & 9 deletions tests/testthat/test-model_parameters.fixest.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,75 @@
test_that("model_parameters.fixest", {
skip_on_cran()
skip_if_not_installed("fixest")
skip_if_not_installed("carData")

# avoid warnings
fixest::setFixest_nthreads(1)

data("qol_cancer")
data(trade, package = "fixest")
data(Greene, package = "carData")
data(iris)

d <- Greene
d$dv <- as.numeric(Greene$decision == "yes")

qol_cancer <- cbind(
qol_cancer,
datawizard::demean(qol_cancer, select = c("phq4", "QoL"), group = "ID")
)

m <- fixest::feols(
QoL ~ time + phq4 | ID,
data = qol_cancer
)
params <- model_parameters(m, verbose = FALSE)
m1 <- fixest::feols(QoL ~ time + phq4 | ID, data = qol_cancer)
m2 <- fixest::femlm(Euros ~ log(dist_km) | Origin + Destination + Product, data = trade)
m3 <- fixest::femlm(log1p(Euros) ~ log(dist_km) | Origin + Destination + Product, data = trade, family = "gaussian")
m4 <- fixest::feglm(Euros ~ log(dist_km) | Origin + Destination + Product, data = trade, family = "poisson")
m5 <- fixest::feols(Sepal.Width ~ Petal.Length | Species | Sepal.Length ~ Petal.Width, data = iris)
m6 <- fixest::feglm(dv ~ language | judge, data = d, cluster = "judge", family = "logit")

params <- model_parameters(m1, verbose = FALSE)
expect_identical(c(nrow(params), ncol(params)), c(2L, 9L))
expect_equal(params$p, as.vector(fixest::pvalue(m)), tolerance = 1e-4)
expect_equal(params$df_error[1], as.vector(fixest::degrees_freedom(m, type = "t")), tolerance = 1e-4)
expect_equal(params$Coefficient, as.vector(coef(m)), tolerance = 1e-4)
expect_equal(params$p, as.vector(fixest::pvalue(m1)), tolerance = 1e-4)
expect_equal(params$df_error[1], as.vector(fixest::degrees_freedom(m1, type = "t")), tolerance = 1e-4)
expect_equal(params$Coefficient, as.vector(coef(m1)), tolerance = 1e-4)

# currently, a bug for fixest 10.4 on R >= 4.3
# skip_if_not(getRversion() < "4.2.0")
expect_snapshot(
model_parameters(m, summary = TRUE, verbose = FALSE)
model_parameters(m1, summary = TRUE, verbose = FALSE)
)

# Poission, df = Inf
params <- model_parameters(m2, verbose = FALSE)
expect_identical(c(nrow(params), ncol(params)), c(1L, 9L))
expect_equal(params$p, as.vector(fixest::pvalue(m2)), tolerance = 1e-4)
expect_identical(params$df_error[1], Inf)
expect_equal(params$Coefficient, as.vector(coef(m2)), tolerance = 1e-4)

params <- model_parameters(m3, verbose = FALSE)
expect_identical(c(nrow(params), ncol(params)), c(1L, 9L))
expect_equal(params$p, as.vector(fixest::pvalue(m3)), tolerance = 1e-4)
expect_equal(params$df_error[1], as.vector(fixest::degrees_freedom(m3, type = "t")), tolerance = 1e-4)
expect_equal(params$Coefficient, as.vector(coef(m3)), tolerance = 1e-4)

# Poission, df = Inf
params <- model_parameters(m4, verbose = FALSE)
expect_identical(c(nrow(params), ncol(params)), c(1L, 9L))
expect_equal(params$p, as.vector(fixest::pvalue(m4)), tolerance = 1e-4)
expect_identical(params$df_error[1], Inf)
expect_equal(params$Coefficient, as.vector(coef(m4)), tolerance = 1e-4)

params <- model_parameters(m5, verbose = FALSE)
expect_identical(c(nrow(params), ncol(params)), c(2L, 9L))
expect_equal(params$p, as.vector(fixest::pvalue(m5)), tolerance = 1e-4)
expect_equal(params$df_error[1], as.vector(fixest::degrees_freedom(m5, type = "t")), tolerance = 1e-4)
expect_equal(params$Coefficient, as.vector(coef(m5)), tolerance = 1e-4)

# logit, df = Inf
params <- model_parameters(m6, verbose = FALSE)
expect_identical(c(nrow(params), ncol(params)), c(1L, 9L))
expect_equal(params$p, as.vector(fixest::pvalue(m6)), tolerance = 1e-4)
expect_identical(params$df_error[1], Inf)
expect_equal(params$Coefficient, as.vector(coef(m6)), tolerance = 1e-4)
})


Expand Down

0 comments on commit 219e6bc

Please sign in to comment.