Skip to main content

Time Series Parameter learning in R


createNetworkStructure <- function() {

network <- new(Network)

x1 <- new(Variable, "X1", VariableValueType$CONTINUOUS)
x2 <- new(Variable, "X2", VariableValueType$CONTINUOUS)

# add a temporal (time series) node, with two continuous variables
nodeX <- new(Node, "X", toVariableArray(x1, x2))


# add temporal links
for (order in 1:4) {
network$getLinks()$add(new(Link, nodeX, nodeX, order))

# at this point the Dynamic Bayesian network structure is fully specified



# we manually construct the network here, but it could be loaded from a file
network <- createNetworkStructure()
x1 <- network$getVariables()$get("X1")
x2 <- network$getVariables()$get("X2")

data <- read.csv("timeseries-data.csv") # data taken from Walkthrough 3 - time series

# now learn the parameters from the data in Walkthrough 3 - Time Series network

# We will use the RelevanceTree algorithm here, as it is optimized for parameter learning
learning <- new(ParameterLearning, network, new(RelevanceTreeInferenceFactory))
learningOptions <- new(ParameterLearningOptions)

temporalDataReaderCommand <- new(DataTableDataReaderCommand, toDataTable(data))

temporalReaderOptions <- new(TemporalReaderOptions, "Case", "Time", TimeValueType$INDEX)

# here we map variables to database columns
# in this case the variables and database columns have the same name
temporalVariableReferences <- toVariableReferenceList(c(
new(VariableReference, x1, ColumnValueType$VALUE, x1$getName()),
new(VariableReference, x2, ColumnValueType$VALUE, x2$getName()))

# note that although this example only has temporal data
# we could have included additional non temporal variables and data

evidenceReaderCommand <- new(DefaultEvidenceReaderCommand,

result <- learning$learn(evidenceReaderCommand, learningOptions)

print(sprintf("Log likelihood = %f", result$getLogLikelihood()))