{"word_count": 713, "character_count": 8565}
Another popular technique for classification (or at least, which used to be popular) is the (linear) discriminant analysis, introduced by Ronald Fisher in 1936. Consider the same dataset as in our previous post&#62; clr1 &#60;- c(rgb(1,0,0,1),rgb(0,0,1,1))&#62; x &#60;- c(.4,.55,.65,.9,.1,.35,.5,.15,.2,.85)&#62; y &#60;- c(.85,.95,.8,.87,.5,.55,.5,.2,.1,.3)&#62; z &#60;- c(1,1,1,1,1,0,0,1,0,0)&#62; df &#60;- data.frame(x,y,z)&#62; plot(x,y,pch=19,cex=2,col=clr1[z+1])The main interest of that technique is not the output, but more the fact that we can make here simple (and explicit) computations. Especially to get a better understanding of theoretical concepts on classification.The code to run a linear discriminent analysis is&#62; library(MASS)&#62; fit_lda &#60;- lda (z ~ x+y, data=df)&#62; fit_ldaCall:lda(z ~ x + y, data = df) Prior probabilities of groups:  0   1 0.4 0.6  Group means:          x      y0 0.4750000 0.36251 0.4583333 0.6950 Coefficients of linear discriminants:        LD1x -2.588390y  4.762615To visualize the prediction, consider&#62; pred_lda &#60;- function(x,y) as.numeric(predict(fit_lda, +       newdata=data.frame(x=x,y=y))$class)&#62; x_grid&#60;-seq(0,1,length=101)&#62; y_grid&#60;-seq(0,1,length=101)&#62; z_grid &#60;- outer(x_grid,y_grid,pred_lda)&#62; clr1=c(rgb(1,0,0,1),rgb(0,0,1,1))&#62; clr2=c(rgb(1,0,0,.2),rgb(0,0,1,.2))&#62; image(x_grid,y_grid,z_grid,col=clr2)&#62; points(x,y,pch=19,cex=2,col=clr1[z+1])Note that it is very close to what we got with our logistic regression. So, what did we do, here ? The idea, here, is thatandUsing Bayes' theorem, it is possible thoses relationship, to compute the probability that  (for instance), given . Here, we can easily estimate the three paramers, since means are just centroids of the groupsand&#62; m0=fit_lda$means[1,]&#62; m1=fit_lda$means[2,]&#62; df$x0=df$x-m0[1]&#62; df$y0=df$y-m0[2]&#62; df$x0[z==1]=df$x[z==1]-m1[1]&#62; df$y0[z==1]=df$y[z==1]-m1[2]&#62; S=var(df[,c("x0","y0")])&#62; library(mnormt)&#62; d0 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m0,varcov=S)&#62; z_0 &#60;- outer(x_grid,y_grid,d0)&#62; d1 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m1,varcov=S)&#62; z_1 &#60;- outer(x_grid,y_grid,d1)&#62; plot(x,y,pch=19,cex=2,col=clr1[z+1])&#62; contour(x_grid,y_grid,z_0,col=clr2[1],add=TRUE)&#62; contour(x_grid,y_grid,z_1,col=clr2[2],add=TRUE)&#62; points(fit_lda$means,col=clr1,pch=4,lwd=2)To visualize the two regions, just compare the densities. For instance, if the ratio of the two densities exceeds one, then we should be in one class,&#62; contour(x_grid,y_grid,z_1/z_0,levels=1)But if we want to be consistent with the Bayesian interpretation, we should not compare the ratios of probabilities with one, but with the ratio of the proportions of points in each categories, sinceThus,if and only if&#62; contour(x_grid,y_grid,z_1/z_0,levels=.4/.6)(which is exacty the graph we got above). Consider now the case where we have three clases (as in the previous post)&#62; clr1=c(rgb(1,0,0,1),rgb(1,1,0,1),rgb(0,0,1,1))&#62; clr2=c(rgb(1,0,0,.2),rgb(1,1,0,.2),rgb(0,0,1,.2))&#62; x=c(.4,.55,.65,.9,.1,.35,.5,.15,.2,.85)&#62; y=c(.85,.95,.8,.87,.5,.55,.5,.2,.1,.3)&#62; z=c(1,2,2,2,1,0,0,1,0,0)&#62; df=data.frame(x,y,z)&#62; plot(x,y,pch=19,cex=2,col=clr1[z+1])The code to run a linear discriminent analysis is the same,&#62; fit_lda &#60;- lda (z ~ x+y, data=df)&#62; fit_ldaCall:lda(z ~ x + y, data = df) Prior probabilities of groups:  0   1   2 0.4 0.3 0.3  Group means:          x         y0 0.4750000 0.36250001 0.2166667 0.51666672 0.7000000 0.8733333 Coefficients of linear discriminants:       LD1       LD2x 2.014510  4.237266y 3.438693 -3.185543To visualise the three classes use&#62; pred_lda &#60;- function(x,y) + as.numeric(predict(fit_lda, + newdata=data.frame(x=x,y=y))$class)&#62; z_grid &#60;- outer(x_grid,y_grid,pred_lda)&#62; image(x_grid,y_grid,z_grid,col=clr2)&#62; points(x,y,pch=19,cex=2,col=clr1[z+1])Here again, we have the interpretation of a mixture of three Gaussian vectors (with given means, and identical variance)&#62; m0=fit_lda$means[1,]&#62; m1=fit_lda$means[2,]&#62; m2=fit_lda$means[3,]&#62; df$x0=df$x-m0[1]&#62; df$y0=df$y-m0[2]&#62; df$x0[z==1]=df$x[z==1]-m1[1]&#62; df$y0[z==1]=df$y[z==1]-m1[2]&#62; df$x0[z==2]=df$x[z==2]-m2[1]&#62; df$y0[z==2]=df$y[z==2]-m2[2]&#62; S=var(df[,c("x0","y0")])&#62; library(mnormt)&#62; d0 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m0,varcov=S)&#62; z_0 &#60;- outer(x_grid,y_grid,d0)&#62; d1 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m1,varcov=S)&#62; z_1 &#60;- outer(x_grid,y_grid,d1)&#62; d2 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m2,varcov=S)&#62; z_2 &#60;- outer(x_grid,y_grid,d2)&#62; plot(x,y,pch=19,cex=2,col=clr1[z+1],)&#62; contour(x_grid,y_grid,z_0,col=clr2[1],add=TRUE)&#62; contour(x_grid,y_grid,z_1,col=clr2[2],add=TRUE)&#62; contour(x_grid,y_grid,z_2,col=clr2[3],add=TRUE)&#62; points(fit_lda$means,col=clr1,pch=4,lwd=2)Again, to visualize lines where two densities are equal, we use&#62; contour(x_grid,y_grid,z_1/z_0,levels=1)&#62; contour(x_grid,y_grid,z_2/z_0,levels=1)&#62; contour(x_grid,y_grid,z_1/z_2,levels=1)or, using Bayes theorem to get an interpretation in terms of conditional probabilities&#62; contour(x_grid,y_grid,z_1/z_0,+ add=TRUE,levels=mean(z==0)/mean(z==1))&#62; contour(x_grid,y_grid,z_2/z_0,+ add=TRUE,levels=mean(z==0)/mean(z==2))&#62; contour(x_grid,y_grid,z_1/z_2,+ add=TRUE,levels=mean(z==2)/mean(z==1))Now, this linear discriminent analysis is very restrictive. We must have the same variance. If not, we get what is called the quadratic discriminent analysis. The code is here&#62; fit_qda &#60;- qda (z ~ x+y, data=df)Again, to visualize the classes, we use&#62; pred_qda &#60;- function(x,y) as.numeric(predict(fit_qda, newdata=data.frame(x=x,y=y))$class)&#62; x_grid&#60;-seq(0,1,length=101)&#62; y_grid&#60;-seq(0,1,length=101)&#62; z_grid &#60;- outer(x_grid,y_grid,pred_qda)&#62; clr1=c(rgb(1,0,0,1),rgb(0,0,1,1))&#62; clr2=c(rgb(1,0,0,.2),rgb(0,0,1,.2))&#62; image(x_grid,y_grid,z_grid,xcol=clr2)&#62; points(x,y,pch=19,cex=2,col=clr1[z+1])&#62; points(x,y,pch=1,cex=2)Here again, the idea is that we have a mixture of two Gaussian vectors (with different variances, this time), i.e.while&#62; m0=fit_qda$means[1,]&#62; m1=fit_qda$means[2,]&#62; S0=var(df[z==0,c("x","y")])&#62; S1=var(df[z==1,c("x","y")])&#62; library(mnormt)&#62; d0 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m0,varcov=S0)&#62; z_0 &#60;- outer(x_grid,y_grid,d0)&#62; d1 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m1,varcov=S1)&#62; z_1 &#60;- outer(x_grid,y_grid,d1)&#62; plot(x,y,pch=19,cex=2,col=clr1[z+1],)&#62; contour(x_grid,y_grid,z_0,col=clr2[1],add=TRUE)&#62; contour(x_grid,y_grid,z_1,col=clr2[2],add=TRUE)&#62; points(fit_lda$means,col=clr1,pch=4,lwd=2)Here they are,To get again the separation curve, we use&#62; contour(x_grid,y_grid,z_1/z_0,levels=.4/.6)The decision boudary, i.e.is quadratic sinceWhat if we focus on the three classes case ? The classes are&#62; z_grid &#60;- outer(x_grid,y_grid,pred_qda)&#62; image(x_grid,y_grid,z_grid,col=clr2)&#62; points(x,y,pch=19,cex=2,col=clr1[z+1])&#62; points(x,y,pch=1,cex=2)The idea behind is the same as in the linear case, we simply relax the assumption of identical variance&#62; m0=fit_qda$means[1,]&#62; m1=fit_qda$means[2,]&#62; m2=fit_qda$means[3,]&#62; S0=var(df[z==0,c("x","y")])&#62; S1=var(df[z==1,c("x","y")])&#62; S2=var(df[z==2,c("x","y")])&#62; library(mnormt)&#62; d0 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m0,varcov=S0)&#62; z_0 &#60;- outer(x_grid,y_grid,d0)&#62; d1 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m1,varcov=S1)&#62; z_1 &#60;- outer(x_grid,y_grid,d1)&#62; d2 &#60;- function(x,y) dmnorm(cbind(x,y),mean=m2,varcov=S2)&#62; z_2 &#60;- outer(x_grid,y_grid,d2)&#62; plot(x,y,pch=19,cex=2,col=clr1[z+1])&#62; contour(x_grid,y_grid,z_0,col=clr2[1],add=TRUE)&#62; contour(x_grid,y_grid,z_1,col=clr2[2],add=TRUE)&#62; contour(x_grid,y_grid,z_2,col=clr2[3],add=TRUE)&#62; points(fit_lda$means,col=clr1,pch=4,lwd=2)&#62; contour(x_grid,y_grid,z_1/z_0,levels=1)&#62; contour(x_grid,y_grid,z_2/z_0,levels=1)&#62; contour(x_grid,y_grid,z_1/z_2,levels=1)to compare densities, but here we do not have the conditional probability interpretation.Note that there is also a mixture discriminant analysis&#62; library(mda)&#62; fit_mda &#60;- mda (z ~ x+y, data=df)          &#62; pred_mda &#60;- function(x,y) + as.numeric(predict(fit_mda,+ newdata=data.frame(x=x,y=y),type="class"))for two classes, but also in the case where we had three classes,