Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed May 23, 2024
1 parent 0445811 commit e5f5ad7
Show file tree
Hide file tree
Showing 25 changed files with 105 additions and 96 deletions.
8 changes: 4 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ plot(contrasts, estimate_means(model)) +
```{r}
model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
estimate_contrasts(model, at = "Petal.Length", length = 3)
estimate_contrasts(model, by = "Petal.Length", length = 3)
```

```{r}
# Recompute contrasts with a higher precision (for a smoother plot)
contrasts <- estimate_contrasts(model, at = "Petal.Length", length = 20)
contrasts <- estimate_contrasts(model, by = "Petal.Length", length = 20)
# Add Contrast column by concatenating
contrasts$Contrast <- paste(contrasts$Level1, "-", contrasts$Level2)
Expand Down Expand Up @@ -302,7 +302,7 @@ model <- mgcv::gam(Sepal.Width ~ s(Petal.Length), data = iris)
# 1. Compute derivatives
deriv <- estimate_slopes(model,
trend = "Petal.Length",
at = "Petal.Length",
by = "Petal.Length",
length = 100
)
Expand Down Expand Up @@ -363,7 +363,7 @@ For instance, the plot below shows that the effect of `hp` (the y-axis) is signi
```{r}
model <- lm(mpg ~ hp * wt, data = mtcars)
slopes <- estimate_slopes(model, trend = "hp", at = "wt")
slopes <- estimate_slopes(model, trend = "hp", by = "wt")
plot(slopes)
```
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ means
## versicolor | 2.77 | 0.05 | [2.68, 2.86]
## virginica | 2.97 | 0.05 | [2.88, 3.07]
##
## Marginal means estimated at Species
## Marginal means estimated at
```

``` r

# 3. Plot
ggplot(iris, aes(x = Species, y = Sepal.Width)) +
Expand Down Expand Up @@ -233,7 +236,7 @@ contrasts
``` r
model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)

estimate_contrasts(model, at = "Petal.Length", length = 3)
estimate_contrasts(model, by = "Petal.Length", length = 3)
## Marginal Contrasts Analysis
##
## Level1 | Level2 | Petal.Length | Difference | 95% CI | SE | t(144) | p
Expand All @@ -254,7 +257,7 @@ estimate_contrasts(model, at = "Petal.Length", length = 3)

``` r
# Recompute contrasts with a higher precision (for a smoother plot)
contrasts <- estimate_contrasts(model, at = "Petal.Length", length = 20)
contrasts <- estimate_contrasts(model, by = "Petal.Length", length = 20)

# Add Contrast column by concatenating
contrasts$Contrast <- paste(contrasts$Level1, "-", contrasts$Level2)
Expand Down Expand Up @@ -306,6 +309,9 @@ head(pred1, n = 5)
## 5.00 | 2.19 | 0.10 | [1.99, 2.39] | -0.79 | 1.40
##
## Variable predicted: Petal.Length
```

``` r

# Same for model 2
model2 <- lm(Petal.Length ~ Sepal.Length * Species, data = iris)
Expand Down Expand Up @@ -354,6 +360,9 @@ random
## cyl | 6 | drat | -0.09 | 0.54 | [-1.15, 0.98]
## cyl | 8 | (Intercept) | 3.32 | 0.73 | [ 1.89, 4.74]
## cyl | 8 | drat | -2.15 | 0.47 | [-3.07, -1.23]
```

``` r

plot(random)
```
Expand Down Expand Up @@ -391,7 +400,7 @@ model <- mgcv::gam(Sepal.Width ~ s(Petal.Length), data = iris)
# 1. Compute derivatives
deriv <- estimate_slopes(model,
trend = "Petal.Length",
at = "Petal.Length",
by = "Petal.Length",
length = 100
)

