Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[paper] finalize #405

Merged
merged 27 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dc56662
Update paper.Rmd
DominiqueMakowski Feb 19, 2025
85b67d1
Merge branch 'main' into paper_final
DominiqueMakowski Feb 19, 2025
f877f44
re-knit PDF
strengejacke Feb 19, 2025
0bb928c
Yes, correct
strengejacke Feb 19, 2025
c518c9f
re-knit PDF
strengejacke Feb 19, 2025
3480085
Merge branch 'main' into paper_final
strengejacke Feb 19, 2025
35dd64c
Merge branch 'main' into paper_final
strengejacke Feb 20, 2025
ddcfde8
Merge branch 'main' into paper_final
strengejacke Feb 20, 2025
bc9ad8f
Merge branch 'main' into paper_final
strengejacke Feb 20, 2025
2b085b3
Merge branch 'main' into paper_final
strengejacke Feb 20, 2025
0dff20e
Merge branch 'main' into paper_final
strengejacke Feb 20, 2025
808cd6d
add mini para on grouplevel()
DominiqueMakowski Feb 21, 2025
68b538a
Merge branch 'main' into paper_final
strengejacke Feb 22, 2025
c446cc7
Merge branch 'main' into paper_final
strengejacke Feb 22, 2025
427b71b
Merge branch 'main' into paper_final
strengejacke Feb 23, 2025
ce327e0
Merge branch 'main' into paper_final
strengejacke Feb 28, 2025
604ca57
Merge branch 'main' into paper_final
strengejacke Mar 1, 2025
d82a1bf
phrasing
DominiqueMakowski Mar 1, 2025
0bce049
Merge branch 'main' into paper_final
DominiqueMakowski Mar 1, 2025
fab11aa
Merge branch 'main' into paper_final
DominiqueMakowski Mar 2, 2025
6287f31
mention "conditional" in docstring
DominiqueMakowski Mar 3, 2025
ee6183b
Merge branch 'main' into paper_final
strengejacke Mar 4, 2025
ed62cd8
knit RD
strengejacke Mar 4, 2025
bc7ced5
knit PDF
strengejacke Mar 4, 2025
35b06f4
update RD with submission PR
strengejacke Mar 4, 2025
d5c4d97
Merge branch 'main' into paper_final
strengejacke Mar 4, 2025
02d5b1b
Merge branch 'main' into paper_final
DominiqueMakowski Mar 5, 2025
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
29 changes: 14 additions & 15 deletions R/estimate_grouplevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@
#' which can be useful to add the random effects to the original data.
#'
#' @param model A mixed model with random effects.
#' @param type If `"random"` (default), the coefficients are the ones estimated
#' natively by the model (as they are returned by, for instance,
#' `lme4::ranef()`). They correspond to the deviation of each individual group
#' from their fixed effect. As such, a coefficient close to 0 means that the
#' participants' effect is the same as the population-level effect (in other
#' words, it is "in the norm"). If `"total"`, it will return the sum of the
#' random effect and its corresponding fixed effects, which corresponds to
#' `coef()` (see `?coef.merMod`). Note that `type = "total"` currently don't
#' have uncertainty indices (such as SE and CI) for models from *lme4* or
#' *glmmTMB*, as these are not computable. However, for Bayesian models, it is
#' possible to compute them.
#' @param type If `"random"` (default), the coefficients correspond to the
#' conditional estimates of the random effects (as they are returned by
#' `lme4::ranef()`). They typically correspond to the deviation of each
#' individual group from their fixed effect. As such, a coefficient close to 0
#' means that the participants' effect is the same as the population-level
#' effect (in other words, it is "in the norm"). If `"total"`, it will return
#' the sum of the random effect and its corresponding fixed effects, which
#' corresponds to `coef()` (see `?coef.merMod`). Note that `type = "total"`
#' currently does not return uncertainty indices (such as SE and CI) for
#' models from *lme4* or *glmmTMB*, as these are not computable. However, for
#' Bayesian models, it is possible to compute them.
#' @param ... Other arguments passed to or from other methods.
#'
#' @examplesIf all(insight::check_if_installed(c("see", "lme4"), quietly = TRUE))
#' @examplesIf all(insight::check_if_installed(c("see", "lme4"), quietly = TRUE)) && packageVersion("insight") > "1.1.0" && packageVersion("parameters") > "0.24.1"

