Curso: Estadística Computacional

Integrantes:

  • Nestor Abel Lopez Lazaro 20180264
  • Alvaro Cesar Tauma Salvador 20181160

1 Aplicación multivariada de Bootstrap

El archivo hatco.txt contiene datos que corresponden a 100 observaciones de 7 variables influyentes en la elección de distribuidor de una empresa .

se registran los siguientes atributos:

  • X1: Velocidad de entrega

  • X2: Nivel de precios

  • X3: Flexibilidad de precios

  • X4: Imagen del fabricante

  • X5: Servicio conjunto

  • X6: Imagen fuerza de ventas

  • X7: Calidad de producto

datos <- read.table(file = "hatco.txt", header = TRUE)
head(datos)
##   id  x1  x2  x3  x4   x5  x6  x7
## 1  1 4.1 0.6 6.9 4.7 2.35 5.2 2.4
## 2  2 1.8 3.0 6.3 6.6 4.00 8.4 2.5
## 3  3 3.4 5.2 5.7 6.0 2.70 8.2 4.3
## 4  4 2.7 1.0 7.1 5.9 2.30 7.8 1.8
## 5  5 6.0 0.9 9.6 7.8 4.60 4.5 3.4
## 6  6 1.9 3.3 7.9 4.8 1.90 9.7 2.6

1.1 Función para hallar la matriz de correlaciones

corMatrix <- function(data) {
  cor <- cor(data[,2:8])
  return(cor)
}

corMatrix(datos)
##             x1         x2          x3         x4          x5         x6
## x1  1.00000000 -0.3492251  0.50929519  0.0504142  0.07742782 -0.4826309
## x2 -0.34922515  1.0000000 -0.48721259  0.2721868  0.18533024  0.4697458
## x3  0.50929519 -0.4872126  1.00000000 -0.1161041 -0.03479587 -0.4481120
## x4  0.05041420  0.2721868 -0.11610408  1.0000000  0.78813516  0.1999811
## x5  0.07742782  0.1853302 -0.03479587  0.7881352  1.00000000  0.1766130
## x6 -0.48263094  0.4697458 -0.44811201  0.1999811  0.17661305  1.0000000
## x7  0.61190069  0.5129808  0.06661728  0.2986774  0.24042771 -0.0551613
##             x7
## x1  0.61190069
## x2  0.51298082
## x3  0.06661728
## x4  0.29867737
## x5  0.24042771
## x6 -0.05516130
## x7  1.00000000
library(psych)
cor.plot(cor(datos[,-1]),
         main="Mapa de Calor", 
         diag=F,
         show.legend = TRUE) 

1.2 Función para estimar la matriz de correlaciones mediante Bootstrap

boot4<-function(datos,B,medida,...){   # Caso matriz -> matriz
  n<-nrow(datos)                                   # nro. de filas 
  p<-ncol(datos)-1                                 # nro. de columnas
  esta<-array(data = rep(0,p*p*B),dim = c(p,p,B))  # crea  B matrices de  pxp
  esboot<-matrix(0,p,p)
  eeboot<-matrix(0,p,p)
  cvboot <- matrix(0,p,p)
  for(i in 1:B){
    indices<- sample(n,n,T)
    esta[,,i]<-medida(datos[indices,],...)         # guardando la m. correlacion de la muestra "i"
     }
  for (i in 1:p) {
    for (j in 1:p) {
      esboot[i,j] <- mean(esta[i,j,])              # media de las "B" capas de matrices de correlacion
      eeboot[i,j] <- sd(esta[i,j,])                # sd de las "B" capas de matrices de correlacion
      cvboot[i,j] <- eeboot[i,j]*100/abs(esboot[i,j])
    }
  }
  estimado<-medida(datos,...)
  return(list(estimado=estimado,esboot=esboot,eeboot=eeboot,cvboot=cvboot))
}

1.3 Bootstrap multivariado

