CC This work is licensed under the Creative Commons Attribution 4.0 International License. For questions please contact Michael Hahsler.

library(recommenderlab)
## Loading required package: Matrix
## Loading required package: arules
## 
## Attaching package: 'arules'
## 
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
## 
## Loading required package: proxy
## 
## Attaching package: 'proxy'
## 
## The following object is masked from 'package:Matrix':
## 
##     as.matrix
## 
## The following objects are masked from 'package:stats':
## 
##     as.dist, dist
## 
## The following object is masked from 'package:base':
## 
##     as.matrix
## 
## Loading required package: registry
data("Jester5k")
Jester5k
## 5000 x 100 rating matrix of class 'realRatingMatrix' with 362106 ratings.
train <- Jester5k[1:4000,]
test <- Jester5k[4001:5000,]

Create a Simple Hybrid Recommender

Create individual recommender models

rec_pop <- Recommender(train, "POPULAR")
rec_ib <-  Recommender(train, "IBCF")
rec_svd <- Recommender(train, "SVD")

Save recommender models for later so we do not have to rebuild them

save(rec_pop, rec_ib, rec_svd, file = "models.rda")

Load models

load("models.rda")

Create predictions

pred_pop <- predict(rec_pop, test[1,], type = "ratings")
pred_ib <-  predict(rec_ib, test[1,], type = "ratings")
pred_svd <- predict(rec_svd, test[1,], type = "ratings")
as(pred_pop, "list")
## $u3464
##          j1          j2          j3          j4          j6          j9 
##  1.85183148  1.19389908  1.10918984 -0.65788120  2.51456824  0.20680256 
##         j10         j23         j24         j28         j30         j33 
##  2.11833610  1.02223886 -0.83023690  2.32250843  0.39708705 -0.61873696 
##         j34         j37         j41         j43         j44         j47 
##  1.80190535 -0.65830029  0.54867828 -0.04515578 -1.44647043  2.35512756 
##         j52         j55         j57         j58         j60         j63 
##  0.76086130  1.29799243 -1.27561833 -3.08934627  0.42085267  1.04511250 
##         j64         j67         j71         j72         j73         j74 
##  0.16941011 -0.01429141  0.01756387  3.48418582  1.74157059 -0.82959757 
##         j75         j76         j77         j78         j79         j80 
##  0.39058276  2.99258748  1.22500213  2.29307587  0.62723250  1.64144929 
##         j81         j82         j83         j85         j86         j87 
##  2.32469570  1.69325195  2.63240911  1.57674141  0.92069425  2.47337066 
##         j88         j89         j90         j91         j92         j93 
##  2.67550238  4.13708907  1.05881595  2.61371288  1.89517187  3.07659495 
##         j94         j95         j96         j97         j98        j100 
##  1.54942247  1.53353024  2.06698347  2.24605235  1.51132359  1.93065904
as(pred_ib, "list")
## $u3464
##         j1         j2         j3         j4         j6         j9 
## -0.9449249  1.3049727 -0.5044573 -1.2374976  3.5384481 -3.3740607 
##        j10        j23        j24        j28        j30        j33 
##  1.3709456 -0.7479066 -2.6690650  2.3321870 -0.8367929 -2.3007447 
##        j34        j37        j41        j43        j44        j47 
##  3.7453982 -3.2564195 -1.0892991 -2.7145201 -3.1107662  3.0489365 
##        j52        j55        j57        j58        j60        j63 
##  2.9396625 -1.8361709 -3.0119828 -2.6633603 -4.1084304 -1.2760335 
##        j64        j67        j71        j72        j73        j74 
## -3.4415371 -2.1871735 -2.8969758  2.3779717  2.7666442 -1.7621165 
##        j75        j76        j77        j78        j79        j80 
## -1.0288039  3.3843211 -2.1076633  1.9623620 -0.8855348  3.9306994 
##        j81        j82        j83        j85        j86        j87 
##  3.1701861 -1.5494428  4.3883344  1.5848156 -3.2009100  2.9011096 
##        j88        j89        j90        j91        j92        j93 
##  2.0598972  2.5154311 -2.3317193  2.8124820  1.3239539  2.7568104 
##        j94        j95        j96        j97        j98       j100 
## -2.5064341 -1.9878839  0.4196987  0.6772776  3.6669779  3.7103185
as(pred_svd, "list")
## $u3464
##        j1        j2        j3        j4        j6        j9       j10 
## 1.2162797 3.3911403 1.4208575 2.2676158 2.0994152 1.9258811 0.7285243 
##       j23       j24       j28       j30       j33       j34       j37 
## 0.8967785 1.4281832 1.5052150 1.6089836 1.2546823 3.7139939 0.7354096 
##       j41       j43       j44       j47       j52       j55       j57 
## 2.3523378 2.0443028 0.7625648 2.0214761 3.1681920 1.6574447 0.5642851 
##       j58       j60       j63       j64       j67       j71       j72 
## 0.3752455 0.3925035 1.0169193 0.2855671 2.2769714 0.7676735 2.1696032 
##       j73       j74       j75       j76       j77       j78       j79 
## 1.5977747 0.6011404 0.8200133 1.7698638 0.9651867 1.4886416 1.2758317 
##       j80       j81       j82       j83       j85       j86       j87 
## 1.9456112 1.9217651 0.7913879 2.1343133 1.5578134 0.6719209 1.5642772 
##       j88       j89       j90       j91       j92       j93       j94 
## 1.0111921 1.6824834 0.4780608 1.3664298 1.2034388 1.4178410 0.8320279 
##       j95       j96       j97       j98      j100 
## 0.7395626 1.0744937 1.1873282 2.7289559 2.3558569

