Variance decomposition for brms models
var_decomp(brmsfit)
Returns a data frame with posterior estimates.
# Simulate data
md = tibble::tibble(
group = factor(sample(1:10, 1000, replace = TRUE)),
f_var = factor(sample(1:3, 1000, replace = TRUE)),
n_var = rnorm(1000, mean = 0, sd = 1),
resp = rnorm(1000, mean = 10, sd = 3))
# Run model
mod = brms_model(Response = "resp",
FixedEffect = c("f_var","n_var"),
RandomEffect = "group",
Family = "gaussian",
Data = md)
#> [1] "No problems so far 🙃"
#> Compiling Stan program...
#> Start sampling
# Variance decomposition
var_decomp(mod)
#> # A tibble: 2,000 × 12
#> Intercept f_var2 f_var3 n_var sigma R2_n_var R2_f_var3 R2_f_var2
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 10.0 -0.197 -0.146 -0.150 3.01 0.00239 0.000517 0.000903
#> 2 10.3 -0.0535 -0.418 0.00927 3.20 0.00000802 0.00373 0.0000585
#> 3 10.1 -0.261 -0.0566 0.0223 2.98 0.0000539 0.0000799 0.00162
#> 4 10.5 -0.141 -0.749 -0.116 2.98 0.00144 0.0138 0.000470
#> 5 10.1 -0.0609 0.275 -0.0639 3.08 0.000415 0.00176 0.0000825
#> 6 10.1 0.193 -0.398 -0.203 2.95 0.00453 0.00400 0.000903
#> 7 10.3 -0.0695 -0.744 -0.0897 3.03 0.000838 0.0132 0.000110
#> 8 10.1 -0.180 -0.134 -0.188 2.95 0.00390 0.000455 0.000788
#> 9 10.2 -0.414 -0.341 -0.0506 3.00 0.000273 0.00285 0.00401
#> 10 10.0 -0.182 -0.317 -0.145 2.96 0.00228 0.00252 0.000790
#> # ℹ 1,990 more rows
#> # ℹ 4 more variables: R2_FixedEffects <dbl>, R2_group <dbl>,
#> # R2_RandomEffects <dbl>, R2_residual <dbl>