Following on from a stackoverflow question "Why does this simple function calling `lm(..., subset)` fail?"
<http://stackoverflow.com/questions/37326815>
----------
myfun <- function(form., data., subs.) lm(form., data., subs.)
myfun(mpg ~ cyl + hp, mtcars, TRUE)
## Error in eval(expr, envir, enclos) : object 'subs.' not found
---------

The answer to the stated question was in ?lm "If not found in data, the variables are taken from environment(formula), typically the environment from which lm is called"; the environment of the formula (mpg ~ cyl + hp) does not contain 'subs.'. A fix is quite straightforward, set the environment of the formula to that of the function, which does contain 'subs.'. There are multiple ways of doing that, this works but to me seems a bit "clunky":
---------------
myfun <- function(form., data., subs.) lm(as.formula(deparse(form.)), data., subs.)
myfun(mpg ~ cyl + hp, mtcars, TRUE)
--------------
To me this seems more elegant, but then I have no taste :-}
----------
myfun <- function(form., data., subs.){
  environment(form.) <- environment()
  lm(form., data., subs.)}
myfun(mpg ~ cyl + hp, mtcars, TRUE)
----------

But the OP went on to consider `expand.model.frame` e.g.
-------------
myfun <- function(form., data., subs.){
  environment(form.) <- environment()
  model <- lm(form., data., subs.)
  print(ls(envir = environment(formula(model))))
  expand.model.frame(model, ~drat)}
myfun(mpg ~ cyl + hp, mtcars, TRUE)
## [1] "data." "form." "model" "subs."
## Error in eval(expr, envir, enclos) : object 'subs.' not found
-------------

myfun can be fixed by (e.g.) avoiding the subset argument of lm
------------
myfun <- function(form., data., subs.){
  environment(form.) <- environment()
  model <- lm(form., data.[subs.,])
  expand.model.frame(model, ~drat)}
myfun(mpg ~ cyl + hp, mtcars, TRUE)
------------
... but this message is about the apparent inconsistency between the behaviour of expand.model.frame and the help text which says:
?expand.model.frame:
-----------------------
Usage

expand.model.frame(model, extras,
                   envir = environment(formula(model)),
                   na.expand = FALSE)
<snip>
envir   an environment to evaluate things in
---------------------

In the example of the `expand.model.frame` issue above the result of the 'ls()' clearly shows that 'subs.' is in that environment, but expand.model.frame fails to find it.

Am I misunderstanding?
Or is there an error in the help text?
Or is there a bug in expand.model.frame?

=====================
I don't think this is relevant, but for completeness
> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows Server 2008 R2 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] graphics grDevices datasets stats tcltk utils tools methods base

other attached packages:
[1] CBRIutils_1.0 stringr_1.0.0 svSocket_0.9-57 TinnR_1.0-5 R2HTML_2.3.1 Hmisc_3.17-4 ggplot2_2.1.0
 [8] Formula_1.2-1   survival_2.39-4 lattice_0.20-33

loaded via a namespace (and not attached):
[1] Rcpp_0.12.5 magrittr_1.5 cluster_2.0.4 splines_3.3.0 devtools_1.11.1 [6] munsell_0.4.3 colorspace_1.2-6 plyr_1.8.3 nnet_7.3-12 grid_3.3.0 [11] data.table_1.9.6 gtable_0.2.0 latticeExtra_0.6-28 withr_1.0.1 svMisc_0.9-70 [16] digest_0.6.9 Matrix_1.2-6 gridExtra_2.2.1 RColorBrewer_1.1-2 acepack_1.3-3.3 [21] rpart_4.1-10 memoise_1.0.0 stringi_1.1.1 scales_0.4.0 foreign_0.8-66
[26] chron_2.3-47

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to