lunes, 14 de septiembre de 2009

Análisis de Correspondencia en R

Correspondencias3

Correspondencias4

Aplicaciones-Ejemplos
Aplicaciones-Ejemplos


# Correspondence Analysis
#cargar los paquetes necesarios para el análisis
library(ade4)
library(ca)
#cargar datos
datos<-read.table("correspondencias3.txt", header=TRUE)
attach(datos)
#crear una tabla de 2 vías o una tabla de contengencia
i) write.table(T,"T4.txt",sep=";")
mydata<-read.table("T4.txt",sep=";")
mydata<-table(datos,exclude=c(NA,NaN)) #Otra posibilidad ;mydata=datos
ii)mytable<-with(datos,table(categoria,consumo)) #ej:A=categoria, B=consumo; A and B are categorical factors.
prop.table(mytable, 1) # row percentages
prop.table(mytable, 2) # column percentages
> T<-mytable
> dimnames(T)<-NULL
> rownames(T)<-c("Blanca","Negra","Otra")
> colnames(T)<-c("Nor-Este","Sur-Este","Oeste")
#aplicar el análisis de correspondencia
i) acs<-dudi.coa(df=mydata,scann=FALSE,nf=3)
> acs
Duality diagramm
class: coa dudi
$call: dudi.coa(df = datos)

$nf: 2 axis-components saved
$rank: 2
eigen values: 0.04332 0.004576 #valores de la inercia
vector length mode content
1 $cw 3 numeric column weights
2 $lw 4 numeric row weights
3 $eig 2 numeric eigen values

data.frame nrow ncol content
1 $tab 4 3 modified array
2 $li 4 2 row coordinates
3 $l1 4 2 row normed scores
4 $co 3 2 column coordinates
5 $c1 3 2 column normed scores
other elements: N

ii) fit <- ca(mytable,nd=3)
> fit
Principal inertias (eigenvalues):
1 2 3
Value 0.084191 0.015423 0.008126
Percentage 78.14% 14.32% 7.54%

Rows:
1 2 3 4 5
Mass 0.130081 0.126016 0.256098 0.341463 0.146341
ChiDist 0.414477 0.426678 0.264753 0.178265 0.479517
Inertia 0.022347 0.022942 0.017951 0.010851 0.033649
Dim. 1 -1.305799 -1.423420 0.791900 -0.228650 1.534124
Dim. 2 -1.330605 -0.654353 -0.332692 1.326862 -0.767570

Columns:
1 2 3 4
Mass 0.284553 0.203252 0.337398 0.174797
ChiDist 0.291026 0.339951 0.202878 0.514458
Inertia 0.024101 0.023489 0.013887 0.046263
Dim. 1 0.855732 1.032369 -0.465832 -1.694316
Dim. 2 -1.083358 0.602050 1.091590 -1.043475

#