El análisis de correspondencias simples (ACS)

La metodología la desarrolló Benzecri, a principios de los años 60 del siglo XX en la Universidad de Renner (Francia). En esencia. es un tipo especial de análisis de componentes principales pero realizado sobre una tabla de contingencia y usando una distancia euclídea ponderada llamada chi-cuadrado

Ejemplo: supongamos 400 tiendas de discos repartidas entre los países de la U.E. Se clasifica a los compradores en 3 categorías distintas: Jóvenes, Edad Media, Mayores, y a los tipos de música en 5 tipos:

\[\begin{array}{ccccc} & \text { Jov } & \text { Med } & \text { May } & \text { Total } \\ \text { A } & 70 & 0 & 0 & 70 \\ \text { B } & 45 & 45 & 0 & 90 \\ \text { C } & 30 & 30 & 30 & 90 \\ \text { D } & 0 & 80 & 20 & 100 \\ \text { E } & 35 & 5 & 10 & 50 \\ \text { Total } & 180 & 160 & 60 & 400 \end{array}\] \[\begin{array}{cccc} & \text { Jov } & \text { Med } & \text { May } \\ \text { A } & 1 & 0 & 0 \\ \text { B } & 0.5 & 0.5 & 0 \\ \text { C } & 0.33 & 0.33 & 0.33 \\ \text { D } & 0 & 0.8 & 0.2 \\ \text { E } & 0.7 & 0.1 & 0.2 \\ \text { Total } & 0.45 & 0.40 & 0.15 \end{array}\]

Si nos centramos en las columnas

\[\begin{array}{lcccc} & \text { Jov } & \text { Med } & \text { Mayores} & \text { Total } \\ \text { A } & 0.39 & 0 & 0 & 0.175 \\ \text { B } & 0.25 & 0.28 & 0 & 0.225 \\ \text { C } & 0.17 & 0.19 & 0.50 & 0.225 \\ \text { D } & 0 & 0.50 & 0.33 & 0.25 \\ \text { E } & 0.19 & 0.03 & 0.17 & 0.125 \end{array}\]

Por ejemplo de los 160 compradores en el caso de los de mediana edad, un 50% compra el tipo de música \(D\) en vez del porcentaje general del 25%.

Independencia

Si el hecho de que aparezca o se presente una categoría junto con otra no es ni más ni menos probable de que se presenten las dos categorías por separado, se dice que las variables son independientes y, en general, se dice que la tabla es homogénea.

Dadas dos variables aleatorias \(X\) e \(Y\) , son independientes si

\[P\left(X=x_{i}, Y=y_{j}\right)=P\left(X=x_{i}\right) \cdot P\left(Y=y_{j}\right)\]

para todo (\(i,j\))

\[ \begin{aligned} p_{i j} &=\frac{n_{i j}}{n . .} \\ p_{i .} &=\frac{n_{i .}}{n . .} \\ p_{. j} &=\frac{n_{. j}}{n . .} \end{aligned} \] Así, si \[ P\left(X=x_{i}, Y=y_{j}\right)=p_{i j}=p_{i} \cdot \times p_{\cdot j} \]

para todo \(i\), \(j\), las variables \(X\) e \(Y\) son independientes y la tabla es homogénea

En el caso de de ser cierta la hipótesis de independencia esperaremos encontrar \(E_{ij}\) objetos dentro de la casilla \((i, j)- ésima\), donde

\[ E_{i j}=n.. p_{i j}=n.. p_{i\cdot} p_{\cdot j}=\frac{n_{i\cdot} n_{\cdot j}}{n_{. .}} \]

Contraste Chi cuadrado

contraste o test que me mida las distancias entre lo que uno observa y lo que esperaría si se cumple la hipótesis nula de independencia

\[ \chi^{2}=\sum_{i=1}^{r} \sum_{j=1}^{c} \frac{\left(n_{i j}-\frac{n_{i \cdot} n_{\cdot j}}{n_{. .}}\right)^{2}}{\frac{n_{i \cdot} n_{\cdot j}}{n_{..}}} \]

Distancia chi cuadrado En una tabla de contingencia donde hay \(r\) filas y \(c\) columnas se puede escribir como

$$ \[\begin{aligned} &\text { Columnas }\\ &\begin{array}{c|c|c|c|c|c} & 1 & 2 & \cdots & c & \\ \hline 1 & n_{11} & n_{12} & \cdots & n_{1 c} & n_{1} . \\ & n_{21} & n_{22} & \cdots & n_{2 c} & n_{2} . \\ & \vdots & \vdots & \vdots & \vdots & \vdots \\ r & n_{r 1} & n_{r 2} & \cdots & n_{r c} & n_{r} . \\ \hline & n_{.1} & n_{.2} & \cdots & n_{. c} & n_{. .} \end{array} \end{aligned}\]

