MOFA2 icon indicating copy to clipboard operation
MOFA2 copied to clipboard

How do I calculate one explained-variance value for each factor?

Open ravipatel4 opened this issue 2 years ago • 2 comments

Hello, I am able to calculate the explained variance for each factor and dataset/view. But I am unable to find a way to calculate one variance value for each factor that combines the variance explained across all datasets. Is this something even possible or makes sense? I considered adding variances across datasets for each factor to get this value, but again, I am unsure if this is valid. Is there any function within MOFA2 that I could use, or if you could suggest me how I can calculate this? This would be quite helpful to me in deciding which factors I should prioritize on.

Thank you for developing such a useful tool. Hope to hear back soon.

ravipatel4 avatar Jun 26 '22 06:06 ravipatel4

Hi @ravipatel4, We calculate a variance explained per factor across all views when determining the order of factors, see here. Therefore, by selecting the top N factors you are selecting the N factors that explain the highest variance across all views. Keep in mind however that this is not an interpretable measure.

To calculate this you could use the cached variance explained values and apply a sum (note that just I copy pasted and didn't run the code, maybe you need to do minor edits):

var_explained_values <- MOFAobject@cache[["variance_explained"]]$r2_per_factor
rowSums(sapply(var_explained_values, function(e) rowSums(e, na.rm = TRUE)))

rargelaguet avatar Jun 26 '22 07:06 rargelaguet

Hi - I had a similar question thus jumping on this thread. When trying to calculate the % of variance captured by each factor using the code @rargelaguet shared - var_explained_values <- MOFAobject@cache[["variance_explained"]]$r2_per_factor rowSums(sapply(var_explained_values, function(e) rowSums(e, na.rm = TRUE))) , I get value>100% for some of the factors in one of the models I am running. I am not sure what am I doing incorrectly. Any thoughts ? Thank you!

aditiqamra avatar Apr 06 '23 14:04 aditiqamra