0

我有一个规则的 excel 文件:先行列,结果列,支持列等

我正在尝试创建一个“规则”对象来使用 arulesViz 绘制它们。

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

new("rules", ...)

我正在尝试创建插槽

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

要创建 itemMatrix 我需要回到稀疏矩阵,但我认为这行不通,有没有办法将规则“导入”到 arulesViz?

Excel文件:

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
4

1 回答 1

1

一种方法是为规则创建一个 PMML 文件并使用read.PMML. 从头开始创建规则对象有点棘手。这是一个例子:

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)

希望这可以帮助!

于 2015-03-22T23:26:17.423 回答