Aggregate predictions

pred_hybrid <- rbind(
  as(pred_pop, "matrix"),
  as(pred_ib, "matrix"),
  as(pred_svd, "matrix")
  )
pred_hybrid
##               j1       j2         j3         j4 j5       j6 j7 j8
## u3464  1.8518315 1.193899  1.1091898 -0.6578812 NA 2.514568 NA NA
## u3464 -0.9449249 1.304973 -0.5044573 -1.2374976 NA 3.538448 NA NA
## u3464  1.2162797 3.391140  1.4208575  2.2676158 NA 2.099415 NA NA
##               j9       j10 j11 j12 j13 j14 j15 j16 j17 j18 j19 j20 j21 j22
## u3464  0.2068026 2.1183361  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
## u3464 -3.3740607 1.3709456  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
## u3464  1.9258811 0.7285243  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
##              j23        j24 j25 j26 j27      j28 j29        j30 j31 j32
## u3464  1.0222389 -0.8302369  NA  NA  NA 2.322508  NA  0.3970870  NA  NA
## u3464 -0.7479066 -2.6690650  NA  NA  NA 2.332187  NA -0.8367929  NA  NA
## u3464  0.8967785  1.4281832  NA  NA  NA 1.505215  NA  1.6089836  NA  NA
##             j33      j34 j35 j36        j37 j38 j39 j40        j41 j42
## u3464 -0.618737 1.801905  NA  NA -0.6583003  NA  NA  NA  0.5486783  NA
## u3464 -2.300745 3.745398  NA  NA -3.2564195  NA  NA  NA -1.0892991  NA
## u3464  1.254682 3.713994  NA  NA  0.7354096  NA  NA  NA  2.3523378  NA
##               j43        j44 j45 j46      j47 j48 j49 j50 j51       j52
## u3464 -0.04515578 -1.4464704  NA  NA 2.355128  NA  NA  NA  NA 0.7608613
## u3464 -2.71452008 -3.1107662  NA  NA 3.048937  NA  NA  NA  NA 2.9396625
## u3464  2.04430283  0.7625648  NA  NA 2.021476  NA  NA  NA  NA 3.1681920
##       j53 j54       j55 j56        j57        j58 j59        j60 j61 j62
## u3464  NA  NA  1.297992  NA -1.2756183 -3.0893463  NA  0.4208527  NA  NA
## u3464  NA  NA -1.836171  NA -3.0119828 -2.6633603  NA -4.1084304  NA  NA
## u3464  NA  NA  1.657445  NA  0.5642851  0.3752455  NA  0.3925035  NA  NA
##             j63        j64 j65 j66         j67 j68 j69 j70         j71
## u3464  1.045112  0.1694101  NA  NA -0.01429141  NA  NA  NA  0.01756387
## u3464 -1.276034 -3.4415371  NA  NA -2.18717354  NA  NA  NA -2.89697576
## u3464  1.016919  0.2855671  NA  NA  2.27697144  NA  NA  NA  0.76767348
##            j72      j73        j74        j75      j76        j77      j78
## u3464 3.484186 1.741571 -0.8295976  0.3905828 2.992587  1.2250021 2.293076
## u3464 2.377972 2.766644 -1.7621165 -1.0288039 3.384321 -2.1076633 1.962362
## u3464 2.169603 1.597775  0.6011404  0.8200133 1.769864  0.9651867 1.488642
##              j79      j80      j81        j82      j83 j84      j85
## u3464  0.6272325 1.641449 2.324696  1.6932520 2.632409  NA 1.576741
## u3464 -0.8855348 3.930699 3.170186 -1.5494428 4.388334  NA 1.584816
## u3464  1.2758317 1.945611 1.921765  0.7913879 2.134313  NA 1.557813
##              j86      j87      j88      j89        j90      j91      j92
## u3464  0.9206942 2.473371 2.675502 4.137089  1.0588159 2.613713 1.895172
## u3464 -3.2009100 2.901110 2.059897 2.515431 -2.3317193 2.812482 1.323954
## u3464  0.6719209 1.564277 1.011192 1.682483  0.4780608 1.366430 1.203439
##            j93        j94        j95       j96       j97      j98 j99
## u3464 3.076595  1.5494225  1.5335302 2.0669835 2.2460524 1.511324  NA
## u3464 2.756810 -2.5064341 -1.9878839 0.4196987 0.6772776 3.666978  NA
## u3464 1.417841  0.8320279  0.7395626 1.0744937 1.1873282 2.728956  NA
##           j100
## u3464 1.930659
## u3464 3.710318
## u3464 2.355857
pred_hybrid <- colMeans(pred_hybrid)
pred_hybrid
##          j1          j2          j3          j4          j5          j6 
##  0.70772876  1.96333734  0.67519668  0.12407898          NA  2.71747716 
##          j7          j8          j9         j10         j11         j12 
##          NA          NA -0.41379236  1.40593535          NA          NA 
##         j13         j14         j15         j16         j17         j18 
##          NA          NA          NA          NA          NA          NA 
##         j19         j20         j21         j22         j23         j24 
##          NA          NA          NA          NA  0.39037028 -0.69037289 
##         j25         j26         j27         j28         j29         j30 
##          NA          NA          NA  2.05330347          NA  0.38975927 
##         j31         j32         j33         j34         j35         j36 
##          NA          NA -0.55493310  3.08709915          NA          NA 
##         j37         j38         j39         j40         j41         j42 
## -1.05977009          NA          NA          NA  0.60390567          NA 
##         j43         j44         j45         j46         j47         j48 
## -0.23845768 -1.26489062          NA          NA  2.47518005          NA 
##         j49         j50         j51         j52         j53         j54 
##          NA          NA          NA  2.28957195          NA          NA 
##         j55         j56         j57         j58         j59         j60 
##  0.37308874          NA -1.24110537 -1.79248702          NA -1.09835808 
##         j61         j62         j63         j64         j65         j66 
##          NA          NA  0.26199941 -0.99551997          NA          NA 
##         j67         j68         j69         j70         j71         j72 
##  0.02516883          NA          NA          NA -0.70391280  2.67725357 
##         j73         j74         j75         j76         j77         j78 
##  2.03532982 -0.66352456  0.06059737  2.71559081  0.02750851  1.91469313 
##         j79         j80         j81         j82         j83         j84 
##  0.33917647  2.50591997  2.47221563  0.31173238  3.05168560          NA 
##         j85         j86         j87         j88         j89         j90 
##  1.57312348 -0.53609829  2.31291917  1.91553058  2.77833451 -0.26494750 
##         j91         j92         j93         j94         j95         j96 
##  2.26420821  1.47418821  2.41708214 -0.04166125  0.09506966  1.18705861 
##         j97         j98         j99        j100 
##  1.37021941  2.63575245          NA  2.66561149

Create a top-N list from the aggregated ratings

pred <- getTopNLists(as(rbind(pred_hybrid), "realRatingMatrix"))
as(pred, "list")
## $pred_hybrid
##  [1] "j34"  "j83"  "j89"  "j6"   "j76"  "j72"  "j100" "j98"  "j80"  "j47"

Compare with the results by the individual recommenders

as(getTopNLists(pred_pop), "list")
## $u3464
##  [1] "j89" "j72" "j93" "j76" "j88" "j83" "j91" "j6"  "j87" "j47"
as(getTopNLists(pred_ib), "list")
## $u3464
##  [1] "j83"  "j80"  "j34"  "j100" "j98"  "j6"   "j76"  "j81"  "j47"  "j52"
as(getTopNLists(pred_svd), "list")
## $u3464
##  [1] "j34"  "j2"   "j52"  "j98"  "j100" "j41"  "j67"  "j4"   "j72"  "j83"