Short summary of the problem
trying to model through a for loop and change model formula dynamically, stuck at the model formula throwing error, wonder if you have input on how to create the formula allowing for a change dynamically
code_to_run_your_model(if_applicable)
allnames<-c(“BRADY”,“ADVANCEDAGE”,“SEPSIS”,“COPD”,“CKD”,“LIVER”)
library(gtools)
library(brms)
permutated<-data.frame(permutations(length(allnames), length(allnames), v=allnames, set=F, repeats.allowed=T))
permutated<-permutated[!duplicated(permutated[c(1:length(allnames))]),]
permutated$fineq<-apply(permutated,1,function(x) gsub("._", " “, paste0(”+",tolower(sort(unique(x))),“int”,"",sort(unique(x)),collapse = “”)))
permutated$fingroups<-apply(permutated[,1:length(allnames)],1,function(x) gsub("._", " “, paste0(”,",tolower(sort(unique(x))),“int”,"~1+ (1|ID/BOV)",collapse = “”)))
permutated$finpri<-apply(permutated[,1:length(allnames)],1,function(x) gsub("._", " “, paste0(”,",“prior(normal(0.001, 100), nlpar = '”,tolower(sort(unique(x))),“int’,lb=0.0001)”,collapse = “”)))
permutated<-permutated[!duplicated(permutated$fineq),]
permutatedfin<-permutated[,(length(allnames)+1):(length(allnames)+3),drop=F]
i<-2
mod_formula<-bf(print(as.formula(paste0(“QT ~ (((RR^alpha)QT0+genSEX)+Acos(3.1416/12(CYRCADIAN-pha)))”,permutatedfin[i,][,1]))),
for (j in 1:length(strsplit(paste0(",alpha ~ 1+ (1|ID/BOV),gen ~ 1,QT0 ~ 1+ (1|ID/BOV),A ~ 1+ (1|ID/BOV),pha~1+ (1|ID/BOV)",permutatedfin[i,][,2]),",")[1][[1]][-1])){
print(as.formula(strsplit(paste0(",alpha ~ 1+ (1|ID/BOV),gen ~ 1,QT0 ~ 1+ (1|ID/BOV),A ~ 1+ (1|ID/BOV),pha~1+ (1|ID/BOV)",permutatedfin[i,][,2]),",")[1][[1]][-1][j]))
},nl = TRUE )
If possible, add also code to simulate data or attach a (subset of) the dataset you work with.
Please also provide the following information in addition to your question:
- Operating System: Windows
- brms Version: 2.16.1
Don’t forget to add relevant tags to your topic (top right of this form) especially for application area. Delete this text before posting your question :-) Thx!
Looking forward to your topic!
Thanks,
Andras