Skip to content

problem with using transace

2 messages · Anne, Frank E Harrell Jr

#
thanks anne
#
anne.piotet at urbanet.ch wrote:
The corrected version (below) will fix that problem but note that there 
is a bug in ace causing it not to allow a monotonicity constraint when a 
variable is on the left hand side.  This is inconsistent with the ace 
documentation.  There are other problems in ace in which it checks 
column numbers against the number of rows in the x matrix instead of the 
number of columns.  The internal version of ace defined inside areg.boot 
fixes the latter problem.  Note that I reported these problems a long 
time ago.

Frank

transace <- function(x, monotonic=NULL, categorical=NULL, binary=NULL, 
pl=TRUE) {
   if(.R.) require('acepack')  # provides ace, avas

nam <- dimnames(x)[[2]]
omit <- is.na(x %*% rep(1,ncol(x)))
omitted <- (1:nrow(x))[omit]
if(length(omitted)) x <- x[!omit,]
p <- ncol(x)
xt <- x  # binary variables retain original coding
if(!length(nam)) stop("x must have column names")
rsq <- rep(NA, p)
names(rsq) <- nam


for(i in (1:p)[!(nam %in% binary)])	{
   lab <- nam[-i]
   w <- 1:(p-1)
   im <- w[lab %in% monotonic]
   ic <- w[lab %in% categorical]
   if(nam[i] %in% monotonic) im <- c(0, im)
   if(nam[i] %in% categorical) ic <- c(0, ic)
   m <- 10*(length(im)>0)+(length(ic)>0)
   if(m==11) a <- ace(x[,-i], x[,i], mon=im, cat=ic)
   else if (m==10) a <- ace(x[,-i], x[,i], mon=im)
   else if(m==1) a <- ace(x[,-i], x[,i], cat=ic)
   else a <- ace(x[,-i], x[,i])
   xt[,i] <- a$ty
   rsq[i] <- a$rsq
   if(pl)plot(x[,i], xt[,i], xlab=nam[i], ylab=paste("Transformed",nam[i]))
}	

cat("R-squared achieved in predicting each variable:\n\n")
print(rsq)

attr(xt, "rsq") <- rsq
attr(xt, "omitted") <- omitted
invisible(xt)
}