Asq*_*qan 1 r machine-learning decision-tree
如何才能知道构造树中实际使用了哪些变量?
model = tree(status~., set.train)
Run Code Online (Sandbox Code Playgroud)
如果我写,我可以看到变量:
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
Run Code Online (Sandbox Code Playgroud)
但是我如何进入矢量,实际使用哪些变量的索引?
您可以使用该str()函数查看对象的结构.在那里查看时,您应该看到几个不同的位置来提取用于创建树模型的变量,这是一个示例:
> library(tree)
>
> fit <- tree(Species ~., data=iris)
> attr(fit$terms,"term.labels")
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
Run Code Online (Sandbox Code Playgroud)
编辑:因为你特别要求索引,你可以只是match()那些支持你的数据集中的变量名称(虽然它们可能总是按顺序 - 我之前没有使用过tree包,所以我不能说).
> 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"
Run Code Online (Sandbox Code Playgroud)
EDIT2:
你是对的!试试这个:
> summary(fit)$used
[1] Petal.Length Petal.Width Sepal.Length
Levels: <leaf> Sepal.Length Sepal.Width Petal.Length Petal.Width
Run Code Online (Sandbox Code Playgroud)