Más aplicaciones estadísticas en:

https://rpubs.com/orlandoan

https://orlandomoscote.blogspot.com

1. Datos.

datos<-matrix(c(20,20,20,30,30,40,10,40,20,20,20,10,40,50,50,10,10,15,15,60),
byrow=TRUE,nrow=4)
dimnames(datos)<-list(edad=c("niño","joven","adulto","mayor"),sabor=c("A","B","C","D","E"))
tabla1<-as.table(datos);tabla1
##         sabor
## edad      A  B  C  D  E
##   niño   20 20 20 30 30
##   joven  40 10 40 20 20
##   adulto 20 10 40 50 50
##   mayor  10 10 15 15 60
balloonplot(t(tabla1),main="Compras",xlab="",ylab="",label=FALSE,show.margins=FALSE)

Totales para filas y columnas.

tabla2<-addmargins(tabla1);tabla2
##         sabor
## edad       A   B   C   D   E Sum
##   niño    20  20  20  30  30 120
##   joven   40  10  40  20  20 130
##   adulto  20  10  40  50  50 170
##   mayor   10  10  15  15  60 110
##   Sum     90  50 115 115 160 530

Porcentajes.

tabla3<-round(addmargins(prop.table(tabla1))*100,2);tabla3
##         sabor
## edad          A      B      C      D      E    Sum
##   niño     3.77   3.77   3.77   5.66   5.66  22.64
##   joven    7.55   1.89   7.55   3.77   3.77  24.53
##   adulto   3.77   1.89   7.55   9.43   9.43  32.08
##   mayor    1.89   1.89   2.83   2.83  11.32  20.75
##   Sum     16.98   9.43  21.70  21.70  30.19 100.00

Perfil fila.

tabla4<-round(addmargins(prop.table(tabla1,1),2),2);tabla4 
##         sabor
## edad        A    B    C    D    E  Sum
##   niño   0.17 0.17 0.17 0.25 0.25 1.00
##   joven  0.31 0.08 0.31 0.15 0.15 1.00
##   adulto 0.12 0.06 0.24 0.29 0.29 1.00
##   mayor  0.09 0.09 0.14 0.14 0.55 1.00

Perfil columna.

tabla5<-round(addmargins(prop.table(tabla1,2),1),2);tabla5 
##         sabor
## edad        A    B    C    D    E
##   niño   0.22 0.40 0.17 0.26 0.19
##   joven  0.44 0.20 0.35 0.17 0.12
##   adulto 0.22 0.20 0.35 0.43 0.31
##   mayor  0.11 0.20 0.13 0.13 0.38
##   Sum    1.00 1.00 1.00 1.00 1.00

Gráfico.

barplot(t(tabla4),col=rainbow(4),beside=TRUE,legend.text=TRUE,
args.legend=list(x = "topleft"))

barplot(t(tabla5),col=rainbow(4),beside=TRUE,legend.text=TRUE,
args.legend=list(x = "top"))

Prueba de independencia.

chi<-chisq.test(datos)
chi
## 
##  Pearson's Chi-squared test
## 
## data:  datos
## X-squared = 83.67, df = 12, p-value = 8.194e-13
mosaicplot(datos,shade=T)

corre <-CA(datos, graph=TRUE);corre

## **Results of the Correspondence Analysis (CA)**
## The row variable has  4  categories; the column variable has 5 categories
## The chi square of independence between the two variables is equal to 83.67042 (p-value =  8.194037e-13 ).
## *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"
get_eigenvalue(corre)
##       eigenvalue variance.percent cumulative.variance.percent
## Dim.1 0.10446839         66.17422                    66.17422
## Dim.2 0.03080587         19.51360                    85.68782
## Dim.3 0.02259446         14.31218                   100.00000
fviz_eig(corre)

get_ca_row(corre)
## Correspondence Analysis - Results for rows
##  ===================================================
##   Name       Description                
## 1 "$coord"   "Coordinates for the rows" 
## 2 "$cos2"    "Cos2 for the rows"        
## 3 "$contrib" "contributions of the rows"
## 4 "$inertia" "Inertia of the rows"
get_ca_col(corre)
## Correspondence Analysis - Results for columns
##  ===================================================
##   Name       Description                   
## 1 "$coord"   "Coordinates for the columns" 
## 2 "$cos2"    "Cos2 for the columns"        
## 3 "$contrib" "contributions of the columns"
## 4 "$inertia" "Inertia of the columns"
fviz_ca_biplot(corre)