패키지 설치하기
시뮬레이션으로 예제 데이터 만들기
library(psych)
Harman74.cor
$cov VisualPerception Cubes PaperFormBoard Flags VisualPerception 1.000 0.318 0.403 0.468 Cubes 0.318 1.000 0.317 0.230 PaperFormBoard 0.403 0.317 1.000 0.305
psych
는 심리학에서 많이 쓰이는 패키지library(MASS)
iq = mvrnorm(145, Harman74.cor$center, Harman74.cor$cov)
iq = iq * 15 + 50
iq = round(iq)
주성분 분석
p = prcomp(iq, scale. = T)
summary(p)
Importance of components: PC1 PC2 PC3 PC4 PC5 PC6 PC7 Standard deviation 3.0026 1.47007 1.27909 1.18395 1.06461 0.96679 0.94400 Proportion of Variance 0.3756 0.09005 0.06817 0.05841 0.04723 0.03895 0.03713 Cumulative Proportion 0.3756 0.46569 0.53386 0.59227 0.63950 0.67844 0.71557 PC8 PC9 PC10 PC11 PC12 PC13 PC14 Standard deviation 0.92006 0.81764 0.79781 0.77208 0.72423 0.71656 0.68450 Proportion of Variance 0.03527 0.02786 0.02652 0.02484 0.02185 0.02139 0.01952 Cumulative Proportion 0.75084 0.77870 0.80522 0.83006 0.85191 0.87331 0.89283 PC15 PC16 PC17 PC18 PC19 PC20 PC21 Standard deviation 0.62122 0.59741 0.58146 0.57014 0.53200 0.5138 0.47465 Proportion of Variance 0.01608 0.01487 0.01409 0.01354 0.01179 0.0110 0.00939 Cumulative Proportion 0.90891 0.92378 0.93787 0.95141 0.96320 0.9742 0.98359 PC22 PC23 PC24 Standard deviation 0.3980 0.35138 0.33465 Proportion of Variance 0.0066 0.00514 0.00467 Cumulative Proportion 0.9902 0.99533 1.00000
주성분 수 결정하기
plot(p)
biplot(p)
psych
라이브러리의 principal
함수로 주성분 분석
principal(iq, 3)
Principal Components Analysis Call: principal(r = iq, nfactors = 3) Standardized loadings (pattern matrix) based upon correlation matrix RC2 RC1 RC3 h2 u2 com VisualPerception 0.02 0.31 0.70 0.58 0.42 1.4 Cubes 0.11 0.08 0.61 0.40 0.60 1.1 PaperFormBoard 0.10 0.06 0.63 0.41 0.59 1.1 Flags 0.16 0.14 0.47 0.26 0.74 1.4 GeneralInformation 0.71 0.38 0.15 0.67 0.33 1.6 PargraphComprehension 0.80 0.14 0.16 0.69 0.31 1.1 SentenceCompletion 0.84 0.12 0.04 0.72 0.28 1.0 WordClassification 0.70 0.24 0.20 0.58 0.42 1.4 WordMeaning 0.81 0.10 0.13 0.69 0.31 1.1 Addition 0.32 0.79 -0.06 0.73 0.27 1.3 Code 0.33 0.71 0.19 0.64 0.36 1.6 CountingDots -0.04 0.76 0.21 0.62 0.38 1.2 StraightCurvedCapitals 0.22 0.62 0.35 0.55 0.45 1.9 WordRecognition 0.46 0.12 0.29 0.31 0.69 1.9 NumberRecognition 0.10 0.27 0.41 0.25 0.75 1.9 FigureRecognition 0.17 0.17 0.67 0.50 0.50 1.3 ObjectNumber 0.10 0.62 0.11 0.41 0.59 1.1 NumberFigure 0.14 0.67 0.40 0.63 0.37 1.7 FigureWord 0.17 0.51 0.36 0.42 0.58 2.1 Deduction 0.52 0.09 0.45 0.49 0.51 2.0 NumericalPuzzles 0.30 0.45 0.49 0.53 0.47 2.7 ProblemReasoning 0.45 0.20 0.48 0.48 0.52 2.3 SeriesCompletion 0.41 0.33 0.59 0.62 0.38 2.4 ArithmeticProblems 0.47 0.56 0.30 0.63 0.37 2.5 RC2 RC1 RC3 SS loadings 4.56 4.35 3.90 Proportion Var 0.19 0.18 0.16 Cumulative Var 0.19 0.37 0.53 Proportion Explained 0.36 0.34 0.30 Cumulative Proportion 0.36 0.70 1.00 Mean item complexity = 1.6 Test of the hypothesis that 3 components are sufficient. The root mean square of the residuals (RMSR) is 0.07 with the empirical chi square 387.48 with prob < 4.3e-13 Fit based upon off diagonal values = 0.96
prcomp
보다 더 많은 정보를 줌PCA 후 oblimin 회전
p = principal(iq, 3, rotate = 'oblimin')
p
Loading required namespace: GPArotation
Principal Components Analysis Call: principal(r = iq, nfactors = 3, rotate = "oblimin") Standardized loadings (pattern matrix) based upon correlation matrix TC2 TC1 TC3 h2 u2 com VisualPerception -0.13 0.19 0.71 0.58 0.42 1.2 Cubes 0.02 -0.07 0.65 0.40 0.60 1.0 PaperFormBoard 0.02 -0.09 0.67 0.41 0.59 1.0 Flags 0.09 0.03 0.47 0.26 0.74 1.1 GeneralInformation 0.67 0.28 0.00 0.67 0.33 1.3 PargraphComprehension 0.82 -0.01 0.04 0.69 0.31 1.0 SentenceCompletion 0.88 -0.01 -0.09 0.72 0.28 1.0 WordClassification 0.68 0.11 0.09 0.58 0.42 1.1 WordMeaning 0.84 -0.05 0.02 0.69 0.31 1.0 Addition 0.19 0.84 -0.24 0.73 0.27 1.3 Code 0.18 0.70 0.04 0.64 0.36 1.1 CountingDots -0.23 0.80 0.11 0.62 0.38 1.2 StraightCurvedCapitals 0.06 0.58 0.25 0.55 0.45 1.4 WordRecognition 0.43 -0.01 0.24 0.31 0.69 1.6 NumberRecognition 0.00 0.20 0.39 0.25 0.75 1.5 FigureRecognition 0.07 0.01 0.68 0.50 0.50 1.0 ObjectNumber -0.03 0.65 0.00 0.41 0.59 1.0 NumberFigure -0.04 0.64 0.30 0.63 0.37 1.4 FigureWord 0.03 0.46 0.29 0.42 0.58 1.7 Deduction 0.49 -0.09 0.41 0.49 0.51 2.0 NumericalPuzzles 0.17 0.34 0.42 0.53 0.47 2.3 ProblemReasoning 0.38 0.04 0.44 0.48 0.52 2.0 SeriesCompletion 0.30 0.16 0.53 0.62 0.38 1.8 ArithmeticProblems 0.35 0.49 0.17 0.63 0.37 2.1 TC2 TC1 TC3 SS loadings 4.48 4.33 4.01 Proportion Var 0.19 0.18 0.17 Cumulative Var 0.19 0.37 0.53 Proportion Explained 0.35 0.34 0.31 Cumulative Proportion 0.35 0.69 1.00 With component correlations of TC2 TC1 TC3 TC2 1.00 0.39 0.33 TC1 0.39 1.00 0.41 TC3 0.33 0.41 1.00 Mean item complexity = 1.4 Test of the hypothesis that 3 components are sufficient. The root mean square of the residuals (RMSR) is 0.07 with the empirical chi square 387.48 with prob < 4.3e-13 Fit based upon off diagonal values = 0.96
principal
의 PC1, PC2, PC3의 순서가 달라졌음oblimin 회전시킨 PCA로 부분 점수 보기
p$scores
TC2 TC1 TC3 [1,] 0.12333655 -0.69805055 -0.284939214 [2,] 1.62237932 1.66824266 2.935444560 [3,] -1.65610724 -1.61380091 -0.528123212 [4,] 1.27483112 0.95287140 -1.338265172