Reputation: 2672
I get Error: $ operator not defined for this S4 class
when I try to run a ctree
from the party package
, but only when the formula is writen as a string that I transform using as.formula()
.
Below the example :
#This works fine :
y <- ctree(formula = quotation ~ minute + temp, data=test[[1]], controls = ctree_control(mincriterion = 0.99))
#While this doesn't :
x <- "ctree(formula = quotation ~ minute + temp, data=test[[1]], controls = ctree_control(mincriterion = 0.99))"
y <- as.formula(x)
Error: $ operator not defined for this S4 class
My ultimate purpose is to create a function that iterates through the list test
to create multiple trees.
Any idea ?
Upvotes: 2
Views: 1578
Reputation: 37879
ctree
is a function and not a formula. formula
is the class of the object resulting from the function '~'
(tilde). You can learn more about formulas from help('~')
and help('formula')
.
The most common way to use as.formula
is to convert a string that represents the formula syntax to an object of class formula. Something like as.formula('y ~ x')
. Also, check class(as.formula(y~x))
.
In your case you saved a string representing function ctree
to variable x. Function ctree
only contains a string representing a formula syntax (quotation ~ minute + temp
) but it cannot be coerced to formula (it does not represent a formula, it just contains a formula syntax string) because it does not follow the formula syntax.
If you want to execute a function from text you need to use eval(parse(text = x))
although this technique is not encouraged..
Upvotes: 1