Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

plot graphical models from PROC HPBNET

Accepted Solution Solved
Reply
Occasional Contributor A_S
Occasional Contributor
Posts: 12
Accepted Solution

plot graphical models from PROC HPBNET

Dear SAS Communities,

 

How do you plot graphical models from PROC HPBNET? Is there another graphical model proceedure that plots the model?

 

Thank you!

acs

 

 


Accepted Solutions
Solution
‎03-28-2017 01:34 PM
SAS Super FREQ
Posts: 269

Re: plot graphical models from PROC HPBNET

[ Edited ]

If you use the HP BN Classifier node in Enterprise Miner, you will automatically get a graphical representation of the results.

 

Or in SAS code you can use the following code to use the SAS Graph Template Language for plotting the network  -- this code will be included in the online proceedings for SAS Global Forum 2017 for this presentation on the HPBNET procedure: https://event.crowdcompass.com/sgf2017-sessions/activity/kKxLYK6nMm (Building Bayesian Network Classifiers Using the HPBNET Procedure).  Hope it helps!

 

 

/*
After running PROC HPBNET, to help visualize the final model, you can view the selected Bayesian 
network structure by using the following macro createBNCdiagram, which takes the target variable
and the output network data as arguments. */ %macro createBNCdiagram(target=Class, outnetwork=net); data outstruct; set &outnetwork; if strip(upcase(_TYPE_)) eq 'STRUCTURE' then output; keep _nodeid_ _childnode_ _parentnode_; run; data networklink; set outstruct; linkid = _N_; label linkid ="Link ID"; run; proc sql; create table work._node1 as select distinct _CHILDNODE_ as node from networklink; create table work._node2 as select distinct _PARENTNODE_ as node from networklink; quit; proc sql; create table work._node as select node from work._node1 UNION select node from work._node2; quit; data bnc_networknode; length NodeType $32.; set work._node; if strip(upcase(node)) eq strip(upcase("&target")) then do; NodeType = "TARGET"; NodeColor=2; end; else do; NodeType = "INPUT"; NodeColor = 1; end; label NodeType ="Node Type" ; label NodeColor ="Node Color" ; run; data parents(rename=(_parentnode_ = _node_)) children(rename=(_childnode_ = _node_)) links; length _parentnode_ _childnode_ $ 32; set networklink; keep _parentnode_ _childnode_ ; run; *get list of all unique nodes; data nodes; set parents children; run; proc sort data=nodes; by _node_; run; data nodes; set nodes; by _node_; if first._node_; _Parentnode_ = _node_; _childnode_ = ""; run; /*merge node color and type */ data nodes; merge nodes bnc_ networknode (rename=(node=_node_ nodeColor=_nodeColor_ nodeType=_nodeType_)); by _node_; run; /*sort color values to ensure a consistent color mapping across networks */ /*note that the color mapping is HTML style dependent though */ proc sort data=nodes; by _nodeType_; run; *combine nodes and links; * need outsummaryall for model report; data bnc_networksummary(drop=_shape_ _nodecolor_ _nodepriority_ _shape_ _nodeID_ _nodetype_ _linkdirection_) bnc_networksummaryall; length _parentnode_ _childnode_ $ 32; set nodes links; drop _node_; if _childnode_ EQ "" thendo; _nodeID_ = _parentnode_; _nodepriority_ = 1; _shape_= "OVAL"; end; else do; _linkdirection_ = "TO"; output bnc_networksummary; end; output bnc_networksummaryall; label _linkdirection_="Link Direction"; run; proc datasets lib=work nolist nowarn; delete _node _node1 _node2 nodes links parents children; run; quit; proc template; define statgraph bpath; begingraph / DesignHeight=720 DesignWidth=720; entrytitle "Bayesian Network Diagram"; layout region; pathdiagram fromid=_parentnode_ toid=_childnode_ / arrangement=GRIP nodeid=_nodeid_ nodetitle=_nodeID_ nodeshape=_shape_ nodepriority=_nodepriority_ linkdirection=_linkdirection_ nodeColorGroup=_NodeColor_ textSizeMin = 10 ; endlayout; endgraph; end; run; ods graphics; proc sgrender data=bnc_networksummaryall template=bpath; run; %mend; %createBNCdiagram;

 

 

View solution in original post


All Replies
Solution
‎03-28-2017 01:34 PM
SAS Super FREQ
Posts: 269

Re: plot graphical models from PROC HPBNET

[ Edited ]

