[R] question on formula and terms.formula()
Pang Du
pangdu at vt.edu
Thu Jul 14 18:10:24 CEST 2011
Points taken and terms(formula) is used now. Thanks.
-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Thursday, July 14, 2011 11:56 AM
To: Pang Du; r-help at r-project.org
Subject: RE: [R] question on formula and terms.formula()
I think you should replace
terms.formula(formula)
by
terms(formula)
When terms() is given a formula object it will
execute terms.formula but for other classes of
inputs it will invoke the appropriate method.
E.g., your formula may already be a terms object,
in which case terms.formula(formula) will waste
time reanalyzing it while terms(formula) will
invoke terms.terms, which just returns its input.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message-----
From: Pang Du [mailto:pangdu at vt.edu]
Sent: Thursday, July 14, 2011 8:36 AM
To: William Dunlap; r-help at r-project.org
Subject: RE: [R] question on formula and terms.formula()
Thank you so much for your suggestion, Bill.
The R program I try to modify needs match.call() for something else. But the
problem does seem to be caused by this statement as you suggested. Following
this clue, I find out that
terms.formula(formula)
does essentially what I want for "terms.formula(mf$formula)".
Pang Du
Virginia Tech
-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Wednesday, July 13, 2011 12:21 PM
To: pangdu at vt.edu; r-help at r-project.org
Subject: RE: [R] question on formula and terms.formula()
Does your code work if you omit the match.call step? terms()
generally doesn't need that. Also, do not call terms.formula(formula):
call just terms(formula). The terms method appropriate to the class
of the formula will be used.
> f1 <- function(formula, ...) {
+ terms(formula, ...)
+ }
> form <- as.formula("y ~ x1 + Error(x2)")
> f1(form, specials="Error")
y ~ x1 + Error(x2)
attr(,"variables")
list(y, x1, Error(x2))
attr(,"factors")
x1 Error(x2)
y 0 0
x1 1 0
Error(x2) 0 1
attr(,"term.labels")
[1] "x1" "Error(x2)"
attr(,"specials")
attr(,"specials")$Error
[1] 3
attr(,"order")
[1] 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
Bill Dunlap
TIBCO Spotfire
________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] on behalf
of pangdu at vt.edu [pangdu at vt.edu]
Sent: Tuesday, July 12, 2011 8:40 PM
To: r-help at r-project.org
Subject: [R] question on formula and terms.formula()
I'm trying to create a formula object to pass on to a function that
applies the function terms.formula() to it.
f <- function(formula, ...)
{
...
mf <- match.call()
term <- terms.formula(mf$formula)
...
}
However, my code below gives an error.
form <- as.formula("y~x")
f(form, ...)
The error message was:
Error in terms.formula(mf$formula): argument is not a valid model.
Could anybody help me figure out the problem and find a solution here?
______________________________________________
R-help at r-project.org mailing list
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.=
More information about the R-help
mailing list