[R] Arules questions. I need some help please

Michael Hahsler michael at hahsler.net
Mon Jul 20 02:16:40 CEST 2009


> Question 2a)
> I am also working with arules package and I have the following problem
> let suppose the matrix b like:
> b<-matrix(c(1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,1,1,1,1,1,1,1),nrow=6)
> rownames(b)=c("T1", "T2", "T3", "T4", "T5", "T6")
> colnames(b)=c("It1", "It2", "It3", "It4")
> bt<-as(b, "transactions")
> rules<-apriori(bt, parameter = list(maxlen=2))
> k<-inspect(rules)
> k
> ##we obtain
>   lhs      rhs     support confidence lift
> 1 {}    => {It4} 1.0000000          1  1.0
> 2 {}    => {It1} 1.0000000          1  1.0
> 3 {It3} => {It2} 0.5000000          1  1.5
> 4 {It3} => {It4} 0.5000000          1  1.0
> 5 {It3} => {It1} 0.5000000          1  1.0
> 6 {It2} => {It4} 0.6666667          1  1.0
> 7 {It2} => {It1} 0.6666667          1  1.0
> 8 {It4} => {It1} 1.0000000          1  1.0
> 9 {It1} => {It4} 1.0000000          1  1.0
> 
> WRITE(rules)
> ##we obtain
> "rules" "support" "confidence" "lift"
> "1" "{} => {It4}" 1 1 1
> "2" "{} => {It1}" 1 1 1
> "3" "{It3} => {It2}" 0.5 1 1.5
> "4" "{It3} => {It4}" 0.5 1 1
> "5" "{It3} => {It1}" 0.5 1 1
> "6" "{It2} => {It4}" 0.666666666666667 1 1
> "7" "{It2} => {It1}" 0.666666666666667 1 1
> "8" "{It4} => {It1}" 1 1 1
> "9" "{It1} => {It4}" 1 1 1
> I want to convert this in a matrix or data frame and the result should be
> something like this
> ##we obtain
>   rules      support  confidence  lift
> 1 {}=>{It4}  1.0000000       1    1.0
> 2 {}=>{It1}  1.0000000       1    1.0
> 3 {It3}=>{It2}  0.5000000       1    1.5
> 4 {It3}=>{It4}  0.5000000       1    1.0
> 5 {It3}=>{It1}  0.5000000       1    1.0
> 6 {It2}=>{It4}  0.6666667       1    1.0
> 7 {It2}=>{It1}  0.6666667       1    1.0
> 8 {It4}=>{It1}  1.0000000       1    1.0
> 9 {It1}=>{It4}  1.0000000       1    1.0


R> data.frame(rules = labels(rules), quality(rules))
            rules   support confidence lift
1    {} => {It4} 1.0000000          1  1.0
2    {} => {It1} 1.0000000          1  1.0
3 {It3} => {It2} 0.5000000          1  1.5
4 {It3} => {It4} 0.5000000          1  1.0
5 {It3} => {It1} 0.5000000          1  1.0
6 {It2} => {It4} 0.6666667          1  1.0
7 {It2} => {It1} 0.6666667          1  1.0
8 {It4} => {It1} 1.0000000          1  1.0
9 {It1} => {It4} 1.0000000          1  1.0


> In another hand is it possible to obtain all the rules where lHS<=>rhs?. In
> our last example that means to obtain
>   lhs      rhs     support confidence lift
> 8 {It4} => {It1} 1.0000000          1  1.0
> 9 {It1} => {It4} 1.0000000          1  1.0

This is a little more tricky.

We can create a new set of rules with rhs and lhs reversed and then see 
if these reversed rules match some of the original rules.

 > rulesRev <- new("rules", rhs=lhs(rules), lhs=rhs(rules))
 > m <- match(rules, rulesRev, nomatch=0)
 > inspect(rules[m,])
   lhs      rhs   support confidence lift
1 {It1} => {It4}       1          1    1
2 {It4} => {It1}       1          1    1

Maybe there is an easier way. I have to think about it.

-Michael

> Thanks again Alberto
> 




-- 
   Michael Hahsler
   email: michael at hahsler.net
   web: http://michael.hahsler.net




More information about the R-help mailing list