# File: lrtest.r
# Author: Roy Wilson
# 01-26-05

check_target <- function(x) {
	return(is.factor(x));
}

construct_and_run_model <- function(x,y) {
	cat("construct_and_run_model:\n")
	cat("class of x:",class(x),"nlevels(x):",nlevels(x),"\n")
	cat("class of y:",class(y),"nlevels(y):",nlevels(y),"\n")
	model.uni <- glm(y ~ x, family = binomial)
	cat("  model built\n")
	results <- summary(model.uni)
	cat("  model ran\n")
	coefficients <- results$coefficients
	stats <- coefficients[2,1:4]
	estimate <- stats[1]
	standerr <- stats[2]
	pvalue <- stats[4]  
	if ((standerr <= 4.0) && (pvalue <= 0.05)) {
	   cat(estimate," ",pvalue,"\n")
	} 
}

attach(newDataSet)
for (cond in 1:2) {
    # Select rows for each condition
    t <- newDataSet[Wcond == cond,] 
    inputdata <- as.matrix(t)
    varnames <- dimnames(inputdata)[[2]]
    ilimit <- ncol(inputdata)-1
    jlimit <- ncol(inputdata)
    for (i in 3:ilimit) {
	if (is.factor(t[,i]) == TRUE) {
	   jstart <- i+1
	   for (j in jstart:jlimit) {
	       if (check_target(t[,i])) {
		  cat("building model:", varnames[i],"~",varnames[j],"\n")
		  construct_and_run_model(t[,j],t[,i])
	       }
           }
	}
    }
}
detach(newDataSet)
# q()