line <-
    function(x, y=NULL)
{
    xy <- xy.coords(x, y)
    ok <- complete.cases(xy$x,xy$y)
    n <- length(ok)
    if(n <= 1) stop("insufficient observations")
    z <- .C("tukeyline",
	    as.double(xy$x[ok]),
	    as.double(xy$y[ok]),
	    double(n),
	    double(n),
	    n,
	    double(2),
	    DUP=FALSE, PACKAGE="eda")
    value <- list(call=sys.call(), coefficients=z[[6]], residuals=z[[3]],
		  fitted.values=xy$y-z[[3]])
    class(value) <- "tukeyline"
    value
}
coef.tukeyline <- coef.lm
residuals.tukeyline <- residuals.lm
fitted.tukeyline <- fitted.lm
print.tukeyline <- print.lm
medpolish <- function (x, eps=0.01, maxiter=10, trace.iter = TRUE)
{
    z <- as.matrix(x)
    nr <- nrow(z)
    nc <- ncol(z)
    t <- 0
    r <- numeric(nr)
    c <- numeric(nc)
    oldsum <- 0
    for(iter in 1:maxiter) {
	rdelta <- apply(z, 1, median)
	z <- z - matrix(rdelta, nr=nr, nc=nc)
	r <- r + rdelta
	delta <- median(c)
	c <- c - delta
	t <- t + delta
	cdelta <- apply(z, 2, median)
	z <- z - matrix(cdelta, nr=nr, nc=nc, byrow=TRUE)
	c <- c + cdelta
	delta <- median(r)
	r <- r - delta
	t <- t + delta
	newsum <- sum(abs(z))
	converged <- newsum==0 || abs(1-oldsum/newsum) < eps
        if(converged) break
	oldsum <- newsum
	if(trace.iter) cat(iter,":", newsum,"\n")
    }
    if(converged) { if(trace.iter) cat("Final:", newsum,"\n")
    } else warning(paste("medpolish() not converged in",maxiter,"iterations"))
    names(r) <- rownames(z)
    names(c) <- colnames(z)
    ans <- list(overall=t, row=r, col=c, residuals=z,
		name=deparse(substitute(x)))
    class(ans) <- "medpolish"
    ans
}

print.medpolish <- function(x, digits=.Options$digits, ...)
{
    cat("\nMedian Polish Results (Dataset: \"", x$name, "\")\n", sep="")
    cat("\nOverall:", x$overall, "\n\nRow Effects:\n")
    print(x$row, digits=digits, ...)
    cat("\nColumn Effects:\n")
    print(x$col, digits=digits, ...)
    cat("\nResiduals:\n")
    print(x$residuals, digits=max(2, digits-2), ...)
    cat("\n")
    invisible(x)
}

plot.medpolish <- function(x, main="Tukey Additivity Plot", ...) {
    plot(outer(x$row,x$col)/x$overall, x$residuals,
	 main=main, xlab="Diagnostic Comparison Values",
	 ylab="Residuals", ...)
    abline(h=0, v=0, lty="dotted")
}
smooth <- function(x)
{
    if(!is.numeric(x))
	stop("attempt to smooth non-numeric values")
    if(any(is.na(x)))
	stop("attempt to smooth NA values")
    n <- length(x)
    smo <- .C("tukeysmooth",
	      as.double(x),
	      double(n),
	      double(n),
	      double(n),
	      n,
	      double(1),
	      DUP=FALSE, PACKAGE="eda")[[2]]
    if(is.ts(x))
	smo <- ts(smo, start=start(x), freq=frequency(x))
    smo
}
.First.lib <- function(lib, pkg) {
    library.dynam("eda", pkg, lib)
    provide(eda)
}
