CaseMemorytream {RNetica} | R Documentation |
This object is subclass of CaseStream
so it is a
wrapper around a Netica stream which is used to read/write cases. In
this subclass, the case stream is associated with a memory buffer that
corresponds to an R data.frame
object.
The function MemoryStreamContents
accesses the contents
as a data frame.
CaseMemoryStream(data.frame, label=deparse(substitute(data.frame)), session=getDefaultSession()) is.MemoryCaseStream(x) getCaseStreamDataFrameName(stream)
data.frame |
A data frame in which columns correspond to Netica nodes, and rows correspond to cases. See details. |
label |
A name for the stream object. |
session |
An object of type |
stream |
A |
x |
A object whose type is to be determined. |
A Netica case file has a format that very much resembles the output of
write.table
. The first row is a header row, which
contains the names of the variables, the second and subsequent rows
contain a set of findings: an assignment of values to the nodes
indicated in the columns. There are no row numbers, and the separator
and missing value codes are the values of
CaseFileDelimiter()
, and
CaseFileMissingCode()
respectively.
In addition to columns representing variables, two special columns are
allowed. The column named “IDnum”, if present should contain
integers which correspond to ID numbers for the cases (this correspond
to the id
argument of WriteFindings
). The column
named “NumCases” should contain number values and this allows
rows to be differentially weighted (this correspond to the freq
argument of WriteFindings
).
A simple way to convert a data frame into a set of cases for use with
various Netica functions that use cases would be to write the data
frame to a file of the proper format, and then create a
CaseFileStream
on the just written file. The
MemoryCaseStream
shortcuts that process by writing the data
frame to a memory buffer and then creating a stream around the memory
buffer. Like the CaseFileStream
, the MemoryCaseStream
is
a subclass of CaseStream
and follows the same
conventions.
The function MemoryCaseStream
opens a new memory stream using
data.frame
as the source. If data.frame
is NULL
a new memory stream for writing is created. The function
CloseCaseStream
closes an open case stream (and is harmless if
the stream is already closed. Although RNetica tries to close open
case streams when they are garbage collected, users should not count
on this behavior and should close them manually. Also be aware that
all case streams are automatically closed when R is closes or RNetica
is unloaded. The function isCaseStreamOpen
tests to see if the
stream is open or closed. The function OpenCaseStream
if
called on a closed MemoryCaseStream
will reopen the stream in Netica
using the current value of MemoryStreamContents
as the
source. (If called on an open stream it will do nothing but issue a
warning).
The function getCaseStreamDataFrameName
provides the value of
label
when the stream was created.
The function OpenMemoryCaseStream
returns a new, open
CaseFileStream
object.
The functions is.MemoryCaseStream
returns a logical value
indicating whether or not the argument is a CaseFileStream
.
The function getCaseStreamDataFrameName
returns the value of
label
used when the stream was created, usually this is the
name of the data.frame
argument.
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
.
In version 0.5 of RNetica, this class was renamed. It is now called
MemoryCaseStream
and the constructor is called
CaseMemoryStream
(while previously the class and the
filename had the same name). This matches the usage of
FileCaseStream
and its constructor
CaseFileStream
.
MemoryCaseStreams
are most useful for small to medium size data
frames. Larger data frames are probably better handled through case
files.
Internally, a weak reference system is used to keep a list of Netica stream objects which need to be closed when RNetica is unloaded. Stream objects should also be forced closed when garbage collected. The weak reference system is somewhat experimental, so well designed code should manually close the streams when the program is through with it.
Stream objects are fragile, and will not survive saving and restoring
an R session. However, the object retains information about itself,
so that calling OpenCaseStream
on the saved object, should
reopen the stream. Note that any position information will be lost.
Russell Almond
http://norsys.com/onLineAPIManual/index.html: NewMemoryStream_ns(), http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
CaseFileDelimiter
, CaseFileMissingCode
,
WriteFindings
, ReadFindings
,
MemoryStreamContents
,CaseStream
sess <- NeticaSession() startSession(sess) abc <- CreateNetwork("ABC", session=sess) A <- NewDiscreteNode(abc,"A",c("A1","A2","A3","A4")) B <- NewDiscreteNode(abc,"B",c("B1","B2","B3")) C <- NewDiscreteNode(abc,"C",c("C1","C2")) AddLink(A,B) AddLink(A,C) AddLink(B,C) ## This is the file written in CaseFileStream help. casefile <- file.path(library(help="RNetica")$path, "testData","abctestcases.cas") CaseFileDelimiter("\t", session=sess) CaseFileMissingCode("*", session=sess) cases <- read.CaseFile(casefile, session=sess) memstream <- CaseMemoryStream(cases, session=sess) ##Case 1 memstream <- ReadFindings(list(A,B,C),memstream,"FIRST") stopifnot(NodeFinding(A) == "A1", NodeFinding(B) == "B1", NodeFinding(C) == "C1", getCaseStreamLastId(memstream)==1001, abs(getCaseStreamLastFreq(memstream)-1.0) <.0001) ##Case 2 memstream <- ReadFindings(list(A,B,C),memstream,"NEXT") stopifnot(NodeFinding(A) == "A2", NodeFinding(B) == "B2", NodeFinding(C) == "C2", getCaseStreamLastId(memstream)==1002, abs(getCaseStreamLastFreq(memstream)-2.0) <.0001) ##Case 3 memstream <- ReadFindings(list(A,B,C),memstream,"NEXT") stopifnot(NodeFinding(A) == "A3", NodeFinding(B) == "B3", NodeFinding(C) == "@NO FINDING", getCaseStreamLastId(memstream)==1003, abs(getCaseStreamLastFreq(memstream)-1.0) <.0001) ## EOF memstream <- ReadFindings(list(A,B,C),memstream,"NEXT") stopifnot (is.na(getCaseStreamPos(memstream))) ##Clean Up CloseCaseStream(memstream) DeleteNetwork(abc) stopSession(sess)