load('D:\\文件\\2022下(大三上)\\classR\\school.RData')
head(school)
# ---------第四章 判别分析----------
#---------距离判别法
school0=school[school$group==0,2:3] #1
school1=school[school$group==1,2:3]
x=data.frame(GPA=3.21,GMAT=497) #2 x=c(3.21,497) 单个个体
dist0=mahalanobis(x,colMeans(school0),cov(school0)) #3
dist1=mahalanobis(x,colMeans(school1),cov(school1))
which.min(c(dist0,dist1))-1 #4
xs=data.frame(GPA=c(3.21,2.1,4.0),GMAT=c(497,365,390)) #2 多个个体
dists0=mahalanobis(xs,colMeans(school0),cov(school0)) #3
dists1=mahalanobis(xs,colMeans(school1),cov(school1))
max.col(-cbind(dists0,dists1))-1 #4
#or
d=data.frame(dists0,dists1)
d
max.col(-d)
#----------bayes判别法
library(MASS)
res.qd=qda(group~GPA+GMAT,data = school) #1.建立bayes二次判别函数
predict(res.qd) #2.判别
predict(res.qd,x)
predict(res.qd,xs)
class.qd=predict(res.qd)$class # 3.列联表---判别精度
table(school$group,class.qd)
library(MASS)
res.ld=lda(group~GPA+GMAT,data = school) #1.建立bayes线性判别函数
predict(res.ld) #2.判别
predict(res.ld,x)
predict(res.ld,xs)
class.ld=predict(res.ld)$class # 3.列联表---判别精度
table(school$group,class.ld)
res.qd.cv=qda(group~GPA+GMAT,data = school,CV=T) # 4.交叉验证
table(school$group,res.qd.cv$class)
第四章 判别分析
最新推荐文章于 2024-08-30 13:29:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)