RNGkind(sample.kind = "Rejection")
set.seed(11)
B <- boot4(datos,1000, corMatrix)
B
## $estimado
##             x1         x2          x3         x4          x5         x6
## x1  1.00000000 -0.3492251  0.50929519  0.0504142  0.07742782 -0.4826309
## x2 -0.34922515  1.0000000 -0.48721259  0.2721868  0.18533024  0.4697458
## x3  0.50929519 -0.4872126  1.00000000 -0.1161041 -0.03479587 -0.4481120
## x4  0.05041420  0.2721868 -0.11610408  1.0000000  0.78813516  0.1999811
## x5  0.07742782  0.1853302 -0.03479587  0.7881352  1.00000000  0.1766130
## x6 -0.48263094  0.4697458 -0.44811201  0.1999811  0.17661305  1.0000000
## x7  0.61190069  0.5129808  0.06661728  0.2986774  0.24042771 -0.0551613
##             x7
## x1  0.61190069
## x2  0.51298082
## x3  0.06661728
## x4  0.29867737
## x5  0.24042771
## x6 -0.05516130
## x7  1.00000000
## 
## $esboot
##             [,1]       [,2]        [,3]        [,4]        [,5]        [,6]
## [1,]  1.00000000 -0.3523156  0.51107898  0.04886231  0.07277115 -0.48296715
## [2,] -0.35231558  1.0000000 -0.48929555  0.27336517  0.18565602  0.46795581
## [3,]  0.51107898 -0.4892956  1.00000000 -0.11455090 -0.03487882 -0.44707589
## [4,]  0.04886231  0.2733652 -0.11455090  1.00000000  0.78391678  0.20101349
## [5,]  0.07277115  0.1856560 -0.03487882  0.78391678  1.00000000  0.17955747
## [6,] -0.48296715  0.4679558 -0.44707589  0.20101349  0.17955747  1.00000000
## [7,]  0.61114938  0.5066393  0.06880257  0.29791534  0.23654202 -0.05914982
##             [,7]
## [1,]  0.61114938
## [2,]  0.50663935
## [3,]  0.06880257
## [4,]  0.29791534
## [5,]  0.23654202
## [6,] -0.05914982
## [7,]  1.00000000
## 
## $eeboot
##            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
## [1,] 0.00000000 0.07710370 0.05560500 0.09918620 0.10741643 0.07206883
## [2,] 0.07710370 0.00000000 0.07279446 0.09662679 0.09316906 0.06523080
## [3,] 0.05560500 0.07279446 0.00000000 0.10025403 0.10474332 0.06983001
## [4,] 0.09918620 0.09662679 0.10025403 0.00000000 0.03772650 0.10201428
## [5,] 0.10741643 0.09316906 0.10474332 0.03772650 0.00000000 0.10158881
## [6,] 0.07206883 0.06523080 0.06983001 0.10201428 0.10158881 0.00000000
## [7,] 0.06047968 0.07004007 0.10018446 0.09517001 0.08885541 0.10913816
##            [,7]
## [1,] 0.06047968
## [2,] 0.07004007
## [3,] 0.10018446
## [4,] 0.09517001
## [5,] 0.08885541
## [6,] 0.10913816
## [7,] 0.00000000
## 
## $cvboot
##            [,1]     [,2]      [,3]       [,4]       [,5]      [,6]       [,7]
## [1,]   0.000000 21.88484  10.87992 202.991230 147.608532  14.92210   9.896055
## [2,]  21.884840  0.00000  14.87740  35.347147  50.183699  13.93952  13.824444
## [3,]  10.879923 14.87740   0.00000  87.519200 300.306417  15.61928 145.611520
## [4,] 202.991230 35.34715  87.51920   0.000000   4.812565  50.74997  31.945321
## [5,] 147.608532 50.18370 300.30642   4.812565   0.000000  56.57732  37.564324
## [6,]  14.922098 13.93952  15.61928  50.749967  56.577321   0.00000 184.511388
## [7,]   9.896055 13.82444 145.61152  31.945321  37.564324 184.51139   0.000000

1.4 Matriz estimada vs Matriz Bootstrap

library(psych)
library(grid)
library(gridExtra)

estimado <- cor.plot(cor(datos[,-1]),
         main="Matriz de correlaciones Estimado ", 
         diag=F,
         show.legend = TRUE) 

bootstr <- cor.plot(r = B$esboot,
         main="Matriz de correlaciones Bootstrap", 
         diag=F,
         show.legend = TRUE) 

2 Función para estimar la matriz de correlaciones mediante Jackknife

