binv <- function(x, y, n, cutp = NULL) {
    if (is.null(cutp)) {
        cutp <- unique(quantile(x, prob = seq(0, 1, length = round(length(x)/n))))
    }
    cutm <- 0.5*(cutp[-1] + cutp[-length(cutp)])
    cutf <- cut(x, cutp, include.lowest = T)
    val <- tapply(y, cutf, mean)
    return(list(xb = cutm, yb = val, xc = cutp))
}

