DAY21. ์ฃผ์ฑ๋ถ๋ถ์, ์์ธ๋ถ์
์ฃผ์ฑ๋ถ๋ถ์
01. ๊ณ ์ ๊ฐ๊ณผ ๊ณ ์ ํ๋ ฌ
์ ๋ฐฉํ๋ ฌA(n, n)์ ๋ํด Av = λv๋ฅผ ๋ง์กฑํ ๋ (v:0์ด ์๋ ๊ณ ์ ๋ฒกํฐ, λ:์์ ๊ณ ์ ๊ฐ)
* ์ ๋ฐฉํ๋ ฌ? ๊ฐ์ ์์ ํ X ๊ฐ์ ์์ ์ด
* ๊ณ ์ ๊ฐ? ์์(scala)๊ฐ λ(๋๋ค)
์ ๋ฐฉํ๋ ฌA(n, n)๋ฅผ ์์ฝํ๋ ๊ฐ(=ํน์ด๊ฐ์ ๋ํ๋ด๋ ๊ฐ)
ํ๋ ฌ์์ ์ฐจ์(์ด:๋ณ์)์ ํน์ง
์์น์ ํฌ๊ธฐ๋ ํน์ง์ ๊ฐ๋
* ๊ณ ์ ๋ฒกํฐ? ๊ณ ์ ๊ฐ์ ํด๋นํ๋ 0์ด ์๋ ๋ฒกํฐ
* ์์ฉ๋ถ์ผ : PCA(์ฃผ์ฑ๋ถ๋ถ์), SVD(ํน์ด๊ฐ๋ถํด), Pseudo-Inverse(์ ์ฌ ์ญํ๋ ฌ), ์ ํ์ฐ๋ฆฝ๋ฐฉ์ ์ ๋ฑ
์ ํ๊ฒฐํฉ-์ ํ๋ณํ
์ ํ๋ณํ? n์ฐจ์์ ํ๋ ฌA๋ฅผ ๋ฒกํฐv์ ๊ณฑํ์ฌ(์ ํ๊ฒฐํฉํ์ฌ) 1์ฐจ์์ ๋ค๋ฅธ ๋ฒกํฐAv๋ก ๋ณํ(์ถ์)
ํ๋ ฌ๊ณฑ ์์ : Av = A %*% v
[์] Y = a1X1 + a2X2 + … +anXn
y = 1*3 + 2*0 = 3
y = 1*8 + 2*(-1) = 6
๊ณ ์ ๊ฐ๊ณผ ๊ณ ์ ๋ฒกํฐ
์ ํ๋ณํ์ ์ํด ๋ง๋ค์ด์ง Av๋ฅผ ๋ฒกํฐv๋ก ๋ง๋ค๊ธฐ ์ํด์ λ=3 ์ง์ ํ ๋, ๋ค์ ์์ด ์ฑ๋ฆฝ
λ = 3 (ํ๋ ฌA์ ๊ณ ์ ๊ฐ)
Av == λv (v : ํ๋ ฌA์ λ์ ๋ํ ๊ณ ์ ๋ฒกํฐ)
* ๋ฒกํฐv๋ฅผ ํ๋ ฌA๋ก ์ ํ๋ณํ ์ํจ ๊ฒฐ๊ณผ, Av๊ฐ ๋ฒกํฐv์ ์์(λ)๋ฐฐ๊ฐ ์ฑ๋ฆฝ ๋ ๋ ๊ณ ์ ๋ฒกํฐ ์กด์ฌ
๊ณ ์ ๊ฐ๊ณผ ๊ณ ์ ๋ฒกํฐ ๊ด๊ณ
๊ณ ์ ๋ฒกํฐ(v) : ๋ฐฉํฅ์ ์ ์ง๋๊ณ , ํฌ๊ธฐ๋ง ๋ณํ๋๋ ๋ฐฉํฅ๋ฒกํฐ
๊ณ ์ ๊ฐ(λ) : ๊ณ ์ ๋ฒกํฐ์ ๋ณํ๋๋ ํฌ๊ธฐ(scale)
02. ์ฃผ์ฑ๋ถ๋ถ์ vs ์์ธ๋ถ์
๊ณตํต์ | ์ฐจ์ด์ |
๋ณ์ ์ถ์ ๊ธฐ๋ฅ : ์๊ด๊ด๊ณ๊ฐ ์๋ ๋ณ์๋ค์ ์ ํ๊ฒฐํฉ | ๋ณ์ ํตํฉ ๊ธฐ์ค • ์ฃผ์ฑ๋ถ๋ถ์ : ์์น์ ์ธ ์๊ด์ฑ์ ๊ธฐ์ค์ผ๋ก ๋ณ์ ํตํฉ • ์์ธ๋ถ์ : ๊ฐ๋ ์ /๋ ผ๋ฆฌ์ ์ธ ์๊ด์ฑ์ ๊ธฐ์ค์ผ๋ก ๋ณ์ ํตํฉ |
๋ฐ์ดํฐ ํจํด ํ์ : ์ฃผ์ฑ๋ถ/์์ธ์ ํตํด์ ๋ณ์ ํน์ฑ ์ดํด | ๋ณ์ ์ถ์์ ๋ชฉ์ ๊ณผ ์์ฑ๋ ๋ณ์ ๊ฐ์ • ์ฃผ์ฑ๋ถ๋ถ์ : ํ์์ ๊ด์ ์ผ๋ก ๋ณดํต 2~3๊ฐ • ์์ธ๋ถ์ : ํ์ธ์ ๊ด์ ์ผ๋ก ์ฃผ์ด์ง ์์ ์์ธ์ ์ |
๋ค๋ฅธ ๋ถ์์ ์ํ ์ฌ์ ๋ถ์ : ํ๊ท๋ถ์์์ ๋ ๋ฆฝ๋ณ์ ๊ฐ ๋ค์ค๊ณต์ ์ฑ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์๊ด์ฑ์ด ๋์ ๋ณ์๋ฅผ ์ฃผ์ฑ๋ถ/์์ธ์ผ๋ก ์ถ์ | ๋ณ์ ๊ฐ์ ์ค์๋ • ์ฃผ์ฑ๋ถ๋ถ์ : ์ 1์ฃผ์ฑ๋ถ์ด ๋ถ์ฐ ๋ณ๋๋ ๊ฐ์ฅ ๋ง์ด ๊ฐ์ง๊ณ ์์(์ค์ํ ๋ณ์) • ์์ธ๋ถ์ : ๋ณ์๋ค์ ์ค์๋๋ ๋๋ฑํ ๊ด๊ณ |
์ ๋ ฅ๋ณ์ : ํ๊ท๋ถ์, ๊ตฐ์ง๋ถ์, ์๊ณ์ด๋ถ์ ๋ฑ์์ ์ ๋ ฅ๋ณ์ ์ฌ์ฉ |
03. ์ฃผ์ฑ๋ถ๋ถ์
๋ค๋ณ๋ ์๋ฃ ๋์ ์์น์ ์๊ด์ฑ์ด ์๋์ง ๊ด์ฐฐ, ‘์ฃผ์ฑ๋ถ’ ํตํฉ
๋ชจ๋ ๋ณ์๋ค์ ๊ณตํต์ ์ธ ์ ํ๋ณํ๋ฅผ ํตํด ์๊ด์ฑ ์๋ ๋ณ์๋ค ๊ฐ์ ์ ๋ณด ๋จ์ํ
๋ณดํต 2~3๊ฐ ์ ๋ ์ฑ๋ถ ์ถ์ถ, ์ 1์ฃผ์ฑ๋ถ์ ๋ณํ๋(์ค๋ช ๋ ฅ)์ด ๊ฐ์ฅ ํฌ๋ค. (์ค์๋๊ฐ ๊ฐ์ฅ ํผ)
์ฃผ์ฑ๋ถ์ ๋์ ๋ณํ๋ 85%์ ๋ ์ด์์ด๋ฉด ์ฃผ์ฑ๋ถ์ผ๋ก ๋์ถ
๋ค๋ณ๋ ์๋ฃ ๋ถ์
๋ค๋ณ๋ ์๋ฃ? ๋ ์ด์์ ์๋ก ์๊ด๊ด๊ณ์ ์๋ ๋ณ์๋ค์ ํฌํจํ๊ณ ์๋ ์๋ฃ
์ฃผ์ฑ๋ถ๋ถ์์ ๋ค๋ณ๋ ์๋ฃ๋ถ์ ๋ฐฉ๋ฒ ์ค ํ๋
์ฃผ์ฑ๋ถ๋ถ์ ํ์์ฑ
1) ์ฐจ์์ ์ ์ฃผ? ์ฐจ์์ด ์ฆ๊ฐํจ์ ๋ฐ๋ผ(=๋ณ์์ ์ ์ฆ๊ฐ) ๋ชจ๋ธ ์ฑ๋ฅ์ด ์ ์ข์์ง๋ ํ์
2) ๋ค์ค๊ณต์ ์ฑ(Multicollinearity) : ํ ๋ ๋ฆฝ๋ณ์์ ๊ฐ์ด ์ฆ๊ฐํ ๋ ๋ค๋ฅธ ๋ ๋ฆฝ๋ณ์์ ๊ฐ์ด ์ด์ ๊ด๋ จํ์ฌ ์ฆ๊ฐํ๊ฑฐ๋ ๊ฐ์ํ๋ ํ์ (ํ๊ท๋ถ์ ๊ฒฐ๊ณผ ์๊ณก)
3) ๊ณผ์ ํฉ(Overfitting) : ํ์ต๋ฐ์ดํ์ ๋ํด์๋ ์ค์ฐจ๊ฐ ๊ฐ์ํ์ง๋ง ์ค์ ๋ฐ ์ดํ์ ๋ํด์๋ ์ค์ฐจ๊ฐ ์ฆ๊ฐํ๋ ํ์
4) ์ฑ๋ฅ์ ํ : ๋ชจ๋ธ๋ง ๊ณผ์ ์์ ์ ์ฅ๊ณต๊ฐ๊ณผ ์ฒ๋ฆฌ์๊ฐ์ด ๋ถํ์ํ๊ฒ ์ฆ๊ฐ
์ ํ๋ณํ
์ฌ๋ฌ ๋ณ์๋ค์ ๋์์ผ๋ก ๊ฐ์ค๊ฒฐํฉ ์ํจ ํํ
n์ฐจ์์ ์ ๋ณด๋ฅผ ๊ฐ์ค์น a์ ๊ณฑํ์ฌ 1์ฐจ์์ผ๋ก ์ถ์ํ๋ ์ฐ์ฐ ๊ณผ์
๋ฐ์ดํฐ ํ์คํ
์ฃผ์ฑ๋ถ๋ถ์์ ์ธก์ ๋จ์์ ๋ฐ๋ผ์ ๋ถ์ฐ์ด ํฌ๊ฒ ๋ฌ๋ผ์ง
ํ์คํ ํ๋ ๊ฒฝ์ฐ | ํ์คํ ํ์ง ์๋ ๊ฒฝ์ฐ |
์ธก์ ๋จ์๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ ์๊ดํ๋ ฌ๋ก๋ถํฐ ์์ํ๋ ์ฃผ์ฑ๋ถ๋ถ์ |
์๋ฃ์ ๋จ์๊ฐ ๋์ผํ ๊ฒฝ์ฐ ๋ถ์ฐ๊ณต๋ถ์ฐ ํ๋ ฌ๋ก๋ถํฐ ์์ํ๋ ์ฃผ์ฑ๋ถ๋ถ์ |
ex. ๋ณ์ ์ค ํ๋๋ cm, ๋ค๋ฅธ ํ๋๋ kg์ธ ๊ฒฝ์ฐ ex. ๋ค๋ฅธ ํ ๋ณ์๋ 1์๋ฆฟ์, ๋ค๋ฅธ ๋ณ์๋ 3์๋ฆฟ์์ธ ๊ฒฝ์ฐ |
๋ณ์์ ๋จ์ ๊ทธ๋๋ก, ๋ณ๋ ๊ทธ๋๋ก ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ๋ชจ์ง๋จ์ ํน์ฑ์ ์ ๋๋ฌ๋ผ ์ ์๋ค. |
์ฃผ์ฑ๋ถ๋ถ์ ๋จ๊ณ
๋ฐ์ดํฐ ํน์ฑ ํ์ : ์๊ด๋ถ์์ผ๋ก ๋ณ์ ๊ฐ ๊ด๊ณ ํ์
๊ฐ์ค๊ณ์ ์ถ์ถ : ์๊ด๊ณ์ ํ๋ ฌ์ ํน์ง์ ๊ฐ์ง๊ณ ์๋ ๊ณ ์ ๊ฐ๊ณผ ๊ณ ์ ๋ฒกํฐ ์ถ์ถ
์ฐจ์์ถ์ : ํ์คํ๋ ๋ฐ์ดํฐ์ ๋์์ผ๋ก ์ฃผ์ฑ๋ถ ๋ถ์
์ฃผ์ฑ๋ถ ํ์ : ์ฃผ์ฑ๋ถ ๊ฐ์ ํ์ ๋ฐ ์ ๋ ฅ๋ณ์ ์์ฑ
[์ค์ต]
์ค์ต ๋ชฉ์ : 6๊ฐ ๊ณผ๋ชฉ์ ํน์ง์ ๋์์ผ๋ก ์ฃผ์ฑ๋ถ๋ถ์ ์ํ -> ์ ์ฌ ๊ณผ๋ชฉ ํ์
๋ณ์ ์ค๋ช : 6๊ฐ ๊ณผ๋ชฉ์ ๋ํ 10๊ฐ์ ํน์ง ์์นํ(5์ ์ฒ๋)
s1 : ์์ฐ๊ณผํ, s2 : ๋ฌผ๋ฆฌํํ
s3 : ์ธ๋ฌธ์ฌํ, s4 : ์ ๋ฌธ๋ฐฉ์ก
s5 : ์์ฉ์ํ, s6 : ์ถ๋ก ํต๊ณ
s1 <- c(1, 2, 1, 2, 3, 4, 2, 3, 4, 5)
s2 <- c(1, 3, 1, 2, 3, 4, 2, 4, 3, 4)
s3 <- c(2, 3, 2, 3, 2, 3, 5, 3, 4, 2)
s4 <- c(2, 4, 2, 3, 2, 3, 5, 3, 4, 1)
s5 <- c(4, 5, 4, 5, 2, 1, 5, 2, 4, 3)
s6 <- c(4, 3, 4, 4, 2, 1, 5, 2, 4, 2)
name <-1:10
dataset ๊ฐ์ ธ์ค๊ธฐ : 1์ฐจ(iris), 2์ฐจ(data.frame)
dataset <- data.frame(s1, s2, s3, s4, s5, s6)
dataset
str(dataset) # 'data.frame': 10 obs. of 6 variables:
[๋จ๊ณ1] ๋ฐ์ดํฐ ํน์ฑ ํ์
: ๋ณ์์ ์๊ด์ฑ ๋ถ์
cor(dataset) # ์๊ด๊ณ์ํ๋ ฌ(์ ๋ฐฉํ๋ ฌ)
s1 s2 s3 s4 s5 s6
s1 1.00000000 0.86692145 0.05847768 -0.1595953 -0.5504588 -0.6262758
s2 0.86692145 1.00000000 0.06745441 -0.0240123 -0.6349581 -0.7968892
s3 0.05847768 0.06745441 1.00000000 0.9239433 0.3506967 0.4428759
s4 -0.15959528 -0.02401230 0.92394333 1.0000000 0.4207582 0.4399890
s5 -0.55045878 -0.63495808 0.35069667 0.4207582 1.0000000 0.8733514
s6 -0.62627585 -0.79688923 0.44287589 0.4399890 0.8733514 1.0000000
* s1:s2, s3:s4, s5:s6 ์๊ด์ฑ ๋์
[๋จ๊ณ2] ๊ฐ์ค๊ณ์ ์ถ์ถ : ๊ณ ์ ๊ฐ๊ณผ ๊ณ ์ ๋ฒกํฐ : chap12_1_Eigenvalue ์ฐธ๊ณ
๊ณ ์ ๊ฐ(λ)
en <- eigen(cor(dataset)) # ์๊ด๊ณ์ํ๋ ฌ -> ๊ณ ์ ๊ฐ,๊ณ ์ ๋ฒกํฐ
names(en) # "values" "vectors"
en$values # $values : ๊ณ ์ ๊ฐ ๋ณด๊ธฐ(๋ณ์ ๊ฐ์์ ์ผ์น)
# 3.44393944 1.88761725 0.43123968 0.19932073 0.02624961 0.01163331
* - ๊ณ ์ ๊ฐ์ด๋ ์ด๋ค ํ๋ ฌ(์๊ด๊ด๊ณ์ ํ๋ ฌ)๋ก๋ถํฐ ์ ๋๋๋ ํน์ ํ ์์๊ฐ
๊ณ ์ ๊ฐ ์ ์ฒดํฉ = ์ด๋ถ์ฐ
sum(en$values) #6
๊ฐ ๋ถ์ฐ์ ๋น์จ = ๊ณ ์ ๊ฐ / ์ด๋ถ์ฐ
en$values / sum(en$values)
0.573989906 0.314602874 0.071873280 0.033220121 0.004374934 0.001938885
0.573989906 + 0.314602874 # 0.8885928
0.573989906 + 0.314602874 + 0.071873280 # 0.9604661
en$vectors # ๊ณ ์ ๋ฒกํฐ
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.4062499 -0.351093036 0.63460534 0.3149622 0.45699508 0.03041553
[2,] -0.4319311 -0.400526644 0.11564711 -0.4422216 -0.57042232 0.34452594
[3,] 0.2542077 -0.628807884 -0.06984072 0.3339036 -0.35389906 -0.54622817
[4,] 0.3017115 -0.566028650 -0.37734321 -0.2468016 0.50326085 0.36333366
[5,] 0.4763815 0.008436692 0.58035475 -0.6016209 0.05643527 -0.26654314
[6,] 0.5155637 0.021286661 0.31595023 0.4133867 -0.28995329 0.61559319
plot(en$values, type="o") # ๊ณ ์ ๊ฐ์ ์ด์ฉํ ์๊ฐํ(elbow point ๊ธฐ์ค)
[๋จ๊ณ3] ์ฐจ์์ถ์ : ์ฃผ์ฑ๋ถ๋ถ์
์๊ด๊ณ์๋ฅผ ์ ์ฉํ ๊ณ ์ ๋ฒกํฐ์ ๊ด์ธก์น ๊ฐ์ ์ ํ๊ฒฐํฉ : dataset์ ์๊ด๊ณ์ํ๋ ฌ์ ์ด์ฉํ์ฌ ์ฃผ์ฑ๋ถ๋ถ์ ์ํ
PCA <- prcomp(dataset, center = TRUE, scale. = TRUE) # ๋ณ์๋ค์ ๋จ์๊ฐ ๋ค๋ฅธ๊ฒฝ์ฐ ์ ์ฉ
# center = TRUE, scale. = TRUE -> ํ๊ท =0, ๋ถ์ฐ=1
PCA # ์ฃผ์ฑ๋ถ์ ๋ถ์ฐ๊ณผ ํ์ ๊ฐ
Standard deviations (1, .., p=6): ํ๋ถํธ์ฐจ(๋ถ์ฐ) = ๊ณ ์ ๊ฐ ์ ๊ณฑ๊ทผ
1.8557854 1.3739058 0.6566884 0.4464535 0.1620173 0.1078578
sqrt(3.44393944) # 1.855785
# Rotation (n x k) = (6 x 6): ๊ณ ์ ๋ฒกํฐ ๋ถํธ ํ์
์ฃผ์ฑ๋ถ๋ถ์์ ์์ฑ
names(PCA) # "sdev" "rotation" "center" "scale" "x"
# sdev, rotation, x ์์ฑ์ด ์ค์ํจ
1) ๊ฐ ์ฃผ์ฑ๋ถ์ ๋ถ์ฐ
PCA$sdev # 1.8557854 1.3739058 0.6566884 0.4464535 0.1620173 0.1078578
2) ๊ฐ ์ฃผ์ฑ๋ถ์ Rotation ๊ฐ
PCA$rotation
3) x : ์ฃผ์ฑ๋ถ ์ ์
str(PCA$x) # num [1:10, 1:6] -> [๊ด์ธก์น, ๋ณ์]
PCA$x
round(PCA$x[,1:3],2)
# PC1 PC2 PC3
#[1,] -1.2195113 -2.0459190 0.2058941
#[2,] -0.8619728 0.4960040 0.0733061
#[3,] -1.2195113 -2.0459190 0.2058941
#[4,] -1.3831687 -0.3387559 -0.3876923
#[5,] 1.5989191 -0.7852011 0.3581558
#[6,] 2.5004922 0.9502748 0.8197018
#[7,] -2.7991446 1.8549340 0.1375841
#[8,] 1.4637914 0.6653457 0.6438323
#[9,] -0.5785476 1.6426768 -0.6461737
#[10,] 2.4986537 -0.3934403 -1.4105023
[๋จ๊ณ4] ์ฃผ์ฑ๋ถ ๊ฐ์ ๊ฒฐ์
summary(PCA) #์ฃผ์ฑ๋ถ ๋ถ์์ ๋ํ ๊ฒฐ๊ณผ ์์ฝ ์ค๋ช
#Importance of components:
# PC1 PC2 PC3 PC4 PC5 PC6
#Standard deviation 1.856 1.3739 0.65669 0.44645 0.16202 0.10786
#Proportion of Variance 0.574 0.3146 0.07187 0.03322 0.00437 0.00194
#Cumulative Proportion 0.574 0.8886 0.96047 0.99369 0.99806 1.00000
* Standard deviation : ํ์คํธ์ฐจ
* Propertion of Variance : ๋ถ์ฐ๋น์จ, ๊ฐ ์ฃผ์ฑ๋ถ์ด ์ฐจ์งํ๋ ๋น์จ
* Cumulative Proportion : ๋ถ์ฐ์ ๋์ ํฉ๊ณ(85%์ด์์ธ ์ฃผ์ฑ๋ถ๊น์ง ์ ํ)
* ์คํฌ๋ฆฌ ํ๋กฏ : ์ฃผ์ฑ๋ถ ๊ฐ์๋ฅผ ์ ํํ ์ ์๋ ๊ทธ๋ํ(Elbow Point : ์๋งํด์ง๊ธฐ ์ด์ ์ ํ)
plot(PCA, type="l", sub = "Scree Plot") #3
biplot(PCA) # ํ๋ ฌ๋(biplot)
* - ์๋ณ์์ PCA๊ฐ์ ๊ด๊ณ๋ฅผ ๊ทธ๋ํ๋ก ํํ(์ฐจ์ ์ถ์ ์ ๋ณด ์ ๊ณต)
* - ํ์ดํ๋ ์๋ณ์์ PC์ ์๊ด๊ณ์. PC์ ํํํ ์๋ก ํด๋น PC์ ํฐ ์ํฅ
3์ฐจ์ ์๊ฐํ : ์ฃผ์ฑ๋ถ 3๊ฐ ์๊ฐํ
install.packages('scatterplot3d')
library(scatterplot3d)
์ฃผ์ฑ๋ถ 3๊ฐ ์ ์ ์ถ์ถ
PC1 <- PCA$x[,1]
PC2 <- PCA$x[,2]
PC3 <- PCA$x[,3]
scatterplot3d(๋ฐ๋ณ, ์ค๋ฅธ์ชฝ๋ณ, ์ผ์ชฝ๋ณ, type='p') # type='p' : ๊ธฐ๋ณธ์ฐ์ ๋ ํ์
d3 <- scatterplot3d(PC1, PC2, PC3)
๊ฐ ์ฃผ์ฑ๋ถ์ rotation๊ฐ(๊ณ ์ ๋ฒกํฐ)
rotation1 <- PCA$rotation[,1]
rotation2 <- PCA$rotation[,2]
rotation3 <- PCA$rotation[,3]
d3$points3d(rotation1, rotation2, rotation3, bg='red',pch=21, cex=2, type='h')
[๋จ๊ณ5] ์ ๋ ฅ๋ณ์ ๋ง๋ค๊ธฐ
str(PCA$x[,1:3]) #matrix -> DataFrame
new_dataset <- data.frame(PCA$x[,1:3])
str(new_dataset) #'data.frame': 10 obs. of 3 variables:
names(new_dataset) <- c('app_science','soc_science','net_science')
new_dataset
# app_science soc_science net_science
#1 -1.2195113 -2.0459190 0.2058941
#2 -0.8619728 0.4960040 0.0733061
#3 -1.2195113 -2.0459190 0.2058941
#4 -1.3831687 -0.3387559 -0.3876923
#5 1.5989191 -0.7852011 0.3581558
#6 2.5004922 0.9502748 0.8197018
#7 -2.7991446 1.8549340 0.1375841
#8 1.4637914 0.6653457 0.6438323
#9 -0.5785476 1.6426768 -0.6461737
#10 2.4986537 -0.3934403 -1.4105023
04. ์์ธ๋ถ์
์ ์ฒด ๋ณ์๋ค ์ค์์ ๊ฐ๋ ์ /๋ ผ๋ฆฌ์ ์ผ๋ก ์ฃผ์ ๊ฐ ๋น์ทํ ๋ณ์๋ค์ ์ ์ฌ์ ์์ธ์ผ๋ก ํตํฉ (ํ๋น์ฑ ๋ถ์)
๊ณตํต ์ฐจ์์ผ๋ก ์ถ์ฝํ๋ ํต๊ณ๊ธฐ๋ฒ(๋ณ์ ์ถ์)
๋์ถ๋๋ ์์ธ์ ๊ฐ์๋ ์ ํ ์์
๋์ ์ค๋ช ๋ ฅ์ ํฉ์ด 85% ์ด์ ์ผ ๋ ์ ์ ์์ธ๊ฐ์ ํ๋จ
ํ์ธ์ ์์ธ๋ถ์ : ์ฌ์ ์ ๋ฌถ์ฌ์ง ๊ฒ์ผ๋ก ๊ธฐ๋๋๋ ํญ๋ชฉ ๋ผ๋ฆฌ ๋ฌถ์ฌ ์ง๋์ง๋ฅผ ๋ถ์ํ๋ ๋ฐฉ๋ฒ(์ฃผ๋ก ์ฌ์ฉํ๋ ๋ถ์)
ํ์์ ์์ธ๋ถ์ : ์ฌ์ ์ ์ด๋ค ๋ณ์๋ค๋ผ๋ฆฌ ๋ฌถ์ด์ผ ํ๋ค๋ ์ ์ ๋ฅผ ๋์ง ์๊ณ ๋ถ์ํ๋ ๋ฐฉ๋ฒ
์์ธ๋ถ์ ์ ์ ์กฐ๊ฑด
ํ์์์ธ์ผ๋ก ๊ตฌ์ฑ๋๋ ๋ฐ์ดํฐ ์ ์ด ์ค๋น๋์ด ์์ด์ผ ํ๋ค.
๋ถ์์ ์ฌ์ฉ๋๋ ๋ณ์๋ ๋ฑ๊ฐ์ฒ๋๋ ๋น์จ์ฒ๋์ด์ฌ์ผ ํ๋ฉฐ, ํ๋ณธ์ ํฌ๊ธฐ๋ ์ต์ 30~50๊ฐ ์ด์์ด ๋ฐ๋์งํ๋ค. (์ค์ฌ๊ทนํ์ ๋ฆฌ)
์์ธ๋ถ์์ ์๊ด๊ด๊ณ๊ฐ ๋์ ๋ณ์๋ค๋ผ๋ฆฌ ๊ทธ๋ฃนํํ๋ ๊ฒ์ด๋ฏ๋ก ๋ณ์๋ค ๊ฐ์ ์๊ด๊ด๊ณ๊ฐ ๋งค์ฐ ๋ฎ๋ค๋ฉด(๋ณดํต ±3 ์ดํ) ๊ทธ ์๋ฃ๋ ์์ธ ๋ถ์์ ์ ํฉํ์ง ์๋ค.
์์ธ๋ถ์์ ๋ชฉ์
1) ์ธก์ ๋๊ตฌ ํ๋น์ฑ ๊ฒ์ฆ : ๋ณ์ธ๋ค์ด ๋์ผํ ์์ธ์ผ๋ก ๋ฌถ์ด๋์ง?
2) ์๋ฃ ์์ฝ : ๋ณ์ธ์ ๋ช ๊ฐ์ ๊ณตํต๋ ๋ณ์ธ์ผ๋ก ๋ฌถ์(์ฐจ์ ์ถ์)
3) ๋ณ์ธ ๊ตฌ์กฐ ํ์ : ๋ณ์ธ๋ค์ ์ํธ๊ด๊ณ ํ์ (๋ ๋ฆฝ์ฑ ๋ฑ)
4) ๋ถํ์ํ ๋ณ์ธ ์ ๊ฑฐ : ์ค์๋๊ฐ ๋จ์ด์ง ๋ณ์ ์ ๊ฑฐ
์์ธ๋ถ์ ๋จ๊ณ
1) ๋ฐ์ดํฐ ํน์ฑ ํ์ : ์๊ด๋ถ์์ผ๋ก ๋ณ์ ๊ฐ ๊ด๊ณ ํ์
2) ์์ธ ์ ๊ฒฐ์ : ์๊ด๊ณ์ ํ๋ ฌ์ ๋ํ ๊ณ ์ ๊ฐ์ผ๋ก ์์ธ์ ๊ฒฐ์
3) ์์ธ๋ถ์ : ํ์คํ๋ ๋ฐ์ดํฐ์ ๋์์ผ๋ก ์์ธ๋ถ์
4) ์์ธํ์ : ์์ธ ๊ฐ์ ํ์ ๋ฐ ์ ๋ ฅ๋ณ์ ์์ฑ
[์ค์ต]
์ค์ต ๋ชฉ์ : 6๊ฐ ๊ณผ๋ชฉ ์ ์(ํน์ง)๋ฅผ ๋์์ผ๋ก ๊ณตํต์์ธ์ ์ฐพ์์ ๊ณผ๋ชฉ ํตํฉ
๋ณ์ ์ค๋ช : 6๊ฐ ๊ณผ๋ชฉ์ ์ ์(5์ ๋ง์ = 5์ ์ฒ๋)
s1 : ์์ฐ๊ณผํ, s2 : ๋ฌผ๋ฆฌํํ
s3 : ์ธ๋ฌธ์ฌํ, s4 : ์ ๋ฌธ๋ฐฉ์ก
s5 : ์์ฉ์ํ, s6 : ์ถ๋ก ํต๊ณ
s1 <- c(1, 2, 1, 2, 3, 4, 2, 3, 4, 5)
s2 <- c(1, 3, 1, 2, 3, 4, 2, 4, 3, 4)
s3 <- c(2, 3, 2, 3, 2, 3, 5, 3, 4, 2)
s4 <- c(2, 4, 2, 3, 2, 3, 5, 3, 4, 1)
s5 <- c(4, 5, 4, 5, 2, 1, 5, 2, 4, 3)
s6 <- c(4, 3, 4, 4, 2, 1, 5, 2, 4, 2)
name <-1:10
subject <- data.frame(s1, s2, s3, s4, s5, s6)
subject
str(subject)
[๋จ๊ณ1] ๋ฐ์ดํฐํน์ฑ ํ์
cor(subject)
[๋จ๊ณ2] ์์ธ์ ๊ฒฐ์ : ๊ณ ์ ๊ฐ ์ด์ฉ
en <- eigen(cor(subject)) # ์๊ด๊ณ์ ํ๋ ฌ -> ๊ณ ์ ๊ฐ ๊ณ์ฐ
names(en) #"values" "vectors"
plot(en$values, type="o") # ๊ณ ์ ๊ฐ์ ์ด์ฉํ ์๊ฐํ(elbow point:1~3 ๊ธ๊ฒฉํ ๊ฐ์, 4 ์๋ง)
[๋จ๊ณ3] ์์ธ๋ถ์
์์ธ์ 3 ์ง์
FA <- factanal(scale(subject), factors = 3, #ํ์คํ๋ dataset์ ํ ๋๋ก ํจ, factors:์์ธ ๊ฐ์ ์ง์
rotation = "varimax", #ํ์ ๋ฐฉ๋ฒ ์ง์ ("varimax", "promax", "none")
scores="regression") #์์ธ์ ์ ๊ณ์ฐ ๋ฐฉ๋ฒ
* ์์ธํ์ ๋ฒ : ์ด๋ค ๋ณ์๊ฐ ์ด๋ค ์์ธ์ ์ํ๋์ง๋ฅผ ๊ฒน์ณ ๋ณด์ด์ง ์๊ฒ๋ ์์ธ์ถ์ ํ์ ์์ผ ์ถ๋ ฅ
FA
* Uniquenesses : ๋ณ์์ ์ ์ผ์ฑ ํต์ -0.5 ์ดํ
* Loadings : Factor1 Factor2 Factor3์ ๊ฐ์ง๊ณ ์๋ ์์ธ ์ ์ฌ๋ (-0.4 ~ + 0.4 ์์ ๋ค์ด๊ฐ ์์ผ๋ฉด ์๋ฏธ์๋ ์ ์ฌ๋)
Factor1 Factor2 Factor3
SS loadings 2.122 2.031 1.486 : ๊ฐ ์์ธ์ ์ ๊ณฑ์ ํฉ
Proportion Var 0.354 0.339 0.248 : ๊ฐ ์์ธ์ ๋ํ ๋ถ์ฐ์ ๋น์จ
Cumulative Var 0.354 0.692 0.940 : ๋์ ๋ถ์ฐ ๋น์จ (0.940์ด ์ต์ข
๋์ ๋ถ์ฐ ๋น์จ)
* ์ ๋ณด์์ค:1-0.940(๋์ ๋ถ์ฐ๋น์จ) = 0.06 (์ ์ฒด ๋ฐ์ดํฐ ์ค ์ฝ 6% ์ ๋ณด ์์ค)
* ์ ๋ณด์์ค์ด ํด์๋ก ์์ธ๋ถ์์ ์๋ฏธ๊ฐ ์๋ค.
[๋จ๊ณ4] ์์ธํ์
์์ธ์ ์ฌ๋ ๋ณด๊ธฐ
names(FA) # loadings, uniquenesses, scores.. 12๊ฐ์ ์นผ๋ผ ํธ์ถ
load <- FA$loadings
load
์์ธ๋ถํ๋ 0.4 ์ด์, ์์์ 2์๋ฆฌ ํ๊ธฐ
print(load, digits = 2, cutoff=0.4)
[ํด์ค] Factor1 : s5,s6(๋์ผ๋ถํธ) / Factor2 : s3,s4 / Factor3 : s1, s2
๋ชจ๋ ์์ธ์ ์ฌ๋ ๋ณด๊ธฐ :๊ฐ์ถ์ด์ง ์์ธ์ ์ฌ๋ ๋ณด๊ธฐ
print(load, cutoff=0) # display every loadings
[์์ธ์ ์๋ฅผ ์ด์ฉํ ์์ธ์ ์ฌ๋ ์๊ฐํ]
๋์์ ์ธ ๊ฐ ์ด์์ ์์ธ์ 2์ฐจ์ ์ฐ์ ๋๋ก ํํํ ์ ์์
1) Factor1, Factor2 ์์ธ์งํ ์๊ฐํ
์์ธ์ ์ํ๋ ฌ
plot(FA$scores[,c(1:2)], main="Factor1๊ณผ Factor2 ์์ธ์ ์ ํ๋ ฌ")
๊ด์ธก์น๋ณ ์ด๋ฆ ๋งคํ(rownames mapping)
text(FA$scores[,1], FA$scores[,2],
labels = name, cex = 0.7, pos = 3, col = "blue")
์์ธ์ ์ฌ๋ plotting
points(FA$loadings[,c(1:2)], pch=19, col = "red")
text(FA$loadings[,1], FA$loadings[,2],
labels = rownames(FA$loadings),
cex = 0.8, pos = 3, col = "red")
2) Factor1, Factor3 ์์ธ์งํ ์๊ฐํ
์์ธ์ ์ํ๋ ฌ
plot(FA$scores[,c(1,3)], main="Factor1๊ณผ Factor3 ์์ธ์ ์ ํ๋ ฌ")
๊ด์ธก์น๋ณ ์ด๋ฆ ๋งคํ(rownames mapping)
text(FA$scores[,1], FA$scores[,3],
labels = name, cex = 0.7, pos = 3, col = "blue")
์์ธ์ ์ฌ๋ plotting
points(FA$loadings[,c(1,3)], pch=19, col = "red")
* Factor1, Factor3 ์์ธ์ ์ฌ๋ ํ์
text(FA$loadings[,1], FA$loadings[,3],
labels = rownames(FA$loadings),
cex = 0.8, pos = 3, col = "red")
3์ฐจ์ ์๊ฐํ : ์์ธ์ 3๊ฐ
library(scatterplot3d)
Factor1 <- FA$scores[,1]
Factor2 <- FA$scores[,2]
Factor3 <- FA$scores[,3]
* scatterplot3d(๋ฐ๋ณ, ์ค๋ฅธ์ชฝ๋ณ, ์ผ์ชฝ๋ณ, type='p') # type='p' : ๊ธฐ๋ณธ์ฐ์ ๋ ํ์
d3 <- scatterplot3d(Factor1, Factor2, Factor3)
์์ธ์ ์ฌ๋ ํ์
loadings1 <- FA$loadings[,1]
loadings2 <- FA$loadings[,2]
loadings3 <- FA$loadings[,3]
d3$points3d(loadings1, loadings2, loadings3, bg='red',pch=21, cex=2, type='h')
์์ธ๋ถ์ : ํ์ธ์
ํ์ธ์ ์์ธ๋ถ์์ผ๋ก ์ฃผ๋ก ์ค๋ฌธ์ง ํ๋น์ฑ ๋ถ์์์ ์ด์ฉ๋๋ค.
์๋ชป ๋ถ๋ฅ๋ ์์ธ์ธ ๋ฐ๊ฒฌ๋ ๊ฒฝ์ฐ ํด๋น ๋ณ์๋ฅผ ์ ๊ฑฐํ ํ new dataset์ ์์ฑํ๋ค.
[๋จ๊ณ1] ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
install.packages('memisc') #spss(file)์ R(dataset)์ผ๋ก ๋ณํํด์ ์ฌ์ฉ ๊ฐ๋ฅ
library(memisc)
setwd('C:\\ITWILL\\2_Rwork\\data')
data.spss <- as.data.set(spss.system.file('drinking_water.sav'))
data.spss
drinking_water <- data.spss[1:11]
drinking_water
drinking_water_df <- as.data.frame(drinking_water)
str(drinking_water_df)
์น๋ฐ๋ : q1,q2,q3,q4
์ ์ ์ฑ : q5,q6,q7
๋ง์กฑ๋ : q8,q9,q10,q11
drinking_water_df = read.csv('drinking_water.csv')
[๋จ๊ณ2] ๊ณ ์ ๊ฐ์ผ๋ก ์์ธ ์ ํ์ธ [์๋ต : ์์ธ์ ์๊ณ ์๋ ๊ฒฝ์ฐ]
en <- eigen(cor(drinking_water_df))
plot(en$values, type="o")
[๋จ๊ณ3] ์์ธ๋ถ์
fact.result <- factanal(drinking_water_df, factors = 3,
rotation = "varimax",
scores = "regression")
fact.result
[๋จ๊ณ4] ์์ธํ์
์์ธ์ ์ฌ๋ ๋ณด๊ธฐ
load <- fact.result$loadings
์์ธ๋ถํ๋ 0.4 ์ด์, ์์์ 2์๋ฆฌ ํ๊ธฐ
print(load, digits = 2, cutoff=0.4)
๋ชจ๋ ์์ธ์ ์ฌ๋ ๋ณด๊ธฐ :๊ฐ์ถ์ด์ง ์์ธ์ ์ฌ๋ ๋ณด๊ธฐ
print(load, cutoff=0) # display every loadings
์๋ชป ๋ถ๋ฅ๋ ๋ณ์(Q4) ๋ฐ๊ฒฌ/์ ๊ฑฐ
new_dw = drinking_water_df[-4] #Q4์ ๊ฑฐ
dim(new_dw) #๋ณ์ 11๊ฐ->10๊ฐ ํ์ธ
์์ธ๋ถ์ (์ฌํ์ธ)
fact.result <- factanal(new_dw, factors = 3,
rotation = "varimax",
scores = "regression")
fact.result
names(fact.result2)
load = fact.result2$loadings
์์ธ๋ถํ๋ 0.4์ด์, ์์์ 2์๋ฆฌ ํฌ๊ธฐ
print(load, digits=2, cutoff=0.4)