# Can we extract marginal and conditional R2 from Mixed Effect Random Forest (MERF) models?

I have a question regarding the extraction of marginal and conditional R2 from Mixed Effect Random Forest (MERF). Considering RF models are quite robust in capturing non-linear responses I like to apply them. However, I am completely ignorant considering the higher mathematical background. I was playing around with my dataset and I wanted to fit a LMM with random effects however this model seemed less successful than the MERF. Yet, the goal was to see how much of my variation was captured by the fixed effects taking the random effects into account. The MiXRF package contains a MERF algorithm, but there is not package available to extract the marginal and conditional R2 from a MERF. Is it even possible to extract marginal and conditional R2 from an MERF? Additionally, does someone known where to located the variance of the fixed-random effects and residual? Thank you in advance. Below an example of what I tried.

library(MixRF)

#create random dataset y = response, x1 and x2 explanatory, r = clustered/random effect
set.seed(123)
data <- data.frame(y=c(runif(500, 0, 5), runif(500, 20, 100)),
x1=c(runif(500, 0, 3), runif(500, 10, 100)),
x2=c(runif(500, 8, 90), runif(500, 3, 10)),
r=sample(1:5, 1000, T))

#Apply MeRF, ignore warnings for singular fit
mod <- MixRF(Y=data$y, X=as.data.frame(data[c("x1", "x2")]), random = "(1|r)", MaxIterations = 10, data=data) #Display captured variation = 72% mod$forest

#Trying to find the variance of fixed, random and residuals but seem not able to locate them
mrf <- mod$$MixedModel #Found standarddeviation of the residuals (I think), but cannot find fixed and random effects mrf@devcomp$$cmp[10]

#Is it possible to divide the variance of the fixed effects by the sum of variance
#from fixed, random and residuals thereby calculating the marginal R2?
#Is it then also possible to calculated the conditional R2 by dividing the sum of
#the variance of the fixed and random effects by the sum of the variance
#from fixed, random and residuals?
#However, for that we first need to locate them in the output!!?


Edit:

With the MuMIn package it is possible to extract the marginal and conditional R2 of the model but this does not seem to add up.

#Captured variation of the RF model is 72%
#marginal and conditional are 0% and 0.00078%???
MuMIn::r.squaredGLMM(mod$MixedModel)  ## 1 Answer According to the author like this var.fix <- var(predict(mod$forest, data))
var.rand  <- as.data.frame(VarCorr(mrf))[1,4]
var.resid <- as.data.frame(VarCorr(mrf))[2,4]

#Marginal R2
var.fix / (var.fix + var.rand + var.resid)

#Conditional R2
(var.fix + var.rand) / (var.fix + var.rand + var.resid)