Directions

From the Hogwarts course catalogue...

Data for demo

Back to the spellbook

1. Load data

##   Student Astronomy Charms Herbology Defence.Against.the.Dark.Arts Potions
## 1       1         0      1         1                             1       1
## 2       2         0      0         1                             0       1
## 3       3         0      1         0                             0       1
## 4       4         0      0         1                             1       1
## 5       5         0      1         0                             0       1
## 6       6         0      0         0                             0       1
##   History.of.Magic Transfiguration Flying Ancient.Runes Arithmancy
## 1                0               1      1             1          0
## 2                0               1      1             0          0
## 3                1               1      1             1          1
## 4                0               1      0             0          0
## 5                0               1      1             1          1
## 6                0               0      0             0          0
##   Care.for.Magical.Creatures Divination Muggle.Studies Quidditch
## 1                          0          0              0         1
## 2                          1          1              0         0
## 3                          1          1              1         0
## 4                          1          0              0         1
## 5                          0          1              1         0
## 6                          0          0              0         1
## [1] 1000
## 'data.frame':    1000 obs. of  15 variables:
##  $ Student                      : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Astronomy                    : int  0 0 0 0 0 0 0 0 0 1 ...
##  $ Charms                       : int  1 0 1 0 1 0 1 0 0 1 ...
##  $ Herbology                    : int  1 1 0 1 0 0 1 1 0 1 ...
##  $ Defence.Against.the.Dark.Arts: int  1 0 0 1 0 0 1 1 0 1 ...
##  $ Potions                      : int  1 1 1 1 1 1 1 0 1 0 ...
##  $ History.of.Magic             : int  0 0 1 0 0 0 0 0 0 0 ...
##  $ Transfiguration              : int  1 1 1 1 1 0 1 1 0 0 ...
##  $ Flying                       : int  1 1 1 0 1 0 1 0 0 0 ...
##  $ Ancient.Runes                : int  1 0 1 0 1 0 1 1 0 1 ...
##  $ Arithmancy                   : int  0 0 1 0 1 0 1 1 0 1 ...
##  $ Care.for.Magical.Creatures   : int  0 1 1 1 0 0 1 0 1 0 ...
##  $ Divination                   : int  0 1 1 0 1 0 1 1 0 0 ...
##  $ Muggle.Studies               : int  0 0 1 0 1 0 0 0 1 0 ...
##  $ Quidditch                    : int  1 0 0 1 0 1 0 0 0 0 ...

Remove the first column because that’s the student ID.

##   Astronomy Charms Herbology Defence.Against.the.Dark.Arts Potions
## 1         0      1         1                             1       1
## 2         0      0         1                             0       1
## 3         0      1         0                             0       1
## 4         0      0         1                             1       1
## 5         0      1         0                             0       1
## 6         0      0         0                             0       1
##   History.of.Magic Transfiguration Flying Ancient.Runes Arithmancy
## 1                0               1      1             1          0
## 2                0               1      1             0          0
## 3                1               1      1             1          1
## 4                0               1      0             0          0
## 5                0               1      1             1          1
## 6                0               0      0             0          0
##   Care.for.Magical.Creatures Divination Muggle.Studies Quidditch
## 1                          0          0              0         1
## 2                          1          1              0         0
## 3                          1          1              1         0
## 4                          1          0              0         1
## 5                          0          1              1         0
## 6                          0          0              0         1
##  [1] "Astronomy"                     "Charms"                       
##  [3] "Herbology"                     "Defence.Against.the.Dark.Arts"
##  [5] "Potions"                       "History.of.Magic"             
##  [7] "Transfiguration"               "Flying"                       
##  [9] "Ancient.Runes"                 "Arithmancy"                   
## [11] "Care.for.Magical.Creatures"    "Divination"                   
## [13] "Muggle.Studies"                "Quidditch"

2. Libraries

## Warning: package 'recommenderlab' was built under R version 3.6.3
## Loading required package: Matrix
## Loading required package: arules
## Warning: package 'arules' was built under R version 3.6.3
## 
## 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
## Registered S3 methods overwritten by 'registry':
##   method               from 
##   print.registry_field proxy
##   print.registry_entry proxy

3. Summary data

##  [1] "Astronomy"                     "Charms"                       
##  [3] "Herbology"                     "Defence.Against.the.Dark.Arts"
##  [5] "Potions"                       "History.of.Magic"             
##  [7] "Transfiguration"               "Flying"                       
##  [9] "Ancient.Runes"                 "Arithmancy"                   
## [11] "Care.for.Magical.Creatures"    "Divination"                   
## [13] "Muggle.Studies"                "Quidditch"
##   Astronomy Charms Herbology Defence.Against.the.Dark.Arts Potions
## 1        54    363       280                           149     442
##   History.of.Magic Transfiguration Flying Ancient.Runes Arithmancy
## 1               42             279    234           357        381
##   Care.for.Magical.Creatures Divination Muggle.Studies Quidditch
## 1                        536        490            322       457

