资源描述:
《R语言聚类分析因子分析t检验程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、R语言聚类分析、因子分析.t检验相关程序及程序运行结果相关程序:#####读入数据x=read.delim("G:\I:机考试数据.txt",header=TRUE,row.names=1)####作系统聚类d=dist(scale(x))hcl=hclust(d);hclhc2=hclust(d/averageu)hc3=hclust(d/,centroidH)hc4=hclust(d,"ward")####绘出谱系图和聚类情况(最长距离法、类平均法)opar=par(mfrow=c(2,1),mar=c(5.2,4,0,0))plclust
2、(hc1,hang=-1)rc1=rcct.hclust(hc1,k=3,border=,lredn)plclust(hc2,hang=-1)re2=rect.hclust(hc2,k=3,border=HredM)par(opar)####绘出谱系图和聚类情况(重心法和Ward法)opar<-par(mfrow=c(2,1),mauc(524,0,0))plclust(hc3,hang=-1)re3=rect.hclust(hc3,k=3,border=HredM)plclust(hc4,hang=-1)re4=rect.hclust(hc4,
3、k=3,border=HredH)par(opar)####动态聚类法km<-kmeans(scale(x),3,nstart=35);kmsort(kmScluster)####因子分析y=read.delim("G:\j2机考试数据.txt",header=TRUE,row.names=1)R=cov(scale(y))fa<-factanal(factors=4,covmat=R);fa####计算因子得分y=read.delim("G:\上机考试数据.txt",header=TRUE,row.names=l)fa<-factanal(
4、~.,factors=4,data=y,scores="Bartlett");fafa$scores####输出因了得分####画出散点图plot(fa$scorcs[,1:2],type="n")text(fa$scores[,1],fa$scores[,2])plot(fa$scores[,3:4],type=nn")text(fa$scores[,3],fa$scores[,4])####t检验al=fa$scores[J]a2=fa$scores[,2]a3=fa$scoresf,31a4=fa$scorcs[,4]t.test(a1,a
5、2,alternative』greater”)t.test(a1,a3,altemative=Hgreatern)t.test(a1,a4,alternative=Hgreatern)t・test(a2,a3,altemative=°'greateF‘)t.test(a2,a4,altemative=,lgreaterH)t.test(a3,a4,alternative』greater”)程序运行结果:>rm(list=ls(all=TRUE))>#####读入数据>x=read.delim(nG:\上机考试数据.txt'*,header=TR
6、UE,row.names=1)>####作系统聚类>d=dist(scale(x))>hcl=hclust(d);hclCall:hclust(d=d)Clustermethod:completeDistance:euclideanNumberofobjects:35>hc2=hclust(d,MaverageH)>hc3=hclust(d,nccntroid,f)>hc4=hclust(d/'ward")>####绘出谱系图和聚类情况(最长距离法、类平均法)>opar=par(mfrow=c(2,1),mar=c(5.2,4,0,0))>plc
7、lust(hc1?hang=-1)>rc1=rcct.hclust(hc1,k=3,border=MredK)>plclust(hc2,hang=-1)>re2=rect.hclust(hc2,k=3,border=Mredn)>par(opar)>####绘出谱系图和聚类情况(重心法和Ward法)>opar<-par(mfrow=c(2,1),mar=c(5・2,4,0,0))>plclust(hc3,hang=-1)>re3=rect.hclust(hc3,k=3,border=MredM)>plclust(hc4,hang=-1)>re4=
8、rect.hclust(hc4,k=3,border=MredH)>par(opar)>####动态聚类法>km<-kmeans(sca