####################################################
######   Exploratory Techniques R Commands   #######
######       Correspondence Analysis         #######
####################################################

## Correspondence analysis helps identify associations in complex data

## The packages needed for these techniques are: 
MASS
ca
FactoMineR
explor

## Don’t forget you need to **install** packages just once, 
## but need to **load** them every time you restart R (if you use them)

###################################################
###  Multiple Correspondence Analysis in MASS  ####
###################################################

# Multiple Correspondence Analysis in MASS is the easiest to apply, 
# But gives very simple (ugly) graphics and no numerical info.

# load the MASS library /  package (this only needs to be done once per session)
library(MASS)

# load the data
df <- read.table(file.choose(), header=T, sep="\t")

# check the data
summary(df)

# Perform Multiple Correspondence Analysis
MCA <- mca(df, abbrev = T)
plot(MCA, rows = F, col = 1, cex = 1)

#############################################
### Binary Correspondence Analysis  in ca ###
#############################################

# The package ca gives you a mathematical output 
# The output tells you about the reliability of the plo
# Binary correspondence analysis is easier to interpet 
# But it can hide important correlations in the data

# load the library / package ca (this only needs to be done once per session)
library (ca)

# load the data
df <- read.table(file.choose(), header=T, sep="\t")

# convert the data to cross-table 
# make sure you have run the script - see file data frame to stab script
xtab = stack.fnc(df)

# check data
xtab

# perform Binary Correspondence
BCA = ca(xtab)
plot(BCA, col= 1)
summary(BCA)

#################################################
###  Multiple Correspondence Analysis  in ca  ###
#################################################

# The ca library has a range of different algorithms for doing the analysis,
# some of these allow us to have numerical output for correspondence analysis

# load the MASS library /  package (this only needs to be done once per session)
library (ca)

# load the data
df <- read.table(file.choose(), header=T, sep="\t")

# check the data
summary(df)

# Burt MCA
MCA_Burt <- mjca(df, lambda= "Burt")
plot(MCA_Burt, labels= c(0, 2), col= c("white", "black")) 
summary(MCA_Burt)

# Joint MCA
MCA_Joint <- mjca(df, lambda= "JCA")
plot(MCA_Joint, labels= c(0, 2), col= c("white", "black")) 
summary(MCA_Joint)

# Adjusted MCA
MCA_ADj <- mjca(df, lambda= "adjusted")
plot(MCA_ADj, labels= c(0, 2), col= c("white", "black")) 
summary(MCA_ADj)

##########################################################
###  Multiple Correspondence Analysis in FactoMineR   ####
##########################################################

# Good for very detailed numeric output

library(FactoMineR)
library(explor)

# load the data
df <- read.table(file.choose(), header=T, sep="\t")

# check the data
summary(df)

# Perform Multiple Correspondence Analysis
MCAFac = MCA(df, graph= F)
explor(MCAFac)