7月 8, 2011

Posted by in 统计应用 | 0 条评论

SAS9.2统计作图小记

参考《Statistical Graphics in SAS》,本文对SAS 9.2作图方式进行了简单的梳理。

1.主要的3种统计图形生成方式:

  1. 利用Output Delivery System(ODS),统计过程步直接产生统计图形。使用“ods graphics on”语句,将会直接输出统计过程步的默认图形。
  2. 利用SG过程步。主要包括proc sgplot,proc sgscatter,proc sgpanel三个过程步。
  3. 利用GTL(Graph Template Language)。主要使用proc template和proc sgrender。

2.实例:

对数据集sashelp.class中的weight关于height作一次回归拟合图。

数据如下:

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*Method_1:STAT PROCs with ods*/
ods graphics on;
proc glm data=sashelp.class;
	model weight=height;
run;
ods graphics off;
 
/*Method_2:SG PROCs*/
proc sgplot data=sashelp.class;
        reg y=weight x=height;
run;
 
/*Method_3:GTL*/
proc template;
    define statgraph classreg;     /*define the tamplate name:classreg*/
        begingraph;
            layout overlay;
                scatterplot y=weight x=height;     /*define the scatter plot*/
                regressionplot y=weight x=height;  /*define the reg plot*/
            endlayout;
        endgraph;
    end;
run;
 
proc sgrender data=sashelp.class template=classreg;
run;

 

得到的结果分别为:

使用proc glm作图结果:

使用proc sgplot作图结果:

使用proc template作图结果:

3.小记:

  1. 对于各个统计分析过程步,其根据产生的数据结果会产生不同的默认统计图形,且图形内容丰富,若对个过程步熟悉,可根据需要灵活使用。
  2. 对于SG过程步(如 proc sgplot),其是对G过程步(如 proc gplt)的改进,语法规则及输出功能都有所改变,是基本的作图过程步。其有很多的参数设置来满足对图形的不同需求。
  3. 对于GTL,定义作图模板,可以生成各种图形,也可基于现有参数制作新的图形模板,功能极为强大。其通过参数设定可满足各种复杂图形需求,当然,参数设置也较为复杂。

另外,对于proc sgplot和proc sgpanel,可以设置tmplout=选项,输出图形模板文件,我们可以利用其改进制作自己的图形模板。
如:

1
2
3
4
/*Use the "tmplout=" option*/
proc sgplot data=sashelp.class tmplout='f:/regtmpl.txt';
reg y=weight x=height;
run;

会产生template文件:

proc template;
define statgraph sgplot;
begingraph;
layout overlay;
   ScatterPlot X=Height Y=Weight / primary=true;
   RegressionPlot X=Height Y=Weight / NAME="REG" LegendLabel="Regression" Maxpoints=10;
   DiscreteLegend "REG";
endlayout;
endgraph;
end;
run;

参考文献:

Statistical Graphics in SAS: An Introduction to the Graph Template Language and the Statistical Graphics Procedures

发表评论

返回顶部