#************************************************************************************
# Thesis title: "Napping: from sensory profiling technique to projective technique"
# Appendix 2: Bananas - Author: Tam Minh LE
# Supervisor : Francois Husson & Sebastien Le
# R version: 3.2.0 - Updated: 19.05.2015
#************************************************************************************

rm(list=ls())
options(max.print=5.5E5)   # max print


# Require package ---------------------------------------------------------

library(FactoMineR)
library(SensoMineR)
library(ggplot2)
library(scales)
library(reshape2)
library(holos)
library(knitr)
library(conjoint)   # traditional conjoint analysis with multiple linear reg.
library(AlgDesign)  # fractional factorial design
library(clusterSim) # segmentation
## Loading required package: cluster
## Loading required package: MASS
library(agricolae)  # design Graeco design


# Import sorting data  ----------------------------------------------------

wd <- "C:/Users/LE Minh-Tam/Dropbox/00. Thesis/Data"
setwd(wd)
list.files(pattern=".rda")
## [1] "ba.qual.rda"    "ba.sort.rda"    "cards.rda"      "emotion_fr.rda"
## [5] "emotion_vn.rda" "videotable.rda"
load("ba.sort.rda")
dim(ba.sort)
## [1]  9 96
# summary(ba.sort)    # 32 subjects


# Example for Introduction ------------------------------------------------

exp.design <- matrix(c(1, 1, 1, 1, 
                       1, 2, 3, 2, 
                       1, 3, 2, 3, 
                       2, 1, 3, 3,
                       2, 2, 2, 1,
                       2, 3, 1, 2, 
                       3, 1, 2, 2,
                       3, 2, 1, 3,
                       3, 3, 3, 1), ncol=4, nrow=9, byrow=TRUE) 

rownames(exp.design) <- paste("profile.", 1:9, sep="") 
colnames(exp.design) <- c("color", "shape", "bruise", "cigar")
exp.design <- as.data.frame(exp.design)
rownames(exp.design) <- rownames(ba.sort)

res.pca <- PCA(exp.design)

barplot(res.pca$eig[,2],main="Eigenvalues",names.arg=1:nrow(res.pca$eig),
        ylim=c(0,40), xlab="dimensions", ylab="Percentage variance")

# Factorial approach for sorting Task -------------------------------------

dat.sort <- ba.sort[,seq(3, ncol(ba.sort), by=3)]
res.fast <- fast(dat.sort, sep.words=";")

## Number of different words :  137
## using MCA
res.mca <- MCA(dat.sort)
barplot(res.mca$eig[,2], main="Eigenvalues", names.arg=1:nrow(res.mca$eig), 
        ylim=c(0,40), xlab="dimensions", ylab="Percentage variance")

# Conjoint analysis -------------------------------------------------------

## Create a data frame: levels of descriptors
color <- c("green", "green-yellow", "yellow")
shape <- c("high.heterogeneity", "heterogeneity", "homogeneity")
bruise <- c("high.bruise", "bruise", "non.bruise")
cigar.end.rot <- c("high.cigar", "med.cigar", "non.cigar")
lev.names <- data.frame(c(color, shape, bruise, cigar.end.rot)) 
colnames(lev.names) <- "levels"
lev.names
##                levels
## 1               green
## 2        green-yellow
## 3              yellow
## 4  high.heterogeneity
## 5       heterogeneity
## 6         homogeneity
## 7         high.bruise
## 8              bruise
## 9          non.bruise
## 10         high.cigar
## 11          med.cigar
## 12          non.cigar
## Create a data frame: quality score 
load("ba.qual.rda")
dim(ba.qual)
## [1]  9 96
#summary(ba.qual) 

quality <- ba.qual[,seq(1, ncol(ba.qual), by=3)] 
quality$id <- rownames(quality)
qual <- as.data.frame(melt(quality), quote=FALSE) # long.table
## Using id as id variables
qual.score <- qual[,3]


