Detailed code see github.com
X=iris[,1:4]
G=gl(3,50)
distinguish.distance(X,G)
## blong
## origin 1 2 3
## 1 50 0 0
## 2 0 47 3
## 3 0 0 50
## blong
## origin 1 2 3
## 1 0.333 0.000 0.000
## 2 0.000 0.313 0.020
## 3 0.000 0.000 0.333
## The index of wrong data are: 71 73 84
distinguish.bayes(X,G)
## blong
## TrnG 1 2 3
## 1 50 0 0
## 2 0 48 2
## 3 0 1 49
## blong
## TrnG 1 2 3
## 1 0.33333 0.00000 0.00000
## 2 0.00000 0.32000 0.01333
## 3 0.00000 0.00667 0.32667
## The index of wrong data are: 71 84 134
mydata=cbind(G, X)
ldist=lda(G~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data=mydata)
print(ldist)
## Call:
## lda(G ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
## data = mydata)
##
## Prior probabilities of groups:
## 1 2 3
## 0.3333333 0.3333333 0.3333333
##
## Group means:
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.006 3.428 1.462 0.246
## 2 5.936 2.770 4.260 1.326
## 3 6.588 2.974 5.552 2.026
##
## Coefficients of linear discriminants:
## LD1 LD2
## Sepal.Length 0.8293776 0.02410215
## Sepal.Width 1.5344731 2.16452123
## Petal.Length -2.2012117 -0.93192121
## Petal.Width -2.8104603 2.83918785
##
## Proportion of trace:
## LD1 LD2
## 0.9912 0.0088
pre.ldist=predict(ldist)
new.ind=pre.ldist$class
data.ldist=cbind(mydata,new.ind)
group=data.ldist$G
new.ind=data.ldist$new.ind
tab1=table(group, new.ind)
print(tab1)
## new.ind
## group 1 2 3
## 1 50 0 0
## 2 0 48 2
## 3 0 1 49
tab2=prop.table(tab1)
print(tab2, digits = 3)
## new.ind
## group 1 2 3
## 1 0.33333 0.00000 0.00000
## 2 0.00000 0.32000 0.01333
## 3 0.00000 0.00667 0.32667
## The index of wrong data are: 71 84 134