我正在查看这里的官方教程。我完全复制了他们的代码,但 RStudio 为我提供了, , B = GOOD
行(错误消息:unexpected ',' in ","
(和dfit
行(错误消息:object dfit not found
(的错误消息。这是我复制的确切代码:
# install.packages("bnlearn")
library(bnlearn)
cptA = matrix(c(0.4, 0.6), ncol = 2, dimnames = list(NULL, c("LOW", "HIGH")))
#cptA
cptB = matrix(c(0.8, 0.2), ncol = 2, dimnames = list(NULL, c("GOOD", "BAD")))
#cptB
cptC = c(0.5, 0.5, 0.4, 0.6, 0.3, 0.7, 0.2, 0.8)
dim(cptC) = c(2, 2, 2)
dimnames(cptC) = list("C" = c("TRUE", "FALSE"), "A" = c("LOW", "HIGH"), "B" = c("GOOD", "BAD"))
#cptC
, , B = GOOD
net = model2network("[A][B][C|A:B]")
dfit = custom.fit(net, dist = list(A = cptA, B = cptB, C = cptC))
dfit
有谁知道这里发生了什么?我做错了什么?
本教程将输出与代码放在一起,您意外复制了其中的一行。您可以在包的文档 (https://cran.r-project.org/web/packages/bnlearn/bnlearn.pdf( 中查看类似的示例。 它应该是:
library(bnlearn)
cptA = matrix(c(0.4, 0.6), ncol = 2, dimnames = list(NULL, c("LOW", "HIGH")))
#cptA
cptB = matrix(c(0.8, 0.2), ncol = 2, dimnames = list(NULL, c("GOOD", "BAD")))
#cptB
cptC = c(0.5, 0.5, 0.4, 0.6, 0.3, 0.7, 0.2, 0.8)
dim(cptC) = c(2, 2, 2)
dimnames(cptC) = list("C" = c("TRUE", "FALSE"), "A" = c("LOW", "HIGH"), "B" = c("GOOD", "BAD"))
cptC
# , , B = GOOD
#
# A
# C LOW HIGH
# TRUE 0.5 0.4
# FALSE 0.5 0.6
#
# , , B = BAD
#
# A
# C LOW HIGH
# TRUE 0.3 0.2
# FALSE 0.7 0.8
net = model2network("[A][B][C|A:B]")
cfit = custom.fit(net, dist = list(A = cptA, B = cptB, C = cptC))