library(psych)
library(GPArotation)
# Import Data dari Packages Sandwich
data(Investment, package = "sandwich")
Investment <- as.data.frame(Investment[,1:6])
Investment
##       GNP Investment  Price Interest   RealGNP  RealInv
## 1   596.7       90.9 0.7167     3.23  832.5659 126.8313
## 2   637.7       97.4 0.7277     3.55  876.3227 133.8464
## 3   691.1      113.5 0.7436     4.04  929.3975 152.6358
## 4   756.0      125.7 0.7676     4.50  984.8880 163.7572
## 5   799.6      122.8 0.7906     4.19 1011.3838 155.3251
## 6   873.4      133.3 0.8254     5.16 1058.1536 161.4975
## 7   944.0      149.3 0.8679     5.87 1087.6829 172.0244
## 8   992.7      144.2 0.9145     5.95 1085.5112 157.6818
## 9  1077.6      166.4 0.9601     4.88 1122.3831 173.3153
## 10 1185.9      195.0 1.0000     4.50 1185.9000 195.0000
## 11 1326.4      229.8 1.0575     6.44 1254.2790 217.3050
## 12 1434.2      228.7 1.1508     7.83 1246.2635 198.7313
## 13 1549.2      206.1 1.2579     6.25 1231.5764 163.8445
## 14 1718.0      257.9 1.3234     5.50 1298.1714 194.8768
## 15 1918.3      324.1 1.4005     5.46 1369.7251 231.4174
## 16 2163.9      386.6 1.5042     7.46 1438.5720 257.0137
## 17 2417.8      423.0 1.6342    10.28 1479.5007 258.8422
## 18 2631.7      401.9 1.7842    11.77 1475.0028 225.2550
## 19 2954.1      474.9 1.9514    13.42 1513.8362 243.3637
## 20 3073.0      414.5 2.0688    11.02 1485.4022 200.3577
#Transformasi Normal Baku
zinvestment <- data.frame(scale(Investment))
zinvestment
##            GNP  Investment       Price    Interest    RealGNP    RealInv
## 1  -1.12491819 -1.14784022 -1.06818487 -1.15272905 -1.6976136 -1.5795163
## 2  -1.07311736 -1.09581120 -1.04239746 -1.04212236 -1.4945241 -1.4017030
## 3  -1.00564994 -0.96693932 -1.00512293 -0.87275588 -1.2481860 -0.9254396
## 4  -0.92365302 -0.86928486 -0.94885949 -0.71375877 -0.9906364 -0.6435429
## 5  -0.86856726 -0.89249780 -0.89494036 -0.82090899 -0.8676606 -0.8572743
## 6  -0.77532576 -0.80845092 -0.81335837 -0.48563248 -0.6505861 -0.7008206
## 7  -0.68612726 -0.68037949 -0.71372519 -0.24022389 -0.5135309 -0.4339895
## 8  -0.62459798 -0.72120226 -0.60448034 -0.21257222 -0.5236105 -0.7975376
## 9  -0.51733236 -0.54350315 -0.49757980 -0.58241333 -0.3524758 -0.4012698
## 10 -0.38050236 -0.31457546 -0.40404183 -0.71375877 -0.0576727  0.1483809
## 11 -0.20298976 -0.03602009 -0.26924401 -0.04320574  0.2596968  0.7137532
## 12 -0.06679148 -0.04482500 -0.05051988  0.43724205  0.2224942  0.2429600
## 13  0.07850353 -0.22572590  0.20055572 -0.10887846  0.1543268 -0.6413291
## 14  0.29177133  0.18890536  0.35410803 -0.36811287  0.4634161  0.1452589
## 15  0.54483734  0.71880092  0.53485433 -0.38193871  0.7955207  1.0714651
## 16  0.85513694  1.21907996  0.77795928  0.30935307  1.1150621  1.7202652
## 17  1.17592306  1.51044247  1.08271959  1.28407449  1.3050256  1.7666141
## 18  1.44617178  1.34154827  1.43436609  1.79908687  1.2841495  0.9152661
## 19  1.85350318  1.92587418  1.82633473  2.36940259  1.4643883  1.3742746
## 20  2.00372559  1.44240452  2.10155673  1.53985245  1.3324164  0.2841844
#Mengecek Bentuk Histogram dan Kurva
par(mfrow=c(1,3))
for (i in 1:3) {
  hist(zinvestment[,i], prob=TRUE, main=paste("x",i,sep = ""), xlab=paste("x",i,sep = ""))
  lines(density(zinvestment[,i]))
}

par(mfrow=c(1,3))
for (i in 4:6) {
  hist(zinvestment[,i], prob=TRUE, main=paste("x",i,sep = ""), xlab=paste("x",i,sep = ""))
  lines(density(zinvestment[,i]))
}

