library(kableExtra)
load("D:/UES/SEXTO CICLO/Metodos para el analisis economico/Portafolio/tarea7/6-2.Rdata")
X6_2 %>% head() %>%
kable(caption = "Matriz de informacion",
align = "c") %>%
kable_material(html_font = "sans-serif")
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 |
|---|---|---|---|---|---|---|---|---|---|
| 4 | 1 | 4 | 3 | 3 | 2 | 4 | 4 | 4 | 4 |
| 5 | 5 | 4 | 4 | 3 | 3 | 4 | 1 | 1 | 3 |
| 2 | 1 | 3 | 1 | 4 | 2 | 1 | 5 | 4 | 5 |
| 1 | 1 | 1 | 1 | 4 | 4 | 2 | 5 | 5 | 4 |
| 1 | 1 | 2 | 1 | 5 | 5 | 4 | 3 | 3 | 2 |
| 5 | 5 | 5 | 5 | 3 | 3 | 4 | 2 | 2 | 1 |
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:kableExtra':
##
## group_rows
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(kableExtra)
centrado <- function(x){x-mean(x)
}
xcentrada <- apply(X = X6_2,MARGIN = 2,centrado)
xcentrada %>% head() %>%
kable(caption = "Matriz de Variables Centradas",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif")
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 |
|---|---|---|---|---|---|---|---|---|---|
| 0.3 | -2.4 | 0.5 | 0.2 | -0.7 | -1.7 | 0.35 | 1.15 | 1.2 | 1.35 |
| 1.3 | 1.6 | 0.5 | 1.2 | -0.7 | -0.7 | 0.35 | -1.85 | -1.8 | 0.35 |
| -1.7 | -2.4 | -0.5 | -1.8 | 0.3 | -1.7 | -2.65 | 2.15 | 1.2 | 2.35 |
| -2.7 | -2.4 | -2.5 | -1.8 | 0.3 | 0.3 | -1.65 | 2.15 | 2.2 | 1.35 |
| -2.7 | -2.4 | -1.5 | -1.8 | 1.3 | 1.3 | 0.35 | 0.15 | 0.2 | -0.65 |
| 1.3 | 1.6 | 1.5 | 2.2 | -0.7 | -0.7 | 0.35 | -0.85 | -0.8 | -1.65 |
n_obs <- nrow(X6_2)
mat_V <- t(xcentrada) %*% xcentrada/ (n_obs-1)
mat_V %>% head(n=10) %>%
kable(caption = "Calculo de V(x) forma manual",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped","hover"))
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| V1 | 1.80 | 1.92 | 1.32 | 1.73 | -0.62 | -0.31 | 0.36 | -1.21 | -1.27 | -0.90 |
| V2 | 1.92 | 2.67 | 1.42 | 2.14 | -0.66 | -0.14 | 0.52 | -1.78 | -1.81 | -1.54 |
| V3 | 1.32 | 1.42 | 1.42 | 1.53 | -0.53 | -0.32 | 0.29 | -0.92 | -1.11 | -0.87 |
| V4 | 1.73 | 2.14 | 1.53 | 2.48 | -0.80 | -0.48 | 0.35 | -1.61 | -1.83 | -1.39 |
| V5 | -0.62 | -0.66 | -0.53 | -0.80 | 0.85 | 0.80 | 0.21 | 0.37 | 0.46 | 0.15 |
| V6 | -0.31 | -0.14 | -0.32 | -0.48 | 0.80 | 1.38 | 0.63 | 0.22 | 0.09 | -0.37 |
| V7 | 0.36 | 0.52 | 0.29 | 0.35 | 0.21 | 0.63 | 1.61 | -0.53 | -0.34 | -0.71 |
| V8 | -1.21 | -1.78 | -0.92 | -1.61 | 0.37 | 0.22 | -0.53 | 1.92 | 1.81 | 1.37 |
| V9 | -1.27 | -1.81 | -1.11 | -1.83 | 0.46 | 0.09 | -0.34 | 1.81 | 2.17 | 1.56 |
| V10 | -0.90 | -1.54 | -0.87 | -1.39 | 0.15 | -0.37 | -0.71 | 1.37 | 1.56 | 1.82 |
library(dplyr)
library(kableExtra)
cov(X6_2) %>%
kable(caption = "Calculo de V(x) a traves de R base",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped","hover"))
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| V1 | 1.80 | 1.92 | 1.32 | 1.73 | -0.62 | -0.31 | 0.36 | -1.21 | -1.27 | -0.90 |
| V2 | 1.92 | 2.67 | 1.42 | 2.14 | -0.66 | -0.14 | 0.52 | -1.78 | -1.81 | -1.54 |
| V3 | 1.32 | 1.42 | 1.42 | 1.53 | -0.53 | -0.32 | 0.29 | -0.92 | -1.11 | -0.87 |
| V4 | 1.73 | 2.14 | 1.53 | 2.48 | -0.80 | -0.48 | 0.35 | -1.61 | -1.83 | -1.39 |
| V5 | -0.62 | -0.66 | -0.53 | -0.80 | 0.85 | 0.80 | 0.21 | 0.37 | 0.46 | 0.15 |
| V6 | -0.31 | -0.14 | -0.32 | -0.48 | 0.80 | 1.38 | 0.63 | 0.22 | 0.09 | -0.37 |
| V7 | 0.36 | 0.52 | 0.29 | 0.35 | 0.21 | 0.63 | 1.61 | -0.53 | -0.34 | -0.71 |
| V8 | -1.21 | -1.78 | -0.92 | -1.61 | 0.37 | 0.22 | -0.53 | 1.92 | 1.81 | 1.37 |
| V9 | -1.27 | -1.81 | -1.11 | -1.83 | 0.46 | 0.09 | -0.34 | 1.81 | 2.17 | 1.56 |
| V10 | -0.90 | -1.54 | -0.87 | -1.39 | 0.15 | -0.37 | -0.71 | 1.37 | 1.56 | 1.82 |
zx <- scale(x = X6_2,center = TRUE)
zx %>%
kable(caption = "Matriz de Variables Estandarizadas",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif")
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 |
|---|---|---|---|---|---|---|---|---|---|
| 0.22 | -1.47 | 0.42 | 0.13 | -0.76 | -1.45 | 0.28 | 0.83 | 0.81 | 1.00 |
| 0.97 | 0.98 | 0.42 | 0.76 | -0.76 | -0.60 | 0.28 | -1.33 | -1.22 | 0.26 |
| -1.27 | -1.47 | -0.42 | -1.14 | 0.32 | -1.45 | -2.09 | 1.55 | 0.81 | 1.74 |
| -2.01 | -1.47 | -2.10 | -1.14 | 0.32 | 0.26 | -1.30 | 1.55 | 1.49 | 1.00 |
| -2.01 | -1.47 | -1.26 | -1.14 | 1.41 | 1.11 | 0.28 | 0.11 | 0.14 | -0.48 |
| 0.97 | 0.98 | 1.26 | 1.40 | -0.76 | -0.60 | 0.28 | -0.61 | -0.54 | -1.22 |
| 0.22 | 0.98 | 0.42 | 0.76 | -1.84 | -1.45 | 1.06 | -1.33 | -1.22 | -1.22 |
| -0.52 | -0.86 | -0.42 | -1.14 | 0.32 | 0.26 | -1.30 | 1.55 | 1.49 | 1.74 |
| 0.22 | 0.37 | 0.42 | 0.13 | 0.32 | 0.26 | -0.51 | -1.33 | -1.22 | -1.22 |
| 0.97 | 0.98 | 1.26 | 1.40 | -1.84 | -1.45 | -0.51 | -0.61 | -0.54 | -0.48 |
| -1.27 | -0.86 | -1.26 | -1.14 | 1.41 | 0.26 | 0.28 | 0.11 | 0.81 | 0.26 |
| 0.22 | 0.37 | 1.26 | 1.40 | 0.32 | 1.11 | 1.06 | -0.61 | -1.22 | -0.48 |
| -0.52 | -0.86 | -1.26 | -1.14 | 0.32 | 1.11 | 0.28 | 0.83 | 0.14 | 0.26 |
| 0.97 | 0.98 | 0.42 | 0.76 | 1.41 | 0.26 | 0.28 | -1.33 | -0.54 | -0.48 |
| 0.22 | -0.24 | -0.42 | -1.14 | 0.32 | 0.26 | 1.06 | 0.11 | 0.81 | 1.00 |
| 0.97 | 0.98 | 0.42 | 0.76 | 0.32 | 1.11 | 0.28 | -0.61 | -1.22 | -1.22 |
| 0.22 | 0.37 | 1.26 | -0.51 | 0.32 | 1.11 | 1.06 | 0.83 | 0.81 | -0.48 |
| 0.97 | 0.98 | 0.42 | 0.76 | -1.84 | -1.45 | -2.09 | -0.61 | -0.54 | 0.26 |
| -0.52 | -0.24 | -1.26 | -0.51 | 0.32 | 0.26 | 1.06 | 0.83 | 1.49 | 1.00 |
| 0.97 | 0.98 | 0.42 | 0.76 | 0.32 | 1.11 | 0.28 | 0.11 | -0.54 | -1.22 |
n__obs <- nrow(X6_2)
mat_R <- t(zx)%*%zx/(n__obs-1)
mat_R %>%
kable(caption = "Calculo R(x) forma manual",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped","hover"))
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| V1 | 1.00 | 0.87 | 0.82 | 0.82 | -0.50 | -0.19 | 0.21 | -0.65 | -0.64 | -0.50 |
| V2 | 0.87 | 1.00 | 0.73 | 0.83 | -0.44 | -0.07 | 0.25 | -0.78 | -0.75 | -0.70 |
| V3 | 0.82 | 0.73 | 1.00 | 0.81 | -0.48 | -0.23 | 0.19 | -0.56 | -0.63 | -0.54 |
| V4 | 0.82 | 0.83 | 0.81 | 1.00 | -0.55 | -0.26 | 0.17 | -0.74 | -0.79 | -0.65 |
| V5 | -0.50 | -0.44 | -0.48 | -0.55 | 1.00 | 0.74 | 0.18 | 0.29 | 0.34 | 0.12 |
| V6 | -0.19 | -0.07 | -0.23 | -0.26 | 0.74 | 1.00 | 0.42 | 0.13 | 0.05 | -0.24 |
| V7 | 0.21 | 0.25 | 0.19 | 0.17 | 0.18 | 0.42 | 1.00 | -0.30 | -0.18 | -0.41 |
| V8 | -0.65 | -0.78 | -0.56 | -0.74 | 0.29 | 0.13 | -0.30 | 1.00 | 0.89 | 0.73 |
| V9 | -0.64 | -0.75 | -0.63 | -0.79 | 0.34 | 0.05 | -0.18 | 0.89 | 1.00 | 0.78 |
| V10 | -0.50 | -0.70 | -0.54 | -0.65 | 0.12 | -0.24 | -0.41 | 0.73 | 0.78 | 1.00 |
library(dplyr)
library(kableExtra)
cor(X6_2) %>%
kable(caption = "Calculo R(x) utilizando cor",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped","hover"))
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| V1 | 1.00 | 0.87 | 0.82 | 0.82 | -0.50 | -0.19 | 0.21 | -0.65 | -0.64 | -0.50 |
| V2 | 0.87 | 1.00 | 0.73 | 0.83 | -0.44 | -0.07 | 0.25 | -0.78 | -0.75 | -0.70 |
| V3 | 0.82 | 0.73 | 1.00 | 0.81 | -0.48 | -0.23 | 0.19 | -0.56 | -0.63 | -0.54 |
| V4 | 0.82 | 0.83 | 0.81 | 1.00 | -0.55 | -0.26 | 0.17 | -0.74 | -0.79 | -0.65 |
| V5 | -0.50 | -0.44 | -0.48 | -0.55 | 1.00 | 0.74 | 0.18 | 0.29 | 0.34 | 0.12 |
| V6 | -0.19 | -0.07 | -0.23 | -0.26 | 0.74 | 1.00 | 0.42 | 0.13 | 0.05 | -0.24 |
| V7 | 0.21 | 0.25 | 0.19 | 0.17 | 0.18 | 0.42 | 1.00 | -0.30 | -0.18 | -0.41 |
| V8 | -0.65 | -0.78 | -0.56 | -0.74 | 0.29 | 0.13 | -0.30 | 1.00 | 0.89 | 0.73 |
| V9 | -0.64 | -0.75 | -0.63 | -0.79 | 0.34 | 0.05 | -0.18 | 0.89 | 1.00 | 0.78 |
| V10 | -0.50 | -0.70 | -0.54 | -0.65 | 0.12 | -0.24 | -0.41 | 0.73 | 0.78 | 1.00 |
library(PerformanceAnalytics)
chart.Correlation(as.matrix(X6_2),histogram = TRUE,pch=12)
library(corrplot)
## corrplot 0.92 loaded
library(grDevices)
library(Hmisc)
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
##
## src, summarize
## 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))
Criterio del porcentaje acumulado de la varianza
En este criterio deberia de retener tantos componentes, a manera que se explique alrededor de el 75% de la varianza de los datos originales. Por tal motivo,este criterio al momento de realizar la evaluación de la trabla resumen, observamos que las primeras dimensiones de “variance.percent” se obtiene un total del 77.70% por lo que ya se cumple el criterio por lo que se deberia seleccionar dichas dimensiones.
Criterio de Autovalores (Criterio de raiz latente)
Se basa únicamente en que se retengan aquellos componentes cuyo autovalor sea maryor que uno o que sea por lo menos de uno. En la tabla “Resumen de PCA” se observa la dimensión 1 y 2 son los que superan el autovalor de valor de 1. Por consiguiente las dos primeras dimensiones antes mencionadas, bajo este criterio, se retendrán.
Criterio del codo
la cantidad de componentes que se deberán retener ubicados antes del codo, otro sector hace hincapie sobre la componentes a retener serán aquellos localizados donde se de el codo. Una mayor explicación, el último gráfico de color verde podemos ver que las primeras tres dimenciones son las que cumplen con dicho criterio y por tal motivo se retendrán.
library(kableExtra)
library(dplyr)
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"))
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| V1 | 1.00 | 0.87 | 0.82 | 0.82 | -0.50 | -0.19 | 0.21 | -0.65 | -0.64 | -0.50 |
| V2 | 0.87 | 1.00 | 0.73 | 0.83 | -0.44 | -0.07 | 0.25 | -0.78 | -0.75 | -0.70 |
| V3 | 0.82 | 0.73 | 1.00 | 0.81 | -0.48 | -0.23 | 0.19 | -0.56 | -0.63 | -0.54 |
| V4 | 0.82 | 0.83 | 0.81 | 1.00 | -0.55 | -0.26 | 0.17 | -0.74 | -0.79 | -0.65 |
| V5 | -0.50 | -0.44 | -0.48 | -0.55 | 1.00 | 0.74 | 0.18 | 0.29 | 0.34 | 0.12 |
| V6 | -0.19 | -0.07 | -0.23 | -0.26 | 0.74 | 1.00 | 0.42 | 0.13 | 0.05 | -0.24 |
| V7 | 0.21 | 0.25 | 0.19 | 0.17 | 0.18 | 0.42 | 1.00 | -0.30 | -0.18 | -0.41 |
| V8 | -0.65 | -0.78 | -0.56 | -0.74 | 0.29 | 0.13 | -0.30 | 1.00 | 0.89 | 0.73 |
| V9 | -0.64 | -0.75 | -0.63 | -0.79 | 0.34 | 0.05 | -0.18 | 0.89 | 1.00 | 0.78 |
| V10 | -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"))
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | |
|---|---|---|---|---|---|---|---|---|---|---|
| V1 | NA | 0.00 | 0.00 | 0.00 | 0.02 | 0.41 | 0.37 | 0.00 | 0.00 | 0.03 |
| V2 | 0.00 | NA | 0.00 | 0.00 | 0.05 | 0.77 | 0.29 | 0.00 | 0.00 | 0.00 |
| V3 | 0.00 | 0.00 | NA | 0.00 | 0.03 | 0.34 | 0.42 | 0.01 | 0.00 | 0.01 |
| V4 | 0.00 | 0.00 | 0.00 | NA | 0.01 | 0.27 | 0.46 | 0.00 | 0.00 | 0.00 |
| V5 | 0.02 | 0.05 | 0.03 | 0.01 | NA | 0.00 | 0.46 | 0.21 | 0.14 | 0.61 |
| V6 | 0.41 | 0.77 | 0.34 | 0.27 | 0.00 | NA | 0.06 | 0.58 | 0.82 | 0.32 |
| V7 | 0.37 | 0.29 | 0.42 | 0.46 | 0.46 | 0.06 | NA | 0.20 | 0.45 | 0.07 |
| V8 | 0.00 | 0.00 | 0.01 | 0.00 | 0.21 | 0.58 | 0.20 | NA | 0.00 | 0.00 |
| V9 | 0.00 | 0.00 | 0.00 | 0.00 | 0.14 | 0.82 | 0.45 | 0.00 | NA | 0.00 |
| V10 | 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"))
| 5.7 | 2.07 | 0.72 | 0.55 | 0.32 | 0.27 | 0.15 | 0.13 | 0.07 | 0.03 |
descomposicion$vectors %>% kable(caption="Autovectores de R(X)",
align = "c",
digits = 2) %>%
kable_classic_2(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
| -0.37 | -0.07 | -0.31 | -0.34 | 0.38 | -0.13 | 0.05 | 0.14 | 0.67 | -0.10 |
| -0.39 | 0.04 | -0.04 | -0.19 | 0.28 | -0.47 | -0.33 | -0.06 | -0.48 | 0.41 |
| -0.35 | -0.08 | -0.32 | -0.36 | -0.31 | 0.56 | -0.11 | 0.34 | -0.32 | -0.09 |
| -0.39 | -0.08 | -0.02 | -0.09 | -0.14 | 0.12 | 0.26 | -0.85 | -0.03 | -0.11 |
| 0.22 | 0.50 | 0.18 | -0.30 | 0.34 | 0.50 | -0.26 | -0.23 | 0.12 | 0.27 |
| 0.08 | 0.63 | 0.00 | -0.40 | -0.13 | -0.33 | 0.38 | 0.09 | -0.18 | -0.36 |
| -0.12 | 0.47 | -0.63 | 0.57 | 0.04 | 0.07 | 0.08 | -0.02 | 0.00 | 0.16 |
| 0.36 | -0.11 | -0.33 | -0.34 | -0.47 | -0.17 | 0.10 | -0.10 | 0.19 | 0.58 |
| 0.37 | -0.10 | -0.43 | -0.10 | 0.03 | -0.14 | -0.56 | -0.28 | -0.08 | -0.49 |
| 0.32 | -0.32 | -0.27 | -0.11 | 0.56 | 0.15 | 0.51 | -0.01 | -0.35 | 0.04 |
library(dplyr)
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(kableExtra)
library(stargazer)
library(ggplot2)
options(scipen = 999999)
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"))
| 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 = "blue",
barfill = "blue",
addlabels = TRUE,
)+labs(title= "Grafico de sedimentacion", subtitle="Usando princomp, con autovalor")+
xlab(label = "Componentes")+
ylab(label = "Autovalores")+
geom_hline(yintercept = 1)
fviz_eig(pc,
choice = "variance",
barcolor = "green",
barfill = "green",
addlabels = TRUE,
)+labs(title = "Gráfico de Sedimentación",
subtitle = "Usando princomp, con %Varianza Explicada")+
xlab(label = "Componentes")+
ylab(label = "%Varianza")