Skip to content

"all" should not include group levels #1064

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

Merged
merged 18 commits into from
Apr 28, 2025
Merged

"all" should not include group levels #1064

merged 18 commits into from
Apr 28, 2025

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Apr 27, 2025

@DominiqueMakowski What would you say how "all" should behave? Return fixed effects, random variances and group level estimates? (as it is now)

Or only return fixed effects and random variances, as for non-bayesian models, and return group level estimates only when requested?

At the moment, we have no option for brms for return fixed effects and random variances only. In parameters, we return both random effects variances and group level effects, and simply filter out group level estimates when group_level = FALSE. This is inefficient for large models.

Thus, I suggest that "all" returns fixed and ran vars only.

@strengejacke
Copy link
Member Author

Suggested change in this PR:

library(insight)
model <- insight::download_model("brms_chocomini_1")

find_parameters(model, effects = "fixed")
#> Loading required namespace: rstan
#> $conditional
#> [1] "b_Intercept"
#> 
#> $delta
#> [1] "b_delta_Intercept"
#> 
#> $k
#> [1] "b_k_Intercept"
#> 
#> $phi
#> [1] "b_phi_Intercept"

find_parameters(model, effects = "all")
#> $conditional
#> [1] "b_Intercept"
#> 
#> $random
#> [1] "sd_Participant__Intercept"
#> 
#> $delta
#> [1] "b_delta_Intercept"
#> 
#> $k
#> [1] "b_k_Intercept"
#> 
#> $phi
#> [1] "b_phi_Intercept"
#> 
#> $delta_random
#> [1] "sd_Participant__delta_Intercept"
#> 
#> $k_random
#> [1] "sd_Participant__k_Intercept"

find_parameters(model, effects = "random_variance")
#> $random
#> [1] "sd_Participant__Intercept"
#> 
#> $delta_random
#> [1] "sd_Participant__delta_Intercept"
#> 
#> $k_random
#> [1] "sd_Participant__k_Intercept"

find_parameters(model, effects = "grouplevel")
#> $random
#>  [1] "r_Participant[S001,Intercept]" "r_Participant[S002,Intercept]"
#>  [3] "r_Participant[S003,Intercept]" "r_Participant[S004,Intercept]"
#>  [5] "r_Participant[S005,Intercept]" "r_Participant[S006,Intercept]"
#>  [7] "r_Participant[S007,Intercept]" "r_Participant[S008,Intercept]"
#>  [9] "r_Participant[S009,Intercept]" "r_Participant[S010,Intercept]"
#> [11] "r_Participant[S011,Intercept]" "r_Participant[S012,Intercept]"
#> [13] "r_Participant[S013,Intercept]" "r_Participant[S014,Intercept]"
#> [15] "r_Participant[S015,Intercept]" "r_Participant[S016,Intercept]"
#> [17] "r_Participant[S017,Intercept]" "r_Participant[S018,Intercept]"
#> [19] "r_Participant[S019,Intercept]" "r_Participant[S020,Intercept]"
#> 
#> $delta_random
#>  [1] "r_Participant__delta[S001,Intercept]"
#>  [2] "r_Participant__delta[S002,Intercept]"
#>  [3] "r_Participant__delta[S003,Intercept]"
#>  [4] "r_Participant__delta[S004,Intercept]"
#>  [5] "r_Participant__delta[S005,Intercept]"
#>  [6] "r_Participant__delta[S006,Intercept]"
#>  [7] "r_Participant__delta[S007,Intercept]"
#>  [8] "r_Participant__delta[S008,Intercept]"
#>  [9] "r_Participant__delta[S009,Intercept]"
#> [10] "r_Participant__delta[S010,Intercept]"
#> [11] "r_Participant__delta[S011,Intercept]"
#> [12] "r_Participant__delta[S012,Intercept]"
#> [13] "r_Participant__delta[S013,Intercept]"
#> [14] "r_Participant__delta[S014,Intercept]"
#> [15] "r_Participant__delta[S015,Intercept]"
#> [16] "r_Participant__delta[S016,Intercept]"
#> [17] "r_Participant__delta[S017,Intercept]"
#> [18] "r_Participant__delta[S018,Intercept]"
#> [19] "r_Participant__delta[S019,Intercept]"
#> [20] "r_Participant__delta[S020,Intercept]"
#> 
#> $k_random
#>  [1] "r_Participant__k[S001,Intercept]" "r_Participant__k[S002,Intercept]"
#>  [3] "r_Participant__k[S003,Intercept]" "r_Participant__k[S004,Intercept]"
#>  [5] "r_Participant__k[S005,Intercept]" "r_Participant__k[S006,Intercept]"
#>  [7] "r_Participant__k[S007,Intercept]" "r_Participant__k[S008,Intercept]"
#>  [9] "r_Participant__k[S009,Intercept]" "r_Participant__k[S010,Intercept]"
#> [11] "r_Participant__k[S011,Intercept]" "r_Participant__k[S012,Intercept]"
#> [13] "r_Participant__k[S013,Intercept]" "r_Participant__k[S014,Intercept]"
#> [15] "r_Participant__k[S015,Intercept]" "r_Participant__k[S016,Intercept]"
#> [17] "r_Participant__k[S017,Intercept]" "r_Participant__k[S018,Intercept]"
#> [19] "r_Participant__k[S019,Intercept]" "r_Participant__k[S020,Intercept]"

