R/na.gam.replace.R

na.gam.replace <-
    function (frame)
{
    vars <- names(frame)
    ##See if there is a response
    if(!is.null(tt <- attr(frame, "terms"))){
        if (0 < (resp <- attr(tt, "response"))) {
            vars <- vars[-resp]
            x <- frame[[resp]]
            pos <- is.na(x)
            if (any(pos)) {
                frame <- frame[!pos, , drop = FALSE]
                warning(paste(sum(pos), "observations omitted due to missing values in the response"))
            }
        }
    }
    for (j in vars) {
        x <- frame[[j]]
        pos <- is.na(x)
        if (any(pos)) {
            if (length(levels(x))) {
                xx <- as.character(x)
                xx[pos] <- "NA"
                x <- factor(xx, exclude = NULL)
            }
            else if (is.matrix(x)) {
                ats <- attributes(x)
                w <- !pos
                x[pos] <- 0
                n <- nrow(x)
                TT <- array(1, c(1, n))
                xbar <- (TT %*% x)/(TT %*% w)
                xbar <- t(TT) %*% xbar
                x[pos] <- xbar[pos]
                attributes(x) <- ats
            }
            else {
                ats <- attributes(x)
                x[pos] <- mean(x[!pos])
                attributes(x) <- ats
            }
            frame[[j]] <- x
        }
    }
    frame
}

Try the gam package in your browser

Any scripts or data that you put into this service are public.

gam documentation built on July 8, 2020, 7:24 p.m.