Advertisement

MicrobiomeStatPlot | 关联规则挖掘教程Association rule minning

阅读量:
0c63e92701f48da3a6b8e64e857cffa7.png

关联规则挖掘简介

关联规则是数据挖掘领域的一种常见算法,用于寻找数据集中的有趣关系。这种算法可以帮助我们发现数据集中的频繁项集,然后将这些频繁项集转化为关联规则。这些规则可以帮助我们理解数据集中的特定模式,并用于预测未来的行为或事件。两种主要的关联规则算法包括Apriori算法和FP-growth算法。

关联规则的基本思想是发现数据集中的频繁项集,并转换为关联规则,这些规则可以用于预测未来的行为或事件。

寻找关联规则是一个需要大量计算的任务,特别是当数据集很大时。因此,我们通常使用机器学习算法来帮助我们快速有效地寻找关联规则。

常用关联规则算法 :

1.Apriori算法(Apriori algorithm) -算法原理(Algorithm principle) Apriori算法是一种常用的关联规则算法,它通过扫描数据集多次,从而找到频繁项集。Apriori算法使用一种称为“先验”的概念,它假设如果一个项集是频繁的,那么它的所有子集也必须是频繁的。

2.FP-growth算法(FP-growth algorithm) -算法原理(Algorithm principle) FP-growth算法是一种基于树的算法,与Apriori算法不同。它使用一种称为“FP树”的数据结构来查找频繁项集。FP-growth算法通过一遍扫描来构建FP树,并使用它来查找频繁项集。相比之下,Apriori算法需要多次扫描数据集,因此效率较低。

FP-growth算法在处理大型数据集时表现良好。

关联规则分析对于挖掘事件之间的联系具有重要作用,尤其是在海量的数据中。中医用药的辨证论治,个性化治疗所产生的海量的方剂,是医生数十年的经验总结,其中包含着众多尚未发现的潜在用药规律,反映了方药配伍的特殊规律与内在联系,应用关联规则方法,对大量验方进行药对挖掘研究,证实类方中一些已知药对的常用性以及发现未知药对,这对于揭示药对配伍的科学内涵,挖掘和提高中医方药理论,以及拓展临床用药思路,都具有十分重要的意义。

规则关联分析常用算法主要有Apriori、FP-Tree、Eclat和灰色关联法;其中Apriori是关联规则分析中最常用、最经典的挖掘频繁项集的算法,核心思想是通过连接产生候选项及其支持度,然后通过剪枝生成频繁项集,无法处理连续型数值变量,往往分析之前需要对数据进行离散化。

相关概念:

项集:项的集合。包含k个项的项集成为k项集,如集合{牛奶、麦片、糖}是一个3项集。 频繁项集:如果项集I的相对支持度满足预定义的最小支持度阈值,则I是频繁项集。支持度(相对支持度):项集A、B同时发生的概率。置信度:项集A发生,则项集B发生的概率。最小支持度:用户或专家定义的衡量支持度的一个阈值,表示项集在统计意义上的最低重要性。最小置信度:用户或专家定义的衡量置信度的一个阈值,表示关联规则的最低可靠性。同时满足最小支持度阈值和最小置信度阈值的规则成为强规则。

参考:

https://mp.weixin.qq.com/s/Rjuc7OFlljMtEA9-RcRtmA

标签: #微生物组数据分析 #MicrobiomeStatPlot #关联规则挖掘 #R语言可视化 #ARM

作者: First draft(初稿):Defeng Bai(白德凤);Proofreading(校对):Ma Chuang(马闯) and Jiani Xun(荀佳妮);Text tutorial(文字教程):Defeng Bai(白德凤)

源代码及测试数据链接:

https://github.com/YongxinLiu/MicrobiomeStatPlot/项目中目录 3.Visualization_and_interpretation/AssociationRuleMining(ARM)

或公众号后台回复“MicrobiomeStatPlot”领取

关联规则挖掘案例

这是来来自于布鲁塞尔自由大学的Karoline Faust和Jeroen Raes在2012年发表于Nature Reviews上的论文用到的一个例子。论文题目为: Microbial interactions: from networks to models
1e31dd4d0f290d4b8bf1192a6ff7dfaf.jpeg

