library(rattle) ?wine head(wine) summary(wine) # sve kovarijate su kvantitativne dim(wine) n=178 pairs(wine) ## library(MASS) lda.fit=lda(Type ~ ., data=wine) lda.fit plot(lda.fit) ## Kako se tocno dobije ovaj graf? ## prvo pokazimo da je ## f_hat(x) = argmax_{k=1,2,3} delta_k(x) = argmin_{k=1,2,3} [ ||V*x - V*mi_k_hat ||/2 - log(pi_k_hat) ] mi1=as.vector(lda.fit\$means[1,]) mi2=as.vector(lda.fit\$means[2,]) mi3=as.vector(lda.fit\$means[3,]) v1=as.vector(lda.fit\$scaling[,1]) ## koeficijenti prvog diskr. vektora v2=as.vector(lda.fit\$scaling[,2]) ## koeficijenti drugog diskr. vektora v=rbind(v1,v2) ### ovo je matrica V pi=as.vector(lda.fit\$prior) delta_1=function(x){norm(v%*%x - v%*%mi1, type="2")^2/2 - log(pi[1])} delta_2=function(x){norm(v%*%x - v%*%mi2, type="2")^2/2 - log(pi[2])} delta_3=function(x){norm(v%*%x - v%*%mi3, type="2")^2/2 - log(pi[3])} f_hat=function(x){ a=c(delta_1(x), delta_2(x), delta_3(x)) return(which.min(a)) } lda.pred <- predict(lda.fit, wine)\$class data=as.matrix(wine[,-1]) lda.pred2=apply(data, 1, f_hat) table(lda.pred, lda.pred2) ## sve isto z1=data%*%v1 ## racunamo prve diskr. varijable na skupu za ucenje z2=data%*%v2 ## druge y=as.matrix(wine[,1]) plot(z1,z2, pch=as.character(y)) points(mi1%*%v1, mi1%*%v2, col="red", pch="1") points(mi2%*%v1, mi2%*%v2, col="red", pch="2") points(mi3%*%v1, mi3%*%v2, col="red", pch="3") mi=as.vector(apply(data, 2, mean)) points(mi%*%v1, mi%*%v2, pch=16, col="blue", cex=1.5) plot(lda.fit) ## jednako do na centriranje i/ili skaliranje? z1=z1- rep(mi%*%v1,n) z2=z2- rep(mi%*%v2,n) plot(z1,z2, pch=as.character(y))