Upload
doanmien
View
235
Download
4
Embed Size (px)
Citation preview
ODS STATISTICAL GRAPHICSIN
SAS® 9.2 TS2M3
Handout
Edmonton SAS User Group
Oct 27, 2010
John Kirtz
Snowy Owl Software
® SAS is the registered trademark of SAS Institute Inc, North Carolina, USA
STATISTICAL GRAPHICS BASICS
Roadmap
SAS Help
SAS/STAT 9.22 : Users Guide : Statistical Graphics Using ODS
Papers and SUGI presentations
Creating Statistical Graphics in SAS 9.2 What Every Statistical User Should Know
Robert N. Rodriguez and Tonya E. Balan SAS Institute Inc.Cary, NC, USA
Books
Statistical Graphics in SAS
Warren F. Kuhfeld
STATISTICAL GRAPHICS BASICSOverview
What are ODS Graphics
New template based graphics system
New graphic template language
SAS putting dollars into this
Automatic - Request Based Graphics
Analytic procedures
Syntactic Tools
SAS/Graph “SG” Procedures
Graphics Template Language
Interactive Tools
ODS Graphic Editor
ODS Graphic Designer
Combination of Tools
Efficient and powerful
STATISTICAL GRAPHICS BASICSGeneral Usage Considerations
Recommended Styles for ODS Graphics
Color Default : gray background optimized for HTML
Statistical: white background
Listing: optimized for color on white paper
Printer : optimized for PS and PDF
RTF : optimized for Word / PowerPoint
Gray Scale
Journal- Fills are gray scale
Black and White
Journal2 : Fills are white
Recommended Options for Export of Graphics
Microsoft Office : RTF , HTML (ODS Destinations)
Latex : Imagefmt=PS ( ODS Graphics statement)
ODS Graphics On Options
ANTIALIAS ANTIALIASMAX BORDER DISCRETEMAX GROUPMAX
HEIGHT/WIDTH IMAGEFMT IMAGEMAP IMAGENAME LABELMAX
MAXLEGENDAREA PANELCELLMAX RESET SCALE TIPMAX
ANALYTIC PROCEDURES
Roadmap
SAS Help
SAS/STAT 9.22 : Users Guide : Statistical Graphics Using ODS
SAS/STAT 9.22 : Users Guide : Procedure Documentation
Papers and SUGI presentations
Creating Statistical Graphics in SAS 9.2 What Every Statistical User Should Know
Modifying ODS Statistical Graphics Templates in SAS 9.2
ANALYTIC PROCEDURESPartial Listing
Base SAS
• CORR• FREQ• UNIVARIATE
SAS/ETS
• ARIMA• AUTOREG• ENTROPY• EXPAND• MODEL• SYSLIN
• TIMESERIES• TSCSREG• UCM• VARMAX• X12
SAS High-Performance Forecasting
• HPF• HPFENGINE
SAS/QC (planned)
• ANOM• CAPABILITY• CUSUM• MACONTROL• PARETO
• RELIABILITY• SHEWHART
SAS/STAT
• ANOVA• BOXPLOT• CALIS• CLUSTER• CORRESP• FACTOR
• GAM• GENMOD• GLM• GLIMMIX• GLMSELECT• KDE
• LIFEREG• LOESS• LOGISTIC• MDS• MI• MIXED
• MULTTEST• NPAR1WAY• PHREG• PLS• PRINQUAL
• QUANTREG• REG• ROBUSTREG• RSREG• TRANSREG
• TTEST
Analytic ProceduresProcess
Analytic
Proc
ODS Graphics
On “options
Ods Dest.
Options
Template
Macro Vars
Active Template
Store
ODS Path
Graphic
Image
Editable
Graphic
Graphic
Editor
Graphic
Image
Proc
Template
ModStyle
Macro
Modified
Style
Defined
Style
Active
Style
Active
Template
•Basic Usage Customized Style Sheet Customized Graphic Template
ODS-SG Plot
Options
ANALYTIC PROCEDURESProc Reg Diagnostics Panel
SAS code
ODS HTML;
ODS GRAPHICS ON;
proc reg data=regData outest=eqnModel
rsquare plots=(diagnostics) **;
MODEL WHEAT_PROTEIN = wl408 wl416 wl424 …
/ SELECTION=Stepwise p r ADJRSQ … ;
run;
quit;
ODS GRAPHICS OFF;
ODS HTML close;
** can be much more complex e.g.
plots=( scree(unpack) loadings(plotref) preloadings(flip) )
SAS/GRAPH “SG”
PROCEDURES
Roadmap
SAS Help
SAS/GRAPH 9.2 : Statistical Graphics Procedures Guide
Papers and SUGI presentations
Using PROC SGPLOT for Quick High-Quality Graphs
Lora D. Delwiche, University of California, Davis, CA
Susan J. Slaughter, Avocet Solutions, Davis, CA
A Visual Revolution – Statistical Graphics in SAS 9.2
Bob Newman, Amadeus Software Limited, Oxfordshire, United Kingdom
Effective Graphics Made Simple Using SAS/GRAPH® SG Procedures
Secrets of the SG Procedures
Dan Heath, SAS Institute Inc., Cary, NC
SAS/Graph “SG” ProceduresBasic Process
„SG”
Proc
ODS Graphics
On options
Ods Dest.
Options
Graphic
Image
Editable
Graphic
Graphic
Editor
Graphic
Image
Proc
Template
ModStyle
Macro
Modified
Style
Defined
Style
Active
Style
•Basic Usage Customized Style Sheet Customized Graphic Template
TMPLOUT
option
Graph
Template
Proc
SGRender
SAS/GRAPH PROCEDURESOverview
“SG” Procedures
family of new procedures
general-purpose visual data analysis
complement specialized analytic proc graphics
clear and concise syntax.
compact code ( not much typing )
Capabilities always expanding
(new in SAS 9.2 Phase 1)
complete page of enhancements
(new in SAS 9.2 Phase 2)
vector plot type for the SGPLOT and SGPANEL
new panel layout types for the SGPANEL
more options are available for PANELBY statement
SGScatter has enhanced support for transparency
…
SAS/GRAPH PROCEDURESProc SgPlot – Basic Graph
proc sgplot data=sashelp.class;
scatter x=height y=weight;
ellipse x=height y=weight;
run;
proc sgplot data=sashelp.class;
histogram height;
density height;
density height / type=kernel;
run;
SAS/GRAPH 9.2 : Statistical Graphics Procedures Guide
Basic plots: scatter, series, step, band, needle, vector
Fit and confidence: loess, regression, B-spline curves, ellipses
Distribution; box , histograms, normal /kernel density estimates
Categorization : dot, bar charts, line charts
SAS/GRAPH PROCEDURESProc SgPanel – Basic Graphs
SAS/GRAPH 9.2 : Statistical Graphics Procedures Guide
SAS/GRAPH PROCEDURESProc SgScatter – Sample Graphs
proc sgscatter data=sashelp.iris
(where=(species eq "Virginica"));
matrix petallength petalwidth sepallength
/ ellipse=(type=mean)
diagonal=(histogram kernel);
run;
proc sgscatter data=sashelp.cars;
compare
y=mpg_highway
x=(weight enginesize horsepower )
/ group=type;
run;
SAS/GRAPH 9.2 : Statistical Graphics Procedures Guide
ODS GRAPHIC EDITOR
Roadmap
SAS Help
SAS/GRAPH : Windows : ODS Graphic Editor
Papers and SUGI presentations
ODS Graphics Editor
Sanjay Matange, SAS Institute, Cary, NC
ODS Graphic EditorEditing Statistical Graphic Output
ODS Graphics Editor
operates only on “SGE” Graphic files
created only in Listing destination
save changes to SGE file and modify at any time
independent of SAS
save to PNG file
Create SGE Graphic
ODS LISTING SGE=ON; * legacy SGEDIT ON command still work;
ODS GRAPHICS ON;
Proc REG data = regData OUTEST=Model rsquare plots=(diagnostics);
ID scancode;
MODEL WHEAT_PROTEIN = wl408 wl416 wl424 wl432 wl440 wl464 …
/ SELECTION=Stepwise p r ADJRSQ RMSE SSE PRESS RSQUARE
OUTSEB;
run;
quit;
ODS GRAPHICS OFF;
ODS LISTING SGE=OFF; * legacy SGEDIT OFF command still work;
ODS Graphic EditorProc Reg Diagnostic Panel: Original vs. Edit
TEMPLATES
AND
GRAPHICS TEMPLATE LANGUAGE
Roadmap
SAS Help
SAS/GRAPH 9.2 : Graphical Template Language User Guide
SAS/GRAPH 9.2 : Graphical Template Language Reference
Papers and SUGI presentations
Introduction to the Graph Template Language
Sanjay Matange, SAS Institute Inc., Cary, NC
Modifying ODS Statistical Graphics Templates in SAS 9.2
Warren F. Kuhfeld, SAS Institute Inc., Cary, NC
Working with TemplatesCreate Your Template
Graph
Template
Write
GTL
SG
Proc
Designer
Help /
Papers
TMPLOUT
option
Analytic
Proc
Export
Template
Copy or
download
ODS Trace
name
Proc Template
Source stmt
Graph
Template
Modify
GTL
Modtmplt
Macro
Store Template
ODS Path
User Catalog 1
...
User catalog n
Sasuser.Templat
SasHelp.Templat
Working with TemplatesUsing Your Template
Analytic
Proc
ODS Graphics
On options
ProcSGRender
Ods Dest.
Options
Template
Macro Vars
Template
Name*
ODS Path
Graphic
Image
Editable
Graphic
Graphic
Editor
Graphic
Image
Proc
Template
ModStyle
Macro
Modified
Style
Defined
Style
Active
Style
Active
Template
* Name is fixed for Analytic Procs, identified by using ODS trace option
Working with TemplatesWhy Create or Modify Graphic Templates
Analytic Proc Graphics
enhance default graphic cosmetics
axis labels, fonts, fills, etc (Graphics Editor)
add/change actual graphics produced
may be missing useful diagnostic or overlay, etc
extract and generalize a useful graphic
SG Procs
enhance graphic produced by procs
source of GTL to use in manually editted templates
ODS Graphics Designer
enhance graphic produced by GUI
generalize graphic (use with other data and in programs)
Create from Scratch
Create complex graphics not otherwise possible
Very efficient if you are GTL expert
Working with TemplatesComplex Graphics Possible with GTL
From Stat.Lifereg.Graphics.AutoCorrPanelneedleplot
x=eval (LAGS(_YVAR1,MAX=MAXLAGS))
y=eval ( ACF(_YVAR1, NLAGS=MAXLAGS))
/ tiplabel=(x="Lag" y=Autocorrelation") lineattrs=(thickness=2);
From stat.reg.Graphics.Diagnosticspanelscatterplot
y=eval (SORT(DROPMISSING(RESIDUAL)))
x=eval (PROBIT((NUMERATE(SORT(DROPMISSING(RESIDUAL))) -0.375)/(0.25 + N(RESIDUAL))))
/ markerattrs=GRAPHDATADEFAULT primary=true
rolename=(s=eval ( SORT(DROPMISSING(RESIDUAL)))
nq=eval (PROBIT((NUMERATE(SORT(DROPMISSING(RESIDUAL))) -0.375)/(0.25 + N(RESIDUAL)))))
tiplabel=(nq="Quantile" s="Residual") tip=(nq s);
From Stat.Sim2d.Graphics.MeansEqtif (_FILL)
if (_FILL = 1)
contourplotparm x=GXC y=GYC z=MEAN / colormodel=THREECOLORRAMP
contourtype=gradient primary=true name='MeansF';
else
if (_FILL = 2)
contourplotparm x=GXC y=GYC z=STDDEVIATION / colormodel=
TWOCOLORRAMP contourtype=gradient primary=true name='StdDevF';
endif;
endif;
endif;
Working with TemplatesGTL and Styles
ContourPlotParm x=DENSITYX y=DENSITYY z=DENSITY / COUNTOUTTYPE=LabeledLineGradient nhint=7
COLORMODEL=ThreeColorRamp Lineattrs=GraphDataDefault LINELABELATTRS=GraphValueText ;
Default Style
Color Gradient
class ThreeColorRamp /
endcolor = GraphColors('gramp3cend')
neutralcolor = GraphColors('gramp3cneutral')
startcolor = GraphColors('gramp3cstart');
class ThreeColorAltRamp /
endcolor = GraphColors('gconramp3cend')
neutralcolor = GraphColors('gconramp3cneutral')
startcolor = GraphColors('gconramp3cstart');
Line Labels
class GraphValueText /
font = GraphFonts('GraphValueFont')
color = GraphColors('gtext');
class GraphLabelText /
font = GraphFonts('GraphLabelFont')
color = GraphColors('glabel');
Line Attributes
class GraphDataDefault /
… linestyle = 1
….;
class GraphData4 /
… linestyle = 4
…;
Working with TemplatesUsing Proc Template and Proc SGRender
Create Template
only syntax checking
syntaxPROC TEMPLATE;
DEFINE STATGRAPH gName </ STORE=libref.catalog>;
DYNAMIC var1 .. varN; * link to runtime variables;
MVAR mVar1 .. mVarN; * character macro variables;
NMVAR nmVar1 .. nmVarN; * numeric runtime variables;
NOTES 'text';
GTL statements
END;
RUN;
Use Template
Execute Proc SGRender ( data _null_ still supported)
supply dataset
basic Syntax%let mVar1=ABC;* define character macro variable(s);
%let nmVar1=123; * define numeric macro variable(s);
PROC SGRENDER DATA=myData Template=gName;
Dynamic var1=“height” var2=“weight”;
RUN;
Working with TemplatesGTL Overview
Graphics Template Language
Comprehensive
allows for many combinations of basic plots
allows for many arrangements(layouts)
large number of options for everything
Smart
has good defaults for unspecified options
label collision algorithms
policies for axis text /scale
aware of scaling e.g. label vs. short label
auto placement of legends and text
built-in computational capabilities
built-in conditional rendering logic
Types of Statements
Conditional : what graph features are rendered
Layout : hierarchical arrangement of graph features
Plot : request specific plots (Overlay)
Axis : specify axis
Text : titles, footnotes , notes
Legend : request and arrange legends
EXAMPLE GRAPHICS
FROM
REFERENCED PAPERS
APPENDIX A
Analytic ProceduresExamples
Modifying ODS Statistical Graphics Templates in SAS® 9.2: Warren F. Kuhfeld, SAS Institute Inc., Cary, NC
SAS/GRAPH PROCEDURESProc SGScatter – Scatter Plot with Regression
A Visual Revolution – Statistical Graphics in SAS 9.2 : Bob Newman, Amadeus Software Limited, Oxfordshire, United Kingdom
SAS Code
title
'Proc SGSCATTER using MATRIX statement';
proc sgscatter data=bp2;
matrix supsys supdia stdsys stddia
/ group=trtcd ellipse=(alpha=0.10)
diagonal=(histogram normal);
run;
SAS/GRAPH PROCEDURESProc SGPlot – Survival Plot with Two Legends
A Visual Revolution – Statistical Graphics in SAS 9.2 : Bob Newman, Amadeus Software Limited, Oxfordshire, United Kingdom
SAS Code
proc sgplot data=lungsurv2;
band x=survtime lower=sdf_lcl upper=sdf_ucl /
modelname="step“ group=cell transparency=0.4
name="band";
step x=survtime y=survival / group=cell name="step";
Keylegend "step" /
title="Survival Estimate"
position=right;
Keylegend "band" / title=" Confidence Band "
position=right ;
run;
SAS/GRAPH PROCEDURESProc SGPlot – High-Low-Close Plot
Secrets of the SG Procedures : Dan Heath, SAS Institute Inc., Cary, N
proc sgplot data=moveavg;
yaxis grid label="Stock Value";
band x=date upper=bolupper lower=bollower
/ transparency=0.5 legendlabel="Bollinger Bands(25,2)" name="boll";
vector x=date y=high / yorigin=low xorigin=date noarrowheads;
scatter x=date y=close / markerattrs=(symbol=plus size=3);
series x=date y=avg25 / lineattrs=GraphFit legendlabel="SMA(25)" name="d25";
series x=date y=avg50 / lineattrs=GraphFit2(pattern=solid) legendlabel="SMA(50) name="d50";
keylegend "boll" "d25" "d50" / across=4 noborder position=TopRight location=inside;
Run;
SAS/Graph ODS Graphics DesignerExample 1
Using the ODS Graphics Designer to Create Your Own Templates : Philip R Holland, Holland Numerics Limited, Royston, Herts, UK
SAS/Graph ODS Graphics DesignerExamples
ODS Graphics Designer - An Interactive Tool for Creating Batchable Graphs : Sanjay Matange, SAS Institute Inc., Cary, NC
Working with TemplatesManual GTL Example
Introduction to the Graph Template Language: Sanjay Matange, SAS Institute, Cary, NC
Working with TemplatesManual GTL Example
begingraph;
entrytitle "Stock Chart";
layout lattice / columns=1 columndatarange=union rowweights=(0.7 0.3);
columnaxes;
columnaxis / offsetmin=0.02 griddisplay=on;
endcolumnaxes;
layout overlay / yaxisopts=(griddisplay=on label=" " display=(line) displaysecondary=all)cycleattrs=true;
bandplot x=date limitupper=bolupper limitlower=bollower
/ datatransparency=0.5 name="boll" outlineattrs=(pattern=solid)
fillattrs=graphconfidence display=(fill outline) legendlabel="Bollinger Bands(25,2)";
vectorplot xorigin=date y=low yorigin=high x=date
/ arrowheads=false group=cindex index=cindex lineattrs=(pattern=solid thickness=1px) shaftprotected=true;
scatterplot x=date y=close / group=cindex index=cindex markerattrs=(symbol=plus size=2);
scatterplot x=date y=close / freq=freq markerattrs=(symbol=starfilled size=7) datalabel=split name="split" legendlabel="Split";
seriesplot x=date y=avg25 / lineattrs=(thickness=1px) legendlabel="SMA(25)" name="d25";
seriesplot x=date y=avg50 / lineattrs=(pattern=solid thickness=1px) legendlabel="SMA(50)“ name="d50";
discretelegend "boll" "d25" "d50" "split" / across=1 border=on valign=top halign=left location=inside;
endlayout;
layout overlay / cycleattrs=true xaxisopts=(griddisplay=on) yaxisopts=(griddisplay=on display=(line) displaysecondary=all);
needleplot x=date y=volume / name="vol" legendlabel="Volume” lineattrs=(pattern=solid thickness=1px);
seriesplot x=date y=vavg / name="vavg" legendlabel="SMA(25)” lineattrs=(pattern=solid thickness=1px);
discretelegend "vol" "vavg" / location=inside border=off halign=left valign=top;
endlayout;
endlayout;
endgraph;
Introduction to the Graph Template Language: Sanjay Matange, SAS Institute, Cary, NC
SAS 9.2 AND ODS STATISTICAL GRAPHICS
John Kirtz
Snowy Owl Software
780-998-7455
HAVE FUN !