jack4<-function(datos,medida,...){
  n<-nrow(datos) 
  p<-ncol(datos)-1 
  esta<-array(data = rep(0,p*p*n),dim = c(p,p,n))#crea  n matrices de  px p
  esjack<-matrix(0,p,p)
  eejack<-matrix(0,p,p)
  cvjack<-matrix(0,p,p)
  for(i in 1:n){
    esta[,,i]<-medida(datos[-i,],...) # se extrae muestras jackknife quitando un valor de la muestra
                                      # aplicando a estas muestras la medida creada (matriz de correlacion)
                                     # asignando  estad superponiendo las n matrices de ceros con las n  medidas (matrices de correlacion)
  }
  for (i in 1:p) {
    for (j in 1:p) {
      esjack[i,j] <- mean(esta[i,j,])# esta[i,j,] :# vector correlaciones de i con j de sacadas de todas las n matrices de correlaciones muestrajack                                      #  Se le agrega  el valor mean(esta[i,j,])  a la posicion i,j de la matriz estimada jack
      eejack[i,j] <- (n-1)*sd(esta[i,j,])/sqrt(n) # de la misma forma  con el error 
      cvjack[i,j] <- eejack[i,j]*100/abs(esjack[i,j])
    }
  }
  estimado<-medida(datos,...)
  return(list(estimado=estimado,esjack=esjack,eejack=eejack,cvjack=cvjack))
}

jack4(datos,corMatrix)
## $estimado
##             x1         x2          x3         x4          x5         x6
## x1  1.00000000 -0.3492251  0.50929519  0.0504142  0.07742782 -0.4826309
## x2 -0.34922515  1.0000000 -0.48721259  0.2721868  0.18533024  0.4697458
## x3  0.50929519 -0.4872126  1.00000000 -0.1161041 -0.03479587 -0.4481120
## x4  0.05041420  0.2721868 -0.11610408  1.0000000  0.78813516  0.1999811
## x5  0.07742782  0.1853302 -0.03479587  0.7881352  1.00000000  0.1766130
## x6 -0.48263094  0.4697458 -0.44811201  0.1999811  0.17661305  1.0000000
## x7  0.61190069  0.5129808  0.06661728  0.2986774  0.24042771 -0.0551613
##             x7
## x1  0.61190069
## x2  0.51298082
## x3  0.06661728
## x4  0.29867737
## x5  0.24042771
## x6 -0.05516130
## x7  1.00000000
## 
## $esjack
##             [,1]       [,2]        [,3]        [,4]        [,5]        [,6]
## [1,]  1.00000000 -0.3492358  0.50929395  0.05041027  0.07740073 -0.48261561
## [2,] -0.34923584  1.0000000 -0.48719710  0.27216711  0.18533909  0.46974602
## [3,]  0.50929395 -0.4871971  1.00000000 -0.11608848 -0.03480093 -0.44810840
## [4,]  0.05041027  0.2721671 -0.11608848  1.00000000  0.78811407  0.20000242
## [5,]  0.07740073  0.1853391 -0.03480093  0.78811407  1.00000000  0.17664774
## [6,] -0.48261561  0.4697460 -0.44810840  0.20000242  0.17664774  1.00000000
## [7,]  0.61187793  0.5129521  0.06663315  0.29865280  0.24040993 -0.05515645
##             [,7]
## [1,]  0.61187793
## [2,]  0.51295209
## [3,]  0.06663315
## [4,]  0.29865280
## [5,]  0.24040993
## [6,] -0.05515645
## [7,]  1.00000000
## 
## $eejack
##            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
## [1,] 0.00000000 0.07836248 0.05530565 0.10109332 0.10996978 0.07320655
## [2,] 0.07836248 0.00000000 0.07222304 0.10035536 0.09586711 0.06366594
## [3,] 0.05530565 0.07222304 0.00000000 0.10530581 0.10942184 0.07274459
## [4,] 0.10109332 0.10035536 0.10530581 0.00000000 0.03728309 0.10213815
## [5,] 0.10996978 0.09586711 0.10942184 0.03728309 0.00000000 0.10220987
## [6,] 0.07320655 0.06366594 0.07274459 0.10213815 0.10220987 0.00000000
## [7,] 0.06137925 0.07067541 0.10140261 0.09807979 0.09083831 0.11083325
##            [,7]
## [1,] 0.06137925
## [2,] 0.07067541
## [3,] 0.10140261
## [4,] 0.09807979
## [5,] 0.09083831
## [6,] 0.11083325
## [7,] 0.00000000
## 
## $cvjack
##           [,1]     [,2]      [,3]       [,4]       [,5]      [,6]      [,7]
## [1,]   0.00000 22.43827  10.85928 200.541132 142.078481  15.16871  10.03129
## [2,]  22.43827  0.00000  14.82419  36.872701  51.725255  13.55327  13.77817
## [3,]  10.85928 14.82419   0.00000  90.711680 314.422209  16.23370 152.18042
## [4,] 200.54113 36.87270  90.71168   0.000000   4.730672  51.06846  32.84074
## [5,] 142.07848 51.72526 314.42221   4.730672   0.000000  57.86084  37.78476
## [6,]  15.16871 13.55327  16.23370  51.068459  57.860843   0.00000 200.94339
## [7,]  10.03129 13.77817 152.18042  32.840741  37.784757 200.94339   0.00000

