*Budeme hledat diskriminacni pravidla pro datovy soubor IRIS; *Zacneme s jednorozmernou analyzou - pouzijeme promennou PETALWIDTH; *Stejne jako minule si definujeme pomocna data potrebna pro vykreslovani odhadu hustot; data plotdata; do PetalWidth=-5 to 30 by 0.5; output; end; run; *Neparametricka diskriminacni analyza; proc discrim data=sashelp.iris method=npar kernel=normal r=.4 testdata=plotdata testout=plotp testoutd=plotd listerr; class Species; var PetalWidth; run; *Makra pro vykresleni odhadu hustot a klasifikacnich pravdepodobnosti; %macro plotden; title3 'Plot of Estimated Densities'; data plotd2; set plotd; if setosa < .002 then setosa = .; if versicolor < .002 then versicolor = .; if virginica < .002 then virginica = .; g = 'Setosa '; Density = setosa; output; g = 'Versicolor'; Density = versicolor; output; g = 'Virginica '; Density = virginica; output; label PetalWidth='Petal Width in mm.'; run; proc sgplot data=plotd2; series y=Density x=PetalWidth / group=g; discretelegend; run; %mend; %macro plotprob; title3 'Plot of Posterior Probabilities'; data plotp2; set plotp; if setosa < .01 then setosa = .; if versicolor < .01 then versicolor = .; if virginica < .01 then virginica = .; g = 'Setosa '; Probability = setosa; output; g = 'Versicolor'; Probability = versicolor; output; g = 'Virginica '; Probability = virginica; output; label PetalWidth='Petal Width in mm.'; run; proc sgplot data=plotp2; series y=Probability x=PetalWidth / group=g; discretelegend; run; %mend; %plotden; %plotprob; title; *Prejdeme do dvou dimenzi. Budou nas zajimat promenne PETALLENGTH a PETALWIDTH; *Opet zacneme s definovanim pomocneho datoveho souboru; data plotdata; do PetalLength = -2 to 72 by 0.5; do PetalWidth= - 5 to 32 by 0.5; output; end; end; run; *Makra pro vykresleni odhadu hustot a klasifikacnich pravdepodobnosti; %let close = thresholdmin=0 thresholdmax=0 offsetmin=0 offsetmax=0; %let close = xaxisopts=(&close) yaxisopts=(&close); proc template; define statgraph contour; begingraph; layout overlayequated / equatetype=equate &close; contourplotparm x=petallength y=petalwidth z=z / contourtype=fill nhint=30; scatterplot x=pl y=pw / group=species name='iris' includemissinggroup=false primary=true; layout gridded / autoalign=(topleft); discretelegend 'iris' / border=false opaque=false; endlayout; endlayout; endgraph; end; run; %macro contden; data contour(keep=PetalWidth PetalLength species z pl pw); merge plotd(in=d) sashelp.iris(keep=PetalWidth PetalLength species rename=(PetalWidth=pw PetalLength=pl)); if d then z = max(setosa,versicolor,virginica); run; title3 'Plot of Estimated Densities'; proc sgrender data=contour template=contour; run; %mend; %macro contprob; data posterior(keep=PetalWidth PetalLength species z pl pw into); merge plotp(in=d) sashelp.iris(keep=PetalWidth PetalLength species rename=(PetalWidth=pw PetalLength=pl)); if d then z = max(setosa,versicolor,virginica); into = 1 * (_into_ =: 'Set') + 2 * (_into_ =: 'Ver') + 3 * (_into_ =: 'Vir'); run; title3 'Plot of Posterior Probabilities '; proc sgrender data=posterior template=contour; run; %mend; %macro contclass; title3 'Plot of Classification Results'; proc sgrender data=posterior(drop=z rename=(into=z)) template=contour; run; %mend; *Neparametricka diskriminacni analyza; proc discrim data=sashelp.iris method=npar kernel=normal r=.5 testoutd=plotd testdata=plotdata testout=plotp listerr; class Species; var Petal:; run; %contden %contprob %contclass title; *Neparametricka diskriminacni analyza se vsemi 4 promennymi; proc discrim data=sashelp.iris method=npar kernel=normal r=.5 listerr; class Species; var PetalLength PetalWidth SepalLength SepalWidth; run; *Metoda kNN; proc discrim data=sashelp.iris method=npar k=7 listerr; class Species; var PetalLength PetalWidth SepalLength SepalWidth; run; *k=15; proc discrim data=sashelp.iris method=npar k=15 listerr; class Species; var PetalLength PetalWidth SepalLength SepalWidth; run; *k=1; proc discrim data=sashelp.iris method=npar k=1 listerr; class Species; var PetalLength PetalWidth SepalLength SepalWidth; run; *Kanonicka diskriminacni analyza; proc candisc data=sashelp.iris all out=can; class Species; var PetalLength PetalWidth SepalLength SepalWidth; run; proc sgplot data=can; scatter x=can1 y=can2/group=Species; run; *Nyni jiz muzeme provest vlastni diskriminaci pomoci LDA; proc discrim data=can method=normal pool=yes listerr; class Species; var can1 can2; run; *Na zaver porovnejme vysledky s PCA; proc princomp data=sashelp.iris out=pca; run; proc sgplot data=pca; scatter y=prin2 x=prin1 / group=Species; run; *Ukoly k procvicovani; *1.) Provedte neparametrickou diskriminacni analyzu na soubor CROPS a TEST z minula; *Jak jsou v SASu nastaveny apriorni pravdepodobnosti? Jak se daji menit? *2.) Na soubory CROPS a TEST z minula provedte metodu kNN;