GenerateRandomCase {RNetica}R Documentation

Generates random cases for nodes in a Netica network

Description

This function generates a random instantiation of the nodes in nodelist using the current (that is posterior to any findings entered into the net) joint probability distribution of those nodes in the network.

Usage

GenerateRandomCase(nodelist, method = "Default", timeout = 100, rng = NULL)

Arguments

nodelist

A list of active NeticaNode objects, all of which belong to the same network.

method

A character scalar used to describe the method used select the random numbers. This should have one of the values "Join_Tree_Sampling", "Forward_Sampling" or "Default_Sampling" (see details). Only the first letter is used and case is ignored, so "J", "F" and "D" are legal values.

timeout

This is a number describing how long to carry on computations under the forward sampling method. It is ignored under the join tree sampling method or when the default sampling method turns out to be join tree.

rng

This either be an existing NeticaRNG object or NULL in which case the default random number generator for the net is used.

Details

The function visits each node in nodelist and randomly sets a finding for that node based on the current beliefs about that node. This takes into account any findings previously entered into the graph (including the previously sampled nodes in the list). In particular, to generate multiple cases, the findings need to be retracted (using RetractNodeFinding(node) or RetractNetFindings(net) between each generation.

Netica supports three methods for doing the sampling:

Join_Tree_Sampling.

For each node in turn, the beliefs are calculated and a random state is selected and entered as a finding (with beliefs propagating). The network must be compiled for this method to work.

Forward_Sampling.

Random cases are generated directly using equations for continuous nodes if these are available. Random results not compatible with the current findings are rejected. This method is not guaranteed to converge, and may be quite slow if the current set of findings has a low probability. It will only run for a period of time indicated by timeout and returns a negative value if it does not complete successfully.

Default_Sampling.

Netica figures out which method is better to use. It uses forward sampling if either rejections aren't a problem (presumably because there are no findings) or if the network is uncompiled. Otherwise it uses join tree sampling.

The rng argument can be used to associate a random number generator with the generation (see NeticaRNG). If the rng argument is NULL, then the default random number generator for the network is used. This is either a random number generator associated with the network using NetworkSetRNG, or else the default Netica random number generator.

Value

Invisibly returns 0 if the case was successfully generated or -1 if the case could not be generated (using the forward sampling method). In the latter case, a warning is issued as well.

Author(s)

Russell Almond

References

http://norsys.com/onLineAPIManual/index.html: GenerateRandomCase_bn()

See Also

NetworkSetRNG(), NeticaRNG(), NodeFinding, RetractNetFindings ReadFindings, CaseStream

Examples

sess <- NeticaSession()
startSession(sess)

irt5 <- ReadNetworks(file.path(library(help="RNetica")$path,
                           "sampleNets","IRT5.dne"), session=sess)

irt5.theta <- NetworkFindNode(irt5,"Theta")
irt5.x <- NetworkFindNode(irt5,paste("Item",1:5,sep="_"))

CompileNetwork(irt5)

GenerateRandomCase(irt5.x)
sapply(irt5.x,NodeFinding)

RetractNetFindings(irt5)

GenerateRandomCase(irt5.x)
sapply(irt5.x,NodeFinding)

## This generates a fixed series of random cases and saves them to a
## file. 
N <- 10L
rnodes <- c(list(irt5.theta),irt5.x)
casefile <- tempfile("irt5testcase",fileext=".cas")
filestream <- CaseFileStream(casefile, session=sess)
rng <- NewNeticaRNG(123456779, session=sess)
WithOpenCaseStream(filestream,
  WithRNG(rng,
    for (n in 1L:N) {
      GenerateRandomCase(rnodes,rng=rng)
      WriteFindings(rnodes,filestream,n)
      lapply(rnodes,RetractNodeFinding) # Only retract findings for
                                        # generated nodes
    }))

## With constructs force closure even on error exit.
stopifnot(!isNeticaRNGActive(rng),
          !isCaseStreamOpen(filestream))

DeleteNetwork(irt5)
stopSession(sess)


[Package RNetica version 0.8-2 Index]