2

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 ?

Yohan Obadia
  • 2,552
  • 2
  • 24
  • 31
  • 1
    The `as.formula` only applies to the formula part. So it should be `x <- as.formula(quotation ~ minute + temp)`; and the line after `y <- ctree(formula=x, ...)`. For example if I use iris data set, it would be: `x1 <- as.formula(Species ~ Petal.Length + Petal.Width);y1 <- ctree(formula = x1, data=iris)` – chappers Sep 10 '15 at 13:18
  • Thanks for your answer, but how is `ctree` considered if not as a formula ? I need to iterate outside of the scope of what you described as x. – Yohan Obadia Sep 10 '15 at 13:43
  • I would recommend you check out the `caret` package. It has `train` function with `tuneGrid` argument which sounds like what you're after. – chappers Sep 10 '15 at 21:46

1 Answers1

1

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..

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • This works great thanks ! May I ask why it is not recommended ? – Yohan Obadia Sep 10 '15 at 15:12
  • 1
    @YohanObadia You are very welcome. Glad I could be of help. Sure, the three main reasons are: 1. It is much slower than running the actual command, 2. It makes it much harder to read the code, and 3. It is a common source of errors when you use it in a package as in many occasions people might misspell the string. There is a discussion about it [here](http://stackoverflow.com/questions/13649979/what-specifically-are-the-dangers-of-evalparse) if you want to have a look. Personally, I agree more with the second most upvoted answer. – LyzandeR Sep 10 '15 at 15:22
  • The link was interesting. The lack of R knowledge is something I am currently facing, and beside of eval(parse()) I don't see any way to do what I want for now. – Yohan Obadia Sep 10 '15 at 15:53