Matriz de Información: X
library(readr)
library(kableExtra)
url_link<-"http://halweb.uc3m.es/esp/Personal/personas/agrane/libro/ficheros_datos/capitulo_7/datos_prob_7_3.txt"
mat_X<-read_table2(url_link,col_names = FALSE)
mat_X %>% head() %>%
kable(caption ="Matriz de información:" ,align = "c",digits = 6) %>%
kable_material(html_font = "sans-serif")
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 |
|---|---|---|---|---|---|---|---|
| 30 | 41 | 670 | 3903 | 12 | 94 | 341 | 1.2 |
| 124 | 46 | 410 | 955 | 6 | 57 | 89 | 0.5 |
| 95 | 48 | 370 | 6 | 5 | 26 | 20 | 0.1 |
| 90 | 43 | 680 | 435 | 8 | 20 | 331 | 1.6 |
| 112 | 41 | 100 | 1293 | 2 | 51 | 22 | 0.1 |
| 73 | 51 | 390 | 6115 | 4 | 35 | 93 | 0.2 |
Cálculo de V(X): Cálculo “Manual”
library(dplyr)
library(kableExtra)
centrado<-function(x){
x-mean(x)
}
Xcentrada<-apply(X = mat_X,MARGIN = 2,centrado)
Xcentrada %>% head() %>%
kable(caption ="Matriz de Variables centradas:",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif")
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 |
|---|---|---|---|---|---|---|---|
| -49.67 | -0.67 | 303.89 | 1463.5 | 2.94 | -60.17 | 196.72 | 0.71 |
| 44.33 | 4.33 | 43.89 | -1484.5 | -3.06 | -97.17 | -55.28 | 0.01 |
| 15.33 | 6.33 | 3.89 | -2433.5 | -4.06 | -128.17 | -124.28 | -0.39 |
| 10.33 | 1.33 | 313.89 | -2004.5 | -1.06 | -134.17 | 186.72 | 1.11 |
| 32.33 | -0.67 | -266.11 | -1146.5 | -7.06 | -103.17 | -122.28 | -0.39 |
| -6.67 | 9.33 | 23.89 | 3675.5 | -5.06 | -119.17 | -51.28 | -0.29 |
n_obs<-nrow(mat_X)
mat_V<-t(Xcentrada)%*%Xcentrada/(n_obs-1)
mat_V %>% kable(caption ="Cálculo de V(X) forma manual:" ,
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | |
|---|---|---|---|---|---|---|---|---|
| X1 | 716.12 | 45.06 | -2689.61 | -16082.06 | -121.63 | -1019.06 | -1844.37 | -5.15 |
| X2 | 45.06 | 46.94 | -144.31 | 2756.71 | -24.63 | -938.41 | -205.25 | -0.42 |
| X3 | -2689.61 | -144.31 | 36389.87 | 123889.71 | 740.82 | 838.33 | 17499.38 | 73.48 |
| X4 | -16082.06 | 2756.71 | 123889.71 | 5736372.38 | 3078.97 | 6672.44 | 140343.50 | 412.79 |
| X5 | -121.63 | -24.63 | 740.82 | 3078.97 | 51.47 | 405.58 | 565.22 | 1.59 |
| X6 | -1019.06 | -938.41 | 838.33 | 6672.44 | 405.58 | 26579.56 | 3149.77 | -2.96 |
| X7 | -1844.37 | -205.25 | 17499.38 | 140343.50 | 565.22 | 3149.77 | 16879.39 | 64.51 |
| X8 | -5.15 | -0.42 | 73.48 | 412.79 | 1.59 | -2.96 | 64.51 | 0.28 |
Cálculo con R base
library(dplyr)
library(kableExtra)
cov(mat_X) %>%
kable(caption="Cálculo de V(X) a través de R base",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | |
|---|---|---|---|---|---|---|---|---|
| X1 | 716.12 | 45.06 | -2689.61 | -16082.06 | -121.63 | -1019.06 | -1844.37 | -5.15 |
| X2 | 45.06 | 46.94 | -144.31 | 2756.71 | -24.63 | -938.41 | -205.25 | -0.42 |
| X3 | -2689.61 | -144.31 | 36389.87 | 123889.71 | 740.82 | 838.33 | 17499.38 | 73.48 |
| X4 | -16082.06 | 2756.71 | 123889.71 | 5736372.38 | 3078.97 | 6672.44 | 140343.50 | 412.79 |
| X5 | -121.63 | -24.63 | 740.82 | 3078.97 | 51.47 | 405.58 | 565.22 | 1.59 |
| X6 | -1019.06 | -938.41 | 838.33 | 6672.44 | 405.58 | 26579.56 | 3149.77 | -2.96 |
| X7 | -1844.37 | -205.25 | 17499.38 | 140343.50 | 565.22 | 3149.77 | 16879.39 | 64.51 |
| X8 | -5.15 | -0.42 | 73.48 | 412.79 | 1.59 | -2.96 | 64.51 | 0.28 |
Cálculo de R(X) Cálculo “Manual”
Zx<-scale(x = mat_X,center =TRUE)
Zx %>% head() %>%
kable(caption ="Matriz de Variables Estandarizadas:",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif")
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 |
|---|---|---|---|---|---|---|---|
| -1.86 | -0.10 | 1.59 | 0.61 | 0.41 | -0.37 | 1.51 | 1.34 |
| 1.66 | 0.63 | 0.23 | -0.62 | -0.43 | -0.60 | -0.43 | 0.02 |
| 0.57 | 0.92 | 0.02 | -1.02 | -0.57 | -0.79 | -0.96 | -0.73 |
| 0.39 | 0.19 | 1.65 | -0.84 | -0.15 | -0.82 | 1.44 | 2.09 |
| 1.21 | -0.10 | -1.39 | -0.48 | -0.98 | -0.63 | -0.94 | -0.73 |
| -0.25 | 1.36 | 0.13 | 1.53 | -0.70 | -0.73 | -0.39 | -0.54 |
n_obs<-nrow(mat_X)
mat_R<-t(Zx)%*%Zx/(n_obs-1)
mat_R %>% kable(caption ="Cálculo de R(X) forma manual:" ,
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | |
|---|---|---|---|---|---|---|---|---|
| X1 | 1.00 | 0.25 | -0.53 | -0.25 | -0.63 | -0.23 | -0.53 | -0.36 |
| X2 | 0.25 | 1.00 | -0.11 | 0.17 | -0.50 | -0.84 | -0.23 | -0.12 |
| X3 | -0.53 | -0.11 | 1.00 | 0.27 | 0.54 | 0.03 | 0.71 | 0.73 |
| X4 | -0.25 | 0.17 | 0.27 | 1.00 | 0.18 | 0.02 | 0.45 | 0.32 |
| X5 | -0.63 | -0.50 | 0.54 | 0.18 | 1.00 | 0.35 | 0.61 | 0.42 |
| X6 | -0.23 | -0.84 | 0.03 | 0.02 | 0.35 | 1.00 | 0.15 | -0.03 |
| X7 | -0.53 | -0.23 | 0.71 | 0.45 | 0.61 | 0.15 | 1.00 | 0.93 |
| X8 | -0.36 | -0.12 | 0.73 | 0.32 | 0.42 | -0.03 | 0.93 | 1.00 |
Cálculo usando R base
library(dplyr)
library(kableExtra)
cor(mat_X) %>%
kable(caption="Cálculo de R(X) a través de R base",
align = "c",
digits = 2) %>%
kable_material(html_font = "sans-serif") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
| X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | |
|---|---|---|---|---|---|---|---|---|
| X1 | 1.00 | 0.25 | -0.53 | -0.25 | -0.63 | -0.23 | -0.53 | -0.36 |
| X2 | 0.25 | 1.00 | -0.11 | 0.17 | -0.50 | -0.84 | -0.23 | -0.12 |
| X3 | -0.53 | -0.11 | 1.00 | 0.27 | 0.54 | 0.03 | 0.71 | 0.73 |
| X4 | -0.25 | 0.17 | 0.27 | 1.00 | 0.18 | 0.02 | 0.45 | 0.32 |
| X5 | -0.63 | -0.50 | 0.54 | 0.18 | 1.00 | 0.35 | 0.61 | 0.42 |
| X6 | -0.23 | -0.84 | 0.03 | 0.02 | 0.35 | 1.00 | 0.15 | -0.03 |
| X7 | -0.53 | -0.23 | 0.71 | 0.45 | 0.61 | 0.15 | 1.00 | 0.93 |
| X8 | -0.36 | -0.12 | 0.73 | 0.32 | 0.42 | -0.03 | 0.93 | 1.00 |
Versiones gráficas de R(X)
Usando el paquete PerformanceAnalytics
library(PerformanceAnalytics)
chart.Correlation(as.matrix(mat_X),histogram = TRUE,pch=12)
Usando el paquete corrplot
Ejemplo de clases
library(corrplot)
library(grDevices)
library(Hmisc)
Mat_R<-rcorr(as.matrix(mat_X))
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))
Ejemplo Anexo 1
library(corrplot)
library(grDevices)
library(Hmisc)
Mat_R<-rcorr(as.matrix(mat_X))
corrplot(Mat_R$r,
p.mat = Mat_R$r,
type="lower",
tl.col="black",
tl.srt = 90,
pch.col = "black",
insig = "p-value",
sig.level = -1,
col = heat.colors(11))
Ejemplo anexo 2
library(corrplot)
library(grDevices)
library(Hmisc)
Mat_R<-rcorr(as.matrix(mat_X))
corrplot(Mat_R$r,
p.mat = Mat_R$r,
method = "color",
type= "full",
tl.col="black",
tl.srt = 90,
order = "hclust", addrect = 3,
pch.col = "blue",
insig = "p-value",
sig.level = -1,
col = c("green","turquoise","gray","violet"))
Ejemplo anexo 3
library(corrplot)
library(grDevices)
library(Hmisc)
Mat_R<-rcorr(as.matrix(mat_X))
corrplot.mixed(Mat_R$r,
upper = "pie",
lower = "color",
p.mat = Mat_R$r,
tl.col="Black",
tl.srt = 0,
order = "hclust", addrect = 3,
pch.col = "black",
insig = "p-value",
sig.level = -1,
)