diff --git a/DESCRIPTION b/DESCRIPTION index 1bddd9f4a..f2f818149 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -46,6 +46,7 @@ Suggests: dplyr (>= 1.1), effectsize, emmeans, + fixest, gamm4, ggplot2 (>= 3.5.0), gt, diff --git a/NEWS.md b/NEWS.md index efc2d873b..d597bbe15 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# datawizard (development) + +CHANGES + +* `standardize()` now works on `fixest` estimations (#665). + # datawizard 1.3.0 BREAKING CHANGES diff --git a/R/standardize.models.R b/R/standardize.models.R index cd11d01ae..12a56d331 100644 --- a/R/standardize.models.R +++ b/R/standardize.models.R @@ -80,14 +80,6 @@ standardize.default <- function( return(x) } - # check model formula. Some notations don't work when standardizing data - insight::formula_ok( - x, - action = "error", - prefix_msg = "Model cannot be standardized.", - verbose = verbose - ) - data_std <- NULL # needed to avoid note .standardize_models( x, @@ -112,6 +104,14 @@ standardize.default <- function( update_expr, ... ) { + # check model formula. Some notations don't work when standardizing data + insight::formula_ok( + x, + action = "error", + prefix_msg = "Model cannot be standardized.", + verbose = verbose + ) + m_info <- .get_model_info(x, ...) model_data <- insight::get_data(x, source = "mf", verbose = FALSE) @@ -423,7 +423,8 @@ standardize.mediate <- function( # # control.value <- temp_vals[1] # treat.value <- temp_vals[2] - # if (verbose) insight::format_alert("control and treatment values have been rescaled to their standardized scales.") + # if (verbose) insight::format_alert("control and treatment values have been + # rescaled to their standardized scales.") # } if (verbose && !all(c(control.value, treat.value) %in% c(0, 1))) { @@ -471,8 +472,29 @@ standardize.biglm <- standardize.wbm # biglm doesn't regit the model to new data - it ADDs MORE data to the model. #' @export -standardize.fixest <- standardize.wbm -# fixest handles its own environment - so we can't update +# Almost the same as `standardize.default()` but we pass `use_calling_env` in +# update(). +standardize.fixest <- function( + x, + robust = FALSE, + two_sd = FALSE, + weights = TRUE, + verbose = TRUE, + include_response = TRUE, + ... +) { + data_std <- NULL # needed to avoid note + .standardize_models( + x, + robust = robust, + two_sd = two_sd, + weights = weights, + verbose = verbose, + include_response = include_response, + update_expr = stats::update(x, data = data_std, use_calling_env = FALSE), + ... + ) +} # helper ---------------------------- diff --git a/tests/testthat/test-standardize_models.R b/tests/testthat/test-standardize_models.R index e2043240e..1c41b03f0 100644 --- a/tests/testthat/test-standardize_models.R +++ b/tests/testthat/test-standardize_models.R @@ -401,3 +401,55 @@ test_that("brms", { regexp = "without adjusting priors may lead to bogus" ) }) + +# fixest -------------------------------------------------------------------- + +test_that("fixest", { + skip_if_not_installed("fixest") + + mtcars_stand <- standardize(mtcars) + orig <- fixest::feols( + drat ~ mpg + hp^2 | cyl + am, + data = mtcars, + se = "hetero" + ) + # TODO: Remove this suppressWarnings() when a new version of `fixest` that + # contains the fix for https://github.com/lrberge/fixest/issues/618 is on CRAN + # (CRAN version is 0.13.2 at the time of writing). + suppressWarnings({ + auto_stand <- standardize(orig) + }) + manual_stand <- fixest::feols( + drat ~ mpg + hp^2 | cyl + am, + data = mtcars_stand, + se = "hetero" + ) + + # Need to unname because I(hp^2) in the manual one becomes I(I(hp ^2)) in the + # automated one. + expect_identical( + unname(auto_stand$coefficients), + unname(manual_stand$coefficients) + ) + expect_identical(unname(auto_stand$se), unname(manual_stand$se)) + + ### Inform the user if some terms are log() or sqrt() + orig <- fixest::feols( + drat ~ log(mpg) | cyl + am, + data = mtcars + ) + # TODO: same as above + expect_message( + suppressWarnings(standardize(orig)), + "Formula contains log- or sqrt-terms" + ) + orig <- fixest::feols( + drat ~ sqrt(mpg) | cyl + am, + data = mtcars + ) + # TODO: same as above + expect_message( + suppressWarnings(standardize(orig)), + "Formula contains log- or sqrt-terms" + ) +})