Association Rules

Movies dataset

Assigment 25

library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Warning: package 'arulesViz' was built under R version 3.5.1
## Loading required package: grid
data <- read.csv("C:\\Users\\RISHI RAHUL\\Desktop\\DS\\8 arules\\Assignment\\my_movies.csv")

data <- data[6:15]
class(data)
## [1] "data.frame"
colnames(data)
##  [1] "Sixth.Sense"   "Gladiator"     "LOTR1"         "Harry.Potter1"
##  [5] "Patriot"       "LOTR2"         "Harry.Potter2" "LOTR"         
##  [9] "Braveheart"    "Green.Mile"
data$Sixth.Sense <- factor(data$Sixth.Sense,levels = c("1","0"),labels = c("Sixth.Sense",""))
data$Gladiator <- factor(data$Gladiator,levels = c("1","0"),labels = c("Gladiator",""))
data$LOTR1 <- factor(data$LOTR1,levels = c("1","0"),labels = c("LOTR1",""))
data$Harry.Potter1 <- factor(data$Harry.Potter1,levels = c("1","0"),labels = c("Harry.Potter1",""))
data$Patriot <- factor(data$Patriot,levels = c("1","0"),labels = c("Patriot",""))
data$LOTR2 <- factor(data$LOTR2,levels = c("1","0"),labels = c("LOTR2",""))
data$Harry.Potter2 <- factor(data$Harry.Potter2,levels = c("1","0"),labels = c("Harry.Potter2",""))
data$LOTR <- factor(data$LOTR,levels = c("1","0"),labels = c("LOTR",""))
data$Braveheart <- factor(data$Braveheart,levels = c("1","0"),labels = c("Braveheart",""))
data$Green.Mile <- factor(data$Green.Mile,levels = c("1","0"),labels = c("Green.Mile",""))

#library(car)
#library(carData)
#library(mvinfluence)

data1 <- as(data,"transactions")

itemFrequencyPlot(data1,topN=15)

rules <- apriori(data1, parameter = list(supp = 0.005, confidence = 0.50, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.005      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 0 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[20 item(s), 10 transaction(s)] done [0.00s].
## sorting and recoding items ... [20 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(data1, parameter = list(supp = 0.005, confidence =
## 0.5, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [1491 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(sort(rules), n = 10))
##      lhs                 rhs              support confidence lift     
## [1]  {LOTR1=}         => {LOTR2=}         0.8     1.0000000  1.2500000
## [2]  {LOTR2=}         => {LOTR1=}         0.8     1.0000000  1.2500000
## [3]  {Harry.Potter1=} => {Harry.Potter2=} 0.8     1.0000000  1.1111111
## [4]  {Harry.Potter2=} => {Harry.Potter1=} 0.8     0.8888889  1.1111111
## [5]  {Green.Mile=}    => {LOTR=}          0.8     1.0000000  1.1111111
## [6]  {LOTR=}          => {Green.Mile=}    0.8     0.8888889  1.1111111
## [7]  {Braveheart=}    => {LOTR=}          0.8     0.8888889  0.9876543
## [8]  {LOTR=}          => {Braveheart=}    0.8     0.8888889  0.9876543
## [9]  {Braveheart=}    => {Harry.Potter2=} 0.8     0.8888889  0.9876543
## [10] {Harry.Potter2=} => {Braveheart=}    0.8     0.8888889  0.9876543
##      count
## [1]  8    
## [2]  8    
## [3]  8    
## [4]  8    
## [5]  8    
## [6]  8    
## [7]  8    
## [8]  8    
## [9]  8    
## [10] 8
plot(head(sort(rules, by = "lift"), n = 10), method = "graph", control = list(cex = 1.0)) #cex = font size

plot(rules)
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

plot(head(sort(rules), n = 10), method = "grouped", control = list(cex = 0.3))
## Warning: Unknown control parameters: cex
## Available control parameters (with default values):
## main  =  Grouped Matrix for 10 Rules
## k     =  20
## rhs_max   =  10
## lhs_items     =  2
## aggr.fun  =  function (x, ...)  UseMethod("mean")
## col   =  c("#EE0000FF", "#EE0303FF", "#EE0606FF", "#EE0909FF", "#EE0C0CFF", "#EE0F0FFF", "#EE1212FF", "#EE1515FF", "#EE1818FF", "#EE1B1BFF", "#EE1E1EFF", "#EE2222FF", "#EE2525FF", "#EE2828FF", "#EE2B2BFF", "#EE2E2EFF", "#EE3131FF", "#EE3434FF", "#EE3737FF", "#EE3A3AFF", "#EE3D3DFF", "#EE4040FF", "#EE4444FF", "#EE4747FF", "#EE4A4AFF", "#EE4D4DFF", "#EE5050FF", "#EE5353FF", "#EE5656FF", "#EE5959FF", "#EE5C5CFF", "#EE5F5FFF", "#EE6262FF", "#EE6666FF", "#EE6969FF", "#EE6C6CFF", "#EE6F6FFF", "#EE7272FF", "#EE7575FF",  "#EE7878FF", "#EE7B7BFF", "#EE7E7EFF", "#EE8181FF", "#EE8484FF", "#EE8888FF", "#EE8B8BFF", "#EE8E8EFF", "#EE9191FF", "#EE9494FF", "#EE9797FF", "#EE9999FF", "#EE9B9BFF", "#EE9D9DFF", "#EE9F9FFF", "#EEA0A0FF", "#EEA2A2FF", "#EEA4A4FF", "#EEA5A5FF", "#EEA7A7FF", "#EEA9A9FF", "#EEABABFF", "#EEACACFF", "#EEAEAEFF", "#EEB0B0FF", "#EEB1B1FF", "#EEB3B3FF", "#EEB5B5FF", "#EEB7B7FF", "#EEB8B8FF", "#EEBABAFF", "#EEBCBCFF", "#EEBDBDFF", "#EEBFBFFF", "#EEC1C1FF", "#EEC3C3FF", "#EEC4C4FF", "#EEC6C6FF", "#EEC8C8FF",  "#EEC9C9FF", "#EECBCBFF", "#EECDCDFF", "#EECFCFFF", "#EED0D0FF", "#EED2D2FF", "#EED4D4FF", "#EED5D5FF", "#EED7D7FF", "#EED9D9FF", "#EEDBDBFF", "#EEDCDCFF", "#EEDEDEFF", "#EEE0E0FF", "#EEE1E1FF", "#EEE3E3FF", "#EEE5E5FF", "#EEE7E7FF", "#EEE8E8FF", "#EEEAEAFF", "#EEECECFF", "#EEEEEEFF")
## reverse   =  TRUE
## xlab  =  NULL
## ylab  =  NULL
## legend    =  Size: support  Color: lift
## spacing   =  -1
## panel.function    =  function (row, size, shading, spacing)  {     size[size == 0] <- NA     shading[is.na(shading)] <- 1     grid.circle(x = c(1:length(size)), y = row, r = size/2 * (1 - spacing), default.units = "native", gp = gpar(fill = shading, col = shading, alpha = 0.9)) }
## gp_main   =  list(cex = 1.2, fontface = "bold", font = c(bold = 2))
## gp_labels     =  list(cex = 0.8)
## gp_labs   =  list(cex = 1.2, fontface = "bold", font = c(bold = 2))
## gp_lines  =  list(col = "gray", lty = 3)
## newpage   =  TRUE
## max.shading   =  NA
## engine    =  default
## verbose   =  FALSE