easystats / easystats/datawizard

`degroup()` with even more complex structures

Open
#639 1 comment 0 reactions 1 assignee View on GitHub

@mattansb is already working on this.

Since Aug 12, 2025.

Breaking 🏴‍☠️ Consistency :green_apple: :apple: Enhancement :boom: Feature idea :fire:
Dominant language
R
Stars
238
Forks
18
Avg merge
1d 15h
Merged PRs (30d)
4

Description

Currently we have support for the following (arbitrary) structures:

  • Crossed: ~ J + K + M + ...
  • Nested: ~ J/K/M/...

It would be nice to have support for more complex arbitrary designs, such as ~ J + K/M

The basic algorithm would be:

For each outcome:

  1. Compute the group means for each crosses structure separately (e.g., J and K/M) according to the currently implemented algorithm.
  2. For each row in the input data, compute the sum of the group means
  3. Finally, subtract the sum from the outcome vector to obtain the cluster mean centered (at level 1) values (issue raised in #637 still applies)
  4. Return group means and cluster mean centered values.

This is already currently possible using multiple calls to dmean():

set.seed(111)
data(iris)
iris$ID <- sample(1:4, nrow(iris), replace = TRUE) # fake-ID
iris$binary <- as.factor(rbinom(150, 1, .35)) # binary variable

# (Step 1)
x1 <- datawizard::demean(
  iris,
  select = c("Sepal.Length"),
  by = "ID",
  append = FALSE
)
x2 <- datawizard::demean(
  iris,
  select = c("Sepal.Length"),
  by = "Species/binary",
  append = FALSE
)

group_means <- data.frame(Sepal.Length_ID_between = x1$Sepal.Length_between, x2[1:2])

# (Step 2)
summ <- rowMeans(group_means)

# (Step 3)
group_means$Sepal.Length_within <- iris$Sepal.Length - summ

# (Step 4)
head(group_means)
#>   Sepal.Length_ID_between Sepal.Length_Species_between Sepal.Length_binary_between Sepal.Length_within
#> 1                5.878788                        5.006                 0.009789474           1.4684742
#> 2                5.843243                        5.006                -0.006000000           1.2855856
#> 3                5.812821                        5.006                -0.006000000           1.0957265
#> 4                5.843243                        5.006                -0.006000000           0.9855856
#> 5                5.843243                        5.006                -0.006000000           1.3855856
#> 6                5.843902                        5.006                 0.009789474           1.7801027

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.