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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: modelbased
Title: Estimation of Model-Based Predictions, Contrasts and Means
Version: 0.8.9.8
Version: 0.8.9.10
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
2 changes: 1 addition & 1 deletion R/estimate_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
# Summarize and clean
trends <- parameters::parameters(estimated, ci = ci, ...)
# remove redundant columns
trends <- datawizard::data_remove(trends, c("Parameter", "Statistic", "SE", "S", "CI", "df", "rowid_dedup"), verbose = FALSE) # nolint
trends <- datawizard::data_remove(trends, c("Statistic", "SE", "S", "CI", "df", "rowid_dedup"), verbose = FALSE) # nolint
trends <- datawizard::data_relocate(trends, "p", after = -1, verbose = FALSE)
# Restore factor levels
datawizard::data_restoretype(trends, insight::get_data(model, verbose = FALSE))
Expand Down Expand Up @@ -232,7 +232,7 @@
ends <- nrow(data)
# Iterate through all rows to find blocks
for (i in 2:nrow(data)) {
if ((data$Confidence[i] != sig) || ((centrality_signs[i] != centrality_sign) && data$Confidence[i] == "Uncertain")) {

Check warning on line 235 in R/estimate_slopes.R

View workflow job for this annotation

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

file=R/estimate_slopes.R,line=235,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.

Check warning on line 235 in R/estimate_slopes.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_slopes.R,line=235,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
centrality_sign <- centrality_signs[i]
sig <- data$Confidence[i]
starts <- c(starts, i)
Expand Down
48 changes: 36 additions & 12 deletions R/get_marginalcontrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,42 @@
# Guess arguments
my_args <- .guess_marginaleffects_arguments(model, by, contrast, ...)

out <- estimate_means(
model = model,
## TODO: once .format_marginaleffects_contrasts() is working, we have to
## pass only "contrast" to the `by` argument, and use `my_args$by` for
## filtering...
by = unique(c(my_args$contrast, my_args$by)),
ci = ci,
hypothesis = method,
predict = predict,
backend = "marginaleffects",
...
)
# check whether contrasts should be made for numerics or categorical
model_data <- insight::get_data(model, source = "mf", verbose = FALSE)
on_the_fly_factors <- attributes(model_data)$factors

# extract first focal term
first_focal <- my_args$contrast[1]

# if first focal term is numeric, we contrast slopes
if (is.numeric(model_data[[first_focal]]) && !first_focal %in% on_the_fly_factors) {
out <- estimate_slopes(
model = model,
trend = my_args$contrast,
## TODO: once .format_marginaleffects_contrasts() is working, we have to
## pass only "contrast" to the `by` argument, and use `my_args$by` for
## filtering...
by = my_args$by,
ci = ci,
hypothesis = method,
backend = "marginaleffects",
...
)
} else {
# for contrasts of categorical predictors, we call avg_predictions
out <- estimate_means(
model = model,
## TODO: once .format_marginaleffects_contrasts() is working, we have to
## pass only "contrast" to the `by` argument, and use `my_args$by` for
## filtering...
by = unique(c(my_args$contrast, my_args$by)),
ci = ci,
hypothesis = method,
predict = predict,
backend = "marginaleffects",
...
)
}

# adjust p-values
out <- .p_adjust(model, out, p_adjust, ...)
Expand Down Expand Up @@ -71,7 +95,7 @@
# base R adjustments
params[["p"]] <- stats::p.adjust(params[["p"]], method = p_adjust)
} else if (tolower(p_adjust) == "tukey") {
if (!is.null(statistic)) {

Check warning on line 98 in R/get_marginalcontrasts.R

View workflow job for this annotation

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

file=R/get_marginalcontrasts.R,line=98,col=11,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.

Check warning on line 98 in R/get_marginalcontrasts.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/get_marginalcontrasts.R,line=98,col=11,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
# tukey adjustment
params[["p"]] <- suppressWarnings(stats::ptukey(
sqrt(2) * abs(statistic),
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,19 @@ test_that("estimate_contrasts - on-the-fly factors", {
expect_identical(nrow(out4), 3L)
expect_equal(out3$Difference, out4$Difference, tolerance = 1e-4)
})


test_that("estimate_contrasts - works with slopes", {
data(iris)
fit <- lm(Sepal.Width ~ Petal.Length * Species, data = iris)

out1 <- estimate_slopes(fit, trend = "Petal.Length", backend = "marginaleffects")
out2 <- suppressMessages(as.data.frame(emmeans::emtrends(fit, specs = ~Petal.Length, var = "Petal.Length")))
expect_equal(out1$Coefficient, out2$Petal.Length.trend, tolerance = 1e-3)

out3 <- estimate_slopes(fit, trend = "Petal.Length", by = "Species", backend = "marginaleffects")
out4 <- estimate_contrasts(fit, contrast = "Petal.Length", by = "Species", backend = "marginaleffects")
out5 <- emmeans::emtrends(fit, specs = pairwise ~ Species, var = "Petal.Length")
expect_equal(out3$Coefficient, as.data.frame(out5$emtrends)$Petal.Length.trend, tolerance = 1e-3)
expect_equal(out4$Coefficient, as.data.frame(out5$contrasts)$estimate, tolerance = 1e-3)
})
Loading