Expand Down Expand Up @@ -465,7 +474,7 @@ is significantly negative only when `wt` is low (`< ~4`).
``` r
model <- lm(mpg ~ hp * wt, data = mtcars)

slopes <- estimate_slopes(model, trend = "hp", at = "wt")
slopes <- estimate_slopes(model, trend = "hp", by = "wt")

plot(slopes)
```
Expand Down
Binary file modified man/figures/unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-14-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-15-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-16-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-17-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-8-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions tests/testthat/test-attributes_visualisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ test_that("attributes_means", {
model <- lm(Sepal.Length ~ Species * Sepal.Width, data = iris)

estim <- suppressMessages(estimate_means(model))
expect_identical(attributes(estim)$at, "Species")
expect_identical(attributes(estim)$fixed, NULL)
expect_identical(attributes(estim)$by, "Species")
expect_null(attributes(estim)$fixed)

estim <- suppressMessages(estimate_means(model, fixed = "Sepal.Width"))
expect_identical(attributes(estim)$at, "Species")
expect_identical(attributes(estim)$by, "Species")
expect_identical(attributes(estim)$fixed, "Sepal.Width")

estim <- suppressMessages(estimate_means(model, at = "all"))
expect_identical(attributes(estim)$at, c("Species", "Sepal.Width"))
estim <- suppressMessages(estimate_means(model, by = "all"))
expect_identical(attributes(estim)$by, c("Species", "Sepal.Width"))
})


Expand All @@ -22,13 +22,13 @@ test_that("attributes_contrasts", {

estim <- suppressMessages(estimate_contrasts(model))
expect_identical(attributes(estim)$contrast, "Species")
expect_identical(attributes(estim)$at, NULL)
expect_identical(attributes(estim)$fixed, NULL)
expect_null(attributes(estim)$by)
expect_null(attributes(estim)$fixed)

estim <- suppressMessages(estimate_contrasts(model, fixed = "Sepal.Width"))
expect_identical(attributes(estim)$contrast, "Species")
expect_identical(attributes(estim)$fixed, "Sepal.Width")
expect_identical(attributes(estim)$modulate, NULL)
expect_null(attributes(estim)$modulate)
})


Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ test_that("estimate_means - brms", {
skip_if_not_installed("emmeans")
model <- brms::brm(Sepal.Length ~ Species * Sepal.Width, data = iris, refresh = 0, iter = 1000)
estim <- estimate_means(model)
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))
})

