deruncie / MegaLMM

R package for fitting high-dimensional multivariate linear mixed effect models
https://deruncie.github.io/MegaLMM/
MIT License
35 stars 14 forks source link

More random effects #8

Open Yansen0515 opened 2 years ago

Yansen0515 commented 2 years ago

Hi Debiel E. Runcie, My model consists of three random effects. Does your algorithm support more randoms effects? According to my check, only two random effects, genetic and residual, can be included in the model. I'm not sure my view is correct, maybe I'm missing something. So please reply me.

Kind Regards, Yansen

deruncie commented 2 years ago

Hi Yansen,

Yes, you can by specifying a model like ~(1|Family) + (1|ID) + .... Each (1|X) term specifies a random effect with group X. Each random effect can be linked to a covariance matrix with the relmat argument (relmat = list(Family = K_fam, ID = K_ID)). If you want two random effects both at the ID level but with different covariance matrices, you have to create duplicate ID columns in your data (ID1, ID2,...), and then each can be linked to its own relmat matrix.

However, a warning - each additional random effect you include will make the memory and computation increase exponentially. The model forms a grid with run_parameters$h2_divisions points per random effect, so if the grid has size 20, it'll work with 20 levels for 1 random effect, but 8,000 with 3 random effects. You can use the function: estimate_memory_initialization_MegaLMM(MegaLMM_state) to estimate the memory requirements before calling initialize_MegaLMM(MegaLMM_state,verbose = T) to make sure your system can handle it.

Finally, depending on your model, you may be able to specify it in such a way that you get additional random effects as columns of your data instead which is much more efficient.

Dan

Yansen0515 commented 2 years ago

Hi Dan, Thank you for your reply. You are right, the two random effects (except residual) both at the ID level but with different covariance matrices. My model is a repeatability model. I have three random effects, which are additive genetic (AG, link to ID), permanent environment (PE, also link to ID, cope to duplicate_ID), and residual. So I will use the "relmat= list(AG=ID, PE=duplicate_ID)".

Thanks again, Yansen

deruncie commented 2 years ago

Are AG and PE both covariance matrices with #rows and #columns equal to the number of levels of ID? If so you'd want to switch around the arguments: formula = ~(1|ID) + (1|duplicate_ID),relmat = list(ID = AG, duplicate_ID = PE)

Yansen0515 commented 2 years ago

Hi Dan, Not yet. Only the covariance matrices of "AG" with rows and columns equal the number of levels of ID. The covariance matrices of "PE" with rows and columns equal the number of records and levels of ID, respectively. So, I don't know how to set such parameters.

Yansen

Yansen0515 commented 2 years ago

Hi Daniel, Sorry, your explanation is correct. I misunderstood. Thank you very much for your help.

Kind regards, Yansen