在R中,我试图使用arules实现apriori,

时间:2016-06-01 07:31:41

标签: r apriori arules

我的数据集看起来像这样

A          B       C       D    PR1 PR2 PR3 PR4
Values  Values  Values  Values  0   1   0   0
Values  Values  Values  Values  1   0   0   0
Values  Values  Values  Values  0   0   1   0
Values  Values  Values  Values  0   0   0   1
Values  Values  Values  Values  0   1   0   0
Values  Values  Values  Values  1   0   0   0
Values  Values  Values  Values  0   0   1   0
Values  Values  Values  Values  0   0   0   1
Values  Values  Values  Values  0   1   0   0
Values  Values  Values  Values  1   0   0   0
Values  Values  Values  Values  0   0   1   0
Values  Values  Values  Values  0   0   0   1
Values  Values  Values  Values  0   1   0   0
Values  Values  Values  Values  1   0   0   0

我想在lhs中只有a,b,c和d列,而rhs必须只包含值为1的PR1,PR2,PR3,PR4列。 例如,我期待的结果是

lhs = {a,b}, RHS{PR1=1}
lhs= {b,c,d}, RHS {PR3 =1}

我一直在使用下面的代码,但没有得到理想的结果

r <- apriori(b, parameter = list(supp = 0.1, conf = 0.9),              
             appearance = list(lhs = c("A","B","C","D")))

有人可以为此提供解决方案

1 个答案:

答案 0 :(得分:1)

&#34;没有得到理想的结果&#34; 并不是出错的最佳描述。例如,在降低所需的最低置信度时,它似乎对我有用:

library(arules)
r <- apriori(b, parameter = list(supp = .2, conf = .2), 
                                 appearance=list(lhs=c("A","B","C","D")))
inspect(subset(r, rhs(r) %ain% "PR1" & lhs(r) %ain% c("A", "B")))
#    lhs          rhs   support   confidence lift
# 41 {A,B}     => {PR1} 0.2857143 0.2857143  1   
# 58 {A,B,C}   => {PR1} 0.2857143 0.2857143  1   
# 59 {A,B,D}   => {PR1} 0.2857143 0.2857143  1   
# 64 {A,B,C,D} => {PR1} 0.2857143 0.2857143  1  
inspect(subset(r, rhs(r) %ain% "PR1" & labels(lhs(r))=="{A,B}"))
#    lhs      rhs   support   confidence lift
# 41 {A,B} => {PR1} 0.2857143 0.2857143  1   
inspect(subset(r, labels(rhs(r))=="{PR3}" & labels(lhs(r))=="{B,C,D}"))
# 45 {B,C,D} => {PR3} 0.2142857 0.2142857  1 

数据:

b <- new("transactions"
    , data = new("ngCMatrix"
    , i = c(0L, 1L, 2L, 3L, 5L, 0L, 1L, 2L, 3L, 4L, 0L, 1L, 2L, 3L, 6L, 
0L, 1L, 2L, 3L, 7L, 0L, 1L, 2L, 3L, 5L, 0L, 1L, 2L, 3L, 4L, 0L, 
1L, 2L, 3L, 6L, 0L, 1L, 2L, 3L, 7L, 0L, 1L, 2L, 3L, 5L, 0L, 1L, 
2L, 3L, 4L, 0L, 1L, 2L, 3L, 6L, 0L, 1L, 2L, 3L, 7L, 0L, 1L, 2L, 
3L, 5L, 0L, 1L, 2L, 3L, 4L)
    , p = c(0L, 5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 
65L, 70L)
    , Dim = c(8L, 14L)
    , Dimnames = list(NULL, NULL)
    , factors = list()
)
    , itemInfo = structure(list(labels = c("A", "B", "C", "D", "PR1", "PR2", "PR3", 
"PR4")), .Names = "labels", row.names = c(NA, -8L), class = "data.frame")
    , itemsetInfo = structure(list(transactionID = c("1", "2", "3", "4", "5", "6", 
"7", "8", "9", "10", "11", "12", "13", "14")), .Names = "transactionID", row.names = c(NA, 
-14L), class = "data.frame")
)