library(mlbench)
library(readr)

library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
library("knitr")


Data01 = read.csv("C:\\Users\\huiwu\\OneDrive\\Desktop\\CAU\\Spring2025\\R_DataScience\\Data_250626.csv")

summary(Data01)
##     paper               pens             pencils               X            
##  Length:9           Length:9           Length:9           Length:9          
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character
itemsets1<-apriori(Data01,parameter=list(minlen=1,maxlen=1,support=0.25,confidence=0.6,target="frequent itemsets"))
## Warning: Column(s) 1, 2, 3, 4 not logical or factor. Applying default
## discretization (see '? discretizeDF').
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##          NA    0.1    1 none FALSE            TRUE       5    0.25      1
##  maxlen            target  ext
##       1 frequent itemsets TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[15 item(s), 9 transaction(s)] done [0.00s].
## sorting and recoding items ... [7 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1
## Warning in apriori(Data01, parameter = list(minlen = 1, maxlen = 1, support =
## 0.25, : Mining stopped (maxlen reached). Only patterns up to a length of 1
## returned!
##  done [0.00s].
## sorting transactions ... done [0.00s].
## writing ... [7 set(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
summary(itemsets1) 
## set of 7 itemsets
## 
## most frequent items:
## paper=paper  paper=pens  pens=cards  pens=ruler    pencils=     (Other) 
##           1           1           1           1           1           2 
## 
## element (itemset/transaction) length distribution:sizes
## 1 
## 7 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1       1       1       1       1       1 
## 
## summary of quality measures:
##     support           count      
##  Min.   :0.3333   Min.   :3.000  
##  1st Qu.:0.3333   1st Qu.:3.000  
##  Median :0.3333   Median :3.000  
##  Mean   :0.4127   Mean   :3.714  
##  3rd Qu.:0.3889   3rd Qu.:3.500  
##  Max.   :0.7778   Max.   :7.000  
## 
## includes transaction ID lists: FALSE 
## 
## mining info:
##    data ntransactions support confidence
##  Data01             9    0.25          1
##                                                                                                                              call
##  apriori(data = Data01, parameter = list(minlen = 1, maxlen = 1, support = 0.25, confidence = 0.6, target = "frequent itemsets"))
inspect(head(sort(itemsets1,by="support"),10)) 
##     items           support   count
## [1] {X=}            0.7777778 7    
## [2] {paper=paper}   0.4444444 4    
## [3] {paper=pens}    0.3333333 3    
## [4] {pens=ruler}    0.3333333 3    
## [5] {pencils=}      0.3333333 3    
## [6] {pens=cards}    0.3333333 3    
## [7] {pencils=cards} 0.3333333 3
itemsets2<-apriori(Data01,parameter=list(minlen=2,maxlen=2,support=0.25,confidence=0.6,target="frequent itemsets"))
## Warning: Column(s) 1, 2, 3, 4 not logical or factor. Applying default
## discretization (see '? discretizeDF').
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##          NA    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen            target  ext
##       2 frequent itemsets TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[15 item(s), 9 transaction(s)] done [0.00s].
## sorting and recoding items ... [7 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2
## Warning in apriori(Data01, parameter = list(minlen = 2, maxlen = 2, support =
## 0.25, : Mining stopped (maxlen reached). Only patterns up to a length of 2
## returned!
##  done [0.00s].
## sorting transactions ... done [0.00s].
## writing ... [4 set(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(sort(itemsets2,by="support"),10)) 
##     items            support   count
## [1] {paper=pens, X=} 0.3333333 3    
## [2] {pens=ruler, X=} 0.3333333 3    
## [3] {pencils=, X=}   0.3333333 3    
## [4] {pens=cards, X=} 0.3333333 3
summary(itemsets2) 
## set of 4 itemsets
## 
## most frequent items:
##         X= paper=pens pens=cards pens=ruler   pencils=    (Other) 
##          4          1          1          1          1          0 
## 
## element (itemset/transaction) length distribution:sizes
## 2 
## 4 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       2       2       2       2       2       2 
## 
## summary of quality measures:
##     support           count  
##  Min.   :0.3333   Min.   :3  
##  1st Qu.:0.3333   1st Qu.:3  
##  Median :0.3333   Median :3  
##  Mean   :0.3333   Mean   :3  
##  3rd Qu.:0.3333   3rd Qu.:3  
##  Max.   :0.3333   Max.   :3  
## 
## includes transaction ID lists: FALSE 
## 
## mining info:
##    data ntransactions support confidence
##  Data01             9    0.25          1
##                                                                                                                              call
##  apriori(data = Data01, parameter = list(minlen = 2, maxlen = 2, support = 0.25, confidence = 0.6, target = "frequent itemsets"))
rules <- apriori(Data01,parameter=list(support=0.25,confidence=0.6,target="rules"))
## Warning: Column(s) 1, 2, 3, 4 not logical or factor. Applying default
## discretization (see '? discretizeDF').
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.6    0.1    1 none FALSE            TRUE       5    0.25      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: 2 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[15 item(s), 9 transaction(s)] done [0.00s].
## sorting and recoding items ... [7 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [5 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules
## set of 5 rules
str(rules)
## Formal class 'rules' [package "arules"] with 4 slots
##   ..@ lhs    :Formal class 'itemMatrix' [package "arules"] with 3 slots
##   .. .. ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
##   .. .. .. .. ..@ i       : int [1:4] 2 6 7 3
##   .. .. .. .. ..@ p       : int [1:6] 0 0 1 2 3 4
##   .. .. .. .. ..@ Dim     : int [1:2] 15 5
##   .. .. .. .. ..@ Dimnames:List of 2
##   .. .. .. .. .. ..$ : NULL
##   .. .. .. .. .. ..$ : NULL
##   .. .. .. .. ..@ factors : list()
##   .. .. ..@ itemInfo   :'data.frame':    15 obs. of  3 variables:
##   .. .. .. ..$ labels   : chr [1:15] "paper=paper" "paper=pencils" "paper=pens" "pens=cards" ...
##   .. .. .. ..$ variables: Factor w/ 4 levels "paper","pencils",..: 1 1 1 3 3 3 3 2 2 2 ...
##   .. .. .. ..$ levels   : Factor w/ 8 levels "","calculator",..: 5 6 7 3 6 7 8 1 2 3 ...
##   .. .. ..@ itemsetInfo:'data.frame':    0 obs. of  0 variables
##   ..@ rhs    :Formal class 'itemMatrix' [package "arules"] with 3 slots
##   .. .. ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
##   .. .. .. .. ..@ i       : int [1:5] 12 12 12 12 12
##   .. .. .. .. ..@ p       : int [1:6] 0 1 2 3 4 5
##   .. .. .. .. ..@ Dim     : int [1:2] 15 5
##   .. .. .. .. ..@ Dimnames:List of 2
##   .. .. .. .. .. ..$ : NULL
##   .. .. .. .. .. ..$ : NULL
##   .. .. .. .. ..@ factors : list()
##   .. .. ..@ itemInfo   :'data.frame':    15 obs. of  3 variables:
##   .. .. .. ..$ labels   : chr [1:15] "paper=paper" "paper=pencils" "paper=pens" "pens=cards" ...
##   .. .. .. ..$ variables: Factor w/ 4 levels "paper","pencils",..: 1 1 1 3 3 3 3 2 2 2 ...
##   .. .. .. ..$ levels   : Factor w/ 8 levels "","calculator",..: 5 6 7 3 6 7 8 1 2 3 ...
##   .. .. ..@ itemsetInfo:'data.frame':    0 obs. of  0 variables
##   ..@ quality:'data.frame':  5 obs. of  5 variables:
##   .. ..$ support   : num [1:5] 0.778 0.333 0.333 0.333 0.333
##   .. ..$ confidence: num [1:5] 0.778 1 1 1 1
##   .. ..$ coverage  : num [1:5] 1 0.333 0.333 0.333 0.333
##   .. ..$ lift      : num [1:5] 1 1.29 1.29 1.29 1.29
##   .. ..$ count     : int [1:5] 7 3 3 3 3
##   ..@ info   :List of 5
##   .. ..$ data         : symbol Data01
##   .. ..$ ntransactions: int 9
##   .. ..$ support      : num 0.25
##   .. ..$ confidence   : num 0.6
##   .. ..$ call         : chr "apriori(data = Data01, parameter = list(support = 0.25, confidence = 0.6, target = \"rules\"))"
summary(rules) 
## set of 5 rules
## 
## rule length distribution (lhs + rhs):sizes
## 1 2 
## 1 4 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0     2.0     2.0     1.8     2.0     2.0 
## 
## summary of quality measures:
##     support         confidence        coverage           lift      
##  Min.   :0.3333   Min.   :0.7778   Min.   :0.3333   Min.   :1.000  
##  1st Qu.:0.3333   1st Qu.:1.0000   1st Qu.:0.3333   1st Qu.:1.286  
##  Median :0.3333   Median :1.0000   Median :0.3333   Median :1.286  
##  Mean   :0.4222   Mean   :0.9556   Mean   :0.4667   Mean   :1.229  
##  3rd Qu.:0.3333   3rd Qu.:1.0000   3rd Qu.:0.3333   3rd Qu.:1.286  
##  Max.   :0.7778   Max.   :1.0000   Max.   :1.0000   Max.   :1.286  
##      count    
##  Min.   :3.0  
##  1st Qu.:3.0  
##  Median :3.0  
##  Mean   :3.8  
##  3rd Qu.:3.0  
##  Max.   :7.0  
## 
## mining info:
##    data ntransactions support confidence
##  Data01             9    0.25        0.6
##                                                                                          call
##  apriori(data = Data01, parameter = list(support = 0.25, confidence = 0.6, target = "rules"))
plot(rules)  
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

inspect(head(sort(rules,by="lift"),10))
##     lhs             rhs  support   confidence coverage  lift     count
## [1] {paper=pens} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [2] {pens=ruler} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [3] {pencils=}   => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [4] {pens=cards} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [5] {}           => {X=} 0.7777778 0.7777778  1.0000000 1.000000 7
inspect( head( sort (rules, by = "lift")))
##     lhs             rhs  support   confidence coverage  lift     count
## [1] {paper=pens} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [2] {pens=ruler} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [3] {pencils=}   => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [4] {pens=cards} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [5] {}           => {X=} 0.7777778 0.7777778  1.0000000 1.000000 7
inspect( head( sort (rules, by = "confidence")))
##     lhs             rhs  support   confidence coverage  lift     count
## [1] {paper=pens} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [2] {pens=ruler} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [3] {pencils=}   => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [4] {pens=cards} => {X=} 0.3333333 1.0000000  0.3333333 1.285714 3    
## [5] {}           => {X=} 0.7777778 0.7777778  1.0000000 1.000000 7







## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```