Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.frame method #20

Open
LiNk-NY opened this issue Jun 26, 2018 · 5 comments
Open

model.frame method #20

LiNk-NY opened this issue Jun 26, 2018 · 5 comments

Comments

@LiNk-NY
Copy link
Contributor

LiNk-NY commented Jun 26, 2018

It seems like when using lm-type of functions, the DataFrame does not behave like the
data.frame.
An example as given in therneau/survival#46:

library(splines)
library(S4Vectors)
data(longley)
test <- DataFrame(longley)
test$year2 <- ns(test$Year, 3)

model.frame(GNP ~ Unemployed + year2, data = test)
## Error: object 'year2' not found

# similarly...


lm(GNP ~ Unemployed + year2, test)
## Error: object 'year2' not found

Regards,
Marcel

@lawremi
Copy link
Collaborator

lawremi commented Jun 26, 2018

How about submitting a PR adding an S3 method for model.frame() on DataFrame? Note that model.frame() can operate on environments, and one can coerce a DataFrame to an environment with as.env(). Thanks!

@LiNk-NY
Copy link
Contributor Author

LiNk-NY commented Aug 10, 2018

Hi Michael, @lawremi

The model.frame method could look like this:

model.frame.DataFrame <- function(formula, ...)
{
    formula <- as.data.frame(formula)
    NextMethod()
}

It uses the model.frame.default method on a data.frame.

model.frame dispatches on the formula class. I'm not sure how to make it work with DataFrame
other than modifying model.frame.default. Coercing to environment beforehand when
model.frame.default works though:

model.frame(GNP ~ Unemployed + year2, data = as.env(test))

@lawremi
Copy link
Collaborator

lawremi commented Aug 10, 2018

Yea, that's what I meant by using as.env(). So you have a way forward, right?

@LiNk-NY
Copy link
Contributor Author

LiNk-NY commented Aug 10, 2018

I'm not sure how to implement this method.

model.frame.DataFrame only works when the formula argument is a DataFrame.
It doesn't work when you have a model.frame(y ~ x, data = DF) type of construction.

What am I missing here?

@lawremi
Copy link
Collaborator

lawremi commented Aug 10, 2018

I see. Passing a formula dispatches to model.frame.default(), so we could introduce a model.frame.formula() that delegates to an S4 generic, which dispatches to a method that calls as.env(). If they pass something other than a formula, this breaks.

Now that I look into it, since 2010 (after we made as.env()), base::as.environment() is generic, dispatching internally. It lacks the extra arguments of as.env(), but a case could be made for model.frame.default() to call as.environment(data). It already works for data.frame. Up for submitting a patch to R?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants