#***************************************************************************
# Digit-tracking:  interpreting the evolution over time of sensory dimensions
#                  of an individual product space issued from holistic methods
# Package holos - Example: Card simulation
# version R: 3.1.2 - Updated: 14.11.2014
# Tam Minh Le & Sebastien Le
#***************************************************************************

rm(list=ls())

# Load packages
library(FactoMineR)
library(SensoMineR)
library(ggplot2)
library(reshape2)
library(holos)             # our package
library(knitr)             # pdf latex
options(max.print=5.5E5)   # max print


# Import data "final configuration" -------------------------------------
data(tabsimu)   # number of subject: J1=40, J2=30, J3=5, J4=5, J5=30
#head(tabsimu)

# Preliminary analysis --------------------------------------------------
card.mfa <- MFA(tabsimu, ncp = Inf, group=rep(2,ncol(tabsimu)/2), 
                name.group=c(paste("S", 1:(ncol(tabsimu)/2), sep="")), 
                graph=FALSE, type=rep("c",ncol(tabsimu)/2))
#card.mfa$eig

plot.MFA(card.mfa, choix="ind", palette=palette(c("black","black")), 
         cex=0.9)                                 # Fig.3a in the paper 

plot.MFA(card.mfa, choix="group", axes=c(1,2))    # Fig.3b in the paper

# Individual trajectory ------------------------------------------------

## Plot individual trajectory 

### J1
j1 <- digitable(ini.j1, dta.j1)
corr.j1 <- traject(tabsimu, j1, axes=c(1,2))      # Fig.4a in the paper  

dimtracking(corr.j1, 1:3)                         # Fig.5a in the paper
## Using id as id variables

### J2 
j2 <- digitable(ini.j2, dta.j2)
corr.j2 <- traject(tabsimu, j2, axes=c(1,2))      # Fig.4b in the paper 

dimtracking(corr.j2, 1:3)                         # Fig.5b in the paper
## Using id as id variables

### J3
j3 <- digitable(ini.j3, dta.j3)                    
corr.j3 <- traject(tabsimu, j3, axes=c(1,2))      # Fig.4c in the paper

dimtracking(corr.j3, 1:3)                         # Fig.4c in the paper          
## Using id as id variables

### J4
j4 <- digitable(ini.j4, dta.j4)
corr.j4 <- traject(tabsimu, j4, axes=c(1,2))      # Fig.4d in the paper

dimtracking(corr.j4, 1:3)                         # Fig.5d in the paper
## Using id as id variables

### J5
j5 <- digitable(ini.j5, dta.j5)
corr.j5 <- traject(tabsimu, j5, axes=c(1,2))      # Fig.4e in the paper

dimtracking(corr.j5, 1:3)                         # Fig.5e in the paper
## Using id as id variables

#***************************** END *********************************


# Draft -------------------------------------------------------------------
## J1
s.tracking <- step.tracking(dta.j1)[[2]] + 1      # +1: taking into account step0.
p <- dimtracking(corr.j1, 1:3)                    # Fig.5a in the paper   
## Using id as id variables
p + geom_vline(xintercept = s.tracking, colour="magenta", linetype = "longdash") +
  scale_x_discrete(labels=0:(nrow(corr.j1)-1))
## Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

## J2
s.tracking <- step.tracking(dta.j2)[[2]] + 1 
p <- dimtracking(corr.j2, 1:3)
## Using id as id variables
p + geom_vline(xintercept = s.tracking, colour="magenta", linetype = "longdash") +
  scale_x_discrete(labels=0:(nrow(corr.j2)-1))  
## Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

## J3
s.tracking <- step.tracking(dta.j3)[[2]] + 1 
p <- dimtracking(corr.j3, 1:3)
## Using id as id variables
p + geom_vline(xintercept = s.tracking, colour="magenta", linetype = "longdash") +
  scale_x_discrete(labels=0:(nrow(corr.j3)-1))    # Fig.5c in the paper
## Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

## J4
s.tracking <- step.tracking(dta.j4)[[2]] + 1 
p <- dimtracking(corr.j4, 1:3)
## Using id as id variables
p + geom_vline(xintercept = s.tracking, colour="magenta", linetype = "longdash") +
  scale_x_discrete(labels=0:(nrow(corr.j4)-1))
## Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.

## J5
s.tracking <- step.tracking(dta.j5)[[2]] + 1 
p <- dimtracking(corr.j5, 1:3)
## Using id as id variables
p + geom_vline(xintercept = s.tracking, colour="magenta", linetype = "longdash") +
    scale_x_discrete(labels=0:(nrow(corr.j5)-1))
## Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale.