BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_S
Fluorite | Level 6 A_S
Fluorite | Level 6

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
WendyCzika
SAS Employee

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

3 REPLIES 3
WendyCzika
SAS Employee

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;

 

 

A_S
Fluorite | Level 6 A_S
Fluorite | Level 6
Thank you!

Where can I find other options for the arrangement statement in the template proceedure?
Red_Fox
Calcite | Level 5

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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

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