第四章 判别分析

该文展示了如何使用R语言进行距离判别法和Bayes判别法。通过`mahalanobis()`函数计算个体与样本集的Mahalanobis距离,用于判断新个体属于哪个类别。同时,利用`qda()`和`lda()`函数建立Bayes二次判别和线性判别模型,并进行预测。文章还涉及了交叉验证来评估模型的性能。
摘要由CSDN通过智能技术生成
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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值