If you use the HP BN Classifier node in Enterprise Miner, you will automatically get a graphical representation of the results.

 

Or in SAS code you can use the following code to use the SAS Graph Template Language for plotting the network  -- this code will be included in the online proceedings for SAS Global Forum 2017 for this presentation on the HPBNET procedure: https://event.crowdcompass.com/sgf2017-sessions/activity/kKxLYK6nMm (Building Bayesian Network Classifiers Using the HPBNET Procedure).  Hope it helps!

 

 

/*
After running PROC HPBNET, to help visualize the final model, you can view the selected Bayesian 
network structure by using the following macro createBNCdiagram, which takes the target variable
and the output network data as arguments. */ %macro createBNCdiagram(target=Class, outnetwork=net); data outstruct; set &outnetwork; if strip(upcase(_TYPE_)) eq 'STRUCTURE' then output; keep _nodeid_ _childnode_ _parentnode_; run; data networklink; set outstruct; linkid = _N_; label linkid ="Link ID"; run; proc sql; create table work._node1 as select distinct _CHILDNODE_ as node from networklink; create table work._node2 as select distinct _PARENTNODE_ as node from networklink; quit; proc sql; create table work._node as select node from work._node1 UNION select node from work._node2; quit; data bnc_networknode; length NodeType $32.; set work._node; if strip(upcase(node)) eq strip(upcase("&target")) then do; NodeType = "TARGET"; NodeColor=2; end; else do; NodeType = "INPUT"; NodeColor = 1; end; label NodeType ="Node Type" ; label NodeColor ="Node Color" ; run; data parents(rename=(_parentnode_ = _node_)) children(rename=(_childnode_ = _node_)) links; length _parentnode_ _childnode_ $ 32; set networklink; keep _parentnode_ _childnode_ ; run; *get list of all unique nodes; data nodes; set parents children; run; proc sort data=nodes; by _node_; run; data nodes; set nodes; by _node_; if first._node_; _Parentnode_ = _node_; _childnode_ = ""; run; /*merge node color and type */ data nodes; merge nodes bnc_ networknode (rename=(node=_node_ nodeColor=_nodeColor_ nodeType=_nodeType_)); by _node_; run; /*sort color values to ensure a consistent color mapping across networks */ /*note that the color mapping is HTML style dependent though */ proc sort data=nodes; by _nodeType_; run; *combine nodes and links; * need outsummaryall for model report; data bnc_networksummary(drop=_shape_ _nodecolor_ _nodepriority_ _shape_ _nodeID_ _nodetype_ _linkdirection_) bnc_networksummaryall; length _parentnode_ _childnode_ $ 32; set nodes links; drop _node_; if _childnode_ EQ "" thendo; _nodeID_ = _parentnode_; _nodepriority_ = 1; _shape_= "OVAL"; end; else do; _linkdirection_ = "TO"; output bnc_networksummary; end; output bnc_networksummaryall; label _linkdirection_="Link Direction"; run; proc datasets lib=work nolist nowarn; delete _node _node1 _node2 nodes links parents children; run; quit; proc template; define statgraph bpath; begingraph / DesignHeight=720 DesignWidth=720; entrytitle "Bayesian Network Diagram"; layout region; pathdiagram fromid=_parentnode_ toid=_childnode_ / arrangement=GRIP nodeid=_nodeid_ nodetitle=_nodeID_ nodeshape=_shape_ nodepriority=_nodepriority_ linkdirection=_linkdirection_ nodeColorGroup=_NodeColor_ textSizeMin = 10 ; endlayout; endgraph; end; run; ods graphics; proc sgrender data=bnc_networksummaryall template=bpath; run; %mend; %createBNCdiagram;

 

 

Occasional Contributor A_S
Occasional Contributor
Posts: 12

Re: plot graphical models from PROC HPBNET

Thank you!

Where can I find other options for the arrangement statement in the template proceedure?
Senior User
Posts: 1

Re: plot graphical models from PROC HPBNET

could you favorably help me? I've tried to use the abovementioned SAS code for my bnet's visualization too - thank you very much! This is incredibly powerful instrument which I would like to upgrade and customize, but when I've started to learn "PATHDIAGRAM" block and it's options I've figured out that there is no information even on the official sites and blogs. All information I've found referred to the "PATHDIAGRAM" in PROC CALIS and PROC FACTOR, which has another structure. Could you share guides or topics you've read before for my more deep diving in programming and visualisation bnets?

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 354 views
  • 2 likes
  • 3 in conversation