[Rd] expand.model.frame() fails when subset is specified (PR#
979)
Warnes, Gregory R
gregory_r_warnes@groton.pfizer.com
Thu, 14 Jun 2001 17:31:14 -0400
> Thanks. This is also present in the current pre-1.3.0. Your patch
> looks correct, but I wonder if the default for "enclos"
> should not be
> environment(formula(model)) rather than parent.frame() as it is now?
>
> (And wouldn't it be better named "envir"?)
Peter,
I was merely making an incremental improvement, your suggestions provide
additional improvements. Go right ahead and make the changes. [This seems
wierd, are you asking
me, non-R-core person that I am, permission to make changes to the R source
code?]
Just for illustration, here's an example of why it is better to use
environment(formula(model)) rather than parent.frame():
> fun <- function() {
+ data <- data.frame(x=1:10,y=1:10,z=1:10,m=1:10)
+ model <- lm( y ~ x, data=data, subset=m<8)
+ model
}
> test <- fun()
>
With my version:
> expand.model.frame(test, ~ z )
Error in as.data.frame.default(data) : can't coerce function into a
data.frame
With the additional changes:
> test <- fun()
> expand.model.frame(test, ~ z )
y x z
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
Pretty nifty.
-Greg
Just in case its helpful, heres the patch:
-------BEGIN PATCH-------
diff -c R-1.2.3.orig/src/library/base/R/expand.model.frame.R
R-.2.3/src/library/base/R/expand.model.frame.R
*** R-1.2.3.orig/src/library/base/R/expand.model.frame.R Thu Jun 14
17:29:43 2001
--- R-1.2.3/src/library/base/R/expand.model.frame.R Thu Jun 14 17:29:43
2001
***************
*** 1,9 ****
! expand.model.frame <- function(model, extras, enclos=parent.frame(),
na.expand=FALSE)
{
## don't use model$call$formula -- it might be a variable name
f <- formula(model)
! data <- eval(model$call$data, enclos)
# new formula (there must be a better way...)
ff <- foo ~ bar + baz
--- 1,10 ----
! expand.model.frame <- function(model, extras,
! envir=environment(formula(model)),
na.expand=FALSE)
{
## don't use model$call$formula -- it might be a variable name
f <- formula(model)
! data <- eval(model$call$data, envir)
# new formula (there must be a better way...)
ff <- foo ~ bar + baz
***************
*** 18,27 ****
if (!na.expand){
naa <- model$call$na.action
subset <- model$call$subset
! rval <- model.frame(ff, data=data, subset=subset, na.action=naa)
} else {
subset <- model$call$subset
! rval <- model.frame(ff, data=data, subset=subset, na.action=I)
oldmf <- model.frame(model)
keep <- match(rownames(oldmf), rownames(rval))
rval <- rval[keep, ]
--- 19,30 ----
if (!na.expand){
naa <- model$call$na.action
subset <- model$call$subset
! rval <- eval(call("model.frame",ff, data = data, subset = subset,
! na.action = naa),envir )
} else {
subset <- model$call$subset
! rval <- eval(call("model.frame",ff, data = data, subset = subset,
! na.action = I), envir)
oldmf <- model.frame(model)
keep <- match(rownames(oldmf), rownames(rval))
rval <- rval[keep, ]
--------END PATCH--------
LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._