我是靠谱客的博主 文艺大雁,最近开发中收集的这篇文章主要介绍代码chaid_R中Chaid回归树到表的转换,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I used the CHAID package from this link ..It gives me a chaid object which can be plotted..I want a decision table with each decision rule in a column instead of a decision tree. .But i dont understand how to access nodes and paths in this chaid object..Kindly help me..

I followed the procedure given in this link

i cant post my data here since it is too long.So i am posting a code which takes the sample dataset provided with chaid to perform the task.

copied from help manual of chaid:

library("CHAID")

### fit tree to subsample

set.seed(290875)

USvoteS

ctrl

chaidUS

print(chaidUS)

plot(chaidUS)

Output:

Model formula:

vote3 ~ gender + ager + empstat + educr + marstat

Fitted party:

[1] root

| [2] marstat in married

| | [3] educr HS: Gore (n = 311, err = 49.5%)

| | [4] educr in College, Post Coll: Bush (n = 249, err = 35.3%)

| [5] marstat in widowed, divorced, never married

| | [6] gender in male: Gore (n = 159, err = 47.8%)

| | [7] gender in female

| | | [8] ager in 18-24, 25-34, 35-44, 45-54: Gore (n = 127, err = 22.0%)

| | | [9] ager in 55-64, 65+: Gore (n = 115, err = 40.9%)

Number of inner nodes: 4

Number of terminal nodes: 5

So my question is how to get this tree data in a decision table with each decision rule(branch/path) in a column..I dont understand how to access different tree paths from this chaid object..

解决方案

CHAID package uses partykit (recursive partitioning) tree structures. You can walk the tree by using party nodes - a node can be terminal or have a list of nodes with information about decision rule (split) and fitted data.

The code below walks the tree and creates the decision table. It is written for demonstration purposes and tested only on one sample tree.

tree2table

df_list

var_names

var_levels

walk_the_tree

# depth-first walk on partynode structure (recursive function)

# decision rules are extracted for every branch

if(missing(rule_branch)) {

rule_branch

rule_branch

rule_branch

}

if(is.terminal(node)) {

rule_branch[["nodeId"]]

rule_branch[["predict"]]

df_list[[as.character(node$id)]] <

} else {

for(i in 1:length(node)) {

rule_branch1

val1

rule_branch1[[names(val1)[1]]]

walk_the_tree(node[i], rule_branch1)

}

}

}

decision_rule

# returns split decision rule in data.frame with variable name an values

var_name

values_vec

values_txt

return( setNames(values_txt, var_name))

}

# compile data frame list

walk_the_tree(party_tree$node)

# merge all dataframes

res_table

return(res_table)

}

call function with the CHAID tree object:

table1

the result should be something like this:

gender ager empstat educr marstat nodeId predict

-------- -------------------------- --------- ------------------ -------------------------------- -------- ---------

NA NA NA HS married 3 Gore

NA NA NA College, Post Coll married 4 Bush

male NA NA NA widowed, divorced, never married 6 Gore

female 18-24, 25-34, 35-44, 45-54 NA NA widowed, divorced, never married 8 Gore

female 55-64, 65+ NA NA widowed, divorced, never married 9 Gore

最后

以上就是文艺大雁为你收集整理的代码chaid_R中Chaid回归树到表的转换的全部内容,希望文章能够帮你解决代码chaid_R中Chaid回归树到表的转换所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部