EJERCICIO 2

Una empresa especializada en el diseño de automoviles de turismo desea estudiar cuales son los desos del publico que comrpa automoviles. Para ello deiseña una encuesta con 10 preguntas donde se le pide a cada uno de los 20 encuestados que valore de 1 a 5 si una caracteristica es o no muy importante. Los encuestados deberan contestar con un 5 si la caracteristica es muy importante, un 4 si es importante, un 3 si tiene regular importancia, un 2 si es poco importante y 1 si no es nada importante. Las 10 caracteristicas (V1 a V10) a valorar son: precio, financiacion, consumo, combustible, seguridad, confort, capacidad, prestaciones, modernidad y aerodinamica. El fichero 6-2.RData recoge los datos a ser utilizados.

REALIZA UN ANALISIS FACTORIAL QUE PERMITA EXTRAER UNOS FACTORES ADECUADOS LOS DATOS QUE RESUMAN CORRECTAMENTE LA INFORMACION QUE CONTIENE. PROPONGA UNA SOLUCION ADECUADA DE LA CANTIDAD DE FACTORES A RETENER Y JUSTIFIQUE SU RESPUESTA, SOBRE LA BASE DE LAS PRUEBAS DE VALIDACION DEL ANALISIS FACTORIAL, ESTUDIADAS EN CLASE E INDIQUE LAS VARIABLES QUE SE AGRUPARIAN EN CADA FACTOR.

load("C:/Users/50379/Downloads/6-2.RData")
colnames(X6_2)<-c("precio","financiacion","consumo","combustible","seguridad","confort","capacidad","prestaciones","modernidad","aerodinamica")

X6_2
##    precio financiacion consumo combustible seguridad confort capacidad
## 1       4            1       4           3         3       2         4
## 2       5            5       4           4         3       3         4
## 3       2            1       3           1         4       2         1
## 4       1            1       1           1         4       4         2
## 5       1            1       2           1         5       5         4
## 6       5            5       5           5         3       3         4
## 7       4            5       4           4         2       2         5
## 8       3            2       3           1         4       4         2
## 9       4            4       4           3         4       4         3
## 10      5            5       5           5         2       2         3
## 11      2            2       2           1         5       4         4
## 12      4            4       5           5         4       5         5
## 13      3            2       2           1         4       5         4
## 14      5            5       4           4         5       4         4
## 15      4            3       3           1         4       4         5
## 16      5            5       4           4         4       5         4
## 17      4            4       5           2         4       5         5
## 18      5            5       4           4         2       2         1
## 19      3            3       2           2         4       4         5
## 20      5            5       4           4         4       5         4
##    prestaciones modernidad aerodinamica
## 1             4          4            4
## 2             1          1            3
## 3             5          4            5
## 4             5          5            4
## 5             3          3            2
## 6             2          2            1
## 7             1          1            1
## 8             5          5            5
## 9             1          1            1
## 10            2          2            2
## 11            3          4            3
## 12            2          1            2
## 13            4          3            3
## 14            1          2            2
## 15            3          4            4
## 16            2          1            1
## 17            4          4            2
## 18            2          2            3
## 19            4          5            4
## 20            3          2            1
#Matriz de Covarianza
cov(X6_2)
##                  precio financiacion    consumo combustible  seguridad
## precio        1.8000000    1.9157895  1.3157895   1.7263158 -0.6210526
## financiacion  1.9157895    2.6736842  1.4210526   2.1368421 -0.6631579
## consumo       1.3157895    1.4210526  1.4210526   1.5263158 -0.5263158
## combustible   1.7263158    2.1368421  1.5263158   2.4842105 -0.8000000
## seguridad    -0.6210526   -0.6631579 -0.5263158  -0.8000000  0.8526316
## confort      -0.3052632   -0.1368421 -0.3157895  -0.4842105  0.8000000
## capacidad     0.3631579    0.5157895  0.2894737   0.3473684  0.2052632
## prestaciones -1.2052632   -1.7789474 -0.9210526  -1.6105263  0.3736842
## modernidad   -1.2736842   -1.8105263 -1.1052632  -1.8315789  0.4631579
## aerodinamica -0.9000000   -1.5368421 -0.8684211  -1.3894737  0.1526316
##                  confort  capacidad prestaciones  modernidad aerodinamica
## precio       -0.30526316  0.3631579   -1.2052632 -1.27368421   -0.9000000
## financiacion -0.13684211  0.5157895   -1.7789474 -1.81052632   -1.5368421
## consumo      -0.31578947  0.2894737   -0.9210526 -1.10526316   -0.8684211
## combustible  -0.48421053  0.3473684   -1.6105263 -1.83157895   -1.3894737
## seguridad     0.80000000  0.2052632    0.3736842  0.46315789    0.1526316
## confort       1.37894737  0.6263158    0.2157895  0.09473684   -0.3736842
## capacidad     0.62631579  1.6078947   -0.5289474 -0.33684211   -0.7078947
## prestaciones  0.21578947 -0.5289474    1.9236842  1.81052632    1.3657895
## modernidad    0.09473684 -0.3368421    1.8105263  2.16842105    1.5578947
## aerodinamica -0.37368421 -0.7078947    1.3657895  1.55789474    1.8184211
#Matriz R