$$

\[ \begin{aligned} &\text { Columnas } \\ &\begin{array}{|c|c|c|c|} 1& 2 & & c \\ \hline \frac{n_{11}}{n_{1} .} & p_{12}=\frac{n_{12}}{n_{1} .} & \cdots & p_{1 c}=\frac{n_{1 c}}{n_{1}} \\ \hline \frac{n_{21}}{n_{2}} & p_{22}=\frac{n_{22}}{n_{2}} & \cdots & p_{2 c}=\frac{n_{2 c}}{n_{2}} \\ \hline & \vdots & \vdots & \vdots \\ \hline \frac{n_{r 1}}{n_{r}} & p_{r 2}=\frac{n_{r 2}}{n_{r}} & \cdots & p_{r c}=\frac{n_{r c}}{n_{r}} \\ \hline \end{array} \end{aligned} \]

\[ \begin{aligned} &\text { Columnas } \\ &\begin{array}{c|c|c|c|} 1 & 2& & c \\ \hline q_{11}= \frac{n_{11}}{n_{.1}} & q_{12}=\frac{n_{12}}{n_{.2}} & \cdots & q_{1 c}=\frac{n_{1 c}}{n_{. c}} \\ \hline q_{21}= \frac{n_{21}}{n_{.2}} & q_{22}=\frac{n_{22}}{n_{.2}} & \cdots & q_{2 c}=\frac{n_{2 c}}{n_{. c}} \\ \hline \vdots & \vdots & \vdots \\ \hline & & \vdots & \vdots \\ \hline \end{array} \end{aligned} \]

La distancia chi cuadrado entre las columnas \(i\) y \(j\) se define, entonces,como

\[ d_{i j}^{\text {col }}=\sum_{k=1}^{r} \frac{1}{p_{k} \cdot}\left(p_{k i}-p_{k j}\right)^{2} \]

donde

\[ p_{k \cdot}=\frac{n_{k \cdot}}{n . .} \]

Se puede definir una distancia similar entre dos filas \(i\) y \(j\) \[ d_{i j}^{\mathrm{fil}}=\sum_{k=1}^{c} \frac{1}{q_{k}}\left(q_{i k}-q_{j k}\right)^{2} \] donde \[ q_{\cdot k}=\frac{n_{\cdot k}}{n_{\cdot .}} \]

Cargar las librerias

library("factoextra")
library("FactoMineR")
library("gplots")
library("dplyr")

Tabla de contingencias observadas

O=matrix(c(11,3,8,2,9,14,12,13,28), nrow = 3, byrow = T)


colnames(O)=c("Mano der", "Mano izq", "Manos iguales")

rownames(O)=c("pie der", "pie izq", "pies iguales")
O
##              Mano der Mano izq Manos iguales
## pie der            11        3             8
## pie izq             2        9            14
## pies iguales       12       13            28

prueba chi cuadrado

O_chisq = chisq.test(O)
O_chisq
## 
##  Pearson's Chi-squared test
## 
## data:  O
## X-squared = 11.942, df = 4, p-value = 0.01779

con una significancia del 5% hay evidencia para decir que el tama?o de manos y pies es dependiente

O_chisq$expected
##              Mano der Mano izq Manos iguales
## pie der          5.50     5.50          11.0
## pie izq          6.25     6.25          12.5
## pies iguales    13.25    13.25          26.5

##Parte 2 Analisis de correspondencia

library("gplots")  # tabla de cotingencia plot
balloonplot(as.table(O),label=T)

El tama?o del punto indica la relaci?n

Analisis de correspondencia

O_CA=CA(O, graph = T)

O_CA
## **Results of the Correspondence Analysis (CA)**
## The row variable has  3  categories; the column variable has 3 categories
## The chi square of independence between the two variables is equal to 11.94209 (p-value =  0.01778711 ).
## *The results are available in the following objects:
## 
##    name              description                   
## 1  "$eig"            "eigenvalues"                 
## 2  "$col"            "results for the columns"     
## 3  "$col$coord"      "coord. for the columns"      
## 4  "$col$cos2"       "cos2 for the columns"        
## 5  "$col$contrib"    "contributions of the columns"
## 6  "$row"            "results for the rows"        
## 7  "$row$coord"      "coord. for the rows"         
## 8  "$row$cos2"       "cos2 for the rows"           
## 9  "$row$contrib"    "contributions of the rows"   
## 10 "$call"           "summary called parameters"   
## 11 "$call$marge.col" "weights of the columns"      
## 12 "$call$marge.row" "weights of the rows"
O_CA$eig
##        eigenvalue percentage of variance cumulative percentage of variance
## dim 1 0.117529499              98.416168                          98.41617
## dim 2 0.001891427               1.583832                         100.00000
get_eigenvalue(O_CA)
##        eigenvalue variance.percent cumulative.variance.percent
## Dim.1 0.117529499        98.416168                    98.41617
## Dim.2 0.001891427         1.583832                   100.00000

