# UNIVERSIDAD NACIONAL DEL ALTIPLANO PUNO
# INGENIERIA ESTADISTICA E INFORMATICA
# TECNICAS ESTADISTICAS MULTIVARIADAS

# ANALISIS DE COMPONENTES PRINCIPALES
library(FactoMineR)
## Warning: package 'FactoMineR' was built under R version 4.1.3
library(psych)
## Warning: package 'psych' was built under R version 4.1.3
library(shiny)
## Warning: package 'shiny' was built under R version 4.1.3
library(Factoshiny)
## Warning: package 'Factoshiny' was built under R version 4.1.3
## Loading required package: FactoInvestigate
## Warning: package 'FactoInvestigate' was built under R version 4.1.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.3
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
library(colourpicker)
## Warning: package 'colourpicker' was built under R version 4.1.3
## 
## Attaching package: 'colourpicker'
## The following object is masked from 'package:shiny':
## 
##     runExample
datos <- read.csv("Estudiantes.csv", header = T, sep = ";", row.names = 1)
datos
##        Matematicas Ciencias Español Historia EdFidica
## Lucia          7.0      6.5     9.2      8.6      8.0
## Pedro          7.5      9.4     7.3      7.0      7.0
## Ines           7.6      9.2     8.0      8.0      7.5
## Luis           5.0      6.5     6.5      7.0      9.0
## Andres         6.0      6.0     7.8      8.9      7.3
## Ana            7.8      9.6     7.7      8.0      6.5
## Carlos         6.3      6.4     8.2      9.0      7.2
## Jose           7.9      9.7     7.5      8.0      6.0
## Sonia          6.0      6.0     6.5      5.5      8.7
## Maria          6.8      7.2     8.7      9.0      7.0
#View(datos)
str(datos) 
## 'data.frame':    10 obs. of  5 variables:
##  $ Matematicas: num  7 7.5 7.6 5 6 7.8 6.3 7.9 6 6.8
##  $ Ciencias   : num  6.5 9.4 9.2 6.5 6 9.6 6.4 9.7 6 7.2
##  $ Español    : num  9.2 7.3 8 6.5 7.8 7.7 8.2 7.5 6.5 8.7
##  $ Historia   : num  8.6 7 8 7 8.9 8 9 8 5.5 9
##  $ EdFidica   : num  8 7 7.5 9 7.3 6.5 7.2 6 8.7 7
head(datos)
##        Matematicas Ciencias Español Historia EdFidica
## Lucia          7.0      6.5     9.2      8.6      8.0
## Pedro          7.5      9.4     7.3      7.0      7.0
## Ines           7.6      9.2     8.0      8.0      7.5
## Luis           5.0      6.5     6.5      7.0      9.0
## Andres         6.0      6.0     7.8      8.9      7.3
## Ana            7.8      9.6     7.7      8.0      6.5
summary(datos)
##   Matematicas       Ciencias        Español        Historia        EdFidica    
##  Min.   :5.000   Min.   :6.000   Min.   :6.50   Min.   :5.500   Min.   :6.000  
##  1st Qu.:6.075   1st Qu.:6.425   1st Qu.:7.35   1st Qu.:7.250   1st Qu.:7.000  
##  Median :6.900   Median :6.850   Median :7.75   Median :8.000   Median :7.250  
##  Mean   :6.790   Mean   :7.650   Mean   :7.74   Mean   :7.900   Mean   :7.420  
##  3rd Qu.:7.575   3rd Qu.:9.350   3rd Qu.:8.15   3rd Qu.:8.825   3rd Qu.:7.875  
##  Max.   :7.900   Max.   :9.700   Max.   :9.20   Max.   :9.000   Max.   :9.000
cor(datos)
##             Matematicas    Ciencias     Español    Historia   EdFidica
## Matematicas   1.0000000  0.85407878  0.38457424  0.20719425 -0.7871627
## Ciencias      0.8540788  1.00000000 -0.02005218 -0.02153942 -0.6877206
## Español       0.3845742 -0.02005218  1.00000000  0.82091619 -0.3655434
## Historia      0.2071943 -0.02153942  0.82091619  1.00000000 -0.5080013
## EdFidica     -0.7871627 -0.68772056 -0.36554342 -0.50800132  1.0000000
# Prueba de Esfericidad de Bartlett
cortest.bartlett(cor(datos),n=dim(datos))
## $chisq
## [1] 41.150599  9.496292
## 
## $p.value
## [1] 1.061203e-05 4.857378e-01
## 
## $df
## [1] 10
# Indicador Kaiser-Meyer-Olkinn KMO y MSA
KMO(datos)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = datos)
## Overall MSA =  0.3
## MSA for each item = 
## Matematicas    Ciencias     Español    Historia    EdFidica 
##        0.33        0.34        0.23        0.25        0.37
modelo <- prcomp(datos)
modelo
## Standard deviations (1, .., p=5):
## [1] 1.97155026 1.41162417 0.54854909 0.40981080 0.09255745
## 
## Rotation (n x k) = (5 x 5):
##                    PC1        PC2         PC3         PC4        PC5
## Matematicas -0.4554991 -0.0301873 -0.47296282 -0.39587804 -0.6412457
## Ciencias    -0.7730822  0.3366382  0.04147109  0.49953935  0.1943173
## Español     -0.1117975 -0.5349732 -0.62844982  0.02458048  0.5529481
## Historia    -0.1416005 -0.7479026  0.35163283  0.39973889 -0.3703433
## EdFidica     0.4028835  0.2005213 -0.50595602  0.65828993 -0.3288450
# Grafico
biplot(modelo)

# observando los resultados de 2 componentes
result<- PCA(datos,scale.unit = T, ncp=2,graph = T)

# PCAshiny(datos)