A little program of correspondence analysis that I make with Mathematica 9
André Dauphiné
Geographer
University of Nice Sophia-antipolis
(*Analyse des Correspondances*)
(*Importation des données, \
suppression du nom des variables*)
(*les noms des lignes sont placées \
dans le fichier nom*)
ClearAll["Global`*"]
d = Import[SystemDialogInput["FileOpen"], "XLS"];
d = Flatten[d, 1];
nomcol = Take[d, 1];
nomcol = Drop[Flatten[nomcol], 1];
d1 = Transpose[d];
nomligne = Take[d1, 1];
nomligne = Drop[Flatten[nomligne], 1];
d2 = Drop[d, 1, 1];
touttot = Total[Flatten[d2]];
totcol = Total[d2]/touttot;
totlig = Total[Transpose[d2]]/touttot;
ncol = Length[totcol];
nlig = Length[totlig];
d3 = d2/touttot;
d4 = Table[d3[[i, All]]/totlig[[i]], {i, 1, nlig}];
d5 = Table[d4[[All, i]]*(1/Sqrt[totcol[[i]]]), {i, 1, ncol}];
d5 = Transpose[d5];
dcent = Table[d5[[All, i]] - Sqrt[totcol[[i]]], {i, 1, ncol}];
dcor = Covariance[Transpose[dcent]];
Print["Matrice des covariances"]
dcotmat = dcor;
Grid[dcotmat, Frame -> All, Alignment -> "."]
{vals, vecs} = Eigensystem[N[dcor]];
recvals = Sqrt[vals];
trac = Total[vals];
Print["Pourcentage de variance par chaque composante"]
pcvals = (vals/trac)*100;
Column[pcvals, Frame -> All, Alignment -> "."]
ListPlot[pcvals, Filling -> Axis]
stur = recvals*vecs;
Print["Saturations composantes variables"]
saturations = Transpose[stur];
sturt = N[saturations];
Grid[sturt, Frame -> All, Alignment -> "."]
proj = vecs.dcent;
projt = Transpose[proj];
Print["Saturations composantes objets"]
projc = N[projt];
Grid[projc, Frame -> All, Alignment -> "."]
ny = DialogInput[
DynamicModule[{name = ""}, Column[{"Choisir la composante 1",
InputField[Dynamic[name], String],
ChoiceButtons[{DialogReturn[name], DialogReturn[]}]}]]]
ny = ToExpression[ny];
nx = DialogInput[
DynamicModule[{name = ""}, Column[{"Choisir la composante 2",
InputField[Dynamic[name], String],
ChoiceButtons[{DialogReturn[name], DialogReturn[]}]}]]]
nx = ToExpression[nx];
Print[]
Print["Graphiques des saturations composantes variables"]
dcomp = Partition[
Riffle[saturations[[All, ny]], saturations[[All, nx]]], 2];
dcompespace = Partition[Riffle[projt[[All, ny]], projt[[All, nx]]], 2];
a1 = ListPlot[dcomp, PlotStyle -> Directive[PointSize[Large], Red],
AxesOrigin -> {0, 0}, AxesLabel -> {ny, nx}]
Print[]
Print["Graphiques des saturations composantes objets"]
a2 = ListPlot[dcompespace,
PlotStyle -> Directive[PointSize[Large], Blue],
AxesOrigin -> {0, 0}, AxesLabel -> {ny, nx}]
Print[]
Print["Graphiques des saturations composantes variables et \
composantes objets"]
Show[a1, a2]