#Uji Kenormalan Menggunakan Shapiro-Wilks 
shapiro.test(zinvestment$GNP)
## 
##  Shapiro-Wilk normality test
## 
## data:  zinvestment$GNP
## W = 0.89545, p-value = 0.03391
shapiro.test(zinvestment$Investment)
## 
##  Shapiro-Wilk normality test
## 
## data:  zinvestment$Investment
## W = 0.88116, p-value = 0.01857
shapiro.test(zinvestment$Price)
## 
##  Shapiro-Wilk normality test
## 
## data:  zinvestment$Price
## W = 0.89299, p-value = 0.03054
shapiro.test(zinvestment$Interest)
## 
##  Shapiro-Wilk normality test
## 
## data:  zinvestment$Interest
## W = 0.8667, p-value = 0.01029
shapiro.test(zinvestment$RealGNP)
## 
##  Shapiro-Wilk normality test
## 
## data:  zinvestment$RealGNP
## W = 0.94756, p-value = 0.3316
shapiro.test(zinvestment$RealInv)
## 
##  Shapiro-Wilk normality test
## 
## data:  zinvestment$RealInv
## W = 0.94862, p-value = 0.3466
#Uji Multivariat Normal
library(MVN)
mvn(data = Investment,mvnTest ="mardia")
## $multivariateNormality
##              Test          Statistic            p value Result
## 1 Mardia Skewness   81.2646154516567 0.0153260019440204     NO
## 2 Mardia Kurtosis -0.298396533551169  0.765400534453533    YES
## 3             MVN               <NA>               <NA>     NO
## 
## $univariateNormality
##               Test   Variable Statistic   p value Normality
## 1 Anderson-Darling    GNP        0.7245    0.0493    NO    
## 2 Anderson-Darling Investment    0.9209    0.0154    NO    
## 3 Anderson-Darling   Price       0.7279    0.0483    NO    
## 4 Anderson-Darling  Interest     1.0673    0.0065    NO    
## 5 Anderson-Darling  RealGNP      0.3130    0.5204    YES   
## 6 Anderson-Darling  RealInv      0.3923    0.3449    YES   
## 
## $Descriptives
##             n       Mean     Std.Dev     Median      Min       Max      25th
## GNP        20 1487.06500 791.4931115 1256.15000 596.7000 3073.0000  854.9500
## Investment 20  234.30000 124.9302795  200.55000  90.9000  474.9000  131.4000
## Price      20    1.17235   0.4265647    1.02875   0.7167    2.0688    0.8167
## Interest   20    6.56500   2.8931343    5.68500   3.2300   13.4200    4.5000
## RealGNP    20 1198.32589 215.4553672 1208.73822 832.5659 1513.8362 1046.4612
## RealInv    20  189.14610  39.4518207  184.09606 126.8313  258.8422  160.5435
##                   75th        Skew   Kurtosis
## GNP        1979.700000  0.67604403 -0.9542482
## Investment  339.725000  0.58362449 -1.2362325
## Price         1.426425  0.70003321 -0.8708394
## Interest      7.552500  0.99777586 -0.2452418
## RealGNP    1386.936823 -0.02656484 -1.3519458
## RealInv     219.292477  0.29791744 -1.1619473
#Uji Korelasi dan Kecukupan Sampel "Barlett Test of Sphericity"
bartlett.test(zinvestment)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  zinvestment
## Bartlett's K-squared = -6.2013e-15, df = 5, p-value = 1
#Penentuan Jumlah Faktor
library(nFactors)
## Loading required package: lattice
## 
## Attaching package: 'nFactors'
## The following object is masked from 'package:lattice':
## 
##     parallel
R = cov(zinvestment)
eigen_data = eigen(R)
eigen_data
## eigen() decomposition
## $values
## [1] 5.5173791599 0.3300284791 0.1151570916 0.0361705689 0.0010496037
## [6] 0.0002150969
## 
## $vectors
##           [,1]        [,2]        [,3]        [,4]        [,5]        [,6]
## [1,] 0.4194705 -0.22343018  0.32000450  0.13760165  0.33486088  0.73529214
## [2,] 0.4230439  0.05771685  0.07624479  0.53240938 -0.72647463 -0.02577197
## [3,] 0.4155335 -0.28516498  0.41772067  0.06975269  0.33813839 -0.67254686
## [4,] 0.3931743 -0.48686777 -0.77062786 -0.11413025  0.02673516 -0.02767562
## [5,] 0.4165632  0.21444001  0.18394897 -0.80469596 -0.31224573  0.04025371
## [6,] 0.3798674  0.76315392 -0.29931383  0.17938590  0.38412637 -0.06305315
ap = parallel(subject = 20, var = 6, rep = 100, cent = 0.05)
nfaktor = nScree(eigen$values, ap$eigen$qevpea)
plotnScree(nfaktor)

#Analisis Faktor Menggunakan Metode Estimasi Principal Component
library(psych)
R = cov(zinvestment)
Solution_pa = fa(R, nfactors = 1, rotate = "varimax", fm = "pa")
## Warning in fa.stats(r = r, f = f, phi = phi, n.obs = n.obs, np.obs = np.obs, :
## The estimated weights for the factor scores are probably incorrect. Try a
## different factor score estimation method.
## Warning in fac(r = r, nfactors = nfactors, n.obs = n.obs, rotate = rotate, : An
## ultra-Heywood case was detected. Examine the results carefully
Solution_pa
## Factor Analysis using method =  pa
## Call: fa(r = R, nfactors = 1, rotate = "varimax", fm = "pa")
## Standardized loadings (pattern matrix) based upon correlation matrix
##             PA1   h2      u2 com
## GNP        0.99 0.98  0.0214   1
## Investment 1.00 1.00 -0.0033   1
## Price      0.98 0.95  0.0488   1
## Interest   0.90 0.81  0.1919   1
## RealGNP    0.98 0.96  0.0434   1
## RealInv    0.86 0.73  0.2659   1
## 
##                 PA1
## SS loadings    5.43
## Proportion Var 0.91
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## The degrees of freedom for the null model are  15  and the objective function was  20.19
## The degrees of freedom for the model are 9  and the objective function was  9.56 
## 
## The root mean square of the residuals (RMSR) is  0.03 
## The df corrected root mean square of the residuals is  0.04 
## 
## Fit based upon off diagonal values = 1