# library arules ,arulesViz, plotly and htmlwidgets to be included
ds = read.csv("AppleCart.csv",header = TRUE)
CT = as.matrix(ds)
txn = as(CT, "transactions")
cat(sprintf("\n The details of the rules formed are as follows\n"))
##
## The details of the rules formed are as follows
rules = apriori(txn,parameter = list(support = 0.1, confidence= 0.5,minlen = 2,target = "rules"))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.5 0.1 1 none FALSE TRUE 5 0.1 2
## 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: 200
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[12 item(s), 2000 transaction(s)] done [0.00s].
## sorting and recoding items ... [10 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 done [0.00s].
## writing ... [15 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
inspect(head(sort(rules,by = "lift"),n =10))
## lhs rhs support confidence coverage
## [1] {RetinaDisplay,Stand} => {Speakers} 0.1020 0.6276923 0.1625
## [2] {RetinaDisplay,Speakers} => {Stand} 0.1020 0.5230769 0.1950
## [3] {RetinaDisplay,X32GB} => {Speakers} 0.1045 0.5773481 0.1810
## [4] {Stand} => {Speakers} 0.1275 0.5290456 0.2410
## [5] {Stand,Speakers} => {RetinaDisplay} 0.1020 0.8000000 0.1275
## [6] {Case} => {Speakers} 0.1105 0.5151515 0.2145
## [7] {X32GB,Speakers} => {RetinaDisplay} 0.1045 0.7411348 0.1410
## [8] {Speakers} => {RetinaDisplay} 0.1950 0.7065217 0.2760
## [9] {Case} => {RetinaDisplay} 0.1515 0.7062937 0.2145
## [10] {Stand} => {RetinaDisplay} 0.1625 0.6742739 0.2410
## lift count
## [1] 2.274247 204
## [2] 2.170444 204
## [3] 2.091841 209
## [4] 1.916832 255
## [5] 1.891253 204
## [6] 1.866491 221
## [7] 1.752092 209
## [8] 1.670264 390
## [9] 1.669725 303
## [10] 1.594028 325
plotly_arules((head(sort(rules,by = "lift"),n =10)))
## Warning: 'plotly_arules' is deprecated.
## Use 'plot' instead.
## See help("Deprecated")
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
itemFrequencyPlot(txn, topN=15, type="absolute", main="Item Frequency")
distance = dissimilarity(txn,which = "items",method = "dice")
fit = hclust(distance,method = "ward.D")
plot(fit,main = "Dendogram of items")
rect.hclust(fit,k=3,border = "red")
plot(rules, method = "graph", engine = "htmlwidget")
cat(sprintf("\n \n"))
10 rules have a support count of 10% and confidence of 50%. Increasing the minimum support required removes spurious rules resulting from coincidence. The risk of raising the minimum support required is that we cull out meaningful rules that involve uncommon items. Antecedent: RetinaDisplay, Stand ; Consequent: Speakers. If an iPad with retina display is purchased with a stand, then speakers are also purchased.The support count of this item set is 204, which means that these three items were purchase together 204 times.The confidence of this rule 62.77%, which means of the 325 times that the retina display and a stand were purchased together, 204 times speakers were also purchased.The lift ratio = 2.27, which means that a customer who has purchased a retina display and a stand is 127% more likely than a randomly selected customer to buy speakers.The top 15 rules have many features in common that allow Apple to focus on these collections. For instance, an iPad with retina display is often purchased along with other accessories, including a stand, speakers, cellular service, and a case. An iPad with retina display often is paired with a memory upgrade. A memory upgrade to 32 GB is commonly associated with speakers and/or retina display. Commonly associated accessories include stand and speakers or case and speakers.