You can look at the structure of an object using the str()
function. While looking in there you should see a few different places to extract the variables used to make your tree model, here is one example:
> library(tree)
>
> fit <- tree(Species ~., data=iris)
> attr(fit$terms,"term.labels")
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
EDIT: And since you specifically asked for the indices, you can just match()
those back the variable names in your dataset (although they may always be in order - I haven't used the tree
package before so I can't say).
> match(attr(fit$terms,"term.labels"),names(iris))
[1] 1 2 3 4
> names(iris)[match(attr(fit$terms,"term.labels"),names(iris))]
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
EDIT2:
You're right! Try this:
> summary(fit)$used
[1] Petal.Length Petal.Width Sepal.Length
Levels: <leaf> Sepal.Length Sepal.Width Petal.Length Petal.Width
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…