存档

文章标签 ‘statistical’

sas的TRANSPOSE过程详解

2010年8月16日 没有评论

今天我第一次用到sas里面的TRANSPOSE过程,为此我查了一些资料,发现网上有些文章写的不是很清楚。下面我谈谈sas里面的TRANSPOSE过程。本文只在sas8.2下调试通过。
先看图,有些简陋。

sas transpose过程说明图

TRANSPOSE过程能实现sas数据集的转置,所谓转置,就是把观测(行)变成变量(列),变量变为观测,如上图中,从表1变成表3。该过程从读入的一个数据集中,创建一个新的数据集。具体用法格式如下:

PROC TRANSPOSE
DATA=input-data-set  LABEL=label LET NAME=name OUT=output-data-set PREFIX=prefix
;
BY  variable-1
... variable-n
;
COPY variable(s);
ID variable;
IDLABEL variable;
VAR variable(s);

sas里的所有过程,都分为选项/option和声明statement两部分,在格式上,选项与过程名是一条语句,而每个声明单独成语句,也就是用分号分隔开。在程序运行上也一样,过程与选项一次性读入后分析。
很多时候,我们也许只用到几个选项,或者几个声明就实现了自己的目的,不过如果你能确切的了解每个过程的选项和声明,会让你的程序更加简洁易读。
以下是从sas support翻译过来的,并加入我自己的解释。

选项/Options

选项1:

DATA=input-data-set

需要进行转置的数据集的名字。 默认情况下,转置最近新建的数据集。

选项2:

LABEL= label

英文的表述是:指定新输出的数据集中新观测的变量的标签,默认是_LABEL_,但是经过我的测试,这个选项没有任何意义,也就是设置与否不起任何作用。

选项3:

LET

允许重复的ID变量,并且当重复时,以最后一个为准,所谓的id变量,就是类上图中表1中name的作用,一般来讲,它的值不允许重复,因为如果重复,会导致生成重复变量。

选项4:

NAME= name

指定新转置生成的数据集里新变量的名称。比如上图中转置后的表2和表三的第一列,也就是第一个变量的名称,如果不设置,会以name of former variable命名,设置后,该变量的标签名依然是name of former variable。我认为前面的label选项,就是用来设置该变量的标签的,但是设置后居然没有变化,不知道是我sas问题还是版本的问题。在上图表3中,我设置name为tnew,所以显示为tnew。在sas9.2版本中,改默认变量为_name_。

选项5:

OUT= output-data-set

输出数据集,也就是转置后的数据集的名称,默认以data后面加数字来命名。

选项6:

PREFIX= prefix

前缀,原来观测变成变量后的前缀,不设置,也就是默认情况下,这些变量会以col1,col2排列下去,有的sas版本会以var1, var2排列下去。设置后,会以设置的字符串后加上数字1、2,并以此排列下去。为便于识别变量,这个选型经常与声明里的id混合使用,如表3,就是使用了id声明与前缀选项的结果。
例程如下,你可以copy到sas里查看运行结果。

data a;
input name $ test1 test2 test3;
cards;
wyt 98 99 97
ycy 78 45 34
ynn 78 90 87
;
run;
proc transpose data=a  name=tnew label=n  out=b  let  ;
run;
proc print data=a;
run;
proc print data=b;
run;

声明/statement

下面是transpose过程的声明。声明使用的例程如下,比上面多的就是在选项之后,与run之间的声明部分。

data c;
input name $ test1 test2 test3;
cards;
wyt 98 99 97
ycy 78 45 34
ynn 78 90 87
;
run;
proc transpose data=a  name=tnew label=n  out=d  prefix=t let  ;
id name;
var name test1 test2;
copy test3;
 
run;
proc print data=d;
run;
proc print data=c;
run;

声明1:

BY  variable-1
<... variable-n>
;

下面这张图,很清楚指明了分组转置是怎么样一个过程,其中的DESCENDING是以降序排列,而NOTSORTED,是不排序。此声明没在sas8.2下测试。

声明2:

ID variable(s);

