LearnCases {RNetica}R Documentation

Learn Conditional Probability Tables from a Netica Case Stream

Description

This function updates the conditional probabilities associated with the given list of nodes based on the findings associated with that node and its parents found in the caseStream argument, which should be a CaseStream object.

Usage

LearnCases(caseStream, nodelist, weight = 1)

Arguments

caseStream

This should be a CaseStream object, or else an object which can be made into a case stream: either a pathname for a case file, or a data frame of the format described in MemoryCaseStream. The case stream can be either opened or closed. If closed it is reopened before updating. In either case, it is closed at the end of the function. Warning, due to a bug in Netica, memory streams are not working and should not be used with Netica API 5.04 or earlier. See below.

nodelist

A list of active NeticaNode objects that reference the conditional probability tables to be updated.

weight

A multiplier for the weights of the cases in terms of number of observations. Negative weights unlearn previously learned cases.

Details

This is like calling the function LearnFindings repeatedly with the values of the nodes set to each of the case rows in turn. Thus, it updates the conditional probability tables for each nodes based on observed counts in the case files, taking the current probability and the NodeExperience as the prior distribution.

If the case stream has a column NumCases, then the weight assigned to Row j is weight*NumCases[j]. If the case stream does not have such a column, then it is treated as if each column has weight 1. (Among other purposes, this allows case data to be stored in a compact format where all of the possible cases are enumerated along with a count of repetitions.) Note that negative weights will unlearn cases.

Value

This function returns the CaseStream used in the analysis. This might have either been passed directly as the caseStream argument, or created from the value of the caseStream argument. In either case, the stream is closed at the end of the function.

Netica Bugs

In version 5.04 of the Netica API, there is a problem with using Memory Streams that seems to affect the functions LearnCases and LearnCPTs. Until this problem is fixed, most uses of Memory Streams will require file streams instead. Write the case file using write.CaseFile, and then create a file stream using CaseFileStream.

Note

To learn without using the current probabilities as priors, call DeleteNodeTable first.

Author(s)

Russell Almond

References

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

See Also

NodeExperience, NodeProbs, NodeFinding, FadeCPT, LearnFindings, DeleteNodeTable, LearnCPTs

Examples

sess <- NeticaSession()
startSession(sess)

abb <- CreateNetwork("ABB", session=sess)
A <- NewDiscreteNode(abb,"A",c("A1","A2"))
B1 <- NewDiscreteNode(abb,"B1",c("B1","B2"))
B2 <- NewDiscreteNode(abb,"B2",c("B1","B2"))

AddLink(A,B1)
AddLink(A,B2)

A[] <- c(.5,.5)
NodeExperience(A) <- 10

B1["A1"] <- c(.8,.2)
B1["A2"] <- c(.2,.8)
B2["A1"] <- c(.8,.2)
B2["A2"] <- c(.2,.8)
NodeExperience(B1) <- c(10,10)
NodeExperience(B2) <- c(10,10)

casesabb <-
  data.frame(A=c("A1","A1","A1","A1","A1","A2","A2","A2","A2","A2"),
             B1=c("B1","B1","B1","B2","B2","B2","B2","B2","B1","B1"),
             B2=c("B1","B1","B1","B1","B2","B2","B2","B2","B2","B1"))
## LearnCases(casesabb,list(A,B1))
## There is currently a bug in Netica, so that this function does not
## work with memory streams.  As a work around, use proper file streams
## instead.

outfile <- tempfile("casesabb",fileext=".cas")
write.CaseFile(casesabb,outfile, session=sess)
LearnCases(outfile,list(A,B1))

## Probs for A & B1 modified, but B2 left alone
stopifnot(
  NodeExperience(A)==20,
  NodeExperience(B1)==c(15,15),
  NodeExperience(B2)==c(10,10),
  sum(abs(NodeProbs(A) - .5)) < .001,
  sum(abs(B1["A1",drop=TRUE] - c(11,4)/15)) < .001,
  sum(abs(B1["A2",drop=TRUE] - c(4,11)/15)) < .001,
  sum(abs(B2["A1",drop=TRUE] - c(8,2)/10)) < .001,
  sum(abs(B2["A2",drop=TRUE] - c(2,8)/10)) < .001
)

DeleteNetwork(abb)
stopSession(sess)


[Package RNetica version 0.8-2 Index]