[Rd] model.matrix() may be misleading for "lme" models
Ivan Krylov
|kry|ov @end|ng |rom d|@root@org
Mon Sep 23 21:54:17 CEST 2024
В Sun, 22 Sep 2024 10:23:50 -0400
John Fox <jfox using mcmaster.ca> пишет:
> > Evaluating object$call$data in the environment of the suggested
> > nlme:::model.matrix.lme function may also not work right. Without an
> > explicit copy of the data, the best environment to evaluate it in
> > would be parent.frame().
>
> I'm afraid that I don't understand the suggestion. Isn't
> parent.frame() the default for the envir argument of eval()? Do you
> mean the parent frame of the call to model.matrix.lme()?
Yes, I do mean the parent frame of the model.matrix.lme() function
call. While eval()'s default for the 'envir' argument is
parent.frame(), this default value is evaluated in the context of the
eval() call. Letting model.matrix.lme() call eval() results in the
'envir' being the eval()'s parent, the model.matrix.lme() call frame.
In most cases, model.matrix.lme() works as intended: either lme() has
been given the 'data' argument, so object$data is not NULL and the
branch to eval() is not taken, or 'data' has not been given, so both
object$data and object$call$data are NULL, and NULL doesn't cause any
harm when evaluated in any environment. In the latter case
model.matrix.default() can access the variables in the environment of
the formula.
With keep.data = FALSE, the function may evaluate object$call$data in
the wrong environment:
maybe_model_matrix <- function(X)
model.matrix(lme(distance ~ Sex, random = ~ 1 | Subject, X,
contrasts=list(Sex=contr.sum), keep.data=FALSE))
maybe_model_matrix(Orthodont)
# Error in eval(object$call$data) : object 'X' not found
...but then model.matrix.default doesn't work on such objects either,
and if the user wanted the data to be accessible, they could have set
keep.data = TRUE. I can't tell whether evaluating object$call$data in
environment(object$formula) is a better or worse idea than
parent.frame().
--
Best regards,
Ivan
More information about the R-devel
mailing list