find_parameters(model, effects = "random")
#> $random
#>  [1] "r_Participant[S001,Intercept]" "r_Participant[S002,Intercept]"
#>  [3] "r_Participant[S003,Intercept]" "r_Participant[S004,Intercept]"
#>  [5] "r_Participant[S005,Intercept]" "r_Participant[S006,Intercept]"
#>  [7] "r_Participant[S007,Intercept]" "r_Participant[S008,Intercept]"
#>  [9] "r_Participant[S009,Intercept]" "r_Participant[S010,Intercept]"
#> [11] "r_Participant[S011,Intercept]" "r_Participant[S012,Intercept]"
#> [13] "r_Participant[S013,Intercept]" "r_Participant[S014,Intercept]"
#> [15] "r_Participant[S015,Intercept]" "r_Participant[S016,Intercept]"
#> [17] "r_Participant[S017,Intercept]" "r_Participant[S018,Intercept]"
#> [19] "r_Participant[S019,Intercept]" "r_Participant[S020,Intercept]"
#> [21] "sd_Participant__Intercept"    
#> 
#> $delta_random
#>  [1] "r_Participant__delta[S001,Intercept]"
#>  [2] "r_Participant__delta[S002,Intercept]"
#>  [3] "r_Participant__delta[S003,Intercept]"
#>  [4] "r_Participant__delta[S004,Intercept]"
#>  [5] "r_Participant__delta[S005,Intercept]"
#>  [6] "r_Participant__delta[S006,Intercept]"
#>  [7] "r_Participant__delta[S007,Intercept]"
#>  [8] "r_Participant__delta[S008,Intercept]"
#>  [9] "r_Participant__delta[S009,Intercept]"
#> [10] "r_Participant__delta[S010,Intercept]"
#> [11] "r_Participant__delta[S011,Intercept]"
#> [12] "r_Participant__delta[S012,Intercept]"
#> [13] "r_Participant__delta[S013,Intercept]"
#> [14] "r_Participant__delta[S014,Intercept]"
#> [15] "r_Participant__delta[S015,Intercept]"
#> [16] "r_Participant__delta[S016,Intercept]"
#> [17] "r_Participant__delta[S017,Intercept]"
#> [18] "r_Participant__delta[S018,Intercept]"
#> [19] "r_Participant__delta[S019,Intercept]"
#> [20] "r_Participant__delta[S020,Intercept]"
#> [21] "sd_Participant__delta_Intercept"     
#> 
#> $k_random
#>  [1] "r_Participant__k[S001,Intercept]" "r_Participant__k[S002,Intercept]"
#>  [3] "r_Participant__k[S003,Intercept]" "r_Participant__k[S004,Intercept]"
#>  [5] "r_Participant__k[S005,Intercept]" "r_Participant__k[S006,Intercept]"
#>  [7] "r_Participant__k[S007,Intercept]" "r_Participant__k[S008,Intercept]"
#>  [9] "r_Participant__k[S009,Intercept]" "r_Participant__k[S010,Intercept]"
#> [11] "r_Participant__k[S011,Intercept]" "r_Participant__k[S012,Intercept]"
#> [13] "r_Participant__k[S013,Intercept]" "r_Participant__k[S014,Intercept]"
#> [15] "r_Participant__k[S015,Intercept]" "r_Participant__k[S016,Intercept]"
#> [17] "r_Participant__k[S017,Intercept]" "r_Participant__k[S018,Intercept]"
#> [19] "r_Participant__k[S019,Intercept]" "r_Participant__k[S020,Intercept]"
#> [21] "sd_Participant__k_Intercept"

Created on 2025-04-27 with reprex v2.1.1

@strengejacke
Copy link
Member Author

strengejacke commented Apr 27, 2025

See also new docs:

For models of class brmsfit, there are additional options:

  • "fixed" returns fixed effects.
  • "random_variance" return random effects parameters (variance and
    correlation components, e.g. those parameters that start with sd_ or
    cor_).
  • "grouplevel" returns random effects group level estimates (BLUPs), i.e.
    those parameters that start with r_.
  • "random" returns both "random_variance" and "grouplevel".
  • "all" returns fixed effects and random effects variances.
  • "full" returns all parameters.

@DominiqueMakowski
Copy link
Member

Thus, I suggest that "all" returns fixed and ran vars only.

Fine by me yes

@strengejacke
Copy link
Member Author

I think we can do the same optimization for stanreg/rstanarm, too. The as.data.frame() method also has a pars argument, to filter parameters.

@strengejacke strengejacke merged commit eb4de27 into main Apr 28, 2025
18 of 23 checks passed
@strengejacke strengejacke deleted the optimize branch April 28, 2025 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants