source("bayesserver.R")
# This example uses an R DataFrame as the data source for a batch query.
# You can also connect to databases using DatabaseDataReaderCommand
# you can use NA for missing data
df <- data.frame(
WasteType = c("Industrial", "Household", NA, "Industrial", "Industrial"),
CO2Concentration = c(-1.7, -2.0, -1.7, NA, 0.5)
)
dt <- toDataTable(df)
network <- new(Network)
network$load("C:/ProgramData/Bayes Server 9.5/Sample Networks/Waste.bayes") # TODO change this path to the 'Waste' example network installed with Bayes Server
links <- network$getLinks()
nodes <- network$getNodes()
variables <- network$getVariables()
# discrete
burningRegimen = variables$get("Burning Regimen", TRUE)
wasteType = variables$get("Waste type", TRUE)
filterState = variables$get("Filter state", TRUE)
burningRegimenStable = burningRegimen$getStates()$get("Stable", TRUE)
# continuous
filterEfficiency = variables$get("Filter efficiency", TRUE)
dustEmission = variables$get("Dust emission", TRUE)
metalsInWaste = variables$get("Metals in waste", TRUE)
co2Concentration = variables$get("CO2 concentration", TRUE)
lightPenetrability = variables$get("Light penetrability", TRUE)
metalsEmission = variables$get("Metals emission", TRUE)
# Create an inference engine to calculate the queries.
# The same inference engine can be re-used for multiple rows of data.
# Although not shown here, you can also create multiple inference engines,
# to take advantage of multiple threads.
factory = new(RelevanceTreeInferenceFactory)
inference = factory$createInferenceEngine(network)
queryOptions = factory$createQueryOptions()
queryOutput = factory$createQueryOutput()
# add some marginal queries
queryBurningRegimen = new(Table, burningRegimen)
inference$getQueryDistributions()$add(queryBurningRegimen)
queryLightPenetrability = new(CLGaussian, lightPenetrability)
inference$getQueryDistributions()$add(queryLightPenetrability)
# You can also add joint queries over > 1 variable
queryJoint <- new(CLGaussian, toVariableArray(filterEfficiency, burningRegimen))
inference$getQueryDistributions()$add(queryJoint)
# we will also query the log-likelihood
queryOptions$setLogLikelihood(TRUE)
dataReaderCommand <- new (DataTableDataReaderCommand, dt)
variableReferences <- c(
new(VariableReference, wasteType, ColumnValueType$NAME, "WasteType"),
new(VariableReference, co2Concentration, ColumnValueType$VALUE, "CO2Concentration")
)
evidenceReaderCommand <- new (DefaultEvidenceReaderCommand,
dataReaderCommand,
toVariableReferenceList(variableReferences),
new(ReaderOptions))
readOptions <- new(DefaultReadOptions)
# Iterate through the data, perform queries for each row.
# In this example, the output is simply written to the console, but the output
# could be streamed into a database or another data store or endpoint.
evidenceReader = evidenceReaderCommand$executeReader()
while (evidenceReader$read(inference$getEvidence(), readOptions))
{
inference$query(queryOptions, queryOutput)
logLikelihood <- queryOutput$getLogLikelihood()
probStable <- queryBurningRegimen$get(toStateArray(burningRegimenStable))
meanLightPenetrability <- queryLightPenetrability$getMean(lightPenetrability)
varianceLightPenetrability <- queryLightPenetrability$getVariance(lightPenetrability)
jointElement <- queryJoint$getMean(filterEfficiency, toStateArray(burningRegimenStable))
print(sprintf("%f %f %f %f %f", logLikelihood, probStable, meanLightPenetrability, varianceLightPenetrability, jointElement))
}
# Expected output ...
# -1.56469966974624 0.934046351645668 1.57952317582283 0.419529880001972 -3.725
# -0.247575914981347 0.98111967122024 1.52305983561012 0.351488920422928 -3.065
# -0.311935701250374 0.934046351645668 1.52238029582283 0.380432496848107 -3.25357124
# -1.25276396849587 0.85 1.5375 0.43747475 -3.725
# -7.21683608441226 1.11889682547702E-11 1.11250000000559 0.420474750002601 -3.725