2.1 RESULTADOS ESTIMADO , BOOTSTRAP y JACKKNIFE

RNGkind(sample.kind = "Rejection")
set.seed(11)
B <- boot4(datos,1000, corMatrix)
B$estimado
##             x1         x2          x3         x4          x5         x6
## x1  1.00000000 -0.3492251  0.50929519  0.0504142  0.07742782 -0.4826309
## x2 -0.34922515  1.0000000 -0.48721259  0.2721868  0.18533024  0.4697458
## x3  0.50929519 -0.4872126  1.00000000 -0.1161041 -0.03479587 -0.4481120
## x4  0.05041420  0.2721868 -0.11610408  1.0000000  0.78813516  0.1999811
## x5  0.07742782  0.1853302 -0.03479587  0.7881352  1.00000000  0.1766130
## x6 -0.48263094  0.4697458 -0.44811201  0.1999811  0.17661305  1.0000000
## x7  0.61190069  0.5129808  0.06661728  0.2986774  0.24042771 -0.0551613
##             x7
## x1  0.61190069
## x2  0.51298082
## x3  0.06661728
## x4  0.29867737
## x5  0.24042771
## x6 -0.05516130
## x7  1.00000000
B$esboot
##             [,1]       [,2]        [,3]        [,4]        [,5]        [,6]
## [1,]  1.00000000 -0.3523156  0.51107898  0.04886231  0.07277115 -0.48296715
## [2,] -0.35231558  1.0000000 -0.48929555  0.27336517  0.18565602  0.46795581
## [3,]  0.51107898 -0.4892956  1.00000000 -0.11455090 -0.03487882 -0.44707589
## [4,]  0.04886231  0.2733652 -0.11455090  1.00000000  0.78391678  0.20101349
## [5,]  0.07277115  0.1856560 -0.03487882  0.78391678  1.00000000  0.17955747
## [6,] -0.48296715  0.4679558 -0.44707589  0.20101349  0.17955747  1.00000000
## [7,]  0.61114938  0.5066393  0.06880257  0.29791534  0.23654202 -0.05914982
##             [,7]
## [1,]  0.61114938
## [2,]  0.50663935
## [3,]  0.06880257
## [4,]  0.29791534
## [5,]  0.23654202
## [6,] -0.05914982
## [7,]  1.00000000
J <- jack4(datos,corMatrix)
J$esjack
##             [,1]       [,2]        [,3]        [,4]        [,5]        [,6]
## [1,]  1.00000000 -0.3492358  0.50929395  0.05041027  0.07740073 -0.48261561
## [2,] -0.34923584  1.0000000 -0.48719710  0.27216711  0.18533909  0.46974602
## [3,]  0.50929395 -0.4871971  1.00000000 -0.11608848 -0.03480093 -0.44810840
## [4,]  0.05041027  0.2721671 -0.11608848  1.00000000  0.78811407  0.20000242
## [5,]  0.07740073  0.1853391 -0.03480093  0.78811407  1.00000000  0.17664774
## [6,] -0.48261561  0.4697460 -0.44810840  0.20000242  0.17664774  1.00000000
## [7,]  0.61187793  0.5129521  0.06663315  0.29865280  0.24040993 -0.05515645
##             [,7]
## [1,]  0.61187793
## [2,]  0.51295209
## [3,]  0.06663315
## [4,]  0.29865280
## [5,]  0.24040993
## [6,] -0.05515645
## [7,]  1.00000000
estimado <- cor.plot(cor(datos[,-1]),
         main="Matriz de correlaciones Estimado ", 
         diag=F,
         show.legend = TRUE) 

bootest <- cor.plot(r = B$esboot,
         main="Matriz de correlaciones Bootstrap", 
         diag=F,
         show.legend = TRUE) 

jackest<- cor.plot(r = J$esjack,
         main="Matriz de correlaciones Jackknife", 
         diag=F,
         show.legend = TRUE)