
# finds the confusion matrix for the output from glm()

nmconfmat <- function(glmout) {
   cm <- matrix(nrow=2,ncol=2)
   fits <- glmout$fitted.values
   y <- glmout$y
   resids <- abs(y - fits)
   correctpred <- as.integer(resids < 0.5)
   true1 <- which(y == 1)
   # could use table(), but do it the plain way
   cm[1,1] <- mean(correctpred[true1])
   cm[1,2] <- 1 - cm[1,1]
   true0 <- which(y == 0)
   cm[2,2] <- mean(correctpred[true0])
   cm[2,1] <- 1 - cm[2,2]
   cm
}

test <- function(n) {
   x <- rnorm(n)
   shift <- 1:(n/2)
   x[shift] <- x[shift] + 1
   lgt <- function(t) 1 / (1 + exp(-t))
   probs <- lgt(x)
   y <- rep(0,n)
   for (i in 1:n) {
      if (runif(1) < probs[i]) y[i] <- 1
   }
   x <<- x
   y <<- y
}

