Skip to content
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Suggests:
dplyr (>= 1.1),
effectsize,
emmeans,
fixest,
gamm4,
ggplot2 (>= 3.5.0),
gt,
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# datawizard (development)

CHANGES

* `standardize()` now works on `fixest` estimations (#665).

# datawizard 1.3.0

BREAKING CHANGES
Expand Down
44 changes: 33 additions & 11 deletions R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@
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,
Expand All @@ -109,9 +101,17 @@
weights = TRUE,
verbose = TRUE,
include_response = TRUE,
update_expr,

Check warning on line 104 in R/standardize.models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/standardize.models.R,line=104,col=3,[function_argument_linter] Arguments without defaults should come before arguments with defaults.

Check warning on line 104 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=104,col=3,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
...
) {
# 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)

Expand Down Expand Up @@ -392,7 +392,7 @@
treat.value <- x$treat.value

if (!is.null(covs)) {
covs <- mapply(

Check warning on line 395 in R/standardize.models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/standardize.models.R,line=395,col=13,[undesirable_function_linter] Avoid undesirable function "mapply".

Check warning on line 395 in R/standardize.models.R

View workflow job for this annotation

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

file=R/standardize.models.R,line=395,col=13,[undesirable_function_linter] Avoid undesirable function "mapply".
.rescale_fixed_values,
covs,
names(covs),
Expand Down Expand Up @@ -423,7 +423,8 @@
#
# 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))) {
Expand Down Expand Up @@ -471,8 +472,29 @@
# 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 ----------------------------

Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/test-standardize_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})
Loading