Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can i get know which variables are actually used in a constructed tree?

model = tree(status~., set.train)

I can see the variables if i write:

summary(model)

tree(formula = status ~ ., data = set.train)
Variables actually used in tree construction:
[1] "spread1"      "MDVP.Fhi.Hz." "DFA"          "D2"           "RPDE"                "MDVP.Shimmer" "Shimmer.APQ5"
Number of terminal nodes:  8 
Residual mean deviance:  0.04225 = 5.831 / 138 
Distribution of residuals:
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-0.9167  0.0000  0.0000  0.0000  0.0000  0.6667 

BUT how can i get in a vector, the indices of which variables are actually used in?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
606 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...