setwd("C:/Users/asus/Desktop/我的文件")
d6.1<- read.table("fisher.txt",header = T)
attach(d6.1)#绑定数据
plot(x1,x2)
text(x1,x2,G,adj=-0.5)#标识点所属类别G

library(MASS)
(ld=lda(G~x1+x2))#线性判别模型
## Call:
## lda(G ~ x1 + x2)
## 
## Prior probabilities of groups:
##   1   2 
## 0.5 0.5 
## 
## Group means:
##      x1   x2
## 1  0.92 2.10
## 2 -0.38 8.85
## 
## Coefficients of linear discriminants:
##           LD1
## x1 -0.1035305
## x2  0.2247957
Z=predict(ld)#根据线性判别模型预测所属类别
newG=Z$class#预测的所属类别结果
cbind(G,Z$x,newG)#显示结果
##    G         LD1 newG
## 1  1 -0.28674901    1
## 2  1 -0.39852439    1
## 3  1 -1.29157053    1
## 4  1 -1.15846657    1
## 5  1 -1.95857603    1
## 6  1  0.94809469    2
## 7  1 -2.50987753    1
## 8  1 -0.47066104    1
## 9  1 -1.06586461    1
## 10 1 -0.06760842    1
## 11 2  0.17022402    2
## 12 2  0.49351760    2
## 13 2  2.03780185    2
## 14 2  0.38346871    2
## 15 2 -1.24038077    1
## 16 2  0.24005867    2
## 17 2  1.42347182    2
## 18 2  2.01119984    2
## 19 2  1.40540244    2
## 20 2  1.33503926    2
(tab=table(G,newG))#混淆矩阵        
##    newG
## G   1 2
##   1 9 1
##   2 1 9
sum(diag(prop.table(tab)))#判对率 
## [1] 0.9