#Moderni metody DA budeme opet ilustrovat na datovem souboru IRIS; iris Species=iris$Species #vektor spravnych trid a=as.numeric(Species) #vektor spravnych trid - ciselny #matice scatterplotu plot(iris[,c(1,2,3,4)],col=a) ################################################################## # Nejprve zopakujeme LDA a QDA ################################################################## library(MASS) lda(Species~.,data=iris) predict(lda(Species~.,data=iris)) PRED_LDA=predict(lda(Species~.,data=iris))$class table(Species,PRED_LDA) #matice zamen qda(Species~.,data=iris) predict(qda(Species~.,data=iris)) PRED_QDA=predict(qda(Species~.,data=iris))$class table(Species,PRED_QDA) #matice scatterplotu - klasifikace plot(iris[,c(1,2,3,4)],col=a,pch=as.numeric(PRED_QDA)) ################################################################## # Moderni metody DA ################################################################## # DA zalozena na hloubce dat library(ddalpha) #pocitejme hloubku vsech bodu vzhledem ke kazde ze tri skupin hloubka_Setosa <- depth.halfspace(iris[,c(1,2,3,4)], iris[Species=='setosa',c(1,2,3,4)]) hloubka_Versicolor <- depth.halfspace(iris[,c(1,2,3,4)], iris[Species=='versicolor',c(1,2,3,4)]) hloubka_Virginica <- depth.halfspace(iris[,c(1,2,3,4)], iris[Species=='virginica',c(1,2,3,4)]) cbind(hloubka_Setosa,hloubka_Versicolor,hloubka_Virginica) #klasifikator maximalni hloubky dd=ddalpha.train(Species ~ ., iris,depth = "halfspace", separator = "maxD",outsider.methods = "LDA") plot(dd) classes_halfspace <- ddalpha.classify(dd,iris[,c(1,2,3,4)]) unlist(classes_halfspace) table(Species,unlist(classes_halfspace)) #uvazujme nyni Mahalanobisovu hloubku vsech bodu vzhledem ke kazde ze tri skupin hloubka_Setosa <- depth.Mahalanobis(iris[,c(1,2,3,4)], iris[Species=='setosa',c(1,2,3,4)]) hloubka_Versicolor <- depth.Mahalanobis(iris[,c(1,2,3,4)], iris[Species=='versicolor',c(1,2,3,4)]) hloubka_Virginica <- depth.Mahalanobis(iris[,c(1,2,3,4)], iris[Species=='virginica',c(1,2,3,4)]) cbind(hloubka_Setosa,hloubka_Versicolor,hloubka_Virginica) dd=ddalpha.train(Species ~ ., iris,depth = "Mahalanobis", separator = "maxD",outsider.methods = "LDA") classes_Mahalanobis <- ddalpha.classify(dd,iris[,c(1,2,3,4)]) unlist(classes_Mahalanobis) table(Species,unlist(classes_Mahalanobis)) #klasifikator, separacni utvar je polynom dd=ddalpha.train(Species ~ ., iris,depth = "Mahalanobis", separator = "polynomial",outsider.methods = "LDA") classes_Mahalanobis <- ddalpha.classify(dd,iris[,c(1,2,3,4)]) unlist(classes_Mahalanobis) table(Species,unlist(classes_Mahalanobis)) #dd-alpha klasifikator dd=ddalpha.train(Species ~ ., iris,depth = "Mahalanobis", separator = "alpha",outsider.methods = "LDA") classes_Mahalanobis <- ddalpha.classify(dd,iris[,c(1,2,3,4)]) unlist(classes_Mahalanobis) table(Species,unlist(classes_Mahalanobis)) #dd-alpha klasifikator s poloprostorovou hloubkou dd=ddalpha.train(Species ~ ., iris,depth = "halfspace", separator = "alpha",outsider.methods = "LDA") classes_halfspace <- ddalpha.classify(dd,iris[,c(1,2,3,4)]) unlist(classes_halfspace) table(Species,unlist(classes_halfspace)) library(mda) #DA smes (mixture DA) model=mda(Species~.,data=iris,subclasses = c(3,3,3)) model PRED_MDA=predict(model) table(Species,PRED_MDA) #pridame dalsi prototypy model=mda(Species~.,data=iris,subclasses = c(3,10,10)) model PRED_MDA=predict(model) table(Species,PRED_MDA) #Regularizova DA library(klaR) #s jedinym parametrem lambda rda(Species~.,data=iris,gamma=0) PRED_RDA=predict(rda(Species~.,data=iris))$class table(Species,PRED_RDA) #s obema parametry lambda a gamma rda(Species~.,data=iris) PRED_RDA2=predict(rda(Species~.,data=iris))$class table(Species,PRED_RDA2)