データ解析基礎論B 因子分析＋クラスター分析

```source('http://peach.l.chiba-u.ac.jp/course_folder/cuUtil02.R')
dat.model1<-factanal(dat,1)
dat.model2<-factanal(dat,2)
dat.model3<-factanal(dat,3)
dat.model4<-factanal(dat,4)

library(sem)

model01=cfa(reference.indicator=FALSE)
F1:extrovert,cheerful, leadership, antisocial, talkative, motivated, hesitance, popularity

model02=cfa(reference.indicator=FALSE)
F2: cheerful, antisocial, talkative, popularity

mod2<-sem(model02, cov(dat), 100)
summary(mod2)
opt <- options(fit.indices = c("RMSEA"))

cldata<-data.frame(var1=c(4,1,5,1,5), var2=c(1,5,4,3,1))
cldata.cluster=hclust(dist(cldata),method="average")
plot(cldata.cluster,cex=2)

dat.cluster=hclust(dist(dat),method="average")
plot(dat.cluster,cex=1.5)

dat.kmeans=kmeans(dat, centers=3, nstart=10)
plot(dat,col=dat.kmeans\$cluster+2,pch=20,cex=2)

plot(dat[,1:2],col=dat.kmeans\$cluster+1,pch=20,cex=5)
text(dat[,1:2],row.names(dat),cex=2)

res<-cu.KMC.rep(dat,10,1000)

res<-cu.KMC.rep(dat,10,1000)

# MDS
dat<-data.frame(p1=c(4,1,5,1,5),p2=c(1,5,4,3,1))
rownames(dat)<-c('a','b','c','d','e')
dat.mds<-cmdscale(dist(dat),2)
plot(dat.mds[,1],dat.mds[,2], type='n')
text(dat.mds[,1],dat.mds[,2],labels=row.names(dat))
dat.cluster=hclust(dist(dat))
plot(dat.cluster,cex=1.5)