Skip to content

Commit

Permalink
Wrong DFs in model_parameters.fixest (#899)
Browse files Browse the repository at this point in the history
* Wrong DFs in `model_parameters.fixest`
Fixes #892

* desc, version

* Wrong DFs in `model_parameters.fixest`
Fixes #892

* add test

* fix test

* fix
  • Loading branch information
strengejacke authored Sep 9, 2023
1 parent a8092dd commit d4c4b5a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 8 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.2
Version: 0.21.1.3
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ S3method(model_parameters,emm_list)
S3method(model_parameters,epi.2by2)
S3method(model_parameters,fa)
S3method(model_parameters,fa.ci)
S3method(model_parameters,feglm)
S3method(model_parameters,fitdistr)
S3method(model_parameters,fixest)
S3method(model_parameters,fixest_multi)
S3method(model_parameters,flac)
S3method(model_parameters,flic)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Bug fixes

* Fixed issue with wrong calculation of test-statistic and p-values in
`model_parameters()` for `fixest` models.

* Minor fixes for `dominance_analysis()`.

# parameters 0.21.1
Expand Down
72 changes: 71 additions & 1 deletion R/methods_fixest.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
# .fixest -----------------------

#' @export
model_parameters.fixest <- function(model,
ci = 0.95,
ci_method = NULL,
bootstrap = FALSE,
iterations = 1000,
standardize = NULL,
exponentiate = FALSE,
p_adjust = NULL,
summary = getOption("parameters_summary", FALSE),
keep = NULL,
drop = NULL,
verbose = TRUE,
vcov = NULL,
vcov_args = NULL,
...) {
# default ci-method, based on statistic
if (is.null(ci_method)) {
if (identical(insight::find_statistic(model), "t-statistic") && identical(model$method, "feols")) {
ci_method <- "wald"
} else {
ci_method <- "normal"
}
}

# extract model parameters table, as data frame
out <- tryCatch(
{
.model_parameters_generic(
model = model,
ci = ci,
ci_method = ci_method,
bootstrap = bootstrap,
iterations = iterations,
merge_by = "Parameter",
standardize = standardize,
exponentiate = exponentiate,
p_adjust = p_adjust,
summary = summary,
keep_parameters = keep,
drop_parameters = drop,
vcov = vcov,
vcov_args = vcov_args,
verbose = verbose,
...
)
},
error = function(e) {
NULL
}
)

if (is.null(out)) {
insight::format_error("Something went wrong... :-/")
}

attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model))
out
}


#' @export
standard_error.fixest <- function(model, vcov = NULL, vcov_args = NULL, ...) {
params <- insight::get_parameters(model)
Expand Down Expand Up @@ -31,8 +92,14 @@ degrees_of_freedom.fixest <- function(model, method = "wald", ...) {
}
method <- match.arg(
tolower(method),
choices = c("wald", "residual")
choices = c("wald", "residual", "normal")
)

# we may have Inf DF, too
if (method == "normal") {
return(Inf)
}

method <- switch(method,
"wald" = "t",
"residual" = "resid"
Expand All @@ -45,6 +112,9 @@ degrees_of_freedom.fixest <- function(model, method = "wald", ...) {

# .feglm -----------------------

#' @export
model_parameters.feglm <- model_parameters.fixest

#' @export
standard_error.feglm <- function(model, ...) {
stats <- stats::coef(summary(model))
Expand Down
2 changes: 1 addition & 1 deletion man/p_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions tests/testthat/test-model_parameters.fixest.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,38 @@ test_that("model_parameters.fixest", {
)
params <- model_parameters(m, verbose = FALSE)

expect_equal(c(nrow(params), ncol(params)), c(2, 9))
expect_equal(params$p, as.vector(fixest::pvalue(m)), tolerance = 1e-3)
expect_equal(params$df_error[1], as.vector(fixest::degrees_freedom(m, type = "t")), tolerance = 1e-3)
expect_equal(params$Coefficient, as.vector(coef(m)), tolerance = 1e-3)
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)

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


test_that("model_parameters.fixest", {
skip_on_cran()
skip_if_not_installed("fixest")
skip_if_not_installed("carData")

data(Greene, package = "carData")
d <- Greene
d$dv <- as.numeric(Greene$decision == "yes")

mod1 <- fixest::feglm(dv ~ language | judge,
data = d,
cluster = "judge", family = "logit"
)
out1 <- model_parameters(mod1)
expect_equal(out1$p, as.vector(fixest::pvalue(mod1)), tolerance = 1e-4, ignore_attr = TRUE)
expect_equal(out1$SE, as.vector(sqrt(diag(vcov(mod1)))), tolerance = 1e-4, ignore_attr = TRUE)
})


test_that("robust standard errors", {
skip_if_not_installed("fixest")
mod <- fixest::feols(mpg ~ hp + am | cyl, data = mtcars)
Expand Down

0 comments on commit d4c4b5a

Please sign in to comment.