From 219e6bcd6ed4c57078d6bec25dc891a09e152f8c Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 10 Sep 2023 11:38:46 +0200 Subject: [PATCH] tests --- DESCRIPTION | 2 +- R/methods_fixest.R | 2 +- tests/testthat/test-model_parameters.fixest.R | 65 ++++++++++++++++--- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f6155e000..0a989c508 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/R/methods_fixest.R b/R/methods_fixest.R index 95330f4a1..9b008b0f5 100644 --- a/R/methods_fixest.R +++ b/R/methods_fixest.R @@ -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" diff --git a/tests/testthat/test-model_parameters.fixest.R b/tests/testthat/test-model_parameters.fixest.R index 8bdc3e70e..87d32810c 100644 --- a/tests/testthat/test-model_parameters.fixest.R +++ b/tests/testthat/test-model_parameters.fixest.R @@ -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) })