Para ver graficamente

fviz_screeplot(O_CA,addlabels=T)

Relacion entre mano y componentes principales

O_CA$col$coord
##                    Dim 1       Dim 2
## Mano der       0.5831949  0.01416751
## Mano izq      -0.2996904  0.06502984
## Manos iguales -0.1417522 -0.03959867

Relacion entre pies y componentes principales

O_CA$row$coord
##                    Dim 1       Dim 2
## pie der       0.58100739  0.03568459
## pie izq      -0.41016232  0.05446898
## pies iguales -0.04770008 -0.04050539
fviz_ca_biplot(O_CA, repel = T)

Se puede observar que la componente principal 1 es

Ejemplo \(1\)

data("housetasks")
housetasks%>%head(15)%>%DT::datatable()

convertir la data como una tabla

df<-as.table(as.matrix(housetasks))
df
##            Wife Alternating Husband Jointly
## Laundry     156          14       2       4
## Main_meal   124          20       5       4
## Dinner       77          11       7      13
## Breakfeast   82          36      15       7
## Tidying      53          11       1      57
## Dishes       32          24       4      53
## Shopping     33          23       9      55
## Official     12          46      23      15
## Driving      10          51      75       3
## Finances     13          13      21      66
## Insurance     8           1      53      77
## Repairs       0           3     160       2
## Holidays      0           1       6     153
balloonplot(t(df), label=F, main="Tareas del hogar")

Prueba chi cuadrado

(prueba de hipotesis)

\(H_0:\) variables independientes (hipótesis nula)

\(H_1:\) variables son dependientes (hipopteis alternativa)

class(housetasks)
## [1] "data.frame"
class(housetasks$Jointly)
## [1] "integer"

prueba chicuadrado

chisq.test(housetasks)
## 
##  Pearson's Chi-squared test
## 
## data:  housetasks
## X-squared = 1944.5, df = 36, p-value < 2.2e-16

p valor.

\(valor\ p = 2.2e-16\) \(2.2\times10^{-16}\)

\(valor \ p= 2.2e-16 <0.05\)

se rechaza la hipotesis nula en favor de la alternativa,

Las parejas se organizan para hacer las tareas del hogar

Analisis de correspondencia

housetasks_CA= CA(housetasks, graph = T)