在前面说过,prefix经常与此声明共同使用,该声明的作用就是,把新生成数据集里的变量用id的值表示出来,如上图中的表三,就是应用了id后把id的值一一指定。在sas的很多过程里均有id的概念,该值一般不重复,因此可以指定为变量,在sas数据集里,观测可以重复,变量不能重复。另外,这个声明还经常与下面的声明仪器出现。

声明3:

IDLABEL variable;

用来指定上面对应变量的label,标签。

声明4:

VAR variable(s);

指明那几个变量进行转置,在本例子中,只讲test1与test2进行了转置,生成如下图的sas数据集。

声明5:

COPY variable(s);

这个声明用来原样拷贝源数据集中的某一个变量,原样不动的含义就是在新数据集里,改变量的值与源数据集中的一样,下面的例子,最后生成的数据集如下图所示,看着有点发懵,呵呵,在仔细看看就懂了

sas transpose statement


另外,当我要获取sas数据集的变量名并赋值给宏的时候,这篇文章帮了大忙,谢谢。具体内容转帖如下:
对一个数据集的变量进行分析,至少需要变量名,对吧。如果变量少,自已逐个输入也没问题,所谓忍一忍就和谐了。如果变量多呢,就无法忍受了–一个个地敲进去,不但麻烦,且易出错。解决之道有三种。

其一是读取表的dictionary.columns.Dictionary表里保存着库名及其成员的名字。以sashelp里的表class为例。

proc
sql;


create
table vars as

  
select varnum,name

  
from dictionary.columns

  
where memname=‘CLASS’;

quit;

其中varnum表示是第几个变量,name表示变量名。值得提醒的是,Dictionary里是以大写字母的形式保存表名。所以这里须要写CLASS

其二是读取SASHELP.VCOLUMN。以数据步读取的形式如下

data vars1;

set sashelp.vcolumn;

where memname=‘CLASS’;

keep varnum name;

run;

其三是利用过程proc contents。这个要来得比读取dictionary和sashelp.vcolumn灵活。

data vars1;

set sashelp.vcolumn;

where memname=‘CLASS’;

keep varnum name;

run;

如果需要将变量名赋给一个宏变量,以空格隔开。其例如下

proc
contents
data=sashelp.class


out=vars3(keep=varnum name)

     noprint;

run;

proc
sql

noprint;


select
distinct name

     into :classname separated by
‘ ‘

     from vars3

     order
by varnum;

quit;

可以用%put &classname;察看其宏变量。得到结果

Name Sex Age Height Weight

当然有时并不想所有的变量都赋给这个宏变量。比如只想取出第1个和第2个变量给宏变量。当然也好解决。

proc
sql
noprint;


select
distinct name

     into :classname separated by
‘ ‘

     from vars3

     where varnum in (1,2)

     order
by varnum;

quit;

注意加了行where varnum in (1,2).或者

proc
contents
data=sashelp.class(keep=Name Sex)


out=vars4(keep=varnum name)

     noprint;

run;

proc
sql
noprint;


select

distinct name

     into :classname separated by
‘ ‘

     from vars4    

     order
by varnum;

quit;

%put &classname;

也是一样的。

分类: 数据可视化 标签: ,

统计课程之主成分分析

2007年5月15日 2 条评论

1、主成分分析(principal component analysis,PCA)

英文学习网站:http://www.dcs.gla.ac.uk/~mc/1stYearReport/2.6_PCA.htm
主成分分析适用情况:多变量大样本分析中,变量间存在共线性,增加了分析的复杂性。若分别分析各个指标,分析有可能是孤立的,而不是综合的;盲目地减少指标又有可能损失很多信息,得出错误结论。欲采用较少指标,反映原资料大部分信息,可采用主成分分析和因子分析。也可以这样讲,任何一个度量指标的好坏除了可靠、真实之外,还必须能充分反映个体间的变异。如果有一项指标,不同个体的取值都大同小异,那么该指标不能用来区分不同的个体。由这一点来看,一项指标在个体间的变异越大越好。因此我们把“变异大”作为“好”的标准来寻求综合指标。

