The convergence problem is something I never came to understand entirely, but I as far as I know, there are some keys in the code to get the model to convergence I am not attaching all the database because long-format exceeds the limits here.
I would actually want to ask 2 questions:
- How can I skip it in a loop? Meaning: some piece of code to skip this variable in the loop
#sthg like
if(error code = 1) next
- I am really intriged, why am I getting results when I run the model individually for the variable is causing trouble?
genes_wide_s18 <- genes_s18 %>% pivot_wider(names_from = gen, values_from = dCt_s18)
genes_wide_s18$time [genes_wide_s18$time == "1"] <- 1
genes_wide_s18$time [genes_wide_s18$time == "3"] <- 3
genes_wide_s18$time<-as.numeric(genes_wide_s18$time)
# names of variables
genes.names <- colnames(genes_wide_s18)[25:79]
gene.list$s18 <- NULL
no.genes <- length(genes.names)
# create a named list to hold the fitted models
gene.list <- as.list(1:no.genes)
names(gene.list) <- genes.names
# loop over gene names
for(i in genes.names){
# print status
print(paste("Running entity:", i, "which is", which(genes.names==i), "out of", no.genes))
# trying to ignore variable because of few observations
if(i == "s18")next
print(i)
# create temporary data matrix and model formula
#1st: adjusting per several variables
tmp <- genes_wide_s18[, c(i,"edad0","time", "grup_int", "id", "sexo", "peso1")]
fml <- as.formula( paste( i, "~", paste(c("sexo:peso1", "edad0","time", "grup_int", "time:grup_int"), collapse=" ")))
#2nd: adjusting just per group
tmp <- genes_wide_s18[, c(i,"grup_int","time", "id")]
fml <- as.formula( paste( i, "~", paste(c("time:grup_int"), collapse=" ")))
gene.list[[i]] <- lme(fml, random= ~ time|id, method="REML", data=tmp, na.action = na.omit)
# assign fit to list by name
gene.list[[i]] <- lme(fml, random= ~ time|id, control = lmeControl(opt = "optim"), method="REML", data=tmp, na.action = na.omit)
}
.
.
.
.
[1] "Running entity: rxra which is 6 out of 55"
[1] "rxra"
Error in lme.formula(fml, random = ~time | id, method = "REML", data = tmp, :
nlminb problem, convergence error code = 1
message = false convergence (8)
However when I run this individually, substituting i = rxra in fml :
lme(as.formula( paste( "rxra", "~", paste(c("time:grup_int"), collapse=" "))), random= ~ time|id, control = lmeControl(opt = "optim"), method="REML", data=tmp, na.action = na.omit)
Linear mixed-effects model fit by REML
Data: tmp
Log-restricted-likelihood: -392.218
Fixed: as.formula(paste("rxra", "~", paste(c("time:grup_int"), collapse = " ")))
(Intercept) time:grup_int1 time:grup_int2 time:grup_int3
3.6723896 -0.2699351 -0.2630362 -0.2104078
Random effects:
Formula: ~time | id
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 1.6296951 (Intr)
time 0.7984285 -0.982
Residual 0.5870576
Number of Observations: 279
Number of Groups: 140
str(tmp)
tibble [15,968 × 4] (S3: tbl_df/tbl/data.frame)
$ rxra : num [1:15968] NA NA NA NA NA ...
$ grup_int: Factor w/ 3 levels "1","2","3": 3 3 3 3 3 3 3 3 3 3 ...
..- attr(*, "label")= chr "GENERAL: Grupo de intervención"
$ time : num [1:15968] 1 1 1 1 1 1 1 1 1 1 ...
CodePudding user response:
you seem to run lme twice for a given i, immediately overwriting the first result with the second. it looks like when you run it manually you mimic the second call where you specifiy control= ; so this is likely the difference between getting the failure message and not.
gene.list[[i]] <- lme(fml, random= ~ time|id, method="REML", data=tmp, na.action = na.omit)
# assign fit to list by name
gene.list[[i]] <- lme(fml, random= ~ time|id, control = lmeControl(opt = "optim"), method="REML", data=tmp, na.action = na.omit)