[英]R loop through association rule mining (arules) with changing supports and item appearances
fairly new to R here. 在这里对R来说还很新。 I am looking to mine association rules in R for specific items, but I want to vary the minimum support target for these rules by each item (ie 10% of the item's total frequency in the transaction list).
我希望在R中挖掘特定项目的关联规则,但是我想按每个项目(例如,交易清单中项目总频率的10%)更改这些规则的最低支持目标。 Each item has a different amount of transactions so I believe there is value in varying the support.
每个项目的交易额都不同,因此我认为改变支持的价值。
I've calculated the support targets for each item in a separate spreadsheet using Excel. 我已经使用Excel在单独的电子表格中计算了每个项目的支持目标。
I can do this manually writing the arules code and manually inputting support minimum and item appearances, but the process is slow, especially with many different items. 我可以手动编写规则代码并手动输入支持的最低数量和项目外观,但是此过程很慢,尤其是对于许多不同的项目。
ex. 例如
arules <- apriori(trans, parameter = list(sup = 0.001, conf = 0.25,target="rules"),
appearance=list(rhs= c("Apples")))
arules2 <- apriori(trans, parameter = list(sup = 0.002, conf = 0.25,target="rules"),
appearance=list(rhs= c("Oranges")))
combined <- c(arules,arules2)
How can I do this using a for loop in R that will calculate the rules for each specified item at a specific support minimum, and also save those generated rules to a new variable each time the loop runs? 如何在R中使用for循环来执行此操作,该循环将以最小支持量计算每个指定项目的规则,并在每次循环运行时将这些生成的规则保存到新变量中? I intend to later group these rules depending on their type.
我打算稍后根据这些规则将它们分组。
Tried something like this which looped through too many times. 尝试过这样的事情,它循环了太多次。 I also couldn't figure out a way to save each loop to a new variable (ie arules1, arules2, arules3....)
我也想不出一种将每个循环保存到新变量的方法(即arules1,arules2,arules3...。)
min_supp <- c(0.001,0.002,0.003)
names <- c("Apples","Oranges","Grape")
for (inames in names) {
for (supports in min_supp) {
apriori(trans, parameter = list(sup = supports, conf = 0.25,target="rules"),
appearance=list(rhs= inames))
}}
Thanks in advance! 提前致谢!
Consider Map
(the simplified wrapper to mapply
) that can iterate elementwise through same length vectors for a m ultiple apply method. 考虑
Map
(简化包装器mapply
),可以的elementwise遍历相同长度的矢量为米 ultiple 应用方法。 Additionally, Map
will output a list of the returned items which can be named with setNames
. 此外,
Map
将输出可以使用setNames
命名的返回项列表。 Lists are always preferred as you avoid separate, similarly structured objects flooding global environment. 始终首选使用列表,因为这样可以避免单独的,结构相似的对象泛滥到全局环境中。
min_supp <- c(0.001,0.002,0.003)
names <- c("Apples","Oranges","Grape")
arules_fun <- function(n, s) apriori(trans, parameter = list(sup = s, conf = 0.25, target="rules"),
appearance=list(rhs= n))
# PROCESS FUNCTION ELEMENTWISE
arules_list <- Map(arules_fun, names, min_supp)
# NAME LIST ITEMS
arules_list <- setNames(arules_list, paste0("arules", 1:length(arules_list)))
arules_list$arules1
arules_list$arules2
arules_list$arules3
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.