cor(X6_2)
##                  precio financiacion    consumo combustible  seguridad
## precio        1.0000000   0.87328595  0.8227068   0.8163752 -0.5013159
## financiacion  0.8732860   1.00000000  0.7290378   0.8291310 -0.4392191
## consumo       0.8227068   0.72903777  1.0000000   0.8123536 -0.4781461
## combustible   0.8163752   0.82913105  0.8123536   1.0000000 -0.5496865
## seguridad    -0.5013159  -0.43921906 -0.4781461  -0.5496865  1.0000000
## confort      -0.1937601  -0.07126739 -0.2255894  -0.2616171  0.7377945
## capacidad     0.2134668   0.24876462  0.1915028   0.1738070  0.1753079
## prestaciones -0.6477072  -0.78440645 -0.5570735  -0.7367273  0.2917811
## modernidad   -0.6446941  -0.75193098 -0.6296349  -0.7891501  0.3406250
## aerodinamica -0.4974610  -0.69699068 -0.5402292  -0.6537458  0.1225791
##                  confort  capacidad prestaciones  modernidad aerodinamica
## precio       -0.19376008  0.2134668   -0.6477072 -0.64469411   -0.4974610
## financiacion -0.07126739  0.2487646   -0.7844064 -0.75193098   -0.6969907
## consumo      -0.22558942  0.1915028   -0.5570735 -0.62963492   -0.5402292
## combustible  -0.26161713  0.1738070   -0.7367273 -0.78915014   -0.6537458
## seguridad     0.73779454  0.1753079    0.2917811  0.34062503    0.1225791
## confort       1.00000000  0.4206208    0.1324920  0.05478646   -0.2359846
## capacidad     0.42062076  1.0000000   -0.3007577 -0.18039552   -0.4139927
## prestaciones  0.13249196 -0.3007577    1.0000000  0.88647429    0.7302468
## modernidad    0.05478646 -0.1803955    0.8864743  1.00000000    0.7845472
## aerodinamica -0.23598461 -0.4139927    0.7302468  0.78454720    1.0000000
library(corrplot)
## corrplot 0.92 loaded
library(grDevices)
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
Mat_R<-rcorr(as.matrix(X6_2))
corrplot(Mat_R$r,
         p.mat = Mat_R$r,
         type="upper",
         tl.col="black",
         tl.srt = 20,
         pch.col = "blue",
         insig = "p-value",
         sig.level = -1,
         col = terrain.colors(100))

library(kableExtra)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:kableExtra':
## 
##     group_rows
## The following objects are masked from 'package:Hmisc':
## 
##     src, summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(Hmisc)
Rx<-X6_2 %>% as.matrix() %>% rcorr()
Rx$r %>% kable(caption="Matriz R(X)",
        align = "c",
        digits = 2) %>% 
  kable_material(html_font = "sans-serif") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))
Matriz R(X)
precio financiacion consumo combustible seguridad confort capacidad prestaciones modernidad aerodinamica
precio 1.00 0.87 0.82 0.82 -0.50 -0.19 0.21 -0.65 -0.64 -0.50
financiacion 0.87 1.00 0.73 0.83 -0.44 -0.07 0.25 -0.78 -0.75 -0.70
consumo 0.82 0.73 1.00 0.81 -0.48 -0.23 0.19 -0.56 -0.63 -0.54
combustible 0.82 0.83 0.81 1.00 -0.55 -0.26 0.17 -0.74 -0.79 -0.65
seguridad -0.50 -0.44 -0.48 -0.55 1.00 0.74 0.18 0.29 0.34 0.12
confort -0.19 -0.07 -0.23 -0.26 0.74 1.00 0.42 0.13 0.05 -0.24
capacidad 0.21 0.25 0.19 0.17 0.18 0.42 1.00 -0.30 -0.18 -0.41
prestaciones -0.65 -0.78 -0.56 -0.74 0.29 0.13 -0.30 1.00 0.89 0.73
modernidad -0.64 -0.75 -0.63 -0.79 0.34 0.05 -0.18 0.89 1.00 0.78
aerodinamica -0.50 -0.70 -0.54 -0.65 0.12 -0.24 -0.41 0.73 0.78 1.00
Rx$P %>% kable(caption="p-values de R(X)",
        align = "c",
        digits = 2) %>% 
  kable_classic_2(html_font = "sans-serif") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))
