# 요인분석 : 변인의 갯수를 축소
#<실습>
#• 변수 설명 6개 과목의 점수 (5 점 만점 = 5 점 척도)
#• g1:물리,g2 :화학, g3:사회,g4:영어, g5:수학 g6:과학
g1 <- c(1, 2, 1, 2, 3, 4, 2, 3, 4, 5)
g2 <- c(1, 3, 1, 2, 3, 4, 2, 4, 3, 4)
g3 <- c(2, 3, 2, 3, 2, 3, 5, 3, 4, 2)
g4 <- c(2, 4, 2, 3, 2, 3, 5, 3, 4, 1)
g5 <- c(4, 5, 4, 5, 2, 1, 5, 2, 4, 3)
g6 <- c(4, 3, 4, 4, 2, 1, 5, 2, 4, 2)
subject <- data.frame(g1,g2,g3,g4,g5,g6)
subject
str(subject)
#주성분 분석으로 요인 수 알아보기
pr <- prcomp(subject)
pr
summary(pr)
plot(pr) #약 2~3개 주성분 변인
#고유값으로 요인수(총분산을 기준) 분석
en <- eigen(cor(subject))
names(en)
en$values #고유값 보기
en$vectors
plot(en$values, type = 'o') # 주성분 변수를 3개
# 상관관계로 확인
cor(subject) #g1:g2... 동일계열로 처리하는 것이 바람직
#요인 회전법
result <- factanal(subject, factors = 2, rotation = "varimax")
result
result <- factanal(subject, factors = 3, rotation = "varimax")
result
# 주성분 분석만으로도 요인 수 축소 판단
head(iris)
pcdata <- princomp(iris[, 1:4], cor=T, scores = T)
pcdata
plot(pcdata, type='l')
summary(pcdata)
install.packages("devtools")
library(devtools)
#install_github("ggbiplot", "vqv")
install_github("vqv/ggbiplot")
library(ggbiplot)
ggbiplot(pcdata, groups = iris$Species)
plot(pcdata$loadings[,1:2],
col=c('black','blue','red','green'),
pch=16)
legend('topleft',
c('Sepal.Length','Sepal.Width','Petal.Length','Petal.Width'),
text.col = c('black','blue','red','green'))
댓글 영역