图 3 | 预测成对关系与复杂关系的示例。

成对关系 (a) 和复杂关系 (b) 是从全球微生物操作分类单元 (OTU) 存在-不存在数据集推断出来的。a | 每个节点代表一个 OTU,每条边代表它们之间的显著成对关联。使用超几何分布 (其 P 值经过多重检验调整) 检测到显著关系。边的厚度随显著性而增加。b | 该网络总结了使用先验算法挖掘的关联规则,并使用 REF. 110 中建议的多重测试校正进行过滤。文本框提供了此类规则的一个示例。由于数据集非常稀疏,规则仅限于涉及最多三个 OTU 的正关联。网络 b 中的每个节点代表一个 OTU,而每条边对应一条规则。与网络 a 相比,如果三个 OTU 都涉及同一条规则,一条边可以连接它们。为了便于解释,相同的 OTU(具有相同的节点填充和边框颜色)可能在网络 b 中出现多次。

结果

通过多元回归或关联规则挖掘推断出的复杂关系可以在生成的网络中表示为一条有向边,该边以有向方式连接两个以上的节点,从独立分类单元指向从属分类单元。具有此类边的网络正式称为有向超图。图 3a 显示了从基于相似性的方法推断出的网络,其中成对关系由连接两个节点的边表示,而图 3b 给出了一个有向超图的示例,该超图是从全局微生物存在-不存在数据集中的关联规则挖掘中得出的,它可视化了复杂关系,超边连接多达三个节点。话虽如此,仍需要更多的发展来设计和应用有针对性的多元方法,真正解开复杂关系并将其可视化。

关联规则分析R语言实战

源代码及测试数据链接:

https://github.com/YongxinLiu/MicrobiomeStatPlot/

或公众号后台回复“MicrobiomeStatPlot”领取

软件包安装

复制代码
 # 基于CRAN安装R包,检测没有则安装

    
 p_list = c("readr", "ggplot2", "dplyr", "arules", "arulesViz", "colorspace"
    
            , "scales", "RColorBrewer")
    
 for(p in p_list){if (!requireNamespace(p)){install.packages(p)}
    
     library(p, character.only = TRUE, quietly = TRUE, warn.conflicts = FALSE)}
    
  
    
  
    
 # 加载R包 Load the package
    
 suppressWarnings(suppressMessages(library(readr)))
    
 suppressWarnings(suppressMessages(library(ggplot2)))
    
 suppressWarnings(suppressMessages(library(dplyr)))
    
 suppressWarnings(suppressMessages(library(arules)))
    
 suppressWarnings(suppressMessages(library(arulesViz)))
    
 suppressWarnings(suppressMessages(library(colorspace)))
    
 suppressWarnings(suppressMessages(library(scales)))
    
 suppressWarnings(suppressMessages(library(RColorBrewer)))
    
    
    
    

实战

参考:https://mp.weixin.qq.com/s/Rjuc7OFlljMtEA9-RcRtmA

