Skip to content

Commit 790a1d9

Browse files
Add support for standardize() on objects of class fixest (#665)
1 parent c65eedc commit 790a1d9

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Suggests:
4646
dplyr (>= 1.1),
4747
effectsize,
4848
emmeans,
49+
fixest,
4950
gamm4,
5051
ggplot2 (>= 3.5.0),
5152
gt,

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# datawizard (development)
2+
3+
CHANGES
4+
5+
* `standardize()` now works on `fixest` estimations (#665).
6+
17
# datawizard 1.3.0
28

39
BREAKING CHANGES

R/standardize.models.R

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ standardize.default <- function(
8080
return(x)
8181
}
8282

83-
# check model formula. Some notations don't work when standardizing data
84-
insight::formula_ok(
85-
x,
86-
action = "error",
87-
prefix_msg = "Model cannot be standardized.",
88-
verbose = verbose
89-
)
90-
9183
data_std <- NULL # needed to avoid note
9284
.standardize_models(
9385
x,
@@ -112,6 +104,14 @@ standardize.default <- function(
112104
update_expr,
113105
...
114106
) {
107+
# check model formula. Some notations don't work when standardizing data
108+
insight::formula_ok(
109+
x,
110+
action = "error",
111+
prefix_msg = "Model cannot be standardized.",
112+
verbose = verbose
113+
)
114+
115115
m_info <- .get_model_info(x, ...)
116116
model_data <- insight::get_data(x, source = "mf", verbose = FALSE)
117117

@@ -423,7 +423,8 @@ standardize.mediate <- function(
423423
#
424424
# control.value <- temp_vals[1]
425425
# treat.value <- temp_vals[2]
426-
# if (verbose) insight::format_alert("control and treatment values have been rescaled to their standardized scales.")
426+
# if (verbose) insight::format_alert("control and treatment values have been
427+
# rescaled to their standardized scales.")
427428
# }
428429

429430
if (verbose && !all(c(control.value, treat.value) %in% c(0, 1))) {
@@ -471,8 +472,29 @@ standardize.biglm <- standardize.wbm
471472
# biglm doesn't regit the model to new data - it ADDs MORE data to the model.
472473

473474
#' @export
474-
standardize.fixest <- standardize.wbm
475-
# fixest handles its own environment - so we can't update
475+
# Almost the same as `standardize.default()` but we pass `use_calling_env` in
476+
# update().
477+
standardize.fixest <- function(
478+
x,
479+
robust = FALSE,
480+
two_sd = FALSE,
481+
weights = TRUE,
482+
verbose = TRUE,
483+
include_response = TRUE,
484+
...
485+
) {
486+
data_std <- NULL # needed to avoid note
487+
.standardize_models(
488+
x,
489+
robust = robust,
490+
two_sd = two_sd,
491+
weights = weights,
492+
verbose = verbose,
493+
include_response = include_response,
494+
update_expr = stats::update(x, data = data_std, use_calling_env = FALSE),
495+
...
496+
)
497+
}
476498

477499
# helper ----------------------------
478500

tests/testthat/test-standardize_models.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,55 @@ test_that("brms", {
401401
regexp = "without adjusting priors may lead to bogus"
402402
)
403403
})
404+
405+
# fixest --------------------------------------------------------------------
406+
407+
test_that("fixest", {
408+
skip_if_not_installed("fixest")
409+
410+
mtcars_stand <- standardize(mtcars)
411+
orig <- fixest::feols(
412+
drat ~ mpg + hp^2 | cyl + am,
413+
data = mtcars,
414+
se = "hetero"
415+
)
416+
# TODO: Remove this suppressWarnings() when a new version of `fixest` that
417+
# contains the fix for https://github.com/lrberge/fixest/issues/618 is on CRAN
418+
# (CRAN version is 0.13.2 at the time of writing).
419+
suppressWarnings({
420+
auto_stand <- standardize(orig)
421+
})
422+
manual_stand <- fixest::feols(
423+
drat ~ mpg + hp^2 | cyl + am,
424+
data = mtcars_stand,
425+
se = "hetero"
426+
)
427+
428+
# Need to unname because I(hp^2) in the manual one becomes I(I(hp ^2)) in the
429+
# automated one.
430+
expect_identical(
431+
unname(auto_stand$coefficients),
432+
unname(manual_stand$coefficients)
433+
)
434+
expect_identical(unname(auto_stand$se), unname(manual_stand$se))
435+
436+
### Inform the user if some terms are log() or sqrt()
437+
orig <- fixest::feols(
438+
drat ~ log(mpg) | cyl + am,
439+
data = mtcars
440+
)
441+
# TODO: same as above
442+
expect_message(
443+
suppressWarnings(standardize(orig)),
444+
"Formula contains log- or sqrt-terms"
445+
)
446+
orig <- fixest::feols(
447+
drat ~ sqrt(mpg) | cyl + am,
448+
data = mtcars
449+
)
450+
# TODO: same as above
451+
expect_message(
452+
suppressWarnings(standardize(orig)),
453+
"Formula contains log- or sqrt-terms"
454+
)
455+
})

0 commit comments

Comments
 (0)