5. Convert to transactions

## transactions in sparse format with
##  1000 transactions (rows) and
##  14 items (columns)
##                     Astronomy                        Charms 
##                         0.054                         0.363 
##                     Herbology Defence.Against.the.Dark.Arts 
##                         0.280                         0.149 
##                       Potions              History.of.Magic 
##                         0.442                         0.042 
##               Transfiguration                        Flying 
##                         0.279                         0.234 
##                 Ancient.Runes                    Arithmancy 
##                         0.357                         0.381 
##    Care.for.Magical.Creatures                    Divination 
##                         0.536                         0.490 
##                Muggle.Studies                     Quidditch 
##                         0.322                         0.457

6. Association rules

## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.02      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 20 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[14 item(s), 1000 transaction(s)] done [0.00s].
## sorting and recoding items ... [14 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 6 7 8 done [0.00s].
## writing ... [4956 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
## set of 4956 rules
##      lhs                  rhs                             support confidence coverage     lift count
## [1]  {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes,                                                                                
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.025  0.9615385    0.026 6.453278    25
## [2]  {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes,                                                                                
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.025  0.9615385    0.026 6.453278    25
## [3]  {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Arithmancy,                                                                                   
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.023  0.9583333    0.024 6.431767    23
## [4]  {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Arithmancy,                                                                                   
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.022  0.9565217    0.023 6.419609    22
## [5]  {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes,                                                                                
##       Arithmancy,                                                                                   
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.021  0.9545455    0.022 6.406345    21
## [6]  {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes,                                                                                
##       Arithmancy,                                                                                   
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.021  0.9545455    0.022 6.406345    21
## [7]  {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.038  0.9268293    0.041 6.220331    38
## [8]  {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.037  0.9250000    0.040 6.208054    37
## [9]  {Charms,                                                                                       
##       Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.026  0.8965517    0.029 6.017126    26
## [10] {Charms,                                                                                       
##       Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.026  0.8965517    0.029 6.017126    26
## [11] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes}   => {Defence.Against.the.Dark.Arts}   0.032  0.8888889    0.036 5.965697    32
## [12] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Arithmancy,                                                                                   
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.031  0.8857143    0.035 5.944391    31
## [13] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Arithmancy}      => {Defence.Against.the.Dark.Arts}   0.029  0.8787879    0.033 5.897905    29
## [14] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Ancient.Runes,                                                                                
##       Arithmancy,                                                                                   
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.029  0.8787879    0.033 5.897905    29
## [15] {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying}          => {Defence.Against.the.Dark.Arts}   0.056  0.8750000    0.064 5.872483    56
## [16] {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes}   => {Defence.Against.the.Dark.Arts}   0.035  0.8750000    0.040 5.872483    35
## [17] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Ancient.Runes,                                                                                
##       Arithmancy}      => {Defence.Against.the.Dark.Arts}   0.028  0.8750000    0.032 5.872483    28
## [18] {Herbology,                                                                                    
##       Transfiguration,                                                                              
##       Flying,                                                                                       
##       Arithmancy}      => {Defence.Against.the.Dark.Arts}   0.034  0.8717949    0.039 5.850972    34
## [19] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Ancient.Runes,                                                                                
##       Quidditch}       => {Defence.Against.the.Dark.Arts}   0.034  0.8717949    0.039 5.850972    34
## [20] {Herbology,                                                                                    
##       Potions,                                                                                      
##       Transfiguration,                                                                              
##       Flying}          => {Defence.Against.the.Dark.Arts}   0.047  0.8703704    0.054 5.841412    47

7. Interpretation

Interpreting the first 3 rules:

Rule 1: After taking Herbology, Transfiguration, Flying, Ancient Runes, and Quidditch, with 96.15% confidence, Defence Against the Dark Arts will also be taken this occurs 2.5% of the time. The lift ratio is 6.45 (much larger than 1.0), indicating strong association.

Rule 2: After taking Herbology, Potions, Transfiguration, Flying, Ancient Runes, and Quidditch, with 96.15% confidence, Defence Against the Dark Arts will also be taken this occurs 2.5% of the time. The lift ratio is 6.45 (much arger than 1.0), indicating strong association.

Rule 3: After taking Herbology, Transfiguration, Flying, Arithmancy, and Quidditch, with 96.15% confidence, Defence Against the Dark Arts will also be taken this occurs 2.5% of the time. The lift ratio is 6.45 (much arger than 1.0), indicating strong association.

The first 5 rules have similar support, confidence, and lift ratios. This indicates similarity in the users’ purchases.Incidentally, the top 20 rules all have a similar consequent, which is Defence Against the Dark Arts. Hence, regardless of the conbination of items purchased, Defence Against the Dark Arts is needed.

This is obvious, because He Who Shall Not Be Named is coming.