1. CFexact <- function(covsRatData,ratColName,qeFtn) { covsRatData[,ratColName] <- as.factor(covsRatData[,ratColName]) qeout <- qeFtn(covsRatData,ratColName,holdout=NULL) res <- qeout class(res) <- c('qeCFexact',class(qeout)) res } predict.qeCFexact <- function(obj,newx) { class(obj) <- class(obj)[2] predout <- predict(obj,newx) predout$probs } 2. NOTE: Instead of a straightforward and efficient loop, two groups searched the Web for a matrix-power package, and used it. I gave full credit for this, but please note that it is very inefficient, since it does not use the value of one power as a basis to compute the next. invViaPowers <- function(m,s) { nr <- nrow(m) i <- diag(nr) im <- i - m frb <- norm(im,'F') if (frb >= 1) return(NA) tot <- i prd <- im for (j in 1:10) { tot <- tot + prd prd <- prd %*% im } tot } m <- rbind(c(1,0.5),c(0.5,1)) invViaPowers(m,6) [,1] [,2] # [1,] 1.328125 -0.656250 # [2,] -0.656250 1.328125 invViaPowers(m,6) %*% m # [,1] [,2] # [1,] 1.0000000 0.0078125 # [2,] 0.0078125 1.0000000