-
Notifications
You must be signed in to change notification settings - Fork 28
Description
If the formula is specified explicitly, e.g.
formula = "tab1$dep_var ~ tab1$p_var + (1 | tab1$m_var)" and dataName = NULL
instead of
formula = "dep_var ~ p_var + (1 | m_var)" and dataName = tab1
then the function fails due to some bugs.
This part is not breaking the code, however the second and third lines are useless (replacements never happen, because "(" is already replaced). If they are relevant, then they should be moved to the top, so that the replacement can happen.
formulatext <- gsub("(", "", formulatext, fixed = TRUE)
formulatext <- gsub("(1", "", formulatext, fixed = TRUE) # this never happens, as "(" is replaced by ""
formulatext <- gsub("(0", "", formulatext, fixed = TRUE) # this never happens, as "(" is replaced by ""
Further below there are some problematic lines of code:
for (i in 1:length(model.variables)) {
elt <- unlist(strsplit(model.variables[i], split = "$",
fixed = TRUE))
if (length(elt) > 1) {
assign(elt[length(elt)], eval(parse(text = model.variables[i]),
envir = parent.frame()), envir = parent.frame())
originalFormula.modified <- gsub(model.variables[i],
elt[length(elt)], originalFormula, fixed = TRUE)
varnames <- append(varnames, elt[length(elt)])
}
else {
varnames <- append(varnames, elt)
}
}
All the replacements in originalFormula.modified except for the last are lost. This is not an issue when dataName is specified. However, when dataName = NULL, then this modified formula will have to be used below. The lines could be corrected this way:
originalFormula.modified <- originalFormula # <- correction 1
for (i in 1:length(model.variables)) {
elt <- unlist(strsplit(model.variables[i], split = "$",
fixed = TRUE))
if (length(elt) > 1) {
assign(elt[length(elt)], eval(parse(text = model.variables[i]),
envir = parent.frame()), envir = parent.frame())
originalFormula.modified <- gsub(model.variables[i],
elt[length(elt)], originalFormula.modified, fixed = TRUE) # <- correction 2
varnames <- append(varnames, elt[length(elt)])
}
else {
varnames <- append(varnames, elt)
}
}
At this stage the following lines would still fail, because the formula with "tab1$" is used (formula2use) and because the data object dataDF is NULL:
iterations = utils::capture.output(mg <- try(lme4::lmer(formula2use,
offset = offset.to.use, weights = weights.to.use,
data = dataDF, REML = REML, verbose = verbose, control = control.obj)))
To correct this, I would just add some lines to update the wrong objects:
# the first 4 lines are new
if (is.null(dataDF)) {
formula2use <- stats::as.formula(originalFormula.modified, env = parent.frame())
dataDF <- as.data.frame(all.data)
}
iterations = utils::capture.output(mg <- try(lme4::lmer(formula2use,
offset = offset.to.use, weights = weights.to.use,
data = dataDF, REML = REML, verbose = verbose, control = control.obj)))