p-values de R(X)
precio financiacion consumo combustible seguridad confort capacidad prestaciones modernidad aerodinamica
precio NA 0.00 0.00 0.00 0.02 0.41 0.37 0.00 0.00 0.03
financiacion 0.00 NA 0.00 0.00 0.05 0.77 0.29 0.00 0.00 0.00
consumo 0.00 0.00 NA 0.00 0.03 0.34 0.42 0.01 0.00 0.01
combustible 0.00 0.00 0.00 NA 0.01 0.27 0.46 0.00 0.00 0.00
seguridad 0.02 0.05 0.03 0.01 NA 0.00 0.46 0.21 0.14 0.61
confort 0.41 0.77 0.34 0.27 0.00 NA 0.06 0.58 0.82 0.32
capacidad 0.37 0.29 0.42 0.46 0.46 0.06 NA 0.20 0.45 0.07
prestaciones 0.00 0.00 0.01 0.00 0.21 0.58 0.20 NA 0.00 0.00
modernidad 0.00 0.00 0.00 0.00 0.14 0.82 0.45 0.00 NA 0.00
aerodinamica 0.03 0.00 0.01 0.00 0.61 0.32 0.07 0.00 0.00 NA
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
descomposicion<-eigen(Rx$r)
t(descomposicion$values) %>% kable(caption="Autovalores de R(X)",
        align = "c",
        digits = 2) %>% 
  kable_classic_2(html_font = "sans-serif") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))
Autovalores de R(X)
5.7 2.07 0.72 0.55 0.32 0.27 0.15 0.13 0.07 0.03
library(dplyr)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(kableExtra)
library(stargazer)
library(ggplot2)
options(scipen = 99999)
PC<-princomp(x = X6_2,cor = TRUE,fix_sign = FALSE)
factoextra::get_eig(PC) %>% kable(caption="Resumen de PCA",
        align = "c",
        digits = 2) %>% 
  kable_material(html_font = "sans-serif") %>% 
  kable_styling(bootstrap_options = c("hover"))
Resumen de PCA
eigenvalue variance.percent cumulative.variance.percent
Dim.1 5.70 57.01 57.01
Dim.2 2.07 20.69 77.70
Dim.3 0.72 7.20 84.91
Dim.4 0.55 5.48 90.39
Dim.5 0.32 3.16 93.54
Dim.6 0.27 2.71 96.25
Dim.7 0.15 1.46 97.72
Dim.8 0.13 1.28 99.00
Dim.9 0.07 0.68 99.68
Dim.10 0.03 0.32 100.00
fviz_eig(PC,
         choice = "eigenvalue",
         barcolor = "red",
         barfill = "red",
         addlabels = TRUE, 
       )+labs(title = "Gráfico de Sedimentación",subtitle = "Usando princomp, con Autovalores")+
  xlab(label = "Componentes")+
  ylab(label = "Autovalores")+geom_hline(yintercept = 1)

2 factores a retener

library(dplyr)
library(factoextra)
library(kableExtra)
variables_pca<-get_pca_var(PC)
variables_pca$coord%>% 
  kable(caption="Correlación de X con las componentes, usando factoextra",
        align = "c",
        digits = 2) %>% 
  kable_material(html_font = "sans-serif") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))
Correlación de X con las componentes, usando factoextra
Dim.1 Dim.2 Dim.3 Dim.4 Dim.5 Dim.6 Dim.7 Dim.8 Dim.9 Dim.10
precio -0.88 -0.10 -0.27 -0.25 0.21 -0.07 0.02 0.05 0.18 -0.02
financiacion -0.92 0.06 -0.03 -0.14 0.16 -0.24 -0.13 -0.02 -0.13 0.07
consumo -0.84 -0.12 -0.27 -0.27 -0.17 0.29 -0.04 0.12 -0.08 -0.02
combustible -0.93 -0.11 -0.02 -0.06 -0.08 0.06 0.10 -0.30 -0.01 -0.02
seguridad 0.53 0.72 0.15 -0.22 0.19 0.26 -0.10 -0.08 0.03 0.05
confort 0.20 0.90 0.00 -0.29 -0.07 -0.17 0.15 0.03 -0.05 -0.06
capacidad -0.28 0.67 -0.54 0.42 0.02 0.04 0.03 -0.01 0.00 0.03
prestaciones 0.86 -0.16 -0.28 -0.25 -0.26 -0.09 0.04 -0.04 0.05 0.10
modernidad 0.88 -0.14 -0.37 -0.07 0.02 -0.07 -0.21 -0.10 -0.02 -0.09
aerodinamica 0.77 -0.45 -0.23 -0.08 0.31 0.08 0.19 0.00 -0.09 0.01
library(corrplot)
corrplot(variables_pca$coord,is.corr = FALSE,method = "square",addCoef.col="black",number.cex = 0.75)

