Entering edit mode
2.7 years ago
ymj
▴
10
Hi
I want to run following linear mixed model, for each metabolite in my dataset where I hace fixed effects gender and timepoint, and random effect the person_id.
For each metabolite seperately, the code works, but if I put it in this for loop, it gives following error:
Error in model.frame.default(data = sample_df_M, drop.unused.levels = TRUE, :
variable lengths differ (found for 'gender')
Is there another way ? Because the code does work if i use the first metabolite, which is Valine (Val) , then I get the output. As soon as I put it in the for loop, it gives the error.
library("lme4")
metabolites <- colnames(metab.matrix)
for (m in metabolites) {
out <- lmer(m ~ gender+ month+ (1|person_id), data=sample_df_M)
out0 <- lmer(m ~ (1|person_id), data = sample_df_M)
comp <- anova(out0, out)
pv <- comp[["Pr(>Chisq)"]][[2]]
print(pv)
}
thank you in advance!
Do all metabolites fail in the for loop execution or just specific metabolites? What happens when you run the loop with just valine in the
metabolites
vector? Does it still work? This would test whether or not the issue is related to any modifications you made specifically for the for loop execution.The error message sounds like the length of
m
andgender
differ but not too sure...Thanks for your reply! I found this answer on stackoverflow, with a bit editing, it worked :) For anyone who has the same problem:
https://stackoverflow.com/questions/56135367/writing-loop-function-to-generate-various-mixed-model-result/56136533