主成分分析概念: PCA是将分散在一组变量上的信息,集中到某几个综合指标(主成分)上的一种探索性统计分析方法。它利用降维的思想,将多个变量化为少数几个互不相关的主成分,从而描述数据集的内部结构。
table.GIF
主成分分析的几何意义:参见上面数据表1例中的的数据点是六维的;也就是说,每个观测值是 6 维空间中的一个点。我们希望把 6 维空间用低维空间表示。

   先假定只有二维,即只有两个变量,它们由横坐标和纵坐标所代表;因此每个观测值都有相应于这两个坐标轴的两个坐标值;如果这些数据形成一个椭圆形状的点 阵(这在变量的二维正态的假定下是可能的),那么这个椭圆有一个长轴和一个短轴。在短轴方向上,数据变化很少;在极端的情况,短轴如果退化成一点,那只有 在长轴的方向才能够解释这些点的变化了;这样,由二维到一维的降维就自然完成了。

  当坐标轴和椭圆的长短轴平行,那么代表长轴的变 量就描述了数据的主要变化,而代表短轴的变量就描述了数据的次要变化。但是,坐标轴通常并不和椭圆的长短轴平行。因此,需要寻找椭圆的长短轴,并进行变 换,使得新变量和椭圆的长短轴平行。如果长轴变量代表了数据包含的大部分信息,就用该变量代替原先的两个变量(舍去次要的一维),降维就完成了。椭圆 (球)的长短轴相差得越大,降维也越有道理。

  对于多维变量的情况和二维类似,也有高维的椭球,只不过无法直观地看见罢了。

  首先把高维椭球的主轴找出来,再用代表大多数数据信息的最长的几个轴作为新变量;这样,主成分分析就基本完成了。

  注意,和二维情况类似,高维椭球的主轴也是互相垂直的。这些互相正交的新变量是原先变量的线性组合,叫做主成分 (principal component) 。

  正如二维椭圆有两个主轴,三维椭球有三个主轴一样,有几个变量,就有几个主成分。

  选择越少的主成分,降维就越好。什么是标准呢?那就是这些被选的主成分所代表的主轴的长度之和占了主轴长度总和的大部分。有些文献建议,所选的主轴总长度占所有主轴长度之和的大约 85% 即可,其实,这只是一个大体的说法;具体选几个,要看实际情况而定。

分类: 数据可视化 标签:

Sensory analysis

2007年4月29日 1 条评论

Sensory analysis

Sensory analysis (or sensory evaluation) is a scientific discipline that applies principles of experimental design and statistical analysis to the use of human senses (sight, smell, taste, touch and hearing) for the purposes of evaluating consumer products. The discipline requires panels of human assessors, on whom the products are tested, and recording the responses made by them. By applying statistical techniques to the results it is possible to make inferences and insights about the products under test. Most large consumer goods companies have departments dedicated to sensory analysis.

Sensory analysis can generally be broken down into three sub-sections:

* Effective testing (dealing with objective facts about products)
* Affective testing (dealing with subjective facts such as preferences)
* Perception (the biochemical and psychological aspects of sensation)
Effective testing

This type of testing is concerned with obtaining objective facts about products. This could range from basic discrimination testing (e.g. Do two or more products differ from each other?) to descriptive profiling (e.g. What are the characteristics of two or more products?). The type of panel required for this type of testing would normally be a trained panel.

Affective testing

Otherwise known as consumer testing, this type of testing is concerned with obtaining subjective data, or how well products are likely to be accepted. Usually large (50 or more) panels of untrained personnel are recruited for this type of testing, although smaller focus groups can be utilised to gain insights into products. The range of testing can vary from simple comparative testing (e.g. Which do you prefer, A or B?) to structured questioning regarding the magnitude of acceptance of individual characteristics (e.g. Please rate the “fruity aroma”: dislike|neither|like).

Perception

Perception involves the biochemical and psychological theories relating to human (and animal) sensations. By understanding the mechanisms involved it may be possible to explain why certain characteristics are preferred over others.

Descriptive analysis involves trained panels (6-30 people) who evaluate products by rating the intensity of various characteristics on a scale. Statistical analyses are applied to look for differences among various products for characteristics of interest.

Consumer testing (sometimes called ‘hedonic testing’) involves having potential consumers of a product evaluate various products and a small number of items on a ballot.