library(psych)
## 
## Attaching package: 'psych'
## The following object is masked from 'package:Hmisc':
## 
##     describe
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(corrplot)
library(dplyr)
#Modelo de 2 Factores (sin rotar)
numero_de_factores<-2
modelo_2_factores<-principal(r = Rx$r,
                             nfactors = numero_de_factores,
                             covar = FALSE,
                             rotate = "none")
modelo_2_factores
## Principal Components Analysis
## Call: principal(r = Rx$r, nfactors = numero_de_factores, rotate = "none", 
##     covar = FALSE)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                PC1   PC2   h2   u2 com
## precio        0.88 -0.10 0.78 0.22 1.0
## financiacion  0.92  0.06 0.86 0.14 1.0
## consumo       0.84 -0.12 0.72 0.28 1.0
## combustible   0.93 -0.11 0.88 0.12 1.0
## seguridad    -0.53  0.72 0.80 0.20 1.8
## confort      -0.20  0.90 0.85 0.15 1.1
## capacidad     0.28  0.67 0.53 0.47 1.3
## prestaciones -0.86 -0.16 0.77 0.23 1.1
## modernidad   -0.88 -0.14 0.79 0.21 1.0
## aerodinamica -0.77 -0.45 0.79 0.21 1.6
## 
##                        PC1  PC2
## SS loadings           5.70 2.07
## Proportion Var        0.57 0.21
## Cumulative Var        0.57 0.78
## Proportion Explained  0.73 0.27
## Cumulative Proportion 0.73 1.00
## 
## Mean item complexity =  1.2
## Test of the hypothesis that 2 components are sufficient.
## 
## The root mean square of the residuals (RMSR) is  0.07 
## 
## Fit based upon off diagonal values = 0.98
correlaciones_modelo<-variables_pca$coord


corrplot(correlaciones_modelo[,1:numero_de_factores],
         is.corr = FALSE,
         method = "square",addCoef.col="black",number.cex = 0.75)

library(psych)
library(corrplot)
library(dplyr)
#Modelo de 2 Factores (Rotado)
numero_de_factores<-2
modelo_2_factores<-principal(r = Rx$r,
                             nfactors = numero_de_factores,
                             covar = FALSE,
                             rotate = "varimax")
modelo_2_factores
## Principal Components Analysis
## Call: principal(r = Rx$r, nfactors = numero_de_factores, rotate = "varimax", 
##     covar = FALSE)
## Standardized loadings (pattern matrix) based upon correlation matrix
##                RC1   RC2   h2   u2 com
## precio        0.87 -0.18 0.78 0.22 1.1
## financiacion  0.93 -0.02 0.86 0.14 1.0
## consumo       0.83 -0.19 0.72 0.28 1.1
## combustible   0.92 -0.20 0.88 0.12 1.1
## seguridad    -0.46  0.77 0.80 0.20 1.6
## confort      -0.11  0.91 0.85 0.15 1.0
## capacidad     0.34  0.64 0.53 0.47 1.5
## prestaciones -0.87 -0.07 0.77 0.23 1.0
## modernidad   -0.89 -0.05 0.79 0.21 1.0
## aerodinamica -0.80 -0.38 0.79 0.21 1.4
## 
##                        RC1  RC2
## SS loadings           5.67 2.10
## Proportion Var        0.57 0.21
## Cumulative Var        0.57 0.78
## Proportion Explained  0.73 0.27
## Cumulative Proportion 0.73 1.00
## 
## Mean item complexity =  1.2
## Test of the hypothesis that 2 components are sufficient.
## 
## The root mean square of the residuals (RMSR) is  0.07 
## 
## Fit based upon off diagonal values = 0.98
correlaciones_modelo<-variables_pca$coord
correlaciones_modelo_rotada<-varimax(correlaciones_modelo[,1:numero_de_factores])$loadings

corrplot(correlaciones_modelo_rotada[,1:numero_de_factores],
         is.corr = FALSE,
         method = "square",
         addCoef.col="black",
         number.cex = 0.75)

library(psych)
Barlett<-cortest.bartlett(X6_2)
## R was not square, finding R from data
print(Barlett)
## $chisq
## [1] 163.4656
## 
## $p.value
## [1] 0.000000000000002362835
## 
## $df
## [1] 45
KMO<-KMO(X6_2)
print(KMO)
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = X6_2)
## Overall MSA =  0.7
## MSA for each item = 
##       precio financiacion      consumo  combustible    seguridad      confort 
##         0.82         0.74         0.84         0.93         0.55         0.32 
##    capacidad prestaciones   modernidad aerodinamica 
##         0.37         0.62         0.68         0.84
library(rela)
KMO<-paf(as.matrix(X6_2))$KMO
print(KMO)
## [1] 0.70012