Check warning on line 21 in R/estimate_grouplevel.R

View workflow job for this annotation

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

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

Check warning on line 21 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=21,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 163 characters.
#' # lme4 model
#' data(mtcars)
#' model <- lme4::lmer(mpg ~ hp + (1 | carb), data = mtcars)
#' random <- estimate_grouplevel(model)
#'
#' # Show group-specific effects
#' random
#'
#' # Visualize random effects
#' plot(random)
#'
#' # Show group-specific effects
#' estimate_grouplevel(model)
#'
#' # Reshape to wide data so that it matches the original dataframe...
#' reshaped <- reshape_grouplevel(random, indices = c("Coefficient", "SE"))
#'
Expand Down Expand Up @@ -75,7 +74,7 @@
# Clean
row.names(random) <- NULL
random$Effects <- NULL
if ("Component" %in% names(random) && insight::n_unique(random$Component) == 1 && unique(random$Component) == "conditional") {

Check warning on line 77 in R/estimate_grouplevel.R

View workflow job for this annotation

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

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

Check warning on line 77 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=77,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 128 characters.
random$Component <- NULL
}

Expand All @@ -87,22 +86,22 @@
# Save brms name (just in case)
random$Name <- random$Parameter
# Filter out non-random effects
random <- random[grepl("^r_", random$Parameter), ]

Check warning on line 89 in R/estimate_grouplevel.R

View workflow job for this annotation

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

file=R/estimate_grouplevel.R,line=89,col=28,[string_boundary_linter] Use !is.na(x) & startsWith(x, string) to detect a fixed initial substring, or, if missingness is not a concern, just startsWith(). Doing so is more readable and more efficient.

Check warning on line 89 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=89,col=28,[string_boundary_linter] Use !is.na(x) & startsWith(x, string) to detect a fixed initial substring, or, if missingness is not a concern, just startsWith(). Doing so is more readable and more efficient.
# Remove Group from Level
random$Level <- sapply(1:nrow(random), function(i) gsub(paste0("^", random$Group[i], "\\."), "", random$Level[i]))

Check warning on line 91 in R/estimate_grouplevel.R

View workflow job for this annotation

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

file=R/estimate_grouplevel.R,line=91,col=28,[seq_linter] Use seq_len(nrow(...)) instead of 1:nrow(...), which is likely to be wrong in the empty edge case.

Check warning on line 91 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=91,col=28,[seq_linter] Use seq_len(nrow(...)) instead of 1:nrow(...), which is likely to be wrong in the empty edge case.
# Find the group name (what follows "r_" and before the first "[" or "__")
random$Group <- gsub("^r_(.*?)(\\[.*|__.*)", "\\1", random$Name)
# Keep Parameter what's in between [ and ]
random$Parameter <- gsub("^r_.*?\\[(.*?)\\].*", "\\1", random$Name)
# Remove Level from it
random$Parameter <- sapply(1:nrow(random), function(i) gsub(paste0("^", random$Level[i], "\\,"), "", random$Parameter[i]))

Check warning on line 97 in R/estimate_grouplevel.R

View workflow job for this annotation

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

file=R/estimate_grouplevel.R,line=97,col=32,[seq_linter] Use seq_len(nrow(...)) instead of 1:nrow(...), which is likely to be wrong in the empty edge case.

Check warning on line 97 in R/estimate_grouplevel.R

View workflow job for this annotation

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

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

Check warning on line 97 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=97,col=32,[seq_linter] Use seq_len(nrow(...)) instead of 1:nrow(...), which is likely to be wrong in the empty edge case.

Check warning on line 97 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=97,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.
# remove temporary name column
random$Name <- NULL
}

# Sort
if ("Component" %in% names(random)) {
random <- random[order(random$Component, random$Group, datawizard::coerce_to_numeric(random$Level), random$Parameter), ]

Check warning on line 104 in R/estimate_grouplevel.R

View workflow job for this annotation

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

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

Check warning on line 104 in R/estimate_grouplevel.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_grouplevel.R,line=104,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
} else {
random <- random[order(random$Group, datawizard::coerce_to_numeric(random$Level), random$Parameter), ]
}
Expand Down
29 changes: 14 additions & 15 deletions man/estimate_grouplevel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading