1、SAS9.2 统计作图SAS92 后,针对常规统计作图,开发了以下三个过程:SGPLOT, SGPANEL, SGSCATTER, 针对特殊情况,用户也可以自定义统计图(TEMPLETE 过程) ,然后用过程 SGRENDER过程进行调用。在所给出的三个常用统计过程中,SGPLOT 的一些基本特性,在其他过程均相似,因此对该过程详述,其他的过程只要注意其特点就可以了。1 PROC SGPLOTSGPLOT 在同一作图单元上进行作图,可以画 16 种图形,分为五类:(一)basic X Y plots: scatter x=var y=var/options; -散点图series x=var
2、y=var/options; -序列图,事先要按x指定变量从小到大排序step x=var y=var/options; -阶梯图needle x=var y=var/options; -火柴棒图vector x=var y=var/options; -方向图(二)band plots: (带状图)band x=var upper=var lower=var/options; -带状图(变量值,最小值,最大值)(三)fit and confidence plots: (拟合与置信区间)reg x=var y=var /options; -回归线图loess x=var y=var/option
3、s; -局部线性回归图pbspline x=var y=var/options; -惩罚样条回归图ellipse x=var y=var/options; -椭园图注:以上三种类型图均可重叠画在一幅图上(四)distribution graphs for continuous DATA: (连续)hbox|vbox response-var/options; -水平| 垂直盒形图histogram response-var/options; - 直方图density response-var/options; -密度函数图注:直方图与密度函数图形可以画在一起;(五)distribution g
4、raphs for categorical DATA:(离散)hbar|vbar category-var/options; -水平| 垂直柱状图hline|vline category-var/options; -水平|垂直 线图(与上图表示相似,柱子用线表示)dot category-var/options; -点图表 1,可以重叠的作图语句,及其相关选项的关系表 2:SGPLOT 的作图语句及其常用选项表 3 图形辅助语句及其选项如参考线的设置,x,y 轴的设置,图形上插入文字等表 4:作图语句中关于填充,线的特征,数据点标签等的设置如填充的颜色,线的特征(线型,线的粗细,线的颜色) ,
5、数据标签(颜色,大小,符号)注:MARKERATTRS 的一些常用符号:LINEATTRS 的一些常用符号:例子:/*例 1*/proc sgplot data=sashelp.class;scatter x=height y=weight / group=sex;run;/*例 2*/proc sgplot data=sashelp.stocks(where=(date = “01jan2000“d and stock = “IBM“);title “Stock Trend“;*Create the series plots;series x=date y=close;series x=da
6、te y=low;series x=date y=high;run;/*例 3*/proc sgplot data=sashelp.class;reg x=height y=weight / CLM CLI;run;/*例 4*/proc sgplot data=sashelp.iris;title “Iris Petal Dimensions“;scatter x=petallength y=petalwidth;ellipse x=petallength y=petalwidth;keylegend / location=inside position=bottomright;run;/*
7、例 5*/proc sgplot data=sashelp.classfit;title “Fit and Confidence Band from Precomputed Data“;band x=height lower=lower upper=upper /legendlabel=“95% CLI“ name=“band1“;band x=height lower=lowermean upper=uppermean /fillattrs=GraphConfidence2legendlabel=“95% CLM“ name=“band2“;scatter x=height y=weight
8、;series x=height y=predict / lineattrs=GraphPredictionlegendlabel=“Predicted Fit“ name=“series“;keylegend “series“ “band1“ “band2“ / location=inside position=bottomright;run;/*例 6*/proc sgplot data=sashelp.class(where=(age= “01jan2000“dand date = “01jan2001“d and stock = “IBM“);title “Stock Volume v
9、s. Close“;vbar date / response=volume;vline date / response=close y2axis;run;2 PROC SGPANEL该过程可以分多个作图区域同时进行作图,其他的作图语句与 sgplot 一样。PROC SGPANEL;PANELBY category-var/options;-据此分类变量将作图区域分组;选项确定分组布局。作图语句(与 sgplot 过程相同,少一个 ellipse 作图语句) ;其他语句;run;PANELBY category-var/options;中的选项:columns=n -分 n 列layout=l
10、attice|panel|columnlattice|rowlatticerows=n -分 n 行spacing=n-定义分组间的距离(单位为 pixels)作图类型:Basic plotsscatter, series, step, band, needle plots, and vector plotsFit and confidence plotsloess, regression, and penalized B-spline curvesDistribution plotsbox plots, histograms, normal density curves, and kerne
11、l density estimatesCategorization plotsdot plots, bar charts, and line plots例子:/*example 1*/proc sgpanel data=sashelp.heart noautolegend;title “Cholesterol Distribution in Heart Study“;*Specify the classification variable for the panel.;panelby sex;histogram cholesterol;density cholesterol;run;/*exa
12、mple 2*/proc sgpanel data=sashelp.iris;title “Scatter plot for Fisher iris data“;panelby species / columns=3;reg x=sepallength y=sepalwidth / cli clm;run;/*example 3*/proc sgpanel data=sashelp.prdsale;title “Yearly Sales by Product“;panelby year / novarname columns=1;hbar product / response=actual;r
13、un;3 PROC SGSCATTER主要作散点图,也可加其他选项作其他类型的图。作图布局:三种方式来设置PLOT (变量组 1)*(变量组 2)/options;通过变量的交叉进行分组,分组各自有自己的独立坐标。COMPARE (变量组 1)*(变量组 2)/options;为比较而设置,x 轴,y 轴均可以相互比较;MATRIX 变量组/options; 矩阵方式给出散点图,对角线上可以画直方图,密度图等作图选项,则可以作以下类型的图形:Basic plotsscatter, series, step, band, needle plots, and vector plotsFit and
14、 confidence plotsloess, regression, and penalized B-spline curvesDistribution plotsbox plots, histograms, normal density curves, and kernel density estimatesCategorization plotsdot plots, bar charts, and line plots例子:例 1:用矩阵语句形成画图布局,并用 group 选项将数据分组proc sgscatter data=sashelp.iris;title “Scatterplot
15、 Matrix for Iris Data“;matrix sepallength petallength sepalwidth petalwidth/ group=species;run;例 2:用 PLOT 语句设置作图布局,用 pbspline 选项叠加罚样条回归proc sgscatter data=sashelp.iris(where=(species=“Virginica“);title “Multi-Celled Spline Curve for Species Virginica“;plot (sepallength sepalwidth)*(petallength petal
16、width)/ pbspline;run;例 3:用 compare 语句设置作图布局用于比较, group 选项将数据分组显示proc sgscatter data=sashelp.iris;title “Iris Data: Length and Width“;compare x=(sepallength petallength) y=(sepalwidth petalwidth)/ group=species;run;注:x,y 轴均可以进行比较。例 4:注意选项的设置proc sgscatter data=sashelp.iris(where=(species=“Versicolor“
17、);title “Versicolor Length and Width“;compare y=(sepalwidth petalwidth) x=(sepallength petallength)/ reg ellipse=(type=mean) spacing=4;run;4 图形文件的设置/*图形名,图形类型,图形大小 */ODS GRAPHICS ON / RESET IMAGENAME = Final IMAGEFMT =JPEG HEIGHT = 2in WIDTH = 3in; /*注意:RESET 重新设置图形格式变为缺省设置IMAGENAME设置图形文件名IMAGEFMT设置
18、图形文件的类型(PNG,BMP,GIF,JPEG,JPG,PDF,PS,TIFF等)HEIGHT,WIDTH-设置图形的长,高(单位为CM, IN, MM, PT, or PX.,缺省为640*480pixels*/*图形存放位置的设置,图形存放的风格*/ODS LISTING GPATH = E:dcai2007_lecturestat ;/*GPATH设置存放图形文件的位置*/*ODS LISTING STYLE=joural;注:通过STYLE=来设置不同风格,满足不同杂志的要求。* Histograms;PROC SGPLOT DATA = sashelp.class;HISTOGRA
19、M weight;TITLE “Mens weight“;RUN;ODS GRAPHICS OFF;/*作椭园图*/PROC SGPLOT DATA = sashelp.class;scatter x=height y=weight;ellipse x=height y=weight/alpha=0.3182;ellipse x=height y=weight/alpha=0.05;ellipse x=height y=weight/alpha=0.0027;TITLE “Mens weight“;RUN;注:对生成的图形要进行编辑,需要下载 SAS 的图形编辑器。注:ODS 统计图形的展现形
20、式是由 GTL 语言编写的程序规定的 。定义了layouts(图形布局): lattices, overlaystypes(作图类型): scatter plot,histogramstitles(标题)footnotes(脚注)insets(图形里插入符号和文字)colors(颜色)symbols(符号)lines(线的属性):线类,线宽,颜色other elements:坐标轴的设置,背景,前景,等/*回归分析*/*reg step 1 建立回归方程*/data regdata;input x1-x4 y;datalines;7 26 6 60 78.5 1 29 15 52 74.3 1
21、1 56 8 20 104.3 11 31 8 47 87.6 7 52 6 33 95.9 11 55 9 22 109.2 3 71 17 6 102.7 1 31 22 44 72.5 2 54 18 22 93.1 21 47 4 26 115.9 1 40 23 34 83.8 11 66 9 12 113.3 10 68 8 12 109.4 run;proc reg data=regdata;model y=x1-x4;run;/*识别多重共线性*/proc reg data=regdata;model y=x1-x4/ vif collin;run;/* 多重共线性的处理选择变
22、量法*/*逐步回归*/proc reg data=regdata;model y=x1-x4/selection=stepwise;run;/*全子集法*/proc reg data=regdata;model y=x1-x4/selection=rsquare adjrsq cp aic bic rmse;run;/*岭嵴回归*/proc reg data=regdata outest=rghald outvif graphics corr;model y=x1-x4/ridge=0 to 1 by 0.1 2 3 4 5 6 ;plot/ridgeplot;run;proc print data=rghald;run;/*主成分回归*/proc princomp data=regdata;var x1-x4;run;proc reg data=regdata outest=pchald outvif;model y=x1-x4/pcomit=1,2 ;run;proc print data=pchald;run;/*回归诊断*/proc reg data=regdata graphics;model y=x1-x2/r ;plot student.*p.;run;