-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Visualization: Collapse by [random] group #130
Comments
library(ggeffects)
library(lme4)
library(dplyr)
data(efc)
efc$e15relat <- as.factor(efc$e15relat)
efc$c161sex <- as.factor(efc$c161sex)
levels(efc$c161sex) <- c("male", "female")
model <- lmer(neg_c_7 ~ c161sex + (1 | e15relat), data = efc)
# ggpredict
me <- ggpredict(model, terms = "c161sex")
collapse_by_group(me, model, "e15relat")
#> x group_col facet random response
#> 1 1 1 1 1 12.297872
#> 2 2 1 1 1 13.347107
#> 3 1 1 1 2 11.585586
#> 4 2 1 1 2 12.118310
#> 5 1 1 1 3 12.166667
#> 6 2 1 1 3 10.545455
#> 7 1 1 1 4 10.750000
#> 8 2 1 1 4 11.726027
#> 9 1 1 1 5 11.333333
#> 10 2 1 1 5 10.235294
#> 11 1 1 1 6 8.200000
#> 12 2 1 1 6 9.235294
#> 13 1 1 1 7 13.000000
#> 14 2 1 1 7 10.400000
#> 15 1 1 1 8 9.666667
#> 16 2 1 1 8 10.955882
# Modelbased
pr <- modelbased::estimate_expectation(model)
dat <- insight::get_data(attributes(pr)$model)
summarise_if(group_by(dat, e15relat), is.numeric, mean)
#> # A tibble: 8 x 2
#> e15relat neg_c_7
#> <fct> <dbl>
#> 1 1 13.1
#> 2 2 12.0
#> 3 3 10.9
#> 4 4 11.6
#> 5 5 10.5
#> 6 6 9
#> 7 7 10.8
#> 8 8 10.6 Created on 2021-07-06 by the reprex package (v2.0.0) lol what is x in ggeffects' output? |
library(ggeffects)
library(lme4)
#> Loading required package: Matrix
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
data(efc)
efc$e15relat <- as.factor(efc$e15relat)
efc$c161sex <- as.factor(efc$c161sex)
levels(efc$c161sex) <- c("male", "female")
model <- lmer(neg_c_7 ~ c161sex + (1 | e15relat), data = efc)
# ggpredict
me <- ggpredict(model, terms = "c161sex")
collapse_by_group(me, model, "e15relat")
#> x group_col facet random response
#> 1 1 1 1 1 12.297872
#> 2 2 1 1 1 13.347107
#> 3 1 1 1 2 11.585586
#> 4 2 1 1 2 12.118310
#> 5 1 1 1 3 12.166667
#> 6 2 1 1 3 10.545455
#> 7 1 1 1 4 10.750000
#> 8 2 1 1 4 11.726027
#> 9 1 1 1 5 11.333333
#> 10 2 1 1 5 10.235294
#> 11 1 1 1 6 8.200000
#> 12 2 1 1 6 9.235294
#> 13 1 1 1 7 13.000000
#> 14 2 1 1 7 10.400000
#> 15 1 1 1 8 9.666667
#> 16 2 1 1 8 10.955882
dat <- insight::get_data(model)
summarise_if(group_by(dat, e15relat, c161sex), is.numeric, mean) # by random group ALSO
#> # A tibble: 16 x 3
#> # Groups: e15relat [8]
#> e15relat c161sex neg_c_7
#> <fct> <fct> <dbl>
#> 1 1 male 12.3
#> 2 1 female 13.3
#> 3 2 male 11.6
#> 4 2 female 12.1
#> 5 3 male 12.2
#> 6 3 female 10.5
#> 7 4 male 10.8
#> 8 4 female 11.7
#> 9 5 male 11.3
#> 10 5 female 10.2
#> 11 6 male 8.2
#> 12 6 female 9.24
#> 13 7 male 13
#> 14 7 female 10.4
#> 15 8 male 9.67
#> 16 8 female 11.0 This makes more sense for plotting: plot(me, add.data = TRUE)
#> Loading required namespace: ggplot2 plot(me, add.data = TRUE, collapse.group = "e15relat") can also be used with residualized plots (here the random effect is residualized) plot(me, residuals = TRUE) plot(me, residuals = TRUE, collapse.group = "e15relat")
#> Error in FUN(X[[i]], ...): object 'group_col' not found Well… it should… Created on 2021-07-06 by the reprex package (v2.0.0) |
I wonder (also for #129 ) how would a modelbased/easystats API look for that kind of stuff |
(This is also only for data overlay) |
@DominiqueMakowski What does the API look like for For #129, I think a different function might be best? How do we do effects plots here generally? |
For plotting? Currently data overlay is added fairly straightforwardly here (allowing for multiple data overlays like a 2D density + points): modelbased/R/visualisation_recipe.estimate_predicted.R Lines 177 to 186 in 5d81124
Any adjustments to the raw-data should be made before that directly to the |
Sorry if I'm talking like I have no idea how this package works - I don't.... Waiting for that vignette... |
We need to translate |
Yeah, basically. Something like:
|
Are we allowed to steal from ourselves?
https://strengejacke.github.io/ggeffects/reference/collapse_by_group.html
The text was updated successfully, but these errors were encountered: