Skip to content

Instantly share code, notes, and snippets.

@y-mitsui
Created February 17, 2019 06:29
Show Gist options
  • Select an option

  • Save y-mitsui/0b2035ea72d9cf910c8362a1bc59f92f to your computer and use it in GitHub Desktop.

Select an option

Save y-mitsui/0b2035ea72d9cf910c8362a1bc59f92f to your computer and use it in GitHub Desktop.
Rでベイジアンネットワーク
# Refererce:http://ushi-goroshi.hatenablog.com/entry/2018/01/11/233317
# install.packages("bnlearn")
# install.packages("BNSL")
library(bnlearn)
library(BNSL)
set.seed(123)
norm <- rnorm(4000)
Data <- matrix(norm, nrow = 1000, ncol = 4, byrow = T)
colnames(Data) <- c("Height", "BMI", "SBP", "FBS")
Data2 <- Data <- as.data.frame(Data)
Data2$Height <- 170 + Data$Height * 10
Data2$SBP <- 120 + Data$SBP * 10
Data2$BMI <- 22 + Data$Height * 5 + Data$SBP * 5
Data2$FBS <- 90 + (Data2$BMI - 22) * 5 + Data$FBS * 10
str_01_gs <- gs(Data2) #構造推定
print(arcs(str_01_gs)) #構造を表示(Type:matrix)
write.dot("str_01_gs.dot", str_01_gs) #構造を表示
fit_01 <- bn.fit(str_01_gs, Data2) #パラメーター推定
coef_01 <- coefficients(fit_01)#パラメーター抽出
print(coef_01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment