From bhs2 at mevik.net  Sat Nov 19 11:03:47 2005
From: bhs2 at mevik.net (=?iso-8859-1?q?Bj=F8rn-Helge_Mevik?=)
Date: Sat, 19 Nov 2005 11:03:47 +0100
Subject: [Rd] challenge: using 'subset = <computed>' inside function ..
In-Reply-To: <17278.14970.987870.440521@stat.math.ethz.ch> (Martin
	Maechler's message of "Fri, 18 Nov 2005 21:32:58 +0100")
References: <17278.14970.987870.440521@stat.math.ethz.ch>
Message-ID: <m0zmo1lzss.fsf@bar.nemo-project.org>

Hmm.. Maybe I'm overlooking something, but why not use do.call()?
For instance

tst <- function(formula, data, na.action = na.omit) {

    stopifnot(inherits(formula,"formula"), length(formula) == 3)
    ## I want to fit a model to those observations that have 'Y > 0'
    ## where 'Y' is the left-hand-side (LHS)
    ## The really natural problem is using 'subset'; since I want to keep 'data' intact
    ## It's really  lm(), glm(), gam(), ... but the problem is with model.frame:

    cat("subsetting expression: ")
    print(substitute(Y > 0, list(Y = formula[[2]])))# is perfect
    YY <- formula[[2]]
    cat("  or   "); print(bquote(.(YY) > 0))

    mf <- do.call("model.frame", list(formula = formula, data = data,
                                      subset = bquote(.(YY) > 0),
                                      na.action = na.action))
    mf
}

It seems to work for me:

> mydata <- data.frame(y = rep(c(-1, 1), each = 5), x = rnorm(10))
> tst(y ~ x, data = mydata)
subsetting expression: y > 0
  or   y > 0
   y          x
6  1  0.9568283
7  1  0.1166081
8  1 -0.9592458
9  1 -0.0974119
10 1  0.2217222


-- 
Bj?rn-Helge Mevik



[Rd] challenge: using 'subset = <computed>' inside function ..
=?iso-8859-1?q?Bj=F8rn-Helge_Mevik?=