Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong DFs in model_parameters.fixest #899

Merged
merged 6 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -7,11 +68,11 @@
if (!is.null(vcov)) {
# we don't want to wrap this in a tryCatch because the `fixest` error is
# informative when `vcov` is wrong.
V <- insight::get_varcov(model, vcov = vcov, vcov_args = vcov_args)

Check warning on line 71 in R/methods_fixest.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_fixest.R,line=71,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
SE <- sqrt(diag(V))

Check warning on line 72 in R/methods_fixest.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_fixest.R,line=72,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
} else {
stats <- summary(model)
SE <- as.vector(stats$se)

Check warning on line 75 in R/methods_fixest.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_fixest.R,line=75,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
}

.data_frame(
Expand All @@ -31,11 +92,17 @@
}
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",

Check warning on line 104 in R/methods_fixest.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_fixest.R,line=104,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"residual" = "resid"

Check warning on line 105 in R/methods_fixest.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_fixest.R,line=105,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
)
fixest::degrees_freedom(model, type = method)
}
Expand All @@ -45,6 +112,9 @@

# .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
Loading