still under development …

load("ic0930.rdata")
library(magrittr)
library(Matrix)
library(recommenderlab)


Segmentation Rules

aisle$dim1 = ""  
aisle$dim1[c(
  4,7,10,11,12,14,15,17,20,22,24,32,34,35,36,38,39,40,41,42,49,54,55,56,58,60,
  63,68,74,75,81,83,85,86,92,95,96,102,104,106,111,112,114,116,118,121,122,123,
  126,131)] = "NEC"
aisle$dim1[c(1,2,5,9,16,18,19,21,25,26,29,30,31,33,43,44,46,47,51,52,53,57,59,
             64,65,66,67,69,70,72,76,82,84,87,88,89,90,91,93,97,98,99,108,110,
             113,117,124,128,130,133)] = "UNN"
aisle$dim1[c(3,8,13,23,27,28,37,45,48,50,61,62,71,73,77,78,79,80,94,101,103,105,
             107,109,115,119,120,125,127,129,132,134)] = "SPE"

aisle$dim2 = ""  
aisle$dim2[c(1,4,5,8,9,13,23,25,27,28,29,30,33,37,38,42,45,48,49,51,52,57,59,61,
             62,64,66,67,69,71,72,76,77,78,79,81,84,88,89,90,91,93,94,95,96,98,99,101,
             103,105,106,107,108,110,113,115,119,120,121,125,128,129,130,132,134)] = "E"
aisle$dim2[c(2,7,10,12,14,15,16,17,18,20,21,24,26,31,32,34,35,39,43,46,50,54,56,58
             ,60,63,68,74,82,83,85,86,87,97,104,111,112,114,116,117,122,123,126,131)] = "S"
aisle$dim2[c(3,11,19,22,36,40,41,44,47,53,55,65,70,73,75,80,92,102,109,118,124,127,133)] = "T"

table(aisle$dim1,aisle$dim2)
##      
##           E  S  T
##        2  0  0  0
##   NEC  0  9 31 10
##   SPE  0 26  1  5
##   UNN  0 30 12  8

Models, Evaluation, Comparison

L = list()
for(dx in c("dim1","dim2")) {
  if(dx == "dim1") 
    dnames = c("NEC","UNN","SPE")
  else 
    dnames = c("E","S","T")
  for(d1 in dnames) {
    aid = aisle$aisle_id[aisle[,dx] == d1]
    pid = subset(prod, aisle_id %in% aid)$product_id
    mx = mxUP[, colnames(mxUP) %in% pid]
    
    mx = mx[, colSums(mx > 0) >= 50]
    mx = mx[rowSums(mx > 0) >= 20 & rowSums(mx > 0) <= 300, ]
    cat(dx, d1, dim(mx), "... ... ... \n")
    
    # build rating matrix
    rx = as(mx, "realRatingMatrix")  # realRatingMatrix
    bx = binarize(rx, minRating=1)   # binaryRatingMatrix
  
    # validation scheme
    set.seed(4321)
    scheme = evaluationScheme(     
      bx, method="split", train = .9,  given=10)
    
    # list of algorithm
    algorithms = list(            
      AR = list(name="AR", param=list(support=0.001, confidence=0.25)),
      RANDOM = list(name="RANDOM", param=NULL),
      POPULAR = list(name="POPULAR", param=NULL),
      UBCF = list(name="UBCF", param=NULL),
      IBCF = list(name="IBCF", param=NULL) )
    
    # model, predict, evaluation & compare   
    t0 = Sys.time()
    results = evaluate(            
      scheme, algorithms, type="topNList",
      n=c(5, 10, 15, 20))
    cat(Sys.time() - t0, "\n\n")
    
    #
    L[[length(L)+1]] = results
    names(L)[length(L)] = d1
  }
}
save(L, file="L.rdata")

Results

load("L.rdata")
cols = c("red", "gray", "orange", "blue", "green")
par(mfcol=c(3,2))
for(i in 1:length(L)) {
  plot(L[[i]], annotate=c(1,3), legend="topleft", 
       pch=19, lwd=2, col=cols)
  text(0.0001,0,names(L)[i],font=2,cex=1.2)
  }