[R] model.frame: how does one use it?
Deepayan Sarkar
deepayan.sarkar at gmail.com
Fri Jun 15 22:08:31 CEST 2007
On 6/15/07, hadley wickham <h.wickham at gmail.com> wrote:
> On 6/15/07, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:
[...]
> > By 'name', I mean a symbol, such as 'Age' or 'myweight'. So
> > basically, everything is as you would expect if the name is visible in
> > data, but if not, the search starts in the environment of the formula,
> > not the environment where the function call is being made (which is
> > the standard evaulation behaviour). This is a feature, not a bug
> > (things would be a lot more confusing if it were the other way round).
>
> Could you give an example? It's always seemed confusing to me and I
> don't see why looking in the environment of the formula helps.
Good question. I remember being convinced that it was a good idea, but
no longer remember why. This is the best I can come up with right now:
## stupid function that fits a model on a random subsample
lmsub <- function(formula, data, p = 0.7)
{
n <- nrow(data)
newdata <- data[sample(round(n * p)), ]
lm(formula, newdata)
}
mydata <- data.frame(x = 1:100, y = rnorm(100))
n <- 2
lmsub(y ~ poly(x, n), data = mydata)
I don't think the use of the name 'n' is unusual in either case, and
we definitely wouldn't want the one inside 'lmsub' to be used for the
formula.
-Deepayan
More information about the R-help
mailing list