r - Use tryCatch() but don't overwrite object when error/warning encountered -
i want iteratively fit lmer() model within for loop , store results. when encounter error, don't want mermod object (the output lmer() model) overwrite mermod object previous iteration. example: # install.packages(c("lme4", "dplyr", "ggplot2"), dependencies = true) library("lme4") library("dplyr") library("ggplot2") predlist <- list() j <- 1 for(i in 2:9){ tr <- sleepstudy %>% filter(days < i) pr <- sleepstudy %>% filter(days == i) fm <- trycatch({lmer(reaction ~ days + (1|subject), data=sleepstudy)}, warning = function(w) {#code move along `predict` without overwriting `fm`}, error = function(e) {#code move along `predict` without overwriting `fm`}) #predict reaction pr$prre <- predict(fm, pr) predlist[[j]] <- pr j = j + 1 } pred <- bind_rows(predlist) %>% arrange(subject, days) ggplot(data=pred, aes(reaction, prre)) + ge...