Skip to content

Incorrect output when applying lmer within a loop.

2 messages · Anton Wasson, Ben Bolker

#
Anton Wasson <anton.wasson at ...> writes:
As it turns out, it's more of an R issue than an lme4 issue.

[snip]
"X180" "X190"
Or 

nam <- c(paste0(X,(2:20)*10),c("md","tc","md90"))

[snip]
It would be cleaner and safer to use unlist(VarCorr(r.mer)) to access the RE
variances,
and sigma(fm1)^2 to get the residual variance.

  You can use 

    reformulate(paste0("(1|",c("GEN","OPR","ROW","RAN"),")"),response="V1")

## V1 ~ (1 | GEN) + (1 | OPR) + (1 | ROW) + (1 | RAN)

to generate formulae with different reponse variables, and use the same
input data set every time.
[snip]

Your problem is that R stops running the code when you try to run the
example with the error, leaving the last four rows equal to zero as
they were initialized.  It is generally safer to fill in NA in the
data frame that will hold the results, so that you can see what hasn't
been computed.

d <- as.data.frame(matrix(NA,nrow=length(nam),ncol=6),
                   dimnames=list(nam,c("REP","ROW","RAN","GEN","OPR","VAR")))

You could use try() to intercept errors, but you would still have to
check the results (if inherits(model_results,"try-error")) to skip
the row appropriately