0

I'm looking at the official tutorial here. I copied their code exactly but RStudio is giving me error messages for the lines , , B = GOOD (error message: unexpected ',' in ",") and dfit (error message: object dfit not found). Here's the exact code I copied:

# 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

Does anyone know what's going on here? What did I do wrong?

JohnDoeVsJoeSchmoe
  • 671
  • 2
  • 8
  • 25

1 Answers1

2

This tutorial put the output together with the code and you copied one line of it by accident. You can look at a similar example in the documentation of the package (https://cran.r-project.org/web/packages/bnlearn/bnlearn.pdf). It should be:

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))
Katia
  • 3,784
  • 1
  • 14
  • 27