Variable generator | R

source("bayesserver.R")

# This example uses an R DataFrame as the data source for adding variables to a Bayesian networ
# You can also connect to databases using DatabaseDataReaderCommand

# NOTE that this can be used to define variables from data, 
# but does not learn the structure or parameters of the network (see other examples for learning) 

df <- data.frame(
  A = c(3.0, 10.0, 14.3, 12.2),
  B = c(16, 12, 3, 6),
  C = c("A", "B", "A", "B"),
  D = I(c("X", "Y", "X", "X")))

dt <- toDataTable(df)

network <- new(Network)

dataReaderCommand <- new (DataTableDataReaderCommand, dt)

options <- new(VariableGeneratorOptions)
variableDefs <- c(
  new(VariableDefinition, "A", "A", VariableValueType$CONTINUOUS),
  new(VariableDefinition, "B", "B", VariableValueType$CONTINUOUS),
  new(VariableDefinition, "C", "C", VariableValueType$DISCRETE),
  new(VariableDefinition, "C", "D", VariableValueType$DISCRETE)
  )

variableInfos <- VariableGenerator$generate(dataReaderCommand, toList(variableDefs), options)

# Note that no variables have yet been added to the network

print(network$getVariables()$size())

variablesAdded <- lapply(variableInfos, function(vi) {
  variable <- vi$getVariable()
  network$getNodes()$add(new(Node, variable))
  return(variable)
})

print(network$getVariables()$size())