Skip to content

Commit

Permalink
also use remotes datawizard (#971)
Browse files Browse the repository at this point in the history
* also use remotes datawizard

* lintr, fix group arg

* fix issues

* Update DESCRIPTION

* fix tests

* fix vignette

* Update DESCRIPTION

* fix lintr
  • Loading branch information
strengejacke authored May 22, 2024
1 parent ee8c603 commit 6b2de8f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 51 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,4 @@ Config/Needs/website:
r-lib/pkgdown,
easystats/easystatstemplate
Config/rcmdcheck/ignore-inconsequential-notes: true
Remotes: easystats/insight
Remotes: easystats/insight, easystats/datawizard, easystats/performance, easystats/bayestestR
30 changes: 15 additions & 15 deletions R/standardize_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ standardize_info.default <- function(model,
types <- parameters_type(model)
# model_matrix <- as.data.frame(stats::model.matrix(model))
model_matrix <- as.data.frame(insight::get_modelmatrix(model))
data <- insight::get_data(model, source = "mf", verbose = FALSE)
model_data <- insight::get_data(model, source = "mf", verbose = FALSE)
wgts <- insight::get_weights(model, na_rm = TRUE)

# validation check for ZI
Expand Down Expand Up @@ -94,7 +94,7 @@ standardize_info.default <- function(model,
# Response - Smart
out <- merge(
out,
.std_info_response_smart(model, mi, data, model_matrix, types, robust = robust, w = wgts),
.std_info_response_smart(model, mi, data = model_data, model_matrix, types, robust = robust, w = wgts),
by = "Parameter", all = TRUE
)

Expand All @@ -109,7 +109,7 @@ standardize_info.default <- function(model,
out <- merge(
out,
.std_info_predictors_smart(model,
data,
data = model_data,
params,
types,
robust = robust,
Expand All @@ -134,7 +134,7 @@ standardize_info.default <- function(model,
model, mi,
params,
model_matrix,
data,
data = model_data,
types = types$Type,
robust = robust,
two_sd = two_sd,
Expand Down Expand Up @@ -181,11 +181,11 @@ standardize_info.default <- function(model,
# Get deviations for all parameters
means <- deviations <- rep(NA_real_, times = length(params))
for (i in seq_along(params)) {
var <- params[i]
variable <- params[i]
info <- .std_info_predictor_smart(
data = data,
variable = types[types$Parameter == var, "Variable"],
type = types[types$Parameter == var, "Type"],
variable = types[types$Parameter == variable, "Variable"],
type = types[types$Parameter == variable, "Type"],
robust = robust,
two_sd = two_sd,
weights = w
Expand Down Expand Up @@ -213,7 +213,7 @@ standardize_info.default <- function(model,
two_sd = FALSE,
weights = NULL,
...) {
if (type == "intercept") {
if (type == "intercept") { # nolint
info <- list(sd = 0, mean = 0)
} else if (type == "numeric") {
info <- .compute_std_info(
Expand Down Expand Up @@ -272,12 +272,12 @@ standardize_info.default <- function(model,
# Get deviations for all parameters
means <- deviations <- rep(NA_real_, length = length(names(model_matrix)))
for (i in seq_along(names(model_matrix))) {
var <- names(model_matrix)[i]
variable <- names(model_matrix)[i]
if (types[i, "Type"] == "intercept") {
means[i] <- deviations[i] <- 0
} else {
std_info <- .compute_std_info(
data = model_matrix, variable = var,
data = model_matrix, variable = variable,
robust = robust, two_sd = two_sd, weights = w
)
deviations[i] <- std_info$sd
Expand Down Expand Up @@ -337,9 +337,9 @@ standardize_info.default <- function(model,
}
means <- deviations <- rep(NA_real_, length = length(names(model_matrix)))
for (i in seq_along(names(model_matrix))) {
var <- names(model_matrix)[i]
if (any(types$Parameter == var) && types$Link[types$Parameter == var] == "Difference") {
parent_var <- types$Variable[types$Parameter == var]
variable <- names(model_matrix)[i]
if (any(types$Parameter == variable) && types$Link[types$Parameter == variable] == "Difference") {
parent_var <- types$Variable[types$Parameter == variable]
intercept <- unique(data[[parent_var]])[1]
response_at_intercept <- response[data[[parent_var]] == intercept]
weights_at_intercept <- if (length(w)) w[data[[parent_var]] == intercept] else NULL
Expand Down Expand Up @@ -433,7 +433,7 @@ standardize_info.default <- function(model,
is_within <- logical(length = length(params))
is_within[] <- NA
for (i in seq_along(params)) {
if (types[i] == "intercept") {
if (types[i] == "intercept") { # nolint
is_within[i] <- FALSE
} else if (types[i] == "numeric") {
is_within[i] <- insight::clean_names(params[i]) %in% within_vars
Expand All @@ -459,7 +459,7 @@ standardize_info.default <- function(model,

dm <- datawizard::demean(cbind(id, temp_d),
select = colnames(temp_d),
group = "id"
by = "id"
)
dm <- dm[, paste0(colnames(temp_d), "_between"), drop = FALSE]

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-marginaleffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ skip_if_not_installed("rstanarm")
test_that("marginaleffects()", {
# Frequentist
x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
model <- marginaleffects::avg_slopes(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length")
model <- marginaleffects::avg_slopes(x, newdata = insight::get_datagrid(x, by = "Species"), variables = "Petal.Length")
out <- parameters(model)
expect_identical(nrow(out), 1L)
cols <- c("Parameter", "Comparison", "Coefficient", "SE", "Statistic", "p", "S", "CI", "CI_low", "CI_high")
Expand All @@ -23,7 +23,7 @@ test_that("marginaleffects()", {
chains = 1
)
)
model <- marginaleffects::avg_slopes(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length")
model <- marginaleffects::avg_slopes(x, newdata = insight::get_datagrid(x, by = "Species"), variables = "Petal.Length")
expect_identical(nrow(parameters(model)), 1L)
})

Expand All @@ -46,7 +46,7 @@ test_that("comparisons()", {
data(iris)
# Frequentist
x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
m <- marginaleffects::avg_comparisons(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length")
m <- marginaleffects::avg_comparisons(x, newdata = insight::get_datagrid(x, by = "Species"), variables = "Petal.Length")
expect_identical(nrow(parameters(m)), 1L)
out <- parameters(m, exponentiate = TRUE)
expect_equal(out$Coefficient, 1.393999, tolerance = 1e-4)
Expand All @@ -63,7 +63,7 @@ test_that("comparisons()", {
)
m <- marginaleffects::avg_slopes(
x,
newdata = insight::get_datagrid(x, at = "Species"),
newdata = insight::get_datagrid(x, by = "Species"),
variables = "Petal.Length"
)
expect_identical(nrow(parameters(m)), 1L)
Expand All @@ -85,7 +85,7 @@ test_that("multiple contrasts: Issue #779", {
cmp <- suppressWarnings(marginaleffects::comparisons(
mod,
variables = c("gear", "cyl"),
newdata = insight::get_datagrid(mod, at = c("gear", "cyl")),
newdata = insight::get_datagrid(mod, by = c("gear", "cyl")),
cross = TRUE
))
cmp <- suppressWarnings(parameters(cmp))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-model_parameters.fixest.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_that("model_parameters.fixest", {

qol_cancer <- cbind(
qol_cancer,
datawizard::demean(qol_cancer, select = c("phq4", "QoL"), group = "ID")
datawizard::demean(qol_cancer, select = c("phq4", "QoL"), by = "ID")
)

m1 <- fixest::feols(QoL ~ time + phq4 | ID, data = qol_cancer)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-model_parameters.mixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test_that("model_parameters.mixed-all_pars", {
data("qol_cancer")
qol_cancer <- cbind(
qol_cancer,
demean(qol_cancer, select = c("phq4", "QoL"), group = "ID")
demean(qol_cancer, select = c("phq4", "QoL"), by = "ID")
)
model <- lme4::lmer(
QoL ~ time + phq4_within + phq4_between + (1 | ID),
Expand Down
52 changes: 32 additions & 20 deletions tests/testthat/test-standardize_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ test_that("standardize_parameters (lm with ci)", {
# aov ---------------------------------------------------------------------
test_that("standardize_parameters (aov)", {
dat2 <- iris
dat2$Cat1 <- rep(c("A", "B"), length.out = nrow(dat2))
dat2$Cat1 <- rep_len(c("A", "B"), nrow(dat2))
dat3 <<- dat2

m_aov <- aov(Sepal.Length ~ Species * Cat1, data = dat3)
Expand Down Expand Up @@ -198,7 +198,9 @@ test_that("standardize_parameters (with functions / interactions)", {
m1 <- lm(exp(cyl) ~ am + sqrt(mpg), mtcars)
m2 <- lm(cyl_exp ~ am + mpg_sqrt, mtcars)

expect_message(stdX <- standardize_parameters(m1, method = "refit"))
expect_message({
stdX <- standardize_parameters(m1, method = "refit")
})
expect_false(isTRUE(all.equal(
stdX[[2]],
standardize_parameters(m2, method = "refit")[[2]]
Expand Down Expand Up @@ -258,7 +260,8 @@ test_that("standardize_parameters (exponentiate)", {
)
expect_equal(
mod_refit[[2]][-1],
exp(standardize_parameters(mod_b, method = "basic")[[2]])[-1]
exp(standardize_parameters(mod_b, method = "basic")[[2]])[-1],
tolerance = 1e-5
)


Expand All @@ -270,15 +273,18 @@ test_that("standardize_parameters (exponentiate)", {

expect_equal(
mod_refit[[2]][-1],
standardize_parameters(mod_b, method = "basic", exponentiate = TRUE)[[2]][-1]
standardize_parameters(mod_b, method = "basic", exponentiate = TRUE)[[2]][-1],
tolerance = 1e-5
)
expect_equal(
mod_refit[[2]][-1],
standardize_parameters(mod_b, method = "posthoc", exponentiate = TRUE)[[2]][-1]
standardize_parameters(mod_b, method = "posthoc", exponentiate = TRUE)[[2]][-1],
tolerance = 1e-5
)
expect_equal(
mod_refit[[2]][-1],
exp(standardize_parameters(mod_b, method = "basic")[[2]])[-1]
exp(standardize_parameters(mod_b, method = "basic")[[2]])[-1],
tolerance = 1e-5
)
})

Expand All @@ -289,12 +295,12 @@ test_that("standardize_parameters (Bayes)", {
skip_if_not_installed("rstanarm")

set.seed(1234)
suppressWarnings(
suppressWarnings({
model <- rstanarm::stan_glm(Sepal.Length ~ Species + Petal.Width,
data = iris,
iter = 500, refresh = 0
)
)
})

expect_equal(
suppressWarnings(standardize_parameters(model, method = "refit")$Std_Median[1:4]),
Expand All @@ -309,7 +315,7 @@ test_that("standardize_parameters (Bayes)", {
)

posts <- standardize_posteriors(model, method = "posthoc")
expect_equal(dim(posts), c(1000, 4))
expect_identical(dim(posts), c(1000L, 4L))
expect_s3_class(posts, "data.frame")
})

Expand All @@ -324,7 +330,7 @@ test_that("standardize_parameters (Pseudo - GLMM)", {
X = rnorm(1000),
Z = rnorm(1000),
C = sample(letters[1:3], size = 1000, replace = TRUE),
ID = sort(rep(letters, length.out = 1000))
ID = sort(rep_len(letters, 1000))
)
dat <- transform(dat, Y = X + Z + rnorm(1000))
dat <- cbind(dat, datawizard::demean(dat, c("X", "Z"), "ID"))
Expand All @@ -340,7 +346,7 @@ test_that("standardize_parameters (Pseudo - GLMM)", {

## Correctly identify within and between terms
dev_resp <- standardize_info(m, include_pseudo = TRUE)$Deviation_Response_Pseudo
expect_equal(insight::n_unique(dev_resp[c(2, 4, 5, 6)]), 1)
expect_identical(insight::n_unique(dev_resp[c(2, 4, 5, 6)]), 1L)
expect_true(dev_resp[2] != dev_resp[3])


Expand All @@ -354,16 +360,18 @@ test_that("standardize_parameters (Pseudo - GLMM)", {

m0 <- lme4::lmer(Y ~ 1 + (1 | ID), data = dat)
m0v <- insight::get_variance(m0)
SD_y <- c(sqrt(m0v$var.residual), sqrt(m0v$var.intercept))
SD_y <- sqrt(c(m0v$var.residual, m0v$var.intercept))
SD_y <- SD_y[c(1, 2, 1, 1, 1)]

expect_equal(
data.frame(Deviation_Response_Pseudo = c(SD_y[2], SD_y), Deviation_Pseudo = c(0, SD_x)),
standardize_info(m, include_pseudo = TRUE)[, c("Deviation_Response_Pseudo", "Deviation_Pseudo")]
standardize_info(m, include_pseudo = TRUE)[, c("Deviation_Response_Pseudo", "Deviation_Pseudo")],
tolerance = 1e-5
)
expect_equal(
standardize_parameters(m, method = "pseudo")$Std_Coefficient[-1],
unname(b * SD_x / SD_y)
unname(b * SD_x / SD_y),
tolerance = 1e-5
)


Expand Down Expand Up @@ -463,8 +471,8 @@ test_that("include_response | (g)lm", {
par_z2 <- standardize_parameters(m, method = "basic", include_response = FALSE)

expect_equal(coef(m_z), par_z1$Std_Coefficient, ignore_attr = TRUE)
expect_equal(par_z1$Std_Coefficient[-1], par_z2$Std_Coefficient[-1])
expect_equal(par_z0$Std_Coefficient * sd(iris$Sepal.Length), par_z2$Std_Coefficient)
expect_equal(par_z1$Std_Coefficient[-1], par_z2$Std_Coefficient[-1], tolerance = 1e-5)
expect_equal(par_z0$Std_Coefficient * sd(iris$Sepal.Length), par_z2$Std_Coefficient, tolerance = 1e-5)

# glm ---
m <- glm(am ~ mpg, mtcars, family = binomial())
Expand All @@ -485,14 +493,14 @@ test_that("include_response | parameters", {
pars <- model_parameters(m, effects = "fixed")
pars_z0 <- standardize_parameters(pars, method = "basic")
pars_z1 <- standardize_parameters(pars, method = "basic", include_response = FALSE)
expect_equal(pars_z0$Std_Coefficient[-1] * sd(iris$Sepal.Length), pars_z1$Std_Coefficient[-1])
expect_equal(pars_z0$Std_Coefficient[-1] * sd(iris$Sepal.Length), pars_z1$Std_Coefficient[-1], tolerance = 1e-5)

# boot ---
skip_if_not_installed("boot")
pars <- bootstrap_parameters(m)
pars_z0 <- standardize_parameters(pars, method = "basic")
pars_z1 <- standardize_parameters(pars, method = "basic", include_response = FALSE)
expect_equal(pars_z0$Std_Coefficient[-1] * sd(iris$Sepal.Length), pars_z1$Std_Coefficient[-1])
expect_equal(pars_z0$Std_Coefficient[-1] * sd(iris$Sepal.Length), pars_z1$Std_Coefficient[-1], tolerance = 1e-5)
})


Expand All @@ -504,8 +512,12 @@ test_that("include_response | bayes", {
iris$Sepal.Length <- iris$Sepal.Length * 5
m <- rstanarm::stan_glm(Sepal.Length ~ Petal.Length + Petal.Width, data = iris, refresh = 0)

expect_warning(m_z <- datawizard::standardize(m, include_response = FALSE))
expect_warning(par_z1 <- standardize_posteriors(m, include_response = FALSE))
expect_warning({
m_z <- datawizard::standardize(m, include_response = FALSE)
})
expect_warning({
par_z1 <- standardize_posteriors(m, include_response = FALSE)
})
par_z0 <- standardize_posteriors(m, method = "basic")
par_z2 <- standardize_posteriors(m, method = "basic", include_response = FALSE)

Expand Down
Loading

0 comments on commit 6b2de8f

Please sign in to comment.