我借鉴的代码来自:http://www.sasanalysis.com/2011/02/visualize-decision-tree-by-coding-proc.html,作者是位资深的SAS用户,非常活跃,是分析方面的专家。
[code:27ws7y78]******** VISUALIZE DECISION TREE RESULTS************;
proc sql;
create table treedata as
select a.parent as act1, a.node, b.NODETEXT, b.U_Target
from nodstat1 as a, nodstat1 as b
where a.parent=b.node
union
select c.node as act1, . as node, c.nodetext, c.U_Target
from nodstat1 as c
;
quit;
data treedata1;
set treedata;
if U_Target=1 then _pattern=1;
else _pattern=2;
nodetext = compress(nodetext,'0920'x);
nodetext = tranwrd(nodetext,':',': ');
run;
*NOTE: USE PROC NETDRAW TO REALIZE PHYSICAL TREE*;
pattern1 c=green; pattern2 v=s c=red;
footnote c=green '1 ' c=red '0 ';
proc netdraw data=treedata1 graphics;
actnet /activity=act1 successor=NODE nolabel id=(NODETEXT)
tree compress rotate rotatetext arrowhead=0
font=simplex ctext=white htext=2;
run;
footnote ' ';[/code:27ws7y78]
最后,我们使用 SAS/GRAPH 提供的 Annotate 功能来画决策边界,这里的决策边界是从决策树的结果表中人工提取的,我没有花时间研究如何通过代码自动生成。
[code:27ws7y78]data rules1;
set rul1;
where role='PRIMARY' and (stat='VARIABLE' or stat='INTERVAL');
run;
data decbounderies;
set rules1;
name=lag(character_value);
value=numeric_value;
if stat='INTERVAL' then output;
keep node name value;
run;
data Lines;
infile datalines dlm='#';
length label $ 27
textcolor $ 9
linecolor $ 9;
retain x1space 'datavalue';
retain y1space 'datavalue';
retain x2space 'datavalue';
retain y2space 'datavalue';
input function $ x1 y1 label x2 y2 textcolor linecolor;
datalines;
line # 0.4215868129 # 0 # # 0.4215868129 # 1 # # blue
line # 0 # 0.7397564595 # # 0.4215868129 # 0.7397564595 # # blue
line # 0.1081274502 # 0.7397564595 # # 0.1081274502 # 1 # # blue
line # 0.4215868129 # 0.2146758047 # # 1 # 0.2146758047 # # blue
line # 0.9196328702 # 0 # # 0.9196328702 # 0.2146758047 # # blue
line # 0.4215868129 # 0.4578804769 # #1 # 0.4578804769 # # blue
line # 0.6309236754 # 0.2146758047 # # 0.6309236754 # 0.4578804769 # # blue
;
run;