test_that("estimate_relation - brms", {
skip_if_not_installed("brms")
skip_if_not_installed("emmeans")
model <- brms::brm(Sepal.Length ~ Species * Sepal.Width, data = iris, refresh = 0, iter = 1000)
estim <- estimate_relation(model, preserve_range = FALSE)
expect_equal(dim(estim), c(30, 6))
expect_identical(dim(estim), c(30L, 6L))

# estim <- estimate_relation(model, preserve_range=FALSE, iterations = 10)
# expect_equal(dim(estim), c(30, 6))
Expand All @@ -23,6 +23,6 @@ test_that("estimate_means - brms", {
skip_if_not_installed("brms")
skip_if_not_installed("emmeans")
model <- brms::brm(Sepal.Length ~ Species * Sepal.Width, data = iris, refresh = 0, iter = 1000)
estim <- estimate_slopes(model, at = "Species")
expect_equal(dim(estim), c(3, 5))
estim <- estimate_slopes(model, by = "Species")
expect_identical(dim(estim), c(3L, 5L))
})
108 changes: 54 additions & 54 deletions tests/testthat/test-estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,51 @@ test_that("estimate_means() - core", {
# Simple
model <- lm(vs ~ cyl, data = dat)
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
expect_identical(dim(estim2), c(3L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)

# Interaction (factor * continuous)
model <- lm(mpg ~ wt * gear, data = dat)
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 6))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
expect_identical(dim(estim2), c(3L, 6L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)

# At specific levels
model <- lm(Sepal.Width ~ Species, data = iris)
estim1 <- suppressMessages(estimate_means(model, at = "Species=c('versicolor', 'virginica')"))
expect_equal(dim(estim1), c(2, 5))
estim2 <- suppressMessages(estimate_means(model, at = "Species=c('versicolor', 'virginica')", backend = "marginaleffects"))
expect_equal(dim(estim2), c(2, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
estim1 <- suppressMessages(estimate_means(model, by = "Species=c('versicolor', 'virginica')"))
expect_identical(dim(estim1), c(2L, 5L))
estim2 <- suppressMessages(estimate_means(model, by = "Species=c('versicolor', 'virginica')", backend = "marginaleffects"))
expect_identical(dim(estim2), c(2L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)

# Interactions between factors
dat <- iris
dat$Petal.Length_factor <- ifelse(dat$Petal.Length < 4.2, "A", "B")
dat <<- dat

model <- lm(Sepal.Width ~ Species * Petal.Length_factor, data = dat)
estim1 <- suppressMessages(estimate_means(model, at = "all"))
expect_equal(dim(estim1), c(6, 6))
estim2 <- suppressWarnings(suppressMessages(estimate_means(model, at = "all", backend = "marginaleffects")))
expect_equal(dim(estim2), c(6, 6))
estim1 <- suppressMessages(estimate_means(model, by = "all"))
expect_identical(dim(estim1), c(6L, 6L))
estim2 <- suppressWarnings(suppressMessages(estimate_means(model, by = "all", backend = "marginaleffects")))
expect_identical(dim(estim2), c(6L, 6L))

# No interaction (two factors)
model <- lm(Petal.Length ~ Sepal.Width + Species, data = iris)
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 6))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
expect_identical(dim(estim2), c(3L, 6L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)


# At specific levels of continuous
estim1 <- suppressMessages(estimate_means(model, at = "Sepal.Width"))
estim1 <- suppressMessages(estimate_means(model, by = "Sepal.Width"))
expect_equal(dim(estim1), c(10, 5))
estim2 <- suppressMessages(estimate_means(model, at = "Sepal.Width", backend = "marginaleffects"))
estim2 <- suppressMessages(estimate_means(model, by = "Sepal.Width", backend = "marginaleffects"))
expect_equal(dim(estim2), c(10, 6))
# Note that the absolute values are different here... for unclear reasons
expect_true(max(diff(estim1$Mean) - diff(estim2$Mean)) < 1e-10)
Expand All @@ -69,57 +69,57 @@ test_that("estimate_means() - core", {
model <- glm(y ~ Species, family = "binomial", data = dat)

estim <- suppressMessages(estimate_means(model))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))
estim <- suppressMessages(estimate_means(model, transform = "response"))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))
expect_true(all(estim$Probability >= 0) & all(estim$Probability <= 1))


model <- lm(Petal.Length ~ Sepal.Width + Species, data = iris)
estim <- suppressMessages(estimate_means(model))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))

estim <- suppressMessages(estimate_means(model, at = "all"))
estim <- suppressMessages(estimate_means(model, by = "all"))
expect_equal(dim(estim), c(30, 6))

# In formula modification
# FIXME: this got broken but it seems just to tedious to fix. Don't use in formula transforms.
# model <- lm(mpg ~ wt * as.factor(gear), data = mtcars)
# estim <- suppressMessages(estimate_means(model))
# expect_equal(dim(estim), c(3, 5))
# expect_equal(dim(estim), c(3L, 5L))

# One continuous and one factor
model <- lm(Petal.Length ~ Species * Sepal.Width, data = iris)

estim <- suppressMessages(estimate_means(model))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))
estim <- suppressMessages(estimate_means(model, fixed = "Sepal.Width"))
expect_equal(dim(estim), c(3, 6))
estim <- suppressMessages(estimate_means(model, at = c("Species", "Sepal.Width"), length = 2))
expect_equal(dim(estim), c(6, 6))
estim <- suppressMessages(estimate_means(model, at = "Species=c('versicolor', 'setosa')"))
expect_equal(dim(estim), c(2, 5))
estim <- suppressMessages(estimate_means(model, at = "Sepal.Width=c(2, 4)"))
expect_equal(dim(estim), c(2, 5))
estim <- suppressMessages(estimate_means(model, at = c("Species", "Sepal.Width=0")))
expect_equal(dim(estim), c(3, 6))
estim <- suppressMessages(estimate_means(model, at = "Sepal.Width", length = 5))
expect_identical(dim(estim), c(3L, 6L))
estim <- suppressMessages(estimate_means(model, by = c("Species", "Sepal.Width"), length = 2))
expect_identical(dim(estim), c(6L, 6L))
estim <- suppressMessages(estimate_means(model, by = "Species=c('versicolor', 'setosa')"))
expect_identical(dim(estim), c(2L, 5L))
estim <- suppressMessages(estimate_means(model, by = "Sepal.Width=c(2, 4)"))
expect_identical(dim(estim), c(2L, 5L))
estim <- suppressMessages(estimate_means(model, by = c("Species", "Sepal.Width=0")))
expect_identical(dim(estim), c(3L, 6L))
estim <- suppressMessages(estimate_means(model, by = "Sepal.Width", length = 5))
expect_equal(dim(estim), c(5, 5))
estim <- suppressMessages(estimate_means(model, at = "Sepal.Width=c(2, 4)"))
expect_equal(dim(estim), c(2, 5))
estim <- suppressMessages(estimate_means(model, by = "Sepal.Width=c(2, 4)"))
expect_identical(dim(estim), c(2L, 5L))

