## Conjoint analysis: Tea example ################################### 
## Load library and data 
library(conjoint) 
## Warning: package 'conjoint' was built under R version 4.1.1
data(tea) 

## Variable levels 
#Price (low:1, medium:2, high:3) 
#Variety (black:1, green:2, red:3) 
#Kind (bags:1, granulated:2, leafy:3) 
#Aroma (yes:1, no:0) 

## Four types of files for conjoint in R 
# tlevn: levels of variables 
# tprefm: customers' ratings  
# tprof: profiles 
# tpref: long data format of tprefm 

# Have to create the four data files before running the conjoint models using library 'conjoint' 
## Structure of the data 
head(tprof) 
##   price variety kind aroma
## 1     3       1    1     1
## 2     1       2    1     1
## 3     2       2    2     1
## 4     2       1    3     1
## 5     3       3    3     1
## 6     2       1    1     2
head(tlevn) 
##   levels
## 1    low
## 2 medium
## 3   high
## 4  black
## 5  green
## 6    red
head(tprefm) 
##   profil1 profil2 profil3 profil4 profil5 profil6 profil7 profil8 profil9
## 1       8       1       1       3       9       2       7       2       2
## 2       0      10       3       5       1       4       8       6       2
## 3       4      10       3       5       4       1       2       0       0
## 4       6       7       4       9       6       3       7       4       8
## 5       5       1       7       8       6      10       7      10       6
## 6      10       1       1       5       1       0       0       0       0
##   profil10 profil11 profil12 profil13
## 1        2        2        3        4
## 2        9        7        5        2
## 3        1        8        9        7
## 4        5        2       10        9
## 5        6        6       10        7
## 6        0        0        1        1
head(tpref) 
##   Y
## 1 8
## 2 1
## 3 1
## 4 3
## 5 9
## 6 2
## As factor  
tprof$price <- as.factor(tprof$price) 
tprof$variety <- as.factor(tprof$variety) 
tprof$kind <- as.factor(tprof$kind) 
tprof$aroma <- as.factor(tprof$aroma) 

is.factor(tprof$price) 
## [1] TRUE
## Calculate the utility value for just the first customer 
caModel(y=tprefm[1,], x=tprof) 
## 
## Call:
## lm(formula = frml)
## 
## Residuals:
##       1       2       3       4       5       6       7       8       9      10 
##  1.1345 -1.4897  0.3103 -0.2655  0.3103  0.1931  1.5931 -1.4310 -1.4310  1.1207 
##      11      12      13 
##  0.3690  1.1931 -1.6069 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)   
## (Intercept)          3.3937     0.5439   6.240  0.00155 **
## factor(x$price)1    -1.5172     0.7944  -1.910  0.11440   
## factor(x$price)2    -1.1414     0.6889  -1.657  0.15844   
## factor(x$variety)1  -0.4747     0.6889  -0.689  0.52141   
## factor(x$variety)2  -0.6747     0.6889  -0.979  0.37234   
## factor(x$kind)1      0.6586     0.6889   0.956  0.38293   
## factor(x$kind)2     -1.5172     0.7944  -1.910  0.11440   
## factor(x$aroma)1     0.6293     0.5093   1.236  0.27150   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.78 on 5 degrees of freedom
## Multiple R-squared:  0.8184, Adjusted R-squared:  0.5642 
## F-statistic:  3.22 on 7 and 5 DF,  p-value: 0.1082
# high price (3) is the most preferred by customer 1 
# red tea (3) is the most preferred 
# granulated (1) is the most preferred 
# aroma (1) is preferred to no aroma by customer 1 
## Utility values for the first 10 customers 
caPartUtilities(y=tprefm[1:10,], x=tprof, z=tlevn) 
##       intercept    low medium   high  black  green    red   bags granulated
##  [1,]     3.394 -1.517 -1.141  2.659 -0.475 -0.675  1.149  0.659     -1.517
##  [2,]     5.049  3.391 -0.695 -2.695 -1.029  0.971  0.057  1.105     -0.609
##  [3,]     4.029  2.563 -1.182 -1.382 -0.248  2.352 -2.103 -0.382     -2.437
##  [4,]     5.856 -1.149 -0.025  1.175 -0.492  1.308 -0.816 -0.825     -0.149
##  [5,]     6.250 -2.333  2.567 -0.233 -0.033 -0.633  0.667 -0.233     -0.333
##  [6,]     1.578 -0.713 -0.144  0.856  1.456 -0.744 -0.713  0.656     -0.713
##  [7,]     2.635 -0.920 -1.040  1.960 -0.707  0.293  0.414 -1.107     -2.586
##  [8,]     4.405 -0.425  0.413  0.013  0.546 -2.454  1.908  1.479      0.241
##  [9,]     3.546 -0.966  0.883  0.083  2.216  1.416 -3.632 -0.917     -0.966
## [10,]     5.460  0.678 -0.639 -0.039  0.228  0.428 -0.655 -1.172     -2.655
##        leafy    yes     no
##  [1,]  0.859  0.629 -0.629
##  [2,] -0.495 -0.681  0.681
##  [3,]  2.818  0.776 -0.776
##  [4,]  0.975  0.121 -0.121
##  [5,]  0.567 -1.250  1.250
##  [6,]  0.056  1.595 -1.595
##  [7,]  3.693  0.147 -0.147
##  [8,] -1.721 -1.060  1.060
##  [9,]  1.883 -0.259  0.259
## [10,]  3.828  1.414 -1.414
## Run the test for all the respondents: Conjoint 
Conjoint(y=tpref, x=tprof, z=tlevn) 
## 
## Call:
## lm(formula = frml)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5,1888 -2,3761 -0,7512  2,2128  7,5134 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         3,55336    0,09068  39,184  < 2e-16 ***
## factor(x$price)1    0,24023    0,13245   1,814    0,070 .  
## factor(x$price)2   -0,14311    0,11485  -1,246    0,213    
## factor(x$variety)1  0,61489    0,11485   5,354 1,02e-07 ***
## factor(x$variety)2  0,03489    0,11485   0,304    0,761    
## factor(x$kind)1     0,13689    0,11485   1,192    0,234    
## factor(x$kind)2    -0,88977    0,13245  -6,718 2,76e-11 ***
## factor(x$aroma)1    0,41078    0,08492   4,837 1,48e-06 ***
## ---
## Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
## 
## Residual standard error: 2,967 on 1292 degrees of freedom
## Multiple R-squared:  0,09003,    Adjusted R-squared:  0,0851 
## F-statistic: 18,26 on 7 and 1292 DF,  p-value: < 2,2e-16
## [1] "Part worths (utilities) of levels (model parameters for whole sample):"
##        levnms    utls
## 1   intercept  3,5534
## 2         low  0,2402
## 3      medium -0,1431
## 4        high -0,0971
## 5       black  0,6149
## 6       green  0,0349
## 7         red -0,6498
## 8        bags  0,1369
## 9  granulated -0,8898
## 10      leafy  0,7529
## 11        yes  0,4108
## 12         no -0,4108
## [1] "Average importance of factors (attributes):"
## [1] 24,76 32,22 27,15 15,88
## [1] Sum of average importance:  100,01
## [1] "Chart of average factors importance"
# low price (1) is the most preferred   
# black tea (1) is the most preferred 
# granulated (1) is the most preferred 
# aroma (1) is preferred over no aroma   
## Relative attribute importance: % 
caImportance(tprefm, tprof) 
## [1] 24.76 32.22 27.15 15.88
# variety (black, green, red) is the most important 
# kind (bags, granulated, leafy)  is the second most important 
# price is third 
# aroma is the least important attribute