print(housetasks_CA)
## **Results of the Correspondence Analysis (CA)**
## The row variable has  13  categories; the column variable has 4 categories
## The chi square of independence between the two variables is equal to 1944.456 (p-value =  0 ).
## *The results are available in the following objects:
## 
##    name              description                   
## 1  "$eig"            "eigenvalues"                 
## 2  "$col"            "results for the columns"     
## 3  "$col$coord"      "coord. for the columns"      
## 4  "$col$cos2"       "cos2 for the columns"        
## 5  "$col$contrib"    "contributions of the columns"
## 6  "$row"            "results for the rows"        
## 7  "$row$coord"      "coord. for the rows"         
## 8  "$row$cos2"       "cos2 for the rows"           
## 9  "$row$contrib"    "contributions of the rows"   
## 10 "$call"           "summary called parameters"   
## 11 "$call$marge.col" "weights of the columns"      
## 12 "$call$marge.row" "weights of the rows"
housetasks_CA$col
## $coord
##                   Dim 1      Dim 2       Dim 3
## Wife        -0.83762154  0.3652207 -0.19991139
## Alternating -0.06218462  0.2915938  0.84858939
## Husband      1.16091847  0.6019199 -0.18885924
## Jointly      0.14942609 -1.0265791 -0.04644302
## 
## $contrib
##                 Dim 1     Dim 2      Dim 3
## Wife        44.462018 10.312237 10.8220753
## Alternating  0.103739  2.782794 82.5492464
## Husband     54.233879 17.786612  6.1331792
## Jointly      1.200364 69.118357  0.4954991
## 
## $cos2
##                   Dim 1     Dim 2       Dim 3
## Wife        0.801875947 0.1524482 0.045675847
## Alternating 0.004779897 0.1051016 0.890118521
## Husband     0.772026244 0.2075420 0.020431728
## Jointly     0.020705858 0.9772939 0.002000236
## 
## $inertia
## [1] 0.3010185 0.1178242 0.3813729 0.3147248
housetasks_CA$row
## $coord
##                 Dim 1      Dim 2       Dim 3
## Laundry    -0.9918368  0.4953220 -0.31672897
## Main_meal  -0.8755855  0.4901092 -0.16406487
## Dinner     -0.6925740  0.3081043 -0.20741377
## Breakfeast -0.5086002  0.4528038  0.22040453
## Tidying    -0.3938084 -0.4343444 -0.09421375
## Dishes     -0.1889641 -0.4419662  0.26694926
## Shopping   -0.1176813 -0.4033171  0.20261512
## Official    0.2266324  0.2536132  0.92336416
## Driving     0.7417696  0.6534143  0.54445849
## Finances    0.2707669 -0.6178684  0.03479681
## Insurance   0.6470759 -0.4737832 -0.28936051
## Repairs     1.5287787  0.8642647 -0.47208778
## Holidays    0.2524863 -1.4350066 -0.12958665
## 
## $contrib
##                 Dim 1      Dim 2       Dim 3
## Laundry    18.2867003  5.5638913  7.96842443
## Main_meal  12.3888433  4.7355230  1.85868941
## Dinner      5.4713982  1.3210221  2.09692603
## Breakfeast  3.8249284  3.6986131  3.06939857
## Tidying     1.9983518  2.9656441  0.48873403
## Dishes      0.4261663  2.8441170  3.63429434
## Shopping    0.1755248  2.5151584  2.22335679
## Official    0.5207837  0.7956201 36.94038942
## Driving     8.0778371  7.6468564 18.59638635
## Finances    0.8750075  5.5585460  0.06175066
## Insurance   6.1470616  4.0203590  5.25263863
## Repairs    40.7300940 15.8806509 16.59639139
## Holidays    1.0773030 42.4539986  1.21261994
## 
## $cos2
##                 Dim 1      Dim 2       Dim 3
## Laundry    0.73998741 0.18455213 0.075460467
## Main_meal  0.74160285 0.23235928 0.026037873
## Dinner     0.77664011 0.15370323 0.069656660
## Breakfeast 0.50494329 0.40023001 0.094826699
## Tidying    0.43981243 0.53501508 0.025172490
## Dishes     0.11811778 0.64615253 0.235729693
## Shopping   0.06365362 0.74765514 0.188691242
## Official   0.05304464 0.06642648 0.880528877
## Driving    0.43201860 0.33522911 0.232752289
## Finances   0.16067678 0.83666958 0.002653634
## Insurance  0.57601197 0.30880208 0.115185951
## Repairs    0.70673575 0.22587147 0.067392778
## Holidays   0.02979239 0.96235977 0.007847841
## 
## $inertia
##  [1] 0.13415976 0.09069235 0.03824633 0.04112368 0.02466697 0.01958732
##  [7] 0.01497017 0.05330000 0.10150885 0.02956446 0.05793584 0.31287411
## [13] 0.19631064
fviz_screeplot(housetasks_CA, addlabel=T)

El 89% de la varianzaa de las variables estan eplicadaas por las dimensiones \(1\) y \(2\)

fviz_ca_biplot(housetasks_CA,repel = T, axes=c(1,2))

  • Azul: filas

  • rojo: columnas

  • tareas como dinner, breafesast, laundry son realizadas con mas frecuencia por la esposa.

  • De las tareas Driving y repairs se encarga con mayor frecuencia el esposo.

  • juntos: holidays, finances, seguridad

  • alternan: Official

fviz_ca_biplot(housetasks_CA,repel = T, axes=c(1,3))

library("corrplot")
## corrplot 0.92 loaded
corrplot(housetasks_CA$col$cos2)

distancia \(cos^2\) para la variable de tareas

corrplot(housetasks_CA$row$cos2 )

  • Componente \(1\): tareas individuales realizadas dentro del hogar
  • Componente \(2\): tareas realizadas en conjunto dentro del hogar

Contribucion de cada asignacion de responsabilidad

fviz_contrib(housetasks_CA, choice = "col" ,axes = 1)

fviz_contrib(housetasks_CA, choice = "col" ,axes = 2)

fviz_contrib(housetasks_CA, choice = "col" ,axes = 1:2)

fviz_contrib(housetasks_CA, choice = "row" ,axes = 1)

fviz_contrib(housetasks_CA, choice = "row" ,axes = 2)

fviz_contrib(housetasks_CA, choice = "row" ,axes = 1:2)

fviz_ca_biplot(housetasks_CA,repel = T, arrow=c(F,T), col.col="cos2", gradient.cols=c("red", "yellow", "green"), alpha.col="contrib")
## Scale for 'colour' is already present. Adding another scale for 'colour',
## which will replace the existing scale.