R小盐准备介绍R语言机器学习与预测模型的学习笔记, 快来收藏关注【科研私家菜】
01 贝叶斯网络
贝叶斯网络是一个模型。它反映了正在建模的世界某个部分的状态,并描述了这些状态如何与概率相关联。该模型可能是您的房屋,汽车,身体,社区,生态系统,股票市场等。任何事情都可以用贝叶斯网络建模。模型的所有可能状态代表可以存在的所有可能世界,即可以配置部件或状态的所有可能方式。汽车发动机可能正常运行或出现故障。它的轮胎可以充气或放气。您的身体可能生病或健康,等等.
安装
install.packages("https://www.bnlearn.com/releases/bnlearn_latest.tar.gz", repos = NULL, type = "source")
install.packages("bnlearn")
效果如下:
bnlearn是一个用于贝叶斯网络学习和推理的 R 软件包
bnlearn implements the following constraint-based structure learning algorithms:
PC (the stable version);
Grow-Shrink (GS);
Incremental Association Markov Blanket (IAMB);
Fast Incremental Association (Fast-IAMB);
Interleaved Incremental Association (Inter-IAMB);
Incremental Association with FDR Correction (IAMB-FDR);
Max-Min Parents & Children (MMPC);
Semi-Interleaved Hiton-PC (SI-HITON-PC);
Hybrid Parents & Children (HPC);
# load the data.
data(alarm)
# create and plot the network structure.
modelstring = paste0("[HIST|LVF][CVP|LVV][PCWP|LVV][HYP][LVV|HYP:LVF][LVF]",
"[STKV|HYP:LVF][ERLO][HRBP|ERLO:HR][HREK|ERCA:HR][ERCA][HRSA|ERCA:HR][ANES]",
"[APL][TPR|APL][ECO2|ACO2:VLNG][KINK][MINV|INT:VLNG][FIO2][PVS|FIO2:VALV]",
"[SAO2|PVS:SHNT][PAP|PMB][PMB][SHNT|INT:PMB][INT][PRSS|INT:KINK:VTUB][DISC]",
"[MVS][VMCH|MVS][VTUB|DISC:VMCH][VLNG|INT:KINK:VTUB][VALV|INT:VLNG]",
"[ACO2|VALV][CCHL|ACO2:ANES:SAO2:TPR][HR|CCHL][CO|HR:STKV][BP|CO:TPR]")
dag = model2network(modelstring)
## Not run: graphviz.plot(dag)
![](https://img.haomeiwen.com/i14463139/5e9c86fe95b404fb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
02 bnlearn R包
bnlearn 是一个 R 包,用于学习贝叶斯网络的图形结构,估计其参数,并执行一些有用的推理。它于2007年首次发布,已经持续开发了10多年(并且仍然很强大)。
install.packages("bnlearn")
library(bnlearn)
data(lizards)
lizards = read.table("lizards.txt", header = TRUE)
#-----------------------------------------------------------------------------#
str(lizards)
summary(lizards)
dim(lizards)
levels(lizards[, "Species"])
levels(lizards[, "Height"])
levels(lizards[, "Diameter"])
table(lizards[, c(3,2,1)])
#-----------------------------------------------------------------------------#
Sagrei.lizards = lizards[lizards$Species == "Sagrei", ]
Distichus.lizards = lizards[lizards$Species == "Distichus", ]
par(mfrow = c(2, 2))
plot(Sagrei.lizards[, "Height"], main = "Perch Height (Sagrei)")
plot(Distichus.lizards[, "Height"], main = "Perch Height (Distichus)")
plot(Sagrei.lizards[, "Diameter"], main = "Perch Diameter (Sagrei)")
plot(Distichus.lizards[, "Diameter"], main = "Perch Diameter (Distichus)")
#-----------------------------------------------------------------------------#
set.seed(42)
diam = numeric(length = nrow(lizards))
narrow = (lizards$Diameter == "narrow")
wide = (lizards$Diameter == "wide")
diam[narrow] = runif(n = 252, min = 2, max = 4)
diam[wide] = runif(n = 157, min = 4, max = 6)
new.data = data.frame(
Species = lizards[, "Species"],
Sim.Diameter = diam)
#-----------------------------------------------------------------------------#
summary(new.data[, "Sim.Diameter"])
is.sagrei = (new.data$Species == "Sagrei")
summary(new.data[is.sagrei, "Sim.Diameter"])
summary(new.data[!is.sagrei, "Sim.Diameter"])
var(new.data[is.sagrei, "Sim.Diameter"])
var(new.data[!is.sagrei, "Sim.Diameter"])
#-----------------------------------------------------------------------------#
boxplot(Sim.Diameter~ Species, data = new.data, ylab = "Diameter (inches)")
abline(h = 4, lty = "dashed")
效果如下:
bnlearn - Bayesian network structure learning
https://zhuanlan.zhihu.com/p/283947217
关注R小盐,关注科研私家菜(VX_GZH: SciPrivate),有问题请联系R小盐。让我们一起来学习 R语言机器学习与临床预测模型
网友评论