# Two factors
dat <- iris
dat$Petal.Length_factor <- ifelse(dat$Petal.Length < 4.2, "A", "B")
dat <<- dat
model <- lm(Petal.Length ~ Species * Petal.Length_factor, data = dat)

estim <- suppressMessages(estimate_means(model, at = "all"))
expect_equal(dim(estim), c(6, 6))
estim <- suppressMessages(estimate_means(model, at = "Petal.Length_factor"))
expect_equal(dim(estim), c(2, 5))
estim <- suppressMessages(estimate_means(model, at = "Petal.Length_factor='B'"))
expect_true(as.character(unique(estim$Petal.Length_factor)) == "B")
estim <- suppressMessages(estimate_means(model, by = "all"))
expect_identical(dim(estim), c(6L, 6L))
estim <- suppressMessages(estimate_means(model, by = "Petal.Length_factor"))
expect_identical(dim(estim), c(2L, 5L))
estim <- suppressMessages(estimate_means(model, by = "Petal.Length_factor='B'"))
expect_identical(as.character(unique(estim$Petal.Length_factor)), "B")


# Three factors
Expand All @@ -132,7 +132,7 @@ test_that("estimate_means() - core", {
expect_equal(dim(estim), c(12, 7))
estim <- suppressMessages(estimate_means(model, fixed = "am"))
expect_equal(dim(estim), c(6, 7))
estim <- suppressMessages(estimate_means(model, at = c("gear='5'", "vs")))
estim <- suppressMessages(estimate_means(model, by = c("gear='5'", "vs")))
expect_equal(dim(estim), c(2, 7))

dat <- iris
Expand All @@ -156,13 +156,13 @@ test_that("estimate_means() - core", {
model <- glm(Petal.Length_factor ~ Species, data = dat, family = "binomial")

estim <- suppressMessages(estimate_means(model))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))
estim <- suppressMessages(estimate_means(model, transform = "none"))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))

model <- glm(Petal.Length ~ Species, data = iris, family = "Gamma")
estim <- suppressMessages(estimate_means(model))
expect_equal(dim(estim), c(3, 5))
expect_identical(dim(estim), c(3L, 5L))
})

test_that("estimate_means() - mixed models", {
Expand All @@ -176,15 +176,15 @@ test_that("estimate_means() - mixed models", {
model <- lme4::lmer(Sepal.Width ~ Species + (1 | Petal.Length_factor), data = dat)

estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
expect_identical(dim(estim2), c(3L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)

model <- lme4::glmer(Sepal.Width ~ Species + (1 | Petal.Length_factor), data = dat, family = "Gamma")
estim1 <- suppressMessages(estimate_means(model))
expect_equal(dim(estim1), c(3, 5))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_equal(dim(estim2), c(3, 5))
expect_true(max(estim1$Mean - estim2$Mean) < 1e-10)
expect_identical(dim(estim2), c(3L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-estimate_predicted.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ test_that("estimate_response - Frequentist", {
expect_equal(dim(estim), c(32, 4))

model <- glm(vs ~ wt + cyl, data = mtcars, family = "binomial")
estim <- estimate_link(model, at = "wt")
estim <- estimate_link(model, by = "wt")
expect_equal(dim(estim), c(10, 6))


Expand Down
Loading

0 comments on commit e5f5ad7

Please sign in to comment.