***********************************************************************;
* Towns.sas -- Multidimensional scaling of imaginary map of towns *;
***********************************************************************;
Options PS=60 LS=65 PageNo=1 NoDate
FORMCHAR='|----|+|---+=|-/\<>*';
/*
* Include the SAS plotit macro for properly
* scaling the MDS plot axes and labelling
* the points.
*/
%Include "plotit7.sas" / NoSource;
Title1 "Multidimensional Scaling of Towns";
Data Towns;
Length Town $1;
Input Town A B C D;
DataLines;
A 0 120 160 97
B . 0 200 85
C . . 0 115
D . . . 0
;
Proc Print Data=Towns;
Run;
Title2 "Absolute Distances";
Proc MDS Data=Towns Level=Absolute Out=MDSout
Dimension=2 PData PConfig PFinal;
Var A--D;
Id Town;
Run;
%Plotit(Data=MDSout,Datatype=mds,Monochro=Black);
Title2 "Non-metric Scaling";
Proc MDS Data=Towns Level=Ordinal Out=MDSout
Dimension=2 PConfig PFinal;
Var A--D;
Id Town;
Run;
%Plotit(Data=MDSout,Datatype=mds,Monochro=Black);
/*
* Now connect the towns with a minimum spanning tree.
* First, include the MST macro.
* Second, reproduce the point configuration so that we
* have the metric solution.
* Third, subset MDSOut data set so that only the point
* configuration is kept.
* Fourth, compute the minimum spanning tree -- it will
* output an annotate data set that you can use with GPLOT.
* Fifth, use GPLOT to plot the graph with MST. Note that
* the axes may not be properly scaled. See later section
* to see how to use the MST macro and the PLOTIT macro together.
*/
%Include "mst.sas" / NoSource;
Title2 "Absolute Distances";
Proc MDS Data=Towns Level=Absolute Out=MDSout
Dimension=2 PData PConfig PFinal;
Var A--D;
Id Town;
Run;
Data MDSOut2;
Set MDSOut;
Where _TYPE_="CONFIG";
Run;
%MST(data=MDSOut2,Out=MSTAnno,Var=Dim1 Dim2,Color=Black);
Proc GPlot Data=MDSOut2 Annotate=MSTAnno;
Plot Dim2*Dim1=1;
Symbol1 C=Black V=Dot I=None;
Run;
Quit;
/*
* Now combine the PLOTIT and MST macros.
*
* First, use the PLOTIT macro to generate the plot information,
* but specify the METHOD=NONE option so that the plot is not
* actually drawn.
* Second, dig into the annotate data set produced by PLOTIT
* to extract the coordinates corresponding to our points.
* Third, put the MDS data set and the PLOTIT data set together.
* Fourth, construct the MST annotate data set, then merge
* it in with the ANNO data set from the PLOTIT macro.
* Fifth, display the graph with ganno.
*/
%Plotit(Data=MDSout,Datatype=mds,Monochro=Black,Method=None);
Data Ann2;
Set Anno;
Where OBSTYPE="CONFIG";
Retain XC YC;
If Function="LABEL" Then
Do;
Town=Text;
Output;
End;
XC=X; YC=Y;
Keep XC YC Town;
Run;
Proc Sort Data=Ann2;
By Town;
Run;
Data MDSOut2;
Merge MDSOut(FirstObs=2) Ann2;
Run;
%MST(data=MDSOut2,Out=MSTAnno,Var=Dim1 Dim2,Var2=XC YC,Color=Black);
Data Anno2;
Set Anno MSTAnno;
Drop HSys XSys YSys ZSys;
Run;
Proc GANNO Annotate=Anno2;
Run;
| Multidimensional Scaling of Towns |
| Obs | Town | A | B | C | D |
| 1 | A | 0 | 120 | 160 | 97 |
| 2 | B | . | 0 | 200 | 85 |
| 3 | C | . | . | 0 | 115 |
| 4 | D | . | . | . | 0 |
| Multidimensional Scaling of Towns |
| Absolute Distances |
| Multidimensional Scaling: Data=WORK.TOWNS.DATA |
| Data Matrix | ||||
| 1 | A | B | C | D |
| A | 0 | 120 | 160 | 97 |
| B | 120 | 0 | 200 | 85 |
| C | 160 | 200 | 0 | 115 |
| D | 97 | 85 | 115 | 0 |
| Multidimensional Scaling of Towns |
| Absolute Distances |
| Multidimensional Scaling: Data=WORK.TOWNS.DATA |
| Shape=TRIANGLE Condition=MATRIX Level=ABSOLUTE |
| Coef=IDENTITY Dimension=2 Formula=1 Fit=1 |
| Gconverge=0.01 Maxiter=100 Over=1 Ridge=0.0001 |
| Iteration | Type | Badness- of-Fit Criterion |
Change in Criterion |
Convergence Measure |
| 0 | Initial | 3.6864E-7 | . | 0.6568 |
| Convergence assumed because the badness-of-fit criterion is less than or equal to MINCRIT=1E-6. |
| Configuration | ||
| Dim1 | Dim2 | |
| A | 25.69 | 69.47 |
| B | 85.77 | -34.41 |
| C | -112.81 | -10.63 |
| D | 1.36 | -24.43 |
| Multidimensional Scaling: Data=WORK.TOWNS.DATA |
| Shape=TRIANGLE Condition=MATRIX Level=ORDINAL |
| Coef=IDENTITY Dimension=2 Formula=1 Fit=1 |
| Mconverge=0.01 Gconverge=0.01 Maxiter=100 Over=2 Ridge=0.0001 |
| Iteration | Type | Badness- of-Fit Criterion |
Change in Criterion |
Convergence Measures | |
| Monotone | Gradient | ||||
| 0 | Initial | 3.6864E-7 | . | . | . |
| Convergence assumed because the badness-of-fit criterion is less than or equal to MINCRIT=1E-6. |
| Configuration | ||
| Dim1 | Dim2 | |
| A | 0.44 | 1.19 |
| B | 1.46 | -0.59 |
| C | -1.93 | -0.18 |
| D | 0.02 | -0.42 |
| Multidimensional Scaling of Towns |
| Absolute Distances |
| Multidimensional Scaling: Data=WORK.TOWNS.DATA |
| Data Matrix | ||||
| 1 | A | B | C | D |
| A | 0 | 120 | 160 | 97 |
| B | 120 | 0 | 200 | 85 |
| C | 160 | 200 | 0 | 115 |
| D | 97 | 85 | 115 | 0 |
| Multidimensional Scaling of Towns |
| Absolute Distances |
| Multidimensional Scaling: Data=WORK.TOWNS.DATA |
| Shape=TRIANGLE Condition=MATRIX Level=ABSOLUTE |
| Coef=IDENTITY Dimension=2 Formula=1 Fit=1 |
| Gconverge=0.01 Maxiter=100 Over=1 Ridge=0.0001 |
| Iteration | Type | Badness- of-Fit Criterion |
Change in Criterion |
Convergence Measure |
| 0 | Initial | 3.6864E-7 | . | 0.6568 |
| Convergence assumed because the badness-of-fit criterion is less than or equal to MINCRIT=1E-6. |
| Configuration | ||
| Dim1 | Dim2 | |
| A | 25.69 | 69.47 |
| B | 85.77 | -34.41 |
| C | -112.81 | -10.63 |
| D | 1.36 | -24.43 |
| Multidimensional Scaling of Towns |
| Absolute Distances |
| X Contains the From Coordinates, |
| Y Contains the To Coordinates |
| OBSNUM | LIST2 |
| 1 | 1 |
| 2 | 4 |
| 3 | 4 |
| 4 | 1 |
| COORD | Y | ||
| 25.690672 | 69.469791 | 25.690672 | 69.469791 |
| 85.767694 | -34.40875 | 1.35564 | -24.42806 |
| -112.814 | -10.63298 | 1.35564 | -24.42806 |
| 1.35564 | -24.42806 | 25.690672 | 69.469791 |
| Multidimensional Scaling of Towns |
| Absolute Distances |
| X Contains the From Coordinates, |
| Y Contains the To Coordinates |
| OBSNUM | LIST2 |
| 1 | 1 |
| 2 | 4 |
| 3 | 4 |
| 4 | 1 |
| COORD | Y | ||
| 25.690672 | 69.469791 | 25.690672 | 69.469791 |
| 85.767694 | -34.40875 | 1.35564 | -24.42806 |
| -112.814 | -10.63298 | 1.35564 | -24.42806 |
| 1.35564 | -24.42806 | 25.690672 | 69.469791 |