复制代码
 # load data

    
 # 读取文件内容
    
 file_content <- readLines("data/fangji2.csv", encoding = "GB2312")
    
 # 转换编码为UTF-8
    
 # Convert encoding to UTF-8
    
 file_content_utf8 <- iconv(file_content, from = "GB2312", to = "UTF-8")
    
 # 将转换后的内容写入新的文件
    
 # Write the converted content to a new file
    
 writeLines(file_content_utf8, "results/fangji_utf8.csv")
    
 # 读取转换后的文件
    
 # Read the converted file
    
 data <- read.csv("results/fangji_utf8.csv", fileEncoding = "UTF-8")
    
 # 查看数据
    
 mydata <- data
    
 # 创建空的dataframe
    
 # Create an empty dataframe
    
 df <- data.frame()
    
 # 判断数据行数
    
 # Determine the number of data rows
    
 nrow<-nrow(mydata)
    
 # 将每组成改为单个
    
 # Change the prescription composition to multiple single medicines
    
 for(i in 1:nrow){
    
   d<-data.frame(mydata[[1]][i],strsplit(mydata[[2]][i],";"))
    
   ID<-d[,1]
    
   ZY<-d[,2]
    
   d<-data.frame(ID,ZY)
    
   df<-rbind (df, d)
    
 }
    
 write.csv(df,"results/df.csv")
    
 # 计算各处方中的药味数
    
 # Calculate the number of medicinal flavors in each prescription
    
 ID_zy <- df %>%group_by(ID)%>%summarise(num=n())
    
 summary(ID_zy)
    
 #>       ID                 num        
    
 #>  Length:1027        Min.   :  1.00  
    
 #>  Class :character   1st Qu.:  4.00  
    
 #>  Mode  :character   Median :  8.00  
    
 #>                     Mean   : 10.93  
    
 #>                     3rd Qu.: 13.00  
    
 #>                     Max.   :102.00
    
 write.csv(ID_zy,file="results/ID_zy.csv")
    
 # 绘制频数图
    
 # Draw a frequency chart
    
 ZY_id <- df %>%
    
   group_by(ZY)%>%
    
   summarise(num = n())
    
 # 统计
    
 # Statistics
    
 summary(ZY_id)
    
 #>       ZY                 num        
    
 #>  Length:974         Min.   :  1.00  
    
 #>  Class :character   1st Qu.:  1.00  
    
 #>  Mode  :character   Median :  1.00  
    
 #>                     Mean   : 11.53  
    
 #>                     3rd Qu.:  6.00  
    
 #>                     Max.   :500.00
    
 # 降序排列
    
 # Sort in descending order
    
 ZY_id<-ZY_id[order(ZY_id$num,decreasing =T),]
    
 # 绘制前30的数据
    
 # Draw the first 30 data
    
 p1 <- ggplot(ZY_id[1:30,],aes(x = reorder(ZY,-num),y = num)) +
    
   theme_bw(base_family = "sans",base_size = 10) +
    
   geom_bar(stat = "identity",fill = "#5ebcc2") +
    
   labs(x = "Medicine",y = "Frequency") + 
    
   coord_flip() + 
    
   geom_text(aes(x = reorder(ZY,-num),y = num+0.1*num,label = num),size = 3)
    
 ggsave('results/ZY_id_top30.pdf', p1, width = 6, height = 4)
    
 # 规则关联分析
    
 # Rule association analysis
    
 mydata <- read.csv("results/df.csv",header=T)
    
 gzgldata<- split(x=mydata$ZY,f=mydata$ID)
    
 sum(sapply(gzgldata,length))
    
 #> [1] 11227
    
 # 过滤掉相同的数据
    
 # Filter out the same data
    
 gzgldata <- lapply(gzgldata,unique)
    
 sum(sapply(gzgldata,length))
    
 #> [1] 11192
    
 # 转换数据形式,用于关联规则分析
    
 # Convert data to association rule analysis
    
 gzgldata <- as(gzgldata, "transactions")
    
 # 绘制频率大于0.1,排名前20
    
 # Draw the top 20 Chinese medicines with a frequency greater than 0.1
    
 # (itemFrequencyPlot(gzgldata, topN=20,support = 0.1,col = "#7a76b7",
    
 #                   xlab = "Medicine",ylab = "Frequency",
    
 #                   main = ""))
    
 # 利用apriori算法挖掘关联规则
    
 # Use apriori algorithm to mine association rules
    
 myrule <- apriori(data = gzgldata,
    
          parameter = list(support = 0.1,
    
                           confidence = 0.7,
    
                           minlen =2))
    
 #> Apriori
    
 #> 
    
 #> Parameter specification:
    
 #>  confidence minval smax arem  aval originalSupport maxtime support minlen
    
 #>         0.7    0.1    1 none FALSE            TRUE       5     0.1      2
    
 #>  maxlen target  ext
    
 #>      10  rules TRUE
    
 #> 
    
 #> Algorithmic control:
    
 #>  filter tree heap memopt load sort verbose
    
 #>     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
    
 #> 
    
 #> Absolute minimum support count: 102 
    
 #> 
    
 #> set item appearances ...[0 item(s)] done [0.00s].
    
 #> set transactions ...[974 item(s), 1027 transaction(s)] done [0.00s].
    
 #> sorting and recoding items ... [22 item(s)] done [0.00s].
    
 #> creating transaction tree ... done [0.00s].
    
 #> checking subsets of size 1 2 3 done [0.00s].
    
 #> writing ... [25 rule(s)] done [0.00s].
    
 #> creating S4 object  ... done [0.00s].
    
 # 将规则按照提升度lift排序
    
 # Sort the rules by lift
    
 sortlift <- arules::sort(myrule,decreasing = TRUE,by = "lift")
    
 # 显示排名前20的规则
    
 # Display the top 20 rules
    
 inspect(sortlift[1:20])
    
 #>      lhs                     rhs       support   confidence coverage  lift    
    
 #> [1]  {Wuchuan}            => {Caowu}   0.1051607 0.7058824  0.1489776 4.530882
    
 #> [2]  {Ruxiang, Xiejie}    => {Moyao}   0.1226874 0.9473684  0.1295034 2.771930
    
 #> [3]  {Ruxiang, Zirantong} => {Moyao}   0.1002921 0.9196429  0.1090555 2.690807
    
 #> [4]  {Danggui, Ruxiang}   => {Moyao}   0.1820837 0.8990385  0.2025316 2.630520
    
 #> [5]  {Moyao, Xiejie}      => {Ruxiang} 0.1226874 0.9264706  0.1324245 2.571582
    
 #> [6]  {Moyao, Zirantong}   => {Ruxiang} 0.1002921 0.9035088  0.1110029 2.507847
    
 #> [7]  {Moyao}              => {Ruxiang} 0.3086660 0.9031339  0.3417722 2.506807
    
 #> [8]  {Ruxiang}            => {Moyao}   0.3086660 0.8567568  0.3602726 2.506807
    
 #> [9]  {Danggui, Moyao}     => {Ruxiang} 0.1820837 0.8904762  0.2044791 2.471673
    
 #> [10] {Xiejie}             => {Moyao}   0.1324245 0.8343558  0.1587147 2.441263
    
 #> [11] {Zirantong}          => {Moyao}   0.1110029 0.7972028  0.1392405 2.332556
    
 #> [12] {Xiejie}             => {Ruxiang} 0.1295034 0.8159509  0.1587147 2.264815
    
 #> [13] {Zirantong}          => {Ruxiang} 0.1090555 0.7832168  0.1392405 2.173956
    
 #> [14] {Chuangong}          => {Danggui} 0.1908471 0.9245283  0.2064265 1.906608
    
 #> [15] {Taoren}             => {Danggui} 0.1100292 0.9040000  0.1217137 1.864273
    
 #> [16] {Chishao}            => {Danggui} 0.1596884 0.8817204  0.1811100 1.818327
    
 #> [17] {Honghua}            => {Danggui} 0.1421616 0.8588235  0.1655307 1.771108
    
 #> [18] {Qianghuo}           => {Danggui} 0.1246349 0.8311688  0.1499513 1.714077
    
 #> [19] {Rougui}             => {Danggui} 0.1178189 0.8175676  0.1441091 1.686028
    
 #> [20] {Shengdihuang}       => {Danggui} 0.1470302 0.8031915  0.1830574 1.656381
    
 #>      count
    
 #> [1]  108  
    
 #> [2]  126  
    
 #> [3]  103  
    
 #> [4]  187  
    
 #> [5]  126  
    
 #> [6]  103  
    
 #> [7]  317  
    
 #> [8]  317  
    
 #> [9]  187  
    
 #> [10] 136  
    
 #> [11] 114  
    
 #> [12] 133  
    
 #> [13] 112  
    
 #> [14] 196  
    
 #> [15] 113  
    
 #> [16] 164  
    
 #> [17] 146  
    
 #> [18] 128  
    
 #> [19] 121  
    
 #> [20] 151
    
 # 规则校验(类似测试集)
    
 # Rule verification (similar to test set)
    
 qualityMeasures<-interestMeasure(myrule,c("coverage","fishersExactTest",
    
                                           "conviction","chiSquared"), 
    
                                  transactions=gzgldata)
    
 # 将结果保存为数据表的形式并导出
    
 # Save the results as a data table and export
    
 ruledf <- as(myrule,"data.frame")
    
 write.csv(ruledf,file="results/ruledf.csv")
    
 # 排名前20规则可视化为网络图的结构
    
 # Visualize the top 20 rules as a network graph structure
    
 pdf(file = "results/rule_network_graph.pdf", width = 10, height = 8)
    
 plot(sortlift[1:20], method = "graph")
    
 dev.off()
    
 #> png 
    
 #>   2
    
 # 可视化所有的规则
    
 # Visualize all rules
    
 p3 <- plot(sortlift, method="graph")
    
 # 所有规则的气泡图1
    
 # Bubble chart of all rules 1
    
 p4 <- plot(myrule, method="scatterplot",
    
      control=list(jitter=2, col = rev(brewer.pal(3, "Blues"))), 
    
      shading = "lift")
    
 # 所有规则的气泡图2
    
 # Bubble chart of all rules 2
    
 p5 <- plot(myrule, 
    
      control=list(jitter=2, col = rev(brewer.pal(3, "Blues"))), 
    
      shading = "lift",method = "grouped")
    
 #> Available control parameters (with default values):
    
 #> k     =  20
    
 #> aggr.fun  =  function (x, ...)  UseMethod("mean")
    
 #> rhs_max   =  10
    
 #> lhs_label_items   =  2
    
 #> col   =  c("#EE0000FF", "#EEEEEEFF")
    
 #> groups    =  NULL
    
 #> engine    =  ggplot2
    
 #> verbose   =  FALSE
    
 # 将规则可视化为grouped matrix的形式
    
 # Visualize the rules as a grouped matrix
    
 p6 <- plot(sortlift[1:20], method = "grouped matrix")
    
    
    
    
