Import customer file

customer = read.csv("V:/acedemics/MB BA/sem 3/Predictive Analytics/Customer.csv")

Head Customer

head(customer)
##   ID Visit.Time Average.Expense Sex Age
## 1  1          3             5.7   0  10
## 2  2          5            14.5   0  27
## 3  3         16            33.5   0  32
## 4  4          5            15.9   0  30
## 5  5         16            24.9   0  23
## 6  6          3            12.0   0  15

normalize the customer data

scustomer <- scale(customer[,-1])

agglomratic clustering

hc = hclust(dist(customer, method='euclidean'), method = "ward.D2")

plot function to plot dendogram

plot(hc, hang = -0.5, cex = 0.6)

hcsingle = hclust(dist(customer, method='euclidean'), method = "single")
plot(hcsingle, hang = -0.5, cex = 0.5)

hcsingle = hclust(dist(customer, method='euclidean'))
plot(hcsingle, hang = -0.5, cex = 0.5)

heirarchial clustering cut tree example

d <- dist(customer, method='euclidean')
fit <- hclust(d, method='ward.D2')
plot(fit,hang = -0.5, cex = 0.5)
groups <- cutree(fit, k=5)
rect.hclust(fit,k=5,border='blue')

placing a rect around a particular cluster

plot(hc, hang=-0.01, cex=0.5)
rect.hclust(hc, k=4, which=3, border='blue')

library(cluster)
dv=diana(customer, metric='euclidean')
summary(dv)
## Merge:
##       [,1] [,2]
##  [1,]  -39  -40
##  [2,]  -23  -26
##  [3,]   -2   -4
##  [4,]  -54  -58
##  [5,]  -10  -11
##  [6,]  -57  -60
##  [7,]  -48  -49
##  [8,]   -8  -12
##  [9,]  -30  -35
## [10,]    1  -43
## [11,]  -42  -44
## [12,]  -32  -34
## [13,]  -17  -19
## [14,]  -25  -28
## [15,]   -7  -13
## [16,]    2  -27
## [17,]  -18  -21
## [18,]  -56  -59
## [19,]  -36  -41
## [20,]  -33  -37
## [21,]  -14  -15
## [22,]   -5    8
## [23,]  -50  -53
## [24,]    4  -55
## [25,]  -45  -46
## [26,]   -6    5
## [27,]  -51   18
## [28,]   16  -29
## [29,]   10   11
## [30,]  -38   29
## [31,]   13  -24
## [32,]   -1   26
## [33,]    9   19
## [34,]  -16   17
## [35,]   -3   15
## [36,]    7   24
## [37,]   23  -52
## [38,]   25   37
## [39,]  -31   30
## [40,]    3   22
## [41,]  -20   14
## [42,]   31  -22
## [43,]   34   28
## [44,]   12   20
## [45,]  -47   36
## [46,]   32   21
## [47,]   45   27
## [48,]   35   -9
## [49,]   40   48
## [50,]   33   39
## [51,]   44   38
## [52,]   42   41
## [53,]   46   43
## [54,]   49   52
## [55,]   50   47
## [56,]   51    6
## [57,]   53   54
## [58,]   55   56
## [59,]   57   58
## Order of objects:
##  [1]  1  6 10 11 14 15 16 18 21 23 26 27 29  2  4  5  8 12  3  7 13  9 17
## [24] 19 24 22 20 25 28 30 35 36 41 31 38 39 40 43 42 44 47 48 49 54 58 55
## [47] 51 56 59 32 34 33 37 45 46 50 53 52 57 60
## Height:
##  [1] 10.881636  8.768694  4.109745 18.300000  7.790379 29.295904 11.819052
##  [8]  6.324555 16.192900  3.104835  6.184658  9.282241 43.848489  3.867816
## [15] 15.942396  8.497647  4.983974 22.538855 11.916375  6.000000 22.538855
## [22] 30.832450  5.855766 10.678951 16.117072 26.474327 15.942396  5.861740
## [29] 72.078846  5.000000 11.808472  7.020684 23.499149 14.425325  9.529428
## [36]  2.256103  5.728001  9.486833  5.766281 32.790852 17.811513  4.586938
## [43] 12.122706  4.060788  8.560958 20.651634  9.046546  6.618912 53.331417
## [50]  5.838664 17.298844  7.376313 24.598577  8.616844 14.208800  8.558037
## [57] 12.806639 33.313811  4.267318
## Divisive coefficient:
## [1] 0.8968104
## 
## 1770 dissimilarities, summarized :
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.256  18.406  27.409  28.336  36.969  72.079 
## Metric :  euclidean 
## Number of objects : 60
## 
## Available components:
## [1] "order"  "height" "dc"     "merge"  "diss"   "call"   "data"