[R] Problem saving logic regression result equation to disk file

Earl F. Glynn efg at stowers-institute.org
Wed Feb 23 19:01:45 CET 2005


I want to get some "simple" logic regression examples to work before
exploring a hard problem.

I can get results, but I'm having some problems using "cat" to save the
logic regression equation to a disk file.

Consider this:

# Simple Logic Regression Example
# efg, 23 Feb 2005

library(LogicReg)

# Create simulated data with known logic equation:

# "noise" logic matrix
X <- matrix(as.numeric(runif(160) < 0.5), 20,8)
colnames(X) <- paste("X", 1:ncol(X), sep="")
rownames(X) <- paste("case", 1:nrow(X), sep="")

# Define expected result:  Y = (NOT X2) AND X6
Y <- as.numeric(!X[,2] & X[,6])

# set seed for reproducible test
set.seed(19937)

# 100 interations too few:  some results in single node with |Parameter| < 1
Annealing <- logreg.anneal.control(start = -1, end = -4, iter = 500, update
= 50)

logicfit <- logreg(resp=Y, bin=X,
                   type = REGRESSION.TYPE<-2,
                   select = FIT.SINGLE.MODEL<-1,
                   ntrees=1,
                   nleaves=2,   # force shape of final tree
                   anneal.control=Annealing)

# I don't always want to see the plot
plot(logicfit)

# I'd like to write my regression equation to a file and
# then run many times to test my parameter selection
# with a known case before exploring unknown cases

logicfit

# In this case I want either of these equivalent answers
# (equivalent via DeMorgan's Theorem), and no others,
# such as single node results.

I want to run this say 100s (later 1000s) of times and look at the variation
in the results.  I want to figure out what parameters I should use so I only
see these results:

score 0
 +1 * (X6 and (not X2))


 -1 * ((not X6) or X2)


# I can't use cat to write this model to a file:
> cat(logicfit)
Error in cat(list(...), file, sep, fill, labels, append) :
        argument 1 not yet handled by cat


> summary(logicfit)
               Length Class       Mode
nsample          1    -none-      numeric
nbinary          1    -none-      numeric
nseparate        1    -none-      numeric
type             1    -none-      character
select           1    -none-      character
anneal.control   5    -none-      list
tree.control     4    -none-      list
seed             1    -none-      numeric
choice           1    -none-      numeric
nleaves          1    -none-      numeric
ntrees           1    -none-      numeric
penalty          1    -none-      numeric
response        20    -none-      numeric
binary         160    -none-      numeric
separate         1    -none-      numeric
censor          20    -none-      numeric
weight          20    -none-      numeric
model            5    logregmodel list
call             8    -none-      call

# Just the logicfit$model would be good enough but I can't "cat" that
either:

> logicfit$model
 +1 * (X6 and (not X2))
> cat(logicfit$model)
Error in cat(list(...), file, sep, fill, labels, append) :
        argument 1 not yet handled by cat

Using sink to get this result seems to be a huge kludge:
> sink("saveresults.txt")
> logicfit$model
> sink()
> results <- readLines("saveresults.txt")
> results
[1] " +1 * (X6 and (not X2))"

# FINALLY something I could write this result to a file:.
> cat(results, "\n")
 +1 * (X6 and (not X2))


What is a simple way to get my logic regression equation as a string that I
can "cat"
without dealing with the internal data structures that are present here?

Thanks for any help with this.

efg
--
Earl F. Glynn
Scientific Programmer
Bioinformatics Department
Stowers Institute for Medical Research




More information about the R-help mailing list