ore.odmAssocRules
関数は、Aprioriアルゴリズムを実装することで、高頻度アイテム・セットを検索して相関モデルを生成します。これによって、マーケット・バスケット分析のような場合の大量のトランザクション・データにおいて、アイテムの同時発生が検出されます。相関ルールは、トランザクション・レコードで一連のアイテムが出現すれば、もう1つの一連のアイテムが存在するというデータのパターンを特定します。ルールの形成に使用されるアイテムのグループは、発生(ルールの支持度)する頻度および後件が前件(ルールの信頼度)に従う回数に応じて、最小しきい値を渡す必要があります。相関モデルは、ユーザー指定のしきい値よりも大きい支持度および信頼度を備えるすべてのルールを生成します。Aprioriアルゴリズムは効率的で、トランザクション数、アイテム数および生成されるアイテム・セットおよびルールの数が有効に測定されます。
formula
の仕様にはform ~ terms
があり、ここでterms
は分析に含まれる一連の列名です。複数の列名は列名の間に+
を使用することで指定されます。~ .
は、dataのすべての列をモデル構築に使用する場合に使用します。列を除外するには、除外する各列名の前に-
を使用します。関数は変換を確認するためにterms
の項目に適用できます。
ore.odmAssocRules
関数は、次の形式のデータを受け入れます。
トランザクション・データ
アイテムIDおよびアイテム値を使用した複数レコードのケース・データ
リレーショナル・データ
データの形式の指定の例および関数の引数の詳細は、help(ore.odmAssocRules)
を呼び出してください。
関数rules
は、クラスore.rules
(一連の相関ルールを指定します)のオブジェクトを返します。ore.pull
を使用して、ore.rules
オブジェクトをローカルのRセッションのメモリーにプルできます。ローカルのインメモリー・オブジェクトは、arules
パッケージで定義されているクラスrules
のものです。help(ore.rules)
を参照してください。
関数itemsets
は、クラスore.itemsets
(一連のアイテムセットを指定します)のオブジェクトを返します。ore.pull
を使用して、ore.itemsets
オブジェクトをローカルのRセッションのメモリーにプルできます。ローカルのインメモリー・オブジェクトは、arules
パッケージで定義されているクラスitemsets
のものです。help(ore.itemsets)
を参照してください。
例4-9 ore.odmAssocRules関数の使用方法
この例では、トランザクション・データセットに基づいて相関モデルを構築します。パッケージarules
およびarulesViz
は、生成されるルールおよびアイテムセットをクライアントのRセッションのメモリーにプルして表示するために必要です。このルールのグラフを図4-2に示します。
# Load the arules and arulesViz packages. library(arules) library(arulesViz) # Create some transactional data. id <- c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3) item <- c("b", "d", "e", "a", "b", "c", "e", "b", "c", "d", "e") # Push the data to the database as an ore.frame object. transdata_of <- ore.push(data.frame(ID = id, ITEM = item)) # Build a model with specifications. ar.mod1 <- ore.odmAssocRules(~., transdata_of, case.id.column = "ID", item.id.column = "ITEM", min.support = 0.6, min.confidence = 0.6, max.rule.length = 3) # Generate itemsets and rules of the model. itemsets <- itemsets(ar.mod1) rules <- rules(ar.mod1) # Convert the rules to the rules object in arules package. rules.arules <- ore.pull(rules) inspect(rules.arules) # Convert itemsets to the itemsets object in arules package. itemsets.arules <- ore.pull(itemsets) inspect(itemsets.arules) # Plot the rules graph. plot(rules.arules, method = "graph", interactive = TRUE)例4-9のリスト
R> # Load the arules and arulesViz packages. R> library(arules) R> library(arulesViz) R> # Create some transactional data. R> id <- c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3) R> item <- c("b", "d", "e", "a", "b", "c", "e", "b", "c", "d", "e") R> # Push the data to the database as an ore.frame object. R> transdata_of <- ore.push(data.frame(ID = id, ITEM = item)) R> # Build a model with specifications. R> ar.mod1 <- ore.odmAssocRules(~., transdata_of, case.id.column = "ID", + item.id.column = "ITEM", min.support = 0.6, min.confidence = 0.6, + max.rule.length = 3) R> # Generate itemsets and rules of the model. R> itemsets <- itemsets(ar.mod1) R> rules <- rules(ar.mod1) R> # Convert the rules to the rules object in arules package. R> rules.arules <- ore.pull(rules) R> inspect(rules.arules) lhs rhs support confidence lift 1 {b} => {e} 1.0000000 1.0000000 1 2 {e} => {b} 1.0000000 1.0000000 1 3 {c} => {e} 0.6666667 1.0000000 1 4 {d, e} => {b} 0.6666667 1.0000000 1 5 {c, e} => {b} 0.6666667 1.0000000 1 6 {b, d} => {e} 0.6666667 1.0000000 1 7 {b, c} => {e} 0.6666667 1.0000000 1 8 {d} => {b} 0.6666667 1.0000000 1 9 {d} => {e} 0.6666667 1.0000000 1 10 {c} => {b} 0.6666667 1.0000000 1 11 {b} => {d} 0.6666667 0.6666667 1 12 {b} => {c} 0.6666667 0.6666667 1 13 {e} => {d} 0.6666667 0.6666667 1 14 {e} => {c} 0.6666667 0.6666667 1 15 {b, e} => {d} 0.6666667 0.6666667 1 16 {b, e} => {c} 0.6666667 0.6666667 1 R> # Convert itemsets to the itemsets object in arules package. R> itemsets.arules <- ore.pull(itemsets) R> inspect(itemsets.arules) items support 1 {b} 1.0000000 2 {e} 1.0000000 3 {b, e} 1.0000000 4 {c} 0.6666667 5 {d} 0.6666667 6 {b, c} 0.6666667 7 {b, d} 0.6666667 8 {c, e} 0.6666667 9 {d, e} 0.6666667 10 {b, c, e} 0.6666667 11 {b, d, e} 0.6666667 R> # Plot the rules graph. R> plot(rules.arules, method = "graph", interactive = TRUE)