0

I have an excel file of rules: a column for Antecedents, another for Consequents, other for Support, etc

I'm trying to create a "rules" object to plot them using arulesViz.

http://www.inside-r.org/packages/cran/arules/docs/rhs

new("rules", ...)

I'm trying to create the Slots

lhs:Object of class itemMatrix; the left-hand-sides of the rules (antecedents)
rhs:Object of class itemMatrix; the right-hand-sides of the rules (consequents)
quality:a data.frame

To create the itemMatrix I need to go back to the sparse matrix,but I think that won't work, Is there some way to "import" the rules to arulesViz?

Excel file:

Antec   Conseq      Supp      Conf
MMMMAAA MMAAAA  0.061945    0.5
MMM,MA  MMAAAA  0.071944    0.6
MMMMAAA MMAAA   0.053948    0.5
MMM,MA  MMAAA   0.054948    0.7
AAAAAA  AAAA    0.090909    0.5
user3437823
  • 351
  • 2
  • 14

1 Answers1

1

One way would be to create a PMML file for the rules and use read.PMML. To create a rules object from scratch is a little tricky. Here is an example:

library("arules")
l <- list(c("MMMMAAA"), c("MMM", "MA"), "MMMMAAA", c("MMM","MA"), "AAAAA")
r <- list("MMAAAA", "MMAAAA", "MMAAA", "MMAAA", "AAAA")
q <- data.frame(
     support = c(0.061945, 0.071944, 0.053948, 0.054948,0.090909), 
     confidence = c(.5, .6, .5, .7, .5),
     lift = c(1,1,1,1,1)
)
### Note that I also added lift since arulesViz uses 
### lift in some visualizations.


### find unique item labels
items <- unique(c(unlist(l), unlist(r)))

### encode data as a rules object
r <- new("rules", lhs = encode(l, items), 
   rhs = encode(r, items), quality = q)

inspect(r)

### use a visualization as a scatter plot.
library("arulesViz")
plot(r)

Hope this helps!

Michael Hahsler
  • 2,965
  • 1
  • 12
  • 16
  • Hi Michael Hahsler, Can you please answer my question (https://stackoverflow.com/questions/68094669/how-to-change-the-font-size-color-and-style-while-setting-engine-htmlwidget) also which is related to rule visualization? – Md. Sabbir Ahmed Jun 23 '21 at 11:21