# Conjoint analysis -------------------------------------------------------
Conjoint(qual.score, exp.design, lev.names)
## 
## Call:
## lm(formula = frml)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -723,31 -131,70   -2,73  127,70  657,75 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        451,594     12,267  36,813  < 2e-16 ***
## factor(x$color)1   -43,500     17,349  -2,507   0,0127 *  
## factor(x$color)2    16,688     17,349   0,962   0,3369    
## factor(x$shape)1    -2,656     17,349  -0,153   0,8784    
## factor(x$shape)2  -106,385     17,349  -6,132 2,95e-09 ***
## factor(x$bruise)1 -173,125     17,349  -9,979  < 2e-16 ***
## factor(x$bruise)2  -71,240     17,349  -4,106 5,29e-05 ***
## factor(x$cigar)1   -16,500     17,349  -0,951   0,3424    
## factor(x$cigar)2    28,146     17,349   1,622   0,1059    
## ---
## Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
## 
## Residual standard error: 208,2 on 279 degrees of freedom
## Multiple R-squared:  0,4922, Adjusted R-squared:  0,4776 
## F-statistic:  33,8 on 8 and 279 DF,  p-value: < 2,2e-16
## [1] "Part worths (utilities) of levels (model parameters for whole sample):"
##                levnms      utls
## 1           intercept  451,5938
## 2               green     -43,5
## 3        green-yellow   16,6875
## 4              yellow   26,8125
## 5  high.heterogeneity   -2,6562
## 6       heterogeneity -106,3854
## 7         homogeneity  109,0417
## 8         high.bruise  -173,125
## 9              bruise  -71,2396
## 10         non.bruise  244,3646
## 11         high.cigar     -16,5
## 12          med.cigar   28,1458
## 13          non.cigar  -11,6458
## [1] "Average importance of factors (attributes):"
## [1] 17,68 23,45 45,62 13,25
## [1] Sum of average importance:  100
## [1] "Chart of average factors importance"
caSegmentation(qual.score, exp.design, 2)
## K-means clustering with 2 clusters of sizes 3, 29
## 
## Cluster means:
##       [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
## 1 531.0000  65.0000 567.3333 258.3333 314.0000 524.3333 547.3333 738.3333
## 2 183.2069 626.8966 420.4828 743.8621 270.0345 422.8276 420.7931 130.2414
##       [,9]
## 1 689.0000
## 2 828.3793
## 
## Clustering vector:
##  [1] 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## 
## Within cluster sum of squares by cluster:
## [1] 1059861 8010402
##  (between_SS / total_SS =  25.0 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"    
## [5] "tot.withinss" "betweenss"    "size"         "iter"        
## [9] "ifault"
#X11(); par(mfrow = c(2,2))
ShowAllUtilities(qual.score, exp.design, lev.names) # estimating all models 
## 
## Call:
## lm(formula = frml)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -723,31 -131,70   -2,73  127,70  657,75 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        451,594     12,267  36,813  < 2e-16 ***
## factor(x$color)1   -43,500     17,349  -2,507   0,0127 *  
## factor(x$color)2    16,688     17,349   0,962   0,3369    
## factor(x$shape)1    -2,656     17,349  -0,153   0,8784    
## factor(x$shape)2  -106,385     17,349  -6,132 2,95e-09 ***
## factor(x$bruise)1 -173,125     17,349  -9,979  < 2e-16 ***
## factor(x$bruise)2  -71,240     17,349  -4,106 5,29e-05 ***
## factor(x$cigar)1   -16,500     17,349  -0,951   0,3424    
## factor(x$cigar)2    28,146     17,349   1,622   0,1059    
## ---
## Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
## 
## Residual standard error: 208,2 on 279 degrees of freedom
## Multiple R-squared:  0,4922, Adjusted R-squared:  0,4776 
## F-statistic:  33,8 on 8 and 279 DF,  p-value: < 2,2e-16
## [1] "Part worths (utilities) of levels (model parameters for whole sample):"
##                levnms      utls
## 1           intercept  451,5938
## 2               green     -43,5
## 3        green-yellow   16,6875
## 4              yellow   26,8125
## 5  high.heterogeneity   -2,6562
## 6       heterogeneity -106,3854
## 7         homogeneity  109,0417
## 8         high.bruise  -173,125
## 9              bruise  -71,2396
## 10         non.bruise  244,3646
## 11         high.cigar     -16,5
## 12          med.cigar   28,1458
## 13          non.cigar  -11,6458
## [1] "Part worths (utilities) of all levels including intercept and reference levels a cross of respondents:"
##       intercept    green green-yellow   yellow high.heterogeneity
##  [1,]   464,000   37,000       31,333  -68,333              7,667
##  [2,]   559,444 -106,778        0,889  105,889             -7,444
##  [3,]   476,333   79,000      -26,667  -52,333             23,667
##  [4,]   551,111   29,556       62,222  -91,778             26,222
##  [5,]   396,778   92,222      -29,111  -63,111             69,222
##  [6,]   396,889   -1,222       30,778  -29,556           -113,556
##  [7,]   385,000 -345,000      205,333  139,667            107,333
##  [8,]   393,667   22,333      172,667 -195,000             24,667
##  [9,]   468,222  -97,556        5,444   92,111             -8,222
## [10,]   372,444  -30,778      -62,444   93,222             31,889
## [11,]   474,444  -37,111       34,889    2,222            -62,111
## [12,]   457,889 -192,889      153,444   39,444           -197,556
## [13,]   461,444  -36,778       48,889  -12,111             64,556
## [14,]   517,889   33,444       32,444  -65,889             27,778
## [15,]   434,222 -101,222       64,444   36,778             30,778
## [16,]   504,667  -39,000       75,333  -36,333             83,000
## [17,]   521,778 -108,111     -217,444  325,556              3,556
## [18,]   517,333 -109,333      -35,000  144,333           -110,333
## [19,]   411,222  135,444      -27,222 -108,222            -24,556
## [20,]   530,222  189,444     -134,556  -54,889           -150,556
## [21,]   378,000  -34,333      -10,333   44,667              2,000
## [22,]   357,778   25,556        5,889  -31,444              1,222
## [23,]   497,889 -167,556      136,778   30,778            -39,222
## [24,]   422,556  -16,556       -5,889   22,444             22,444
## [25,]   563,889  -35,556        4,111   31,444             29,111
## [26,]   430,000  -73,000        1,667   71,333             77,000
## [27,]   459,444   78,889      -69,444   -9,444             81,556
## [28,]   329,889    3,111       25,444  -28,556              4,444
## [29,]   452,556  -41,889       31,444   10,444            -45,556
## [30,]   369,667 -155,000       -2,667  157,667            -30,333
## [31,]   567,667 -162,667        8,333  154,333            -19,667
## [32,]   326,667 -225,667       23,000  202,667              6,000
##       heterogeneity homogeneity high.bruise   bruise non.bruise high.cigar
##  [1,]      -199,667     192,000    -154,333 -186,333    340,667     11,667
##  [2,]       -94,778     102,222    -208,111  -92,444    300,556   -128,111
##  [3,]      -130,333     106,667    -142,667 -106,000    248,667     28,667
##  [4,]      -125,111      98,889    -153,444 -124,778    278,222    -96,444
##  [5,]      -164,444      95,222    -131,444 -161,444    292,889     63,222
##  [6,]      -279,556     393,111     -74,889 -117,889    192,778    -30,222
##  [7,]      -189,333      82,000    -231,333  -18,000    249,333     84,000
##  [8,]       -37,000      12,333    -110,333 -101,000    211,333   -330,333
##  [9,]       -23,889      32,111    -327,889   -0,222    328,111     19,444
## [10,]        58,889     -90,778     179,222  100,889   -280,111     92,222
## [11,]      -111,111     173,222    -248,444  -81,444    329,889    -67,778
## [12,]        38,444     159,111     -72,556 -198,889    271,444      5,111
## [13,]      -107,778      43,222    -339,444  -26,778    366,222    -29,778
## [14,]      -166,556     138,778    -106,889 -206,556    313,444     35,778
## [15,]       -66,222      35,444    -257,889  -33,556    291,444     39,111
## [16,]       -49,667     -33,333    -355,000   14,333    340,667    -23,667
## [17,]      -136,111     132,556     130,556   26,556   -157,111    117,222
## [18,]      -217,000     327,333      72,333 -110,333     38,000    -87,000
## [19,]      -177,889     202,444    -249,222 -180,222    429,444    -25,889
## [20,]       -52,889     203,444    -256,556  182,778     73,778    187,444
## [21,]       -93,000      91,000    -233,000  -79,000    312,000    -24,667
## [22,]      -151,444     150,222    -173,444  -85,111    258,556    -36,111
## [23,]      -127,556     166,778    -176,222 -119,222    295,444    -50,889
## [24,]       -97,222      74,778    -371,556 -105,556    477,111    -56,889
## [25,]      -125,556      96,444    -249,222  -29,222    278,444    -31,222
## [26,]      -162,667      85,667    -205,000  -34,000    239,000    -12,000
## [27,]      -187,778     106,222    -252,778  -72,111    324,889     -1,111
## [28,]      -140,889     136,444    -132,556  -99,556    232,111    -22,889
## [29,]      -232,556     278,111    -163,889  -48,222    212,111   -109,222
## [30,]        62,333     -32,000    -149,000 -124,000    273,000    -35,333
## [31,]        70,333     -50,667    -283,333   -2,333    285,667    -17,000
## [32,]        13,667     -19,667    -111,667  -60,000    171,667      4,667
##       med.cigar non.cigar
##  [1,]    30,000   -41,667
##  [2,]   119,556     8,556
##  [3,]   -28,667     0,000
##  [4,]    99,222    -2,778
##  [5,]   -25,444   -37,778
##  [6,]    43,111   -12,889
##  [7,]    20,000  -104,000
##  [8,]   192,667   137,667
##  [9,]     8,111   -27,556
## [10,]  -120,444    28,222
## [11,]    31,889    35,889
## [12,]   117,111  -122,222
## [13,]    31,889    -2,111
## [14,]    38,778   -74,556
## [15,]    57,778   -96,889
## [16,]    87,333   -63,667
## [17,]  -120,444     3,222
## [18,]   -34,000   121,000
## [19,]   -98,222   124,111
## [20,]   -21,556  -165,889
## [21,]   121,333   -96,667
## [22,]    37,556    -1,444
## [23,]    94,778   -43,889
## [24,]    33,111    23,778
## [25,]    63,778   -32,556
## [26,]    49,667   -37,667
## [27,]    10,556    -9,444
## [28,]    50,778   -27,889
## [29,]    83,778    25,444
## [30,]     4,000    31,333
## [31,]    -7,000    24,000
## [32,]   -70,333    65,667
## [1] "Total utilities of profiles a cross of respondents:"
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]  366  672  465  802  121  563  247    0  940
##  [2,]  109  778  471  862  245  574  685  371  940
##  [3,]  465  645  556  722  242  385  313  151  808
##  [4,]  357  833  552  915  267  658  460  178  740
##  [5,]  490  592  385  692  105  306  216    0  785
##  [6,]  177  352  658  494    0  789  179    0  923
##  [7,]    0  120    0  843  467  461  634    0  940
##  [8,]    0  783  465  940   98  661  315  189   92
##  [9,]   54  683  375  766  469  186  560  181  940
## [10,]  645    0  380   90  562  278  478  732  187
## [11,]   59  688  565  813  249  466  365  153  912
## [12,]    0  692  103  563  456  815  218  341  933
## [13,]  120  715  439  939  346  246  519    0  829
## [14,]  508  737  409  817  213  621  312  104  940
## [15,]  145  616  238  724  438  334  526   50  837
## [16,]  170  844  383  940  521  279  653    0  752
## [17,]  665    0  576  154  312  447  757  845  940
## [18,]  283  195  746  531   68  848  407  638  940
## [19,]  247  700  693  913    0  239    0    0  909
## [20,]  500  719  940  153  713  321  486    0  940
## [21,]   88  684  259  585  171  347  467    0  801
## [22,]  175  528  447  622   91  378  280    0  699
## [23,]   64  593  334  847  337  720  465  181  940
## [24,]    0  819  399  940  157  153  395    0  940
## [25,]  277  745  563  843  382  479  659  188  939
## [26,]  217  483  371  710  223  362  594   96  814
## [27,]  366  686  563  787  129  254  470    0  880
## [28,]  182  475  342  564   92  410  257    0  647
## [29,]   92  474  666  676   94  682  453   92  844
## [30,]    0  554   90  641  270  190  377  472  733
## [31,]   85  754  376  866  627  235  693  533  940
## [32,]    0  216   87  593  308  148  405  497  686
## [1] "Average importance of factors (attributes):"
## [1] 17,68 23,45 45,62 13,25
## [1] Sum of average importance:  100
#X11(); par(mfrow = c(2,2))
utility <- ShowAllUtilities(qual.score, exp.design, lev.names)
## 
## Call:
## lm(formula = frml)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -723,31 -131,70   -2,73  127,70  657,75 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        451,594     12,267  36,813  < 2e-16 ***
## factor(x$color)1   -43,500     17,349  -2,507   0,0127 *  
## factor(x$color)2    16,688     17,349   0,962   0,3369    
## factor(x$shape)1    -2,656     17,349  -0,153   0,8784    
## factor(x$shape)2  -106,385     17,349  -6,132 2,95e-09 ***
## factor(x$bruise)1 -173,125     17,349  -9,979  < 2e-16 ***
## factor(x$bruise)2  -71,240     17,349  -4,106 5,29e-05 ***
## factor(x$cigar)1   -16,500     17,349  -0,951   0,3424    
## factor(x$cigar)2    28,146     17,349   1,622   0,1059    
## ---
## Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
## 
## Residual standard error: 208,2 on 279 degrees of freedom
## Multiple R-squared:  0,4922, Adjusted R-squared:  0,4776 
## F-statistic:  33,8 on 8 and 279 DF,  p-value: < 2,2e-16
## [1] "Part worths (utilities) of levels (model parameters for whole sample):"
##                levnms      utls
## 1           intercept  451,5938
## 2               green     -43,5
## 3        green-yellow   16,6875
## 4              yellow   26,8125
## 5  high.heterogeneity   -2,6562
## 6       heterogeneity -106,3854
## 7         homogeneity  109,0417
## 8         high.bruise  -173,125
## 9              bruise  -71,2396
## 10         non.bruise  244,3646
## 11         high.cigar     -16,5
## 12          med.cigar   28,1458
## 13          non.cigar  -11,6458
## [1] "Part worths (utilities) of all levels including intercept and reference levels a cross of respondents:"
##       intercept    green green-yellow   yellow high.heterogeneity
##  [1,]   464,000   37,000       31,333  -68,333              7,667
##  [2,]   559,444 -106,778        0,889  105,889             -7,444
##  [3,]   476,333   79,000      -26,667  -52,333             23,667
##  [4,]   551,111   29,556       62,222  -91,778             26,222
##  [5,]   396,778   92,222      -29,111  -63,111             69,222
##  [6,]   396,889   -1,222       30,778  -29,556           -113,556
##  [7,]   385,000 -345,000      205,333  139,667            107,333
##  [8,]   393,667   22,333      172,667 -195,000             24,667
##  [9,]   468,222  -97,556        5,444   92,111             -8,222
## [10,]   372,444  -30,778      -62,444   93,222             31,889
## [11,]   474,444  -37,111       34,889    2,222            -62,111
## [12,]   457,889 -192,889      153,444   39,444           -197,556
## [13,]   461,444  -36,778       48,889  -12,111             64,556
## [14,]   517,889   33,444       32,444  -65,889             27,778
## [15,]   434,222 -101,222       64,444   36,778             30,778
## [16,]   504,667  -39,000       75,333  -36,333             83,000
## [17,]   521,778 -108,111     -217,444  325,556              3,556
## [18,]   517,333 -109,333      -35,000  144,333           -110,333
## [19,]   411,222  135,444      -27,222 -108,222            -24,556
## [20,]   530,222  189,444     -134,556  -54,889           -150,556
## [21,]   378,000  -34,333      -10,333   44,667              2,000
## [22,]   357,778   25,556        5,889  -31,444              1,222
## [23,]   497,889 -167,556      136,778   30,778            -39,222
## [24,]   422,556  -16,556       -5,889   22,444             22,444
## [25,]   563,889  -35,556        4,111   31,444             29,111
## [26,]   430,000  -73,000        1,667   71,333             77,000
## [27,]   459,444   78,889      -69,444   -9,444             81,556
## [28,]   329,889    3,111       25,444  -28,556              4,444
## [29,]   452,556  -41,889       31,444   10,444            -45,556
## [30,]   369,667 -155,000       -2,667  157,667            -30,333
## [31,]   567,667 -162,667        8,333  154,333            -19,667
## [32,]   326,667 -225,667       23,000  202,667              6,000
##       heterogeneity homogeneity high.bruise   bruise non.bruise high.cigar
##  [1,]      -199,667     192,000    -154,333 -186,333    340,667     11,667
##  [2,]       -94,778     102,222    -208,111  -92,444    300,556   -128,111
##  [3,]      -130,333     106,667    -142,667 -106,000    248,667     28,667
##  [4,]      -125,111      98,889    -153,444 -124,778    278,222    -96,444
##  [5,]      -164,444      95,222    -131,444 -161,444    292,889     63,222
##  [6,]      -279,556     393,111     -74,889 -117,889    192,778    -30,222
##  [7,]      -189,333      82,000    -231,333  -18,000    249,333     84,000
##  [8,]       -37,000      12,333    -110,333 -101,000    211,333   -330,333
##  [9,]       -23,889      32,111    -327,889   -0,222    328,111     19,444
## [10,]        58,889     -90,778     179,222  100,889   -280,111     92,222
## [11,]      -111,111     173,222    -248,444  -81,444    329,889    -67,778
## [12,]        38,444     159,111     -72,556 -198,889    271,444      5,111
## [13,]      -107,778      43,222    -339,444  -26,778    366,222    -29,778
## [14,]      -166,556     138,778    -106,889 -206,556    313,444     35,778
## [15,]       -66,222      35,444    -257,889  -33,556    291,444     39,111
## [16,]       -49,667     -33,333    -355,000   14,333    340,667    -23,667
## [17,]      -136,111     132,556     130,556   26,556   -157,111    117,222
## [18,]      -217,000     327,333      72,333 -110,333     38,000    -87,000
## [19,]      -177,889     202,444    -249,222 -180,222    429,444    -25,889
## [20,]       -52,889     203,444    -256,556  182,778     73,778    187,444
## [21,]       -93,000      91,000    -233,000  -79,000    312,000    -24,667
## [22,]      -151,444     150,222    -173,444  -85,111    258,556    -36,111
## [23,]      -127,556     166,778    -176,222 -119,222    295,444    -50,889
## [24,]       -97,222      74,778    -371,556 -105,556    477,111    -56,889
## [25,]      -125,556      96,444    -249,222  -29,222    278,444    -31,222
## [26,]      -162,667      85,667    -205,000  -34,000    239,000    -12,000
## [27,]      -187,778     106,222    -252,778  -72,111    324,889     -1,111
## [28,]      -140,889     136,444    -132,556  -99,556    232,111    -22,889
## [29,]      -232,556     278,111    -163,889  -48,222    212,111   -109,222
## [30,]        62,333     -32,000    -149,000 -124,000    273,000    -35,333
## [31,]        70,333     -50,667    -283,333   -2,333    285,667    -17,000
## [32,]        13,667     -19,667    -111,667  -60,000    171,667      4,667
##       med.cigar non.cigar
##  [1,]    30,000   -41,667
##  [2,]   119,556     8,556
##  [3,]   -28,667     0,000
##  [4,]    99,222    -2,778
##  [5,]   -25,444   -37,778
##  [6,]    43,111   -12,889
##  [7,]    20,000  -104,000
##  [8,]   192,667   137,667
##  [9,]     8,111   -27,556
## [10,]  -120,444    28,222
## [11,]    31,889    35,889
## [12,]   117,111  -122,222
## [13,]    31,889    -2,111
## [14,]    38,778   -74,556
## [15,]    57,778   -96,889
## [16,]    87,333   -63,667
## [17,]  -120,444     3,222
## [18,]   -34,000   121,000
## [19,]   -98,222   124,111
## [20,]   -21,556  -165,889
## [21,]   121,333   -96,667
## [22,]    37,556    -1,444
## [23,]    94,778   -43,889
## [24,]    33,111    23,778
## [25,]    63,778   -32,556
## [26,]    49,667   -37,667
## [27,]    10,556    -9,444
## [28,]    50,778   -27,889
## [29,]    83,778    25,444
## [30,]     4,000    31,333
## [31,]    -7,000    24,000
## [32,]   -70,333    65,667
## [1] "Total utilities of profiles a cross of respondents:"
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]  366  672  465  802  121  563  247    0  940
##  [2,]  109  778  471  862  245  574  685  371  940
##  [3,]  465  645  556  722  242  385  313  151  808
##  [4,]  357  833  552  915  267  658  460  178  740
##  [5,]  490  592  385  692  105  306  216    0  785
##  [6,]  177  352  658  494    0  789  179    0  923
##  [7,]    0  120    0  843  467  461  634    0  940
##  [8,]    0  783  465  940   98  661  315  189   92
##  [9,]   54  683  375  766  469  186  560  181  940
## [10,]  645    0  380   90  562  278  478  732  187
## [11,]   59  688  565  813  249  466  365  153  912
## [12,]    0  692  103  563  456  815  218  341  933
## [13,]  120  715  439  939  346  246  519    0  829
## [14,]  508  737  409  817  213  621  312  104  940
## [15,]  145  616  238  724  438  334  526   50  837
## [16,]  170  844  383  940  521  279  653    0  752
## [17,]  665    0  576  154  312  447  757  845  940
## [18,]  283  195  746  531   68  848  407  638  940
## [19,]  247  700  693  913    0  239    0    0  909
## [20,]  500  719  940  153  713  321  486    0  940
## [21,]   88  684  259  585  171  347  467    0  801
## [22,]  175  528  447  622   91  378  280    0  699
## [23,]   64  593  334  847  337  720  465  181  940
## [24,]    0  819  399  940  157  153  395    0  940
## [25,]  277  745  563  843  382  479  659  188  939
## [26,]  217  483  371  710  223  362  594   96  814
## [27,]  366  686  563  787  129  254  470    0  880
## [28,]  182  475  342  564   92  410  257    0  647
## [29,]   92  474  666  676   94  682  453   92  844
## [30,]    0  554   90  641  270  190  377  472  733
## [31,]   85  754  376  866  627  235  693  533  940
## [32,]    0  216   87  593  308  148  405  497  686
## [1] "Average importance of factors (attributes):"
## [1] 17,68 23,45 45,62 13,25
## [1] Sum of average importance:  100
#**************************** END *****************************************

# # Conjoint analysis step by step ----------------------------------------
# 
# ## A experimental design for 32 subjects
# design <- exp.design
# design$id <- rownames(design)
# df <- design[rep(1:nrow(design),each=32),] 
# rownames(df) <- NULL 
# # df
# 
# ## A vector of quality score for 32 subjects
# t.hedo <- t(ba.qual[,seq(1, ncol(ba.qual), by=3)])
# hedo.anova <- as.matrix(t.hedo)
# hedo <- melt(hedo.anova, id=1)
# colnames(hedo) = c("subject","product","score")
# hedo.plan <- cbind(df, hedo)
# 
# 
# ## model1: variables are considered as continuous variables
# ## For understand, dont use
# # model1 <- aov(score ~ color + shape + bruise + cigar, data=hedo.plan) 
# # summary(model1) # factors: bruise > shape > color
# 
# ## model2: varibales are considered as dummy variables
# model2 <- lm(score ~ factor(color) + factor(shape) + factor(bruise) + factor(cigar), 
#              data=hedo.plan)
# summary(model2)