Earl F. Glynn
2005-Feb-23 18:01 UTC
[R] Problem saving logic regression result equation to disk file
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
Perhaps I can ask a more focused question: "Earl F. Glynn" <efg at stowers-institute.org> wrote in message news:cvig76$3jq$1 at sea.gmane.org...> I want to get some "simple" logic regression examples to work before > exploring a hard problem...> 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)OK, I have a logic regression equation and just want it in a character form. For example, I can see this equation from the R command prompt:> logicfitscore 0.968 +2.14 * (((not X4) or ((not X13) and X19)) and (not X3)) -1.25 * ((((not X1) or (not X3)) and ((not X2) or X20)) and (((not X17) and X16) or ((not X20) and (not X1)))) But I cannot figure out how to get this equation in character form: Why does this NOT work?> typeof(logicfit)[1] "list"> class(logicfit)[1] "logreg"> x <- paste(print(logicfit))score 0.968 +2.14 * (((not X4) or ((not X13) and X19)) and (not X3)) -1.25 * ((((not X1) or (not X3)) and ((not X2) or X20)) and (((not X17) and X16) or ((not X20) and (not X1))))> xcharacter(0)> cat(x, "\n")[nothing]>The "print" makes the following error go away but does not give me the string displayed on the R console:> cat(logicfit)Error in cat(list(...), file, sep, fill, labels, append) : argument 1 not yet handled by cat This simpler analogy DOES work:> x <- paste( print("string") )[1] "string"> x[1] "string"> cat(x, "\n")string>Any clues? Thanks for any help with this. efg