Thanks a lot for your answer. I am sorry for the confusion. My analyses consist of two parts. First, calculating how much variability a term adds to the model (the ICC in the empty model, just with my random intercepts) I have these results already. Then for the second part, I want to quantify the variance explained by my predictors in each level (I think this is the R2 within my subgroups).
My dataset is actually structured in 3 levels: participants (identr), meal type (fco_code), and data level.
Is the following the correct use of the R2 by subgroups measure?
#R2 level 3 (identr)
post_identr <- posterior_predict(fit,re.form = ~ (1|identr))
postlin_identr <- posterior_linpred(fit,transform=TRUE,re.form = ~ (1|identr))
r2_identr <- mean(apply(postlin_identr,1,FUN=var))/ mean(apply(post_identr,1,FUN=var))
#R2 level 2 (fco_code)
post_fco <- posterior_predict(fit,re.form= ~ (1|fco_code:identr))
postlin_fco <- posterior_linpred(fit,transform= TRUE, re.form = ~ (1|fco_code:identr))
r2_fco <- mean(apply(postlin_fco,1,FUN=var))/mean(apply(post_fco,1,FUN=var))
#R2 level 1 (data level)
postlin_data <- posterior_linpred(fit,transform=TRUE, re.form=NA)
post_data <- posterior_predict(fit, re.form=NA)
r2_data <- mean(apply(postlin_data,1,FUN=var))/ mean(apply(post_data,1,FUN=var))
For example in level 3, does the result give only the variance explained by the predictors in my fit model within this subgroup (or does this include also the observation level)?
And in the case of level 2, this R2 means the variance explained by the predictors only on the fco_code level? (note that these are random intercepts within level 3).
My doubts arise because I am getting unexpected results: among the predictors in the model are sex, which I would expect to explain an important part of the variance in the participant (identr) level, however, the value comes out to be around 0.08.