039c4b2a5f25b3e4d52b17f12d8d280a.png

使用此脚本,请引用下文:

Yong-Xin Liu, Lei Chen, Tengfei Ma, Xiaofang Li, Maosheng Zheng, Xin Zhou, Liang Chen, Xubo Qian, Jiao Xi, Hongye Lu, Huiluo Cao, Xiaoya Ma, Bian Bian, Pengfan Zhang, Jiqiu Wu, Ren-You Gan, Baolei Jia, Linyang Sun, Zhicheng Ju, Yunyun Gao, Tao Wen, Tong Chen. 2023. EasyAmplicon: An easy-to-use, open-source, reproducible, and community-based pipeline for amplicon data analysis in microbiome research. iMeta 2: e83. https://doi.org/10.1002/imt2.83

Copyright 2016-2024 Defeng Bai baidefeng@caas.cn, Chuang Ma 22720765@stu.ahau.edu.cn, Jiani Xun 15231572937@163.com, Yong-Xin Liu liuyongxin@caas.cn

宏基因组推荐

本公众号现全面开放投稿,希望文章作者讲出自己的科研故事,分享论文的精华与亮点。投稿请联系小编(微信号:yongxinliu 或 meta-genomics)

猜你喜欢

iMeta高引文章 fastp 复杂热图 ggtree 绘图imageGP 网络iNAP
iMeta网页工具 代谢组MetOrigin 美吉云乳酸化预测DeepKla
iMeta综述 肠菌菌群 植物菌群 口腔菌群 蛋白质结构预测

10000+:菌群分析 宝宝与猫狗 梅毒狂想曲 提DNA发Nature

系列教程:微生物组入门 Biostar 微生物组 宏基因组

专业技能:学术图表 高分文章 生信宝典 不可或缺的人

一文读懂:宏基因组 寄生虫益处 进化树 必备技能:提问 搜索 Endnote

扩增子分析:图表解读 分析流程 统计绘图

16S功能预测 PICRUSt FAPROTAX Bugbase Tax4Fun

生物科普: 肠道细菌 人体上的生命 生命大跃进 细胞暗战 人体奥秘

写在后面

为鼓励读者交流快速解决科研困难,我们建立了“宏基因组”讨论群,己有国内外6000+ 科研人员加入。请添加主编微信meta-genomics带你入群,务必备注“姓名-单位-研究方向-职称/年级”。高级职称请注明身份,另有海内外微生物PI群供大佬合作交流。技术问题寻求帮助,首先阅读《如何优雅的提问》学习解决问题思路,仍未解决群内讨论,问题不私聊,帮助同行。

点击阅读原文

全部评论 (0)

还没有任何评论哟~