JenniNiku / gllvm

Generalized Linear Latent Variable Models
https://jenniniku.github.io/gllvm/
49 stars 20 forks source link

Multiple random intercepts #16

Closed jebyrnes closed 6 months ago

jebyrnes commented 4 years ago

Something that is unclear to me - let's say I have a study with a random effect of site and year. This would lead to a random intercept. In the past, in, say, lme4, I would use

response ~ predictor + (1|site) + (1|year)

I've been trying to figure this out in gllvm, but, haven't gotten a clear idea yet beyond having each site:year as a row and using row.eff = "random" - but that doesn't accommodate, say, multiple samples per site:year - I have some intuition that randomX should be the answer, but, again, am unclear how to implement.

Suggestions? Anything that might be in a vignette to show this fairly common example?

BertvanderVeen commented 12 months ago

Hello Jarret! I have been working on making this possible, and have just opened PR #138. Only three years late, but perhaps it is still of use to you. If you end up testing it, I would be happy if you could let me know if you find any bugs.

As an example of how simple crossed/nested random intercept example works in an unconstrained ordination:

devtools::install_github("https://github.com/JenniNiku/gllvm")
library(gllvm)
data(spider)
# nested
model <- gllvm(spider$abund, num.lv = 2, family="poisson", row.eff= ~(1|group/plot), studyDesign = data.frame(group=rep(1:2,14), plot = rep(1:4, each = 7)))

# crossed
model2 <- gllvm(spider$abund, num.lv = 2, family="poisson", row.eff= ~(1|group)+(1|plot), studyDesign = data.frame(group=rep(1:2,14), plot = rep(1:4, each = 7)))

though it is of course also possible to combine with all other models in gllvm, e.g., constrained or concurrent ordination, or the 4th corner model.

Edit: updated the associated repo, this is in the main branch now.

jebyrnes commented 12 months ago

Definitely still of use! I have a project coming up where this will be perfect!

AlainZuur commented 11 months ago

That is a nice Sinterklaas gift!

joao-pompeu commented 6 months ago

Hey Bert,

I have a study with repeated measurements in different sites, and I would like to work with the nested design in my analysis. But when I tried to replicate your example above but I've got an error: "Error in xtfrm.data.frame(x) : cannot xtfrm data frames". Any ideas in what it would be?

BertvanderVeen commented 6 months ago

Hey Bert,

I have a study with repeated measurements in different sites, and I would like to work with the nested design in my analysis. But when I tried to replicate your example above but I've got an error: "Error in xtfrm.data.frame(x) : cannot xtfrm data frames". Any ideas in what it would be?

Great! I will need a little more information to be able to assist. What version of the package are you using and what is the model that you are trying to fit?

joao-pompeu commented 6 months ago

Bert, I'm using version 1.4.3. In my case, I'm trying to do constrained ordination to understand which of my predictors help to understand pest attack, it's more 'multivariable' like than 'multivariate'. In details: I am measuring pest attack from the same plots every month in 2 fields and assessing plants responses by remote sensing imagery, extracting information from each plot. So I have hundreds of predictors and many of them are correlated. My initial trial was:

m0 <- gllvm(Y, num.lv = 1, studyDesign = st_design, row.eff = ~(1|plot/area), family = "tweedie"). Where Y contains only the pest attack observed in each plot in each month, st_design is a dataframe with the coded plots and fields for random effects, and tried with num.lv = 0 and 1.

I tried in this way just to see if it would run as expected by your example above. However trying your spider example or my case gave the same output.

BertvanderVeen commented 6 months ago

Thanks for the clarification. The possibility to have multiple row effects is relatively new, and came after v1.4.3. (which is the current CRAN version). See NEWS.

Please install the github version of the package via devtools::install_github("JenniNiku/gllvm), and see if you can manage to fit your model with that.