[ Retour aux Travaux Dirigés de SAS ]

/* Statistiques avec SAS, Corrigé du TD 7 */
/* Jean-Sebastien Roy (js@jeannot.org), 2001 */

%let chemin=//diamant/Data Mi2/sas/stats;
libname tables "&chemin/tables";

/* Quelques macros utiles */
%include "&chemin\util_td7.sas";

/* Normalisation */
data commlimi_norm;
  set tables.commlimi;
  array all h00--femmes;
  do over all;
    all=all/pop*100;
  end;
run;

/* Classification hierarchique */
/* dipl2 à dipl9 */
proc cluster data=commlimi_norm method=ward outtree=arbre standard ;
  id nom;
  copy dc;
  var dipl2-dipl9;
run;
/* On aurait pu aussi choisir :
  var jeunes adultes agees;
  var cs2-cs7;
*/


/* Visualisation de l'arbre */
proc tree data=arbre horizontal;
  id nom;
run;

/* Visualisation de la variation du critere d'agrégation */
proc sort data=arbre; by _height_run;
proc gplot data=arbre;
  where _freq_>1;
  symbol interpol=join value=dot;
  plot _height_*_ncl_;
run;
quit;

/* On garde 3 classes */
proc tree data=arbre noprint out=classes ncl=3;
  id dc;
run;

proc sort data=commlimi_norm; by dc; run;
proc sort data=classes; by dc; run;

/* On le note dans une table */
data commlimi_norm;
  merge commlimi_norm classes;
  by dc;
run;

/* Tentative de représentation graphique */
%labelg(commlimi_norm,cs5,cs4,nom);
proc gplot data=commlimi_norm anno=_anno;
  symbol interpol=none value=dot;
  plot cs4*cs5=cluster;
run;
quit;

/* ACP */
data commlimi_norm;
  set commlimi_norm;
  poids=1;
run;

/* Cette macro est très frustre, mais fourni par rapport
  à la proc Princomp, des détails suplémentaires */

%acp(commlimi_norm,CS2-CS7 CS81 CS84 CS85 CS86 CS23
    JEUNES ADULTES AGEES
    FRANC EURO MAGHREB AFRIQ WORLD IBER);

/* On garde deux axes */
goptions hsize=vsize=6;
%acp_plot(1,2,nom);
%aide_acp(2,dc nom);

/* La commune 92051 est un point abérrant et nuit à l'ACP,
  on la supprime */

data filt;
  set commlimi_norm;
  where dc ne '92051';
run;

%acp(filt,CS2-CS7 CS81 CS84 CS85 CS86 CS23
    JEUNES ADULTES AGEES
    FRANC EURO MAGHREB AFRIQ WORLD IBER);
%acp_plot(1,2,nom);
%aide_acp(2,dc nom);

/* Idem, 92064 */
data filt;
  set commlimi_norm;
  where dc not in ('92051','92064');
run;

%acp(filt,CS2-CS7 CS81 CS84 CS85 CS86 CS23
    JEUNES ADULTES AGEES
    FRANC EURO MAGHREB AFRIQ WORLD IBER);

/* On garde 3 axes*/
%acp_plot(1,2,nom);
%acp_plot(2,3,nom);
%aide_acp(3,dc nom);

/* Queques graphiques combinés ACP/Classification
  la macro labelg permet de mettre des labels sur les graphes
  (normalement inutile, mais rendu necessaire par un bug de SAS)
*/

%labelg(acp,prin1,prin2,nom,anno=anno1);
%labelg(acp,prin2,prin3,nom,anno=anno2);

proc gplot data=acp;
  symbol1 interpol=none value=dot color=black;
  symbol2 interpol=none value=dot color=red;
  symbol3 interpol=none value=dot color=blue;
  plot prin2*prin1=cluster / anno=anno1;
  plot prin3*prin2=cluster / anno=anno2;
run;
quit;

/* Il faudrait pouvoir visualiser des variables suplémentaires
  et des individus suplémentaires.
  Notamment les variables DIPL0-DIPL9 CELI MARIE VEUF DIVORCE
*/


/* ACM */
/* Lecture du jeu de données du à Tenenhaus sur les chiens */
proc import datafile='D:/roy/add/chiens.txt' out=chiens
  dbms=DLM replace;
run;

/* Sans format, impossible de lire les sorties d'une ACM */
proc format;
  value tai
    1='TAILLE -'
    2='TAILLE +'
    3='TAILLE ++';
  value poi
    1='POIDS -'
    2='POIDS +'
    3='POIDS ++';
  value vel
    1='VELOCITE -'
    2='VELOCITE +'
    3='VELOCITE ++';
  value int
    1='INTEL -'
    2='INTEL +'
    3='INTEL ++';
  value aff
    1='AFFECTION -'
    2='AFFECTION +';
  value agr
    1='AGRESSIVITE -'
    2='AGRESSIVITE +';
  value fon
    1='Compagnie'
    2='Chasse'
    3='Utilite';
run;

proc datasets lib=work nolist;
  modify chiens;
    format tai tai. poi poi. vel vel.
           int int. aff aff. agr agr.
           fon fon.;
run;
quit;

/* L'ACM */
proc corresp data=chiens out=acm all;
  tables chiens,tai poi vel int aff agr fon;
  supplementary fon;
run;

/* Visualisation des sorties */
data _acm; set acm; where _type_ ne 'INERTIA'run;

%labelg(_acm,dim1,dim2,_name_);

proc gplot data=_acm anno=_anno;
  symbol1 interpol=none value=dot color=black;
  symbol2 interpol=none value=dot color=red;
  symbol3 interpol=none value=dot color=blue;
  plot dim2*dim1=_type_ / haxis=-1.4 to 1.4 by .2 vaxis=-1.4 to 1.4 by .2;
run;
quit;

/* Idem, mais en colorant par fonction du chien */
proc sort data=acm out=acm2; by _name_run;

data fon;
  set chiens;
  rename chiens=_name_;
  keep chiens fon;
run;

proc sort data=fon; by _name_run;

data acm2;
  merge acm2 fon;
  by _name_;
  if fon ne . then _type_=putn(fon,'fon.');
  if _type_ ne 'INERTIA';
  keep _type_ _name_ dim1 dim2;
run;

proc sort data=acm2; by _type_run;

%labelg(acm2,dim1,dim2,_name_);

proc gplot data=acm2 anno=_anno;
  symbol1 value=dot color=black;
  symbol2 value=dot color=red;
  symbol3 value=star color=green;
  symbol4 value=dot  color=blue;
  symbol5 value=star color=black;
  plot dim2*dim1=_type_ / haxis=-1.4 to 1.4 by .2 vaxis=-1.4 to 1.4 by .2;
run;
quit;