A simple first step you could take to speed up your code (by almost 100%) is to not run the lme()
model twice; assign the result of try(lme(...))
to m1
and then test to see if it inherits an error; compare your code from above (placed into a function) to a function with this approach:
sim_slow <- function() {
y=rnorm(10000,100,15)
x=rnorm(10000,100,15)
t=rep(seq(1,100,1),100)
i=sort(rep(seq(1,100,1),100))
my.data=data.frame(i,t,x,y)
if(!inherits(try(nlme::lme(y~x+t,random=~x+t|i,data=my.data), silent=T),"try-error"))
{
return(nlme::lme(y~x+t,random=~x+t|i,data=my.data))
}
return(NULL)
}
sim_fast <- function(no_warn = TRUE) {
if ( no_warn ) { # If you also don't want the warnings
warn_option <- getOption("warn")
options(warn = -1)
on.exit(options(warn = warn_option))
}
y <- rnorm(10000, 100, 15)
x <- rnorm(10000, 100, 15)
t <- rep(seq(1, 100,1), 100)
i <- sort(rep(seq(1, 100, 1), 100))
my.data <- data.frame(i, t, x, y)
m1 <- try(nlme::lme(y ~ x + t, random = ~x + t|i, data = my.data), silent = TRUE)
return("if"(inherits(m1, "try-error"), NULL, m1))
}
Created on 2018-10-26 by the reprex package (v0.2.1)
Now let's see if there's a speed difference:
set.seed(16420)
system.time(slow_sims <- replicate(10, sim_slow()))
#> Warning in logLik.lmeStructInt(lmeSt, lmePars): Singular precision matrix
#> in level -1, block 1
#> Warning in logLik.lmeStructInt(lmeSt, lmePars): Singular precision matrix
#> in level -1, block 1
#> (Many similar warnings are omitted here for space)
#> user system elapsed
#> 19.612 0.008 19.621
set.seed(16420)
system.time(fast_sims <- replicate(10, sim_fast()))
#> user system elapsed
#> 11.704 0.000 11.703
sum(sapply(slow_sims, is.null))
#> [1] 3
sum(sapply(fast_sims, is.null))
#> [1] 3
all.equal(slow_sims, fast_sims)
#> [1] TRUE
Created on